|
| 1 | +import { TestBed } from '@angular/core/testing'; |
| 2 | +import { describe, expect, it } from 'vitest'; |
| 3 | +import { page } from 'vitest/browser'; |
| 4 | +import type { Recipe } from './recipe'; |
| 5 | +import { |
| 6 | + provideRecipeRepositoryFake, |
| 7 | + RecipeRepositoryFake, |
| 8 | +} from './recipe-repository/recipe-repository.fake'; |
| 9 | +import { RecipeSearch } from './recipe-search.ng'; |
| 10 | +import { recipeMother } from './recipe.mother'; |
| 11 | + |
| 12 | +describe(RecipeSearch.name, () => { |
| 13 | + it('shows prompt before search', async () => { |
| 14 | + const burger = recipeMother.withBasicInfo('Burger').build(); |
| 15 | + const salad = recipeMother.withBasicInfo('Salad').build(); |
| 16 | + |
| 17 | + const { recipeNames, keywordsInput, searchButton } = |
| 18 | + await setUpRecipeSearch([burger, salad]); |
| 19 | + |
| 20 | + await expect.element(keywordsInput).toBeVisible(); |
| 21 | + await expect.element(searchButton).toBeVisible(); |
| 22 | + await expect.element(recipeNames).not.toBeInTheDocument(); |
| 23 | + }); |
| 24 | + |
| 25 | + it('displays results after search', async () => { |
| 26 | + const burger = recipeMother.withBasicInfo('Burger').build(); |
| 27 | + const salad = recipeMother.withBasicInfo('Salad').build(); |
| 28 | + |
| 29 | + const { keywordsInput, searchButton } = await setUpRecipeSearch([ |
| 30 | + burger, |
| 31 | + salad, |
| 32 | + ]); |
| 33 | + |
| 34 | + await keywordsInput.fill(''); |
| 35 | + await searchButton.click(); |
| 36 | + |
| 37 | + await expect |
| 38 | + .element(page.getByTestId('recipe-name').filter({ hasText: 'Burger' })) |
| 39 | + .toBeVisible(); |
| 40 | + await expect |
| 41 | + .element(page.getByTestId('recipe-name').filter({ hasText: 'Salad' })) |
| 42 | + .toBeVisible(); |
| 43 | + }); |
| 44 | + |
| 45 | + it('shows empty state when no matches', async () => { |
| 46 | + const burger = recipeMother.withBasicInfo('Burger').build(); |
| 47 | + |
| 48 | + const { keywordsInput, searchButton, emptyState } = await setUpRecipeSearch( |
| 49 | + [burger], |
| 50 | + ); |
| 51 | + |
| 52 | + await keywordsInput.fill('xyz'); |
| 53 | + await searchButton.click(); |
| 54 | + |
| 55 | + await expect.element(emptyState).toHaveTextContent(/no recipes found/i); |
| 56 | + }); |
| 57 | +}); |
| 58 | + |
| 59 | +async function setUpRecipeSearch(recipes: Recipe[]) { |
| 60 | + TestBed.resetTestingModule(); |
| 61 | + |
| 62 | + await TestBed.configureTestingModule({ |
| 63 | + imports: [RecipeSearch], |
| 64 | + providers: [provideRecipeRepositoryFake()], |
| 65 | + }).compileComponents(); |
| 66 | + |
| 67 | + const repository = TestBed.inject(RecipeRepositoryFake); |
| 68 | + repository.configure({ recipes }); |
| 69 | + |
| 70 | + TestBed.createComponent(RecipeSearch); |
| 71 | + |
| 72 | + return { |
| 73 | + recipeNames: page.getByTestId('recipe-name'), |
| 74 | + emptyState: page.getByTestId('empty-state'), |
| 75 | + keywordsInput: page.getByLabelText('Keywords'), |
| 76 | + searchButton: page.getByRole('button', { name: 'Search' }), |
| 77 | + }; |
| 78 | +} |
0 commit comments