Skip to content

Commit d972cf0

Browse files
committed
test: tidy up
1 parent dd34d06 commit d972cf0

4 files changed

Lines changed: 86 additions & 114 deletions

File tree

apps/whiskmate/src/app/recipe/recipe-filter-form.ng.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ import {
1111
selector: 'wm-recipe-filter-form',
1212
imports: [ReactiveFormsModule],
1313
template: `<form [formGroup]="form">
14-
<label>
15-
Keywords
16-
<input name="keywords" formControlName="keywords" />
17-
</label>
18-
<button type="submit" (click)="submit()">Search</button>
19-
</form>`,
14+
<label>
15+
Keywords
16+
<input name="keywords" formControlName="keywords" />
17+
</label>
18+
<button type="submit" (click)="submit()">Search</button>
19+
</form>`,
2020
})
2121
export class RecipeFilterForm {
2222
filterChange = output<RecipeFilterCriteria>();
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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+
}

apps/whiskmate/src/app/recipe/recipe-search.ng.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,18 @@
11
import {
22
ChangeDetectionStrategy,
33
Component,
4-
computed,
54
inject,
65
signal,
76
} from '@angular/core';
8-
import { rxResource, toObservable, toSignal } from '@angular/core/rxjs-interop';
9-
import { of, switchMap } from 'rxjs';
7+
import { rxResource } from '@angular/core/rxjs-interop';
108
import { Catalog } from '../shared/catalog.ng';
119
import {
1210
createDefaultRecipeFilterCriteria,
1311
type RecipeFilterCriteria,
1412
} from './recipe-filter-criteria';
1513
import { RecipeFilterForm } from './recipe-filter-form.ng';
16-
import { RecipeRepository } from './recipe-repository/recipe-repository';
1714
import { RecipePreview } from './recipe-preview.ng';
15+
import { RecipeRepository } from './recipe-repository/recipe-repository';
1816

1917
@Component({
2018
changeDetection: ChangeDetectionStrategy.OnPush,

apps/whiskmate/src/app/recipe/recipe-search.spec.ts

Lines changed: 0 additions & 104 deletions
This file was deleted.

0 commit comments

Comments
 (0)