/
Film.stories.tsx
executable file
·118 lines (105 loc) · 2.92 KB
/
Film.stories.tsx
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
import React from 'react'
import { MemoryRouter as Router, Route } from 'react-router-dom'
import { QueryClient, QueryClientProvider } from 'react-query'
import { http, HttpResponse, delay } from 'msw'
import Film from './Film'
const meta = {
title: 'Demos/React Router + RQ/Page Stories/Film',
component: Film,
}
export default meta
const defaultQueryClient = new QueryClient()
export const DefaultBehavior = () => (
<QueryClientProvider client={defaultQueryClient}>
<Router initialEntries={['/films/1']}>
<Route exact path="/films/:filmId">
<Film />
</Route>
</Router>
</QueryClientProvider>
)
const mockedQueryClient = new QueryClient({
defaultOptions: {
queries: {
retry: false,
},
},
})
const MockTemplate = () => (
<QueryClientProvider client={mockedQueryClient}>
<Router initialEntries={['/films/1']}>
<Route exact path="/films/:filmId">
<Film />
</Route>
</Router>
</QueryClientProvider>
)
export const MockedSuccess = {
render: MockTemplate,
parameters: {
msw: {
handlers: [
http.get('https://swapi.dev/api/films/1', () => {
return HttpResponse.json({
title: '(Mocked) A New Hope',
episode_id: 4,
opening_crawl: `Rebel spaceships have won their first victory against the evil Galactic Empire.`,
characters: ['http://swapi.dev/api/people/1/', 'http://swapi.dev/api/people/2/'],
})
}),
http.get('https://swapi.dev/api/people/1', () => {
return HttpResponse.json({
name: '(Mocked) Luke Skywalker',
})
}),
http.get('https://swapi.dev/api/people/2', () => {
return HttpResponse.json({
name: '(Mocked) C-3PO',
})
}),
],
},
},
}
export const MockedFilmApiError = {
render: MockTemplate,
parameters: {
msw: {
handlers: [
http.get('https://swapi.dev/api/films/1', async () => {
await delay(300)
return new HttpResponse(null, {
status: 403,
})
}),
],
},
},
}
export const MockedCharacterApiError = {
render: MockTemplate,
parameters: {
msw: {
handlers: [
http.get('https://swapi.dev/api/people/1', async () => {
return new HttpResponse(null, {
status: 403,
})
}),
http.get('https://swapi.dev/api/people/2', () => {
return HttpResponse.json({
name: '(Mocked) C-3PO',
})
}),
http.get('https://swapi.dev/api/films/1', () => {
return HttpResponse.json({
title: '(Mocked) A New Hope',
episode_id: 4,
opening_crawl: `Rebel spaceships have won their first victory against the evil Galactic Empire.`,
characters: ['http://swapi.dev/api/people/1/', 'http://swapi.dev/api/people/2/'],
})
}),
],
},
},
}