Skip to content

Commit 8ed3662

Browse files
committed
test: ✅ add recipe preview glove
1 parent 10e8e7c commit 8ed3662

3 files changed

Lines changed: 44 additions & 22 deletions

File tree

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { screen, within } from '@testing-library/dom';
2+
3+
export const recipePreviewGlove = {
4+
async findAllRecipePreviews() {
5+
const recipePreviewEls = await screen.findAllByTestId('recipe-preview');
6+
return recipePreviewEls.map((el) => {
7+
return {
8+
findName: () => within(el).findByRole('heading', { level: 2 }),
9+
};
10+
});
11+
},
12+
};

apps/302-recipe-search-integration-testing-library-starter/src/app/recipe/recipe-preview.ng.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,11 @@ import type { Recipe } from './recipe';
66
changeDetection: ChangeDetectionStrategy.OnPush,
77
selector: 'wm-recipe-preview',
88
imports: [Card],
9-
template: `<wm-card [pictureUri]="recipe().pictureUri">
10-
<h2 data-testid="recipe-name">{{ recipe().name }}</h2>
9+
template: `<wm-card
10+
data-testid="recipe-preview"
11+
[pictureUri]="recipe().pictureUri"
12+
>
13+
<h2>{{ recipe().name }}</h2>
1114
<ng-content />
1215
</wm-card>`,
1316
styles: [
Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,45 @@
1+
import { InjectionToken, Type } from '@angular/core';
12
import { TestBed } from '@angular/core/testing';
2-
import { By } from '@angular/platform-browser';
3-
import { recipeMother } from '../testing/recipe.mother';
3+
import { recipePreviewGlove } from './recipe-preview.glove';
44
import {
55
provideRecipeRepositoryFake,
66
RecipeRepositoryFake,
77
} from './recipe-repository.fake';
8+
import { recipeMother } from '../testing/recipe.mother';
89
import { RecipeSearch } from './recipe-search.ng';
910

1011
describe(RecipeSearch.name, () => {
1112
it('should search recipes without filtering', async () => {
12-
const { getRecipeNames } = await renderComponent();
13+
await renderComponent();
1314

14-
expect(getRecipeNames()).toEqual(['Burger', 'Salad']);
15+
const recipePreviews = await recipePreviewGlove.findAllRecipePreviews();
16+
expect.soft(recipePreviews).toHaveLength(2);
17+
expect.soft(await recipePreviews[0].findName()).toHaveTextContent('Burger');
18+
expect.soft(await recipePreviews[1].findName()).toHaveTextContent('Salad');
1519
});
1620

1721
async function renderComponent() {
18-
TestBed.configureTestingModule({
19-
providers: [provideRecipeRepositoryFake()],
20-
});
21-
22-
TestBed.inject(RecipeRepositoryFake).setRecipes([
22+
t.configure({ providers: [provideRecipeRepositoryFake()] });
23+
t.inject(RecipeRepositoryFake).setRecipes([
2324
recipeMother.withBasicInfo('Burger').build(),
2425
recipeMother.withBasicInfo('Salad').build(),
2526
]);
26-
27-
const fixture = TestBed.createComponent(RecipeSearch);
28-
await fixture.whenStable();
29-
30-
return {
31-
getRecipeNames() {
32-
return fixture.debugElement
33-
.queryAll(By.css('[data-testid=recipe-name]'))
34-
.map((el) => el.nativeElement.textContent);
35-
},
36-
};
27+
await t.mount(RecipeSearch);
3728
}
3829
});
30+
31+
const t = {
32+
configure(args: Parameters<typeof TestBed.configureTestingModule>[0]) {
33+
TestBed.configureTestingModule(args);
34+
},
35+
inject<T extends Type<unknown>>(
36+
token: T | InjectionToken<T>,
37+
): InstanceType<T> {
38+
return TestBed.inject(token);
39+
},
40+
async mount<CMP extends Type<unknown>>(component: CMP) {
41+
const fixture = TestBed.createComponent(component);
42+
await fixture.whenStable();
43+
return fixture;
44+
},
45+
};

0 commit comments

Comments
 (0)