How to use @testing-library/angular - 10 common examples

To help you get started, we’ve selected a few @testing-library/angular examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github testing-library / angular-testing-library / src / app / examples / 04-forms-with-material.spec.ts View on Github external
test('is possible to fill in a form and verify error messages (with the help of jest-dom https://testing-library.com/docs/ecosystem-jest-dom)', async () => {
  const component = await render(MaterialFormsComponent, {
    imports: [ReactiveFormsModule, MaterialModule],
  });

  const nameControl = component.getByPlaceholderText('Name');
  const scoreControl = component.getByPlaceholderText(/score/i);
  const colorControl = component.getByPlaceholderText('color', { exact: false });
  const errors = component.getByRole('alert');

  expect(errors).toContainElement(component.queryByText('name is required'));
  expect(errors).toContainElement(component.queryByText('score must be greater than 1'));
  expect(errors).toContainElement(component.queryByText('color is required'));

  component.type(nameControl, 'Tim');
  component.type(scoreControl, 12);
  component.selectOptions(colorControl, 'Green');
github testing-library / angular-testing-library / src / app / examples / 10-inject-token-dependency.spec.ts View on Github external
test('injects data into the component', async () => {
  const component = await render(DataInjectedComponent, {
    providers: [
      {
        provide: DATA,
        useValue: { text: 'Hello boys and girls' },
      },
    ],
  });

  expect(component.getByText(/Hello boys and girls/i)).toBeInTheDocument();
});
github testing-library / angular-testing-library / src / app / app.component.spec.ts View on Github external
test(`should have form validations`, async () => {
    const component = await render(AppComponent, {
      imports: [ReactiveFormsModule],
      providers: [provideMockStore()],
    });

    const appComponent = component.fixture.componentInstance as AppComponent;
    expect(appComponent.form.valid).toBe(false);

    const nameInput = component.getByLabelText('Name:');
    const ageInput = component.getByLabelText('Age:');

    const nameValue = appComponent.form.get('name');
    const ageValue = appComponent.form.get('age');

    component.type(nameInput, 'B');
    expect(nameValue.valid).toBe(false);
github testing-library / angular-testing-library / src / app / examples / 07-with-ngrx-mock-store.spec.ts View on Github external
test('works with provideMockStore', async () => {
  const component = await render(WithNgRxMockStoreComponent, {
    providers: [
      provideMockStore({
        selectors: [
          {
            selector: selectItems,
            value: ['Four', 'Seven'],
          },
        ],
      }),
    ],
  });

  const store = TestBed.get(Store) as MockStore;
  store.dispatch = jest.fn();

  component.getByText('Four');
github testing-library / angular-testing-library / src / app / examples / 03-forms.spec.ts View on Github external
test('is possible to fill in a form and verify error messages (with the help of jest-dom https://testing-library.com/docs/ecosystem-jest-dom)', async () => {
  const component = await render(FormsComponent, {
    imports: [ReactiveFormsModule],
  });

  const nameControl = component.getByLabelText('Name');
  const scoreControl = component.getByLabelText(/score/i);
  const colorControl = component.getByLabelText('color', { exact: false });
  const errors = component.getByRole('alert');

  expect(errors).toContainElement(component.queryByText('name is required'));
  expect(errors).toContainElement(component.queryByText('score must be greater than 1'));
  expect(errors).toContainElement(component.queryByText('color is required'));

  expect(nameControl).toBeInvalid();
  component.type(nameControl, 'Tim');
  component.type(scoreControl, '12');
  component.blur(scoreControl);
github testing-library / angular-testing-library / src / app / app.component.spec.ts View on Github external
test(`should render title in a h1 tag`, async () => {
  const { container } = await render('', {
    declarations: [AppComponent],
    imports: [ReactiveFormsModule],
    providers: [provideMockStore()],
  });
  expect(container.querySelector('h1').textContent).toContain('Welcome to app!');
});
github testing-library / angular-testing-library / src / app / examples / 05-component-provider.spec.ts View on Github external
test('renders the current value and can increment and decrement with a mocked jest-utils service', async () => {
  const counter = createMock(CounterService);
  let fakeCounterValue = 50;
  counter.increment.mockImplementation(() => (fakeCounterValue += 10));
  counter.decrement.mockImplementation(() => (fakeCounterValue -= 10));
  counter.value.mockImplementation(() => fakeCounterValue);

  const component = await render(ComponentWithProviderComponent, {
    componentProviders: [
      {
        provide: CounterService,
        useValue: counter,
      },
    ],
  });

  const incrementControl = component.getByText('Increment');
  const decrementControl = component.getByText('Decrement');
  const valueControl = component.getByTestId('value');

  expect(valueControl.textContent).toBe('50');

  component.click(incrementControl);
  component.click(incrementControl);
github testing-library / angular-testing-library / src / app / app.component.spec.ts View on Github external
test(`should provide a mock greet service`, async () => {
  const component = await render(AppComponent, {
    declarations: [AppComponent],
    imports: [ReactiveFormsModule],
    providers: [provideMockStore(), provideMock(GreetService)],
  });
  const service: GreetService = TestBed.get(GreetService);

  component.click(component.getByText('Greet'));

  expect(service.greet).toHaveBeenCalled();
});
github testing-library / angular-testing-library / src / app / examples / 05-component-provider.spec.ts View on Github external
test('renders the current value and can increment and decrement with provideMocked from jest-utils', async () => {
  const component = await render(ComponentWithProviderComponent, {
    componentProviders: [provideMock(CounterService)],
  });

  const incrementControl = component.getByText('Increment');
  const decrementControl = component.getByText('Decrement');

  component.click(incrementControl);
  component.click(incrementControl);
  component.click(decrementControl);

  const counterService = TestBed.get(CounterService) as Mock;
  expect(counterService.increment).toHaveBeenCalledTimes(2);
  expect(counterService.decrement).toHaveBeenCalledTimes(1);
});
github testing-library / angular-testing-library / src / app / examples / 05-component-provider.spec.ts View on Github external
test('renders the current value and can increment and decrement with a mocked jest-utils service', async () => {
  const counter = createMock(CounterService);
  let fakeCounterValue = 50;
  counter.increment.mockImplementation(() => (fakeCounterValue += 10));
  counter.decrement.mockImplementation(() => (fakeCounterValue -= 10));
  counter.value.mockImplementation(() => fakeCounterValue);

  const component = await render(ComponentWithProviderComponent, {
    componentProviders: [
      {
        provide: CounterService,
        useValue: counter,
      },
    ],
  });

  const incrementControl = component.getByText('Increment');
  const decrementControl = component.getByText('Decrement');

@testing-library/angular

Test your Angular components with the dom-testing-library

MIT
Latest version published 16 days ago

Package Health Score

90 / 100
Full package analysis