-
Notifications
You must be signed in to change notification settings - Fork 93
/
Copy path09-router.spec.ts
121 lines (100 loc) · 4.32 KB
/
09-router.spec.ts
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import { render, screen } from '@testing-library/angular';
import userEvent from '@testing-library/user-event';
import { DetailComponent, RootComponent, HiddenDetailComponent } from './09-router';
test('it can navigate to routes', async () => {
const user = userEvent.setup();
await render(RootComponent, {
routes: [
{
path: '',
children: [
{
path: 'detail/:id',
component: DetailComponent,
},
{
path: 'hidden-detail',
component: HiddenDetailComponent,
},
],
},
],
});
expect(screen.queryByText(/Detail one/i)).not.toBeInTheDocument();
await user.click(screen.getByRole('link', { name: /load one/i }));
expect(await screen.findByRole('heading', { name: /Detail one/i })).toBeInTheDocument();
await user.click(screen.getByRole('link', { name: /load three/i }));
expect(screen.queryByRole('heading', { name: /Detail one/i })).not.toBeInTheDocument();
expect(await screen.findByRole('heading', { name: /Detail three/i })).toBeInTheDocument();
await user.click(screen.getByRole('link', { name: /back to parent/i }));
expect(screen.queryByRole('heading', { name: /Detail three/i })).not.toBeInTheDocument();
await user.click(screen.getByRole('link', { name: /load two/i }));
expect(await screen.findByRole('heading', { name: /Detail two/i })).toBeInTheDocument();
await user.click(screen.getByRole('link', { name: /hidden x/i }));
expect(await screen.findByText(/You found the treasure!/i)).toBeInTheDocument();
});
test('it can navigate to routes - workaround', async () => {
const { navigate } = await render(RootComponent, {
routes: [
{
path: '',
children: [
{
path: 'detail/:id',
component: DetailComponent,
},
{
path: 'hidden-detail',
component: HiddenDetailComponent,
},
],
},
],
});
expect(screen.queryByText(/Detail one/i)).not.toBeInTheDocument();
await navigate(screen.getByRole('link', { name: /load one/i }));
expect(screen.getByRole('heading', { name: /Detail one/i })).toBeInTheDocument();
await navigate(screen.getByRole('link', { name: /load three/i }));
expect(screen.queryByRole('heading', { name: /Detail one/i })).not.toBeInTheDocument();
expect(screen.getByRole('heading', { name: /Detail three/i })).toBeInTheDocument();
await navigate(screen.getByRole('link', { name: /back to parent/i }));
expect(screen.queryByRole('heading', { name: /Detail three/i })).not.toBeInTheDocument();
await navigate(screen.getByRole('link', { name: /load two/i }));
expect(screen.getByRole('heading', { name: /Detail two/i })).toBeInTheDocument();
await navigate(screen.getByRole('link', { name: /hidden x/i }));
expect(screen.getByText(/You found the treasure!/i)).toBeInTheDocument();
});
test('it can navigate to routes with a base path', async () => {
const basePath = 'base';
const { navigate } = await render(RootComponent, {
routes: [
{
path: basePath,
children: [
{
path: 'detail/:id',
component: DetailComponent,
},
{
path: 'hidden-detail',
component: HiddenDetailComponent,
},
],
},
],
});
expect(screen.queryByRole('heading', { name: /Detail one/i })).not.toBeInTheDocument();
await navigate(screen.getByRole('link', { name: /load one/i }), basePath);
expect(screen.getByRole('heading', { name: /Detail one/i })).toBeInTheDocument();
await navigate(screen.getByRole('link', { name: /load three/i }), basePath);
expect(screen.queryByRole('heading', { name: /Detail one/i })).not.toBeInTheDocument();
expect(screen.getByRole('heading', { name: /Detail three/i })).toBeInTheDocument();
await navigate(screen.getByRole('link', { name: /back to parent/i }));
expect(screen.queryByRole('heading', { name: /Detail three/i })).not.toBeInTheDocument();
// It's possible to just use strings
await navigate('base/detail/two?text=Hello&subtext=World');
expect(screen.getByRole('heading', { name: /Detail two/i })).toBeInTheDocument();
expect(screen.getByText(/Hello World/i)).toBeInTheDocument();
await navigate('/hidden-detail', basePath);
expect(screen.getByText(/You found the treasure!/i)).toBeInTheDocument();
});