forked from tomalaforge/angular-challenges
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.component.spec.ts
More file actions
86 lines (72 loc) · 2.84 KB
/
app.component.spec.ts
File metadata and controls
86 lines (72 loc) · 2.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import { render, screen } from '@testing-library/angular';
import userEvent from '@testing-library/user-event';
import { AppComponent } from './app.component';
import { appRoutes } from './app.routes';
import exp = require('constants');
describe.only('AppComponent', () => {
const search = async (text: string) => {
await render(AppComponent, { routes: appRoutes });
const searchControl = screen.getByRole('textbox', {
name: /search book by author or title/i,
});
const borrowBtn = screen.getByRole('button', { name: /borrow/i });
await userEvent.type(searchControl, text);
await userEvent.click(borrowBtn);
};
describe('Given no search criteria', () => {
it('Then shows error message and disabled button', async () => {
await render(AppComponent, { routes: appRoutes });
const searchControl = screen.getByRole('textbox', {
name: /search book by author or title/i,
});
userEvent.clear(searchControl);
expect(screen.getByTestId('search-error-msg')).toBeInTheDocument();
expect(screen.getByRole('button', { name: /borrow/i })).toBeDisabled();
});
});
describe('Given a search criteria with no book match', () => {
it('Then shows No book found', async () => {
await search('NonExistingBook');
expect(screen.getByTestId('no-book-found-msg')).toBeInTheDocument();
});
});
describe('Given a search criteria with one book match', () => {
it('Then shows One book and no error', async () => {
await search('1984');
expect(screen.queryByTestId('search-error-msg')).not.toBeInTheDocument();
expect(screen.getByText(/1984 by George Orwell/i)).toBeInTheDocument();
});
});
describe('Given a search criteria in Uppercase with one book match', () => {
it('Then shows One book and no error', async () => {
await search('HOBBIT');
expect(screen.queryByTestId('search-error-msg')).not.toBeInTheDocument();
expect(
screen.getByText(/The Hobbit by J.R.R. Tolkien/i),
).toBeInTheDocument();
});
});
describe('Given a search criteria with multple books matches', () => {
it('Then shows a list of books', async () => {
await search('the');
expect(
screen.getByText(/The Hobbit by J.R.R. Tolkien/i),
).toBeInTheDocument();
expect(
screen.getByText(/The Catcher in the Rye by J.D. Salinger/i),
).toBeInTheDocument();
expect(
screen.getByText(/The Lord of the Rings by J.R.R. Tolkien/i),
).toBeInTheDocument();
expect(
screen.getByText(/The Great Gats by F. Scott Fitzgerald/i),
).toBeInTheDocument();
expect(
screen.getByText(
/Harry Potter and the Philosopher's Stone by J.K. Rowling/i,
),
).toBeInTheDocument();
expect(screen.getByText(/The Hunger Games/i)).toBeInTheDocument();
});
});
});