-
Notifications
You must be signed in to change notification settings - Fork 10.3k
/
query-running.ts
200 lines (172 loc) · 4.74 KB
/
query-running.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
import { queryRunningMachine } from "../query-running"
import { queryActions } from "../query-running/actions"
import { interpret, Interpreter } from "xstate"
import { IProgram } from "../../commands/types"
import { store } from "../../redux"
import reporter from "gatsby-cli/lib/reporter"
import pDefer from "p-defer"
import { IGroupedQueryIds } from "../../services/types"
const services = {
extractQueries: jest.fn(async () => {}),
writeOutRequires: jest.fn(async () => {}),
calculateDirtyQueries: jest.fn(
async (): Promise<{
queryIds: IGroupedQueryIds
}> => {
return {
queryIds: {
pageQueryIds: [],
staticQueryIds: [],
},
}
}
),
}
const machine = queryRunningMachine.withConfig(
{
actions: queryActions,
services,
},
{
program: {} as IProgram,
store,
reporter,
pendingQueryRuns: new Set([`/`]),
}
)
const resetMocks = (mocks: Record<string, jest.Mock>): void =>
Object.values(mocks).forEach(mock => mock.mockClear())
const resetAllMocks = (): void => {
resetMocks(services)
}
const finished = async (
service: Interpreter<any, any, any, any, any>
): Promise<void> =>
new Promise(resolve => {
service.onDone(() => resolve())
})
function debug(service: Interpreter<any, any, any, any, any>): void {
let last: any
service.onTransition(state => {
if (!last) {
last = state
} else if (!state.changed) {
return
}
reporter.info(
`---onTransition---\n${require(`util`).inspect(
{
stateValue: state.value,
event: state.event,
pendingQueryRuns: state.context.pendingQueryRuns,
changedStateValue: state.value !== last.value,
},
{ depth: Infinity }
)}`
)
last = state
})
}
expect.extend({
toHaveInSet(received, item) {
if (received.has(item)) {
return {
pass: true,
message: (): string =>
`Expected ${Array.from(received)} not to contain ${item}`,
}
} else {
return {
pass: false,
message: (): string =>
`Expected ${Array.from(received)} not to contain ${item}`,
}
}
},
})
/* eslint-disable @typescript-eslint/no-namespace */
declare global {
namespace jest {
// eslint-disable-next-line @typescript-eslint/naming-convention
interface Expect {
toHaveInSet(item: any): any
}
}
}
describe(`query-running state machine`, () => {
beforeEach(() => {
resetAllMocks()
})
it(`initialises`, async () => {
const service = interpret(machine)
// debug(service)
service.start()
expect(service.state.value).toBe(`extractingQueries`)
})
it(`doesn't drop pendingQueryRuns that were added during calculation of dirty queries`, async () => {
const deferred = pDefer<{
queryIds: IGroupedQueryIds
}>()
const waitForExecutionOfCalcDirtyQueries = pDefer()
services.calculateDirtyQueries.mockImplementation(
async (): Promise<{
queryIds: IGroupedQueryIds
}> => {
waitForExecutionOfCalcDirtyQueries.resolve()
// allow test to execute some code before resuming service
await deferred.promise
return {
queryIds: {
pageQueryIds: [],
staticQueryIds: [],
},
}
}
)
const service = interpret(machine)
// debug(service)
service.send({
type: `QUERY_RUN_REQUESTED`,
payload: {
pagePath: `/bar/`,
},
})
service.start()
await waitForExecutionOfCalcDirtyQueries.promise
// we are in middle of execution of calcDirtyQueries service
// let's dispatch QUERY_RUN_REQUESTED for page /foo/
service.send({
type: `QUERY_RUN_REQUESTED`,
payload: {
pagePath: `/foo/`,
},
})
deferred.resolve()
// let state machine reach final state
await finished(service)
// let's make sure that we called calculateDirtyQueries service
// with every page that was requested, even if page was requested
// while we were executing calcDirtyQueries service
expect(services.calculateDirtyQueries).toHaveBeenCalledWith(
expect.objectContaining({
currentlyHandledPendingQueryRuns: expect.toHaveInSet(`/`),
}),
expect.anything(),
expect.anything()
)
expect(services.calculateDirtyQueries).toHaveBeenCalledWith(
expect.objectContaining({
currentlyHandledPendingQueryRuns: expect.toHaveInSet(`/bar/`),
}),
expect.anything(),
expect.anything()
)
expect(services.calculateDirtyQueries).toHaveBeenCalledWith(
expect.objectContaining({
currentlyHandledPendingQueryRuns: expect.toHaveInSet(`/foo/`),
}),
expect.anything(),
expect.anything()
)
})
})