Skip to content

Commit 6367275

Browse files
committed
test: ✅ tidy up tests
1 parent ba6000a commit 6367275

File tree

19 files changed

+170
-129
lines changed

19 files changed

+170
-129
lines changed

apps/1-recipe-search-solution/src/app/recipe/recipe-search.integration.spec.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,9 @@ describe(RecipeSearch.name, () => {
1313
});
1414

1515
async function renderComponent() {
16-
TestBed.configureTestingModule({
17-
providers: [provideHttpClient()],
18-
});
19-
const fixture = TestBed.createComponent(RecipeSearch);
16+
TestBed.configureTestingModule({ providers: [provideHttpClient()] });
2017

18+
const fixture = TestBed.createComponent(RecipeSearch);
2119
await fixture.whenStable();
2220

2321
return {

apps/1-recipe-search-solution/src/app/recipe/recipe-search.isolated.spec.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@ import { RecipeSearch } from './recipe-search.ng';
55

66
describe(RecipeSearch.name, () => {
77
it('searches recipes without filtering', async () => {
8-
const { getRecipeNames } = createComponent();
8+
const { getRecipeNames } = await createComponent();
99

10-
expect(await getRecipeNames()).toEqual(
10+
expect(getRecipeNames()).toEqual(
1111
expect.arrayContaining(['Burger', 'Salad']),
1212
);
1313
});
1414

15-
function createComponent() {
15+
async function createComponent() {
1616
TestBed.configureTestingModule({
1717
providers: [RecipeSearch, provideHttpClient()],
1818
});
@@ -21,9 +21,10 @@ describe(RecipeSearch.name, () => {
2121

2222
component.ngOnInit();
2323

24+
await whenAppStable();
25+
2426
return {
25-
async getRecipeNames() {
26-
await whenAppStable();
27+
getRecipeNames() {
2728
return component.recipes?.map((recipe) => recipe.name);
2829
},
2930
};

apps/1-recipe-search-starter/src/app/recipe/recipe-search.integration.spec.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ describe(RecipeSearch.name, () => {
99
TestBed.configureTestingModule({ providers: [provideHttpClient()] });
1010

1111
const fixture = TestBed.createComponent(RecipeSearch);
12-
1312
await fixture.whenStable();
1413

1514
return {

apps/2-test-double-solution/src/app/recipe/recipe-search.integration.spec.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,31 @@
11
import { TestBed } from '@angular/core/testing';
22
import { screen } from '@testing-library/dom';
33
import userEvent from '@testing-library/user-event';
4-
import { RecipeSearch } from './recipe-search.ng';
54
import {
65
provideRecipeRepositoryFake,
76
RecipeRepositoryFake,
87
} from './recipe-repository.fake';
8+
import { RecipeSearch } from './recipe-search.ng';
99
import { recipeMother } from './recipe.mother';
1010

1111
describe(RecipeSearch.name, () => {
1212
it('searches recipes without filtering', async () => {
1313
const { getRecipeNames } = await renderComponent();
1414

15-
expect(getRecipeNames()).toEqual(['Burger', 'Salad']);
15+
const els = getRecipeNames();
16+
expect.soft(els).toHaveLength(2);
17+
expect.soft(els[0]).toHaveTextContent('Burger');
18+
expect.soft(els[1]).toHaveTextContent('Salad');
1619
});
1720

1821
it('filters recipes by keywords', async () => {
1922
const { getRecipeNames, setKeywords } = await renderComponent();
2023

2124
await setKeywords('Burg');
2225

23-
expect(getRecipeNames()).toEqual(['Burger']);
26+
const els = getRecipeNames();
27+
expect.soft(els).toHaveLength(1);
28+
expect.soft(els[0]).toHaveTextContent('Burger');
2429
});
2530

2631
async function renderComponent() {
@@ -39,10 +44,11 @@ describe(RecipeSearch.name, () => {
3944

4045
return {
4146
getRecipeNames() {
42-
return screen.queryAllByRole('heading').map((el) => el.textContent);
47+
return screen.queryAllByRole('heading');
4348
},
4449
async setKeywords(keywords: string) {
4550
await userEvent.type(screen.getByLabelText('Keywords'), keywords);
51+
await fixture.whenStable();
4652
},
4753
};
4854
}

apps/2-test-double-solution/src/app/recipe/recipe-search.isolated.spec.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,24 @@
1+
import { TestBed } from '@angular/core/testing';
12
import {
23
provideRecipeRepositoryFake,
34
RecipeRepositoryFake,
45
} from './recipe-repository.fake';
5-
import { TestBed } from '@angular/core/testing';
6-
import { whenAppStable } from '../../testing/when-app-stable';
76
import { RecipeSearch } from './recipe-search.ng';
87
import { recipeMother } from './recipe.mother';
98

109
describe(RecipeSearch.name, () => {
1110
it('searches recipes without filtering', async () => {
1211
const { getRecipeNames } = createComponent();
1312

14-
expect(await getRecipeNames()).toEqual(['Burger', 'Salad']);
13+
expect(getRecipeNames()).toEqual(['Burger', 'Salad']);
1514
});
1615

1716
it('filters recipes', async () => {
1817
const { getRecipeNames, setKeywords } = createComponent();
1918

20-
setKeywords('Burger');
19+
setKeywords('Burg');
2120

22-
expect(await getRecipeNames()).toEqual(['Burger']);
21+
expect(getRecipeNames()).toEqual(['Burger']);
2322
});
2423

2524
function createComponent() {
@@ -37,8 +36,7 @@ describe(RecipeSearch.name, () => {
3736
component.ngOnInit();
3837

3938
return {
40-
async getRecipeNames() {
41-
await whenAppStable();
39+
getRecipeNames() {
4240
return component.recipes?.hasValue
4341
? component.recipes.value.map((recipe) => recipe.name)
4442
: null;

apps/2-test-double-starter/src/app/recipe/recipe-search.integration.spec.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,9 @@ describe(RecipeSearch.name, () => {
1313
});
1414

1515
async function renderComponent() {
16-
TestBed.configureTestingModule({
17-
providers: [provideHttpClient()],
18-
});
19-
const fixture = TestBed.createComponent(RecipeSearch);
16+
TestBed.configureTestingModule({ providers: [provideHttpClient()] });
2017

18+
const fixture = TestBed.createComponent(RecipeSearch);
2119
await fixture.whenStable();
2220

2321
return {

apps/2-test-double-starter/src/app/recipe/recipe-search.isolated.spec.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@ import { RecipeSearch } from './recipe-search.ng';
55

66
describe(RecipeSearch.name, () => {
77
it('searches recipes without filtering', async () => {
8-
const { getRecipeNames } = createComponent();
8+
const { getRecipeNames } = await createComponent();
99

10-
expect(await getRecipeNames()).toEqual(
10+
expect(getRecipeNames()).toEqual(
1111
expect.arrayContaining(['Burger', 'Salad']),
1212
);
1313
});
1414

15-
function createComponent() {
15+
async function createComponent() {
1616
TestBed.configureTestingModule({
1717
providers: [RecipeSearch, provideHttpClient()],
1818
});
@@ -21,9 +21,10 @@ describe(RecipeSearch.name, () => {
2121

2222
component.ngOnInit();
2323

24+
await whenAppStable();
25+
2426
return {
25-
async getRecipeNames() {
26-
await whenAppStable();
27+
getRecipeNames() {
2728
return component.recipes?.map((recipe) => recipe.name);
2829
},
2930
};

apps/3-refactor-solution/src/app/recipe/recipe-search.integration.spec.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,31 @@
11
import { TestBed } from '@angular/core/testing';
22
import { screen } from '@testing-library/dom';
33
import userEvent from '@testing-library/user-event';
4-
import { RecipeSearch } from './recipe-search.ng';
54
import {
65
provideRecipeRepositoryFake,
76
RecipeRepositoryFake,
87
} from './recipe-repository.fake';
8+
import { RecipeSearch } from './recipe-search.ng';
99
import { recipeMother } from './recipe.mother';
1010

1111
describe(RecipeSearch.name, () => {
1212
it('searches recipes without filtering', async () => {
1313
const { getRecipeNames } = await renderComponent();
1414

15-
expect(getRecipeNames()).toEqual(['Burger', 'Salad']);
15+
const els = getRecipeNames();
16+
expect.soft(els).toHaveLength(2);
17+
expect.soft(els[0]).toHaveTextContent('Burger');
18+
expect.soft(els[1]).toHaveTextContent('Salad');
1619
});
1720

1821
it('filters recipes by keywords', async () => {
1922
const { getRecipeNames, setKeywords } = await renderComponent();
2023

2124
await setKeywords('Burg');
2225

23-
expect(getRecipeNames()).toEqual(['Burger']);
26+
const els = getRecipeNames();
27+
expect.soft(els).toHaveLength(1);
28+
expect.soft(els[0]).toHaveTextContent('Burger');
2429
});
2530

2631
async function renderComponent() {
@@ -39,10 +44,11 @@ describe(RecipeSearch.name, () => {
3944

4045
return {
4146
getRecipeNames() {
42-
return screen.queryAllByRole('heading').map((el) => el.textContent);
47+
return screen.queryAllByRole('heading');
4348
},
4449
async setKeywords(keywords: string) {
4550
await userEvent.type(screen.getByLabelText('Keywords'), keywords);
51+
await fixture.whenStable();
4652
},
4753
};
4854
}

apps/3-refactor-solution/src/app/recipe/recipe-search.isolated.spec.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,20 @@ import { recipeMother } from './recipe.mother';
1212

1313
describe(RecipeSearch.name, () => {
1414
it('searches recipes without filtering', async () => {
15-
const { getRecipeNames } = createComponent();
15+
const { getRecipeNames } = await createComponent();
1616

17-
expect(await getRecipeNames()).toEqual(['Burger', 'Salad']);
17+
expect(getRecipeNames()).toEqual(['Burger', 'Salad']);
1818
});
1919

2020
it('filters recipes', async () => {
21-
const { getRecipeNames, setKeywords } = createComponent();
21+
const { getRecipeNames, setKeywords } = await createComponent();
2222

23-
setKeywords('Burger');
23+
await setKeywords('Burg');
2424

25-
expect(await getRecipeNames()).toEqual(['Burger']);
25+
expect(getRecipeNames()).toEqual(['Burger']);
2626
});
2727

28-
function createComponent() {
28+
async function createComponent() {
2929
TestBed.configureTestingModule({
3030
providers: [RecipeSearch, provideRecipeRepositoryFake()],
3131
});
@@ -44,15 +44,17 @@ describe(RecipeSearch.name, () => {
4444
recipes: ResourceRef<Recipe[]>;
4545
};
4646

47+
await whenAppStable();
48+
4749
return {
48-
async getRecipeNames() {
49-
await whenAppStable();
50+
getRecipeNames() {
5051
return component.recipes.hasValue()
5152
? component.recipes.value().map((recipe) => recipe.name)
5253
: null;
5354
},
54-
setKeywords(keywords: string) {
55+
async setKeywords(keywords: string) {
5556
component.filter.set({ keywords });
57+
await whenAppStable();
5658
},
5759
};
5860
}

apps/3-refactor-starter/src/app/recipe/recipe-search.integration.spec.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,31 @@
11
import { TestBed } from '@angular/core/testing';
22
import { screen } from '@testing-library/dom';
33
import userEvent from '@testing-library/user-event';
4-
import { RecipeSearch } from './recipe-search.ng';
54
import {
65
provideRecipeRepositoryFake,
76
RecipeRepositoryFake,
87
} from './recipe-repository.fake';
8+
import { RecipeSearch } from './recipe-search.ng';
99
import { recipeMother } from './recipe.mother';
1010

1111
describe(RecipeSearch.name, () => {
1212
it('searches recipes without filtering', async () => {
1313
const { getRecipeNames } = await renderComponent();
1414

15-
expect(getRecipeNames()).toEqual(['Burger', 'Salad']);
15+
const els = getRecipeNames();
16+
expect.soft(els).toHaveLength(2);
17+
expect.soft(els[0]).toHaveTextContent('Burger');
18+
expect.soft(els[1]).toHaveTextContent('Salad');
1619
});
1720

1821
it('filters recipes by keywords', async () => {
1922
const { getRecipeNames, setKeywords } = await renderComponent();
2023

2124
await setKeywords('Burg');
2225

23-
expect(getRecipeNames()).toEqual(['Burger']);
26+
const els = getRecipeNames();
27+
expect.soft(els).toHaveLength(1);
28+
expect.soft(els[0]).toHaveTextContent('Burger');
2429
});
2530

2631
async function renderComponent() {
@@ -39,10 +44,11 @@ describe(RecipeSearch.name, () => {
3944

4045
return {
4146
getRecipeNames() {
42-
return screen.queryAllByRole('heading').map((el) => el.textContent);
47+
return screen.queryAllByRole('heading');
4348
},
4449
async setKeywords(keywords: string) {
4550
await userEvent.type(screen.getByLabelText('Keywords'), keywords);
51+
await fixture.whenStable();
4652
},
4753
};
4854
}

0 commit comments

Comments
 (0)