Skip to content

docs: add component with service spec #114

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions src/app/examples/12-service-component.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { of } from 'rxjs';
import { render, screen } from '@testing-library/angular';
import { createMock } from '@testing-library/angular/jest-utils';

import { Customer, CustomersComponent, CustomersService } from './12-service-component';

test('renders the provided customers with manual mock', async () => {
const customers: Customer[] = [
{
id: '1',
name: 'sarah',
},
{
id: '2',
name: 'charlotte',
},
];
await render(CustomersComponent, {
componentProviders: [
{
provide: CustomersService,
useValue: {
load() {
return of(customers);
},
},
},
],
});

const listItems = screen.getAllByRole('listitem');
expect(listItems.length).toBe(customers.length);

customers.forEach((customer) => screen.getByText(new RegExp(customer.name, 'i')));
});

test('renders the provided customers with createMock', async () => {
const customers: Customer[] = [
{
id: '1',
name: 'sarah',
},
{
id: '2',
name: 'charlotte',
},
];

const customersService = createMock(CustomersService);
customersService.load = jest.fn(() => of(customers));

await render(CustomersComponent, {
componentProviders: [
{
provide: CustomersService,
useValue: customersService,
},
],
});

const listItems = screen.getAllByRole('listitem');
expect(listItems.length).toBe(customers.length);

customers.forEach((customer) => screen.getByText(new RegExp(customer.name, 'i')));
});
31 changes: 31 additions & 0 deletions src/app/examples/12-service-component.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { Component, Injectable } from '@angular/core';
import { Observable, of } from 'rxjs';

export class Customer {
id: string;
name: string;
}

@Injectable({
providedIn: 'root',
})
export class CustomersService {
load(): Observable<Customer[]> {
return of([]);
}
}

@Component({
selector: 'app-fixture',
template: `
<ul>
<li *ngFor="let customer of customers$ | async">
{{ customer.name }}
</li>
</ul>
`,
})
export class CustomersComponent {
customers$ = this.service.load();
constructor(private service: CustomersService) {}
}
13 changes: 10 additions & 3 deletions src/app/issues/issue-106.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Component } from '@angular/core';
import { BehaviorSubject } from 'rxjs';
import { tap } from 'rxjs/operators';
import { render, screen, fireEvent, waitFor } from '@testing-library/angular';

@Component({
Expand Down Expand Up @@ -28,8 +27,16 @@ it('https://github.com/testing-library/angular-testing-library/issues/106', asyn
// await waitFor(() => expect(hiddenText).not.toBeNull());

// succeeds
// await waitFor(() => expect(screen.queryByTestId('getme')).not.toBeNull());
await waitFor(() => expect(screen.queryByTestId('getme')).not.toBeNull());
});

it('better https://github.com/testing-library/angular-testing-library/issues/106', async () => {
await render(TestSelectComponent);
const toggle = screen.getByTestId('toggle');
const hiddenText = screen.queryByTestId('getme');

expect(hiddenText).toBeNull();
fireEvent.click(toggle);

// better
screen.getByTestId('getme');
});