|
| 1 | +import { InjectionToken, Type } from '@angular/core'; |
1 | 2 | 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'; |
4 | 4 | import { |
5 | 5 | provideRecipeRepositoryFake, |
6 | 6 | RecipeRepositoryFake, |
7 | 7 | } from './recipe-repository.fake'; |
| 8 | +import { recipeMother } from '../testing/recipe.mother'; |
8 | 9 | import { RecipeSearch } from './recipe-search.ng'; |
9 | 10 |
|
10 | 11 | describe(RecipeSearch.name, () => { |
11 | 12 | it('should search recipes without filtering', async () => { |
12 | | - const { getRecipeNames } = await renderComponent(); |
| 13 | + await renderComponent(); |
13 | 14 |
|
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'); |
15 | 19 | }); |
16 | 20 |
|
17 | 21 | 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([ |
23 | 24 | recipeMother.withBasicInfo('Burger').build(), |
24 | 25 | recipeMother.withBasicInfo('Salad').build(), |
25 | 26 | ]); |
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); |
37 | 28 | } |
38 | 29 | }); |
| 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