|
| 1 | +import { queryRunningMachine } from "../query-running" |
| 2 | +import { queryActions } from "../query-running/actions" |
| 3 | +import { interpret, Interpreter } from "xstate" |
| 4 | +import { IProgram } from "../../commands/types" |
| 5 | +import { store } from "../../redux" |
| 6 | +import reporter from "gatsby-cli/lib/reporter" |
| 7 | +import pDefer from "p-defer" |
| 8 | +import { IGroupedQueryIds } from "../../services/types" |
| 9 | + |
| 10 | +const services = { |
| 11 | + extractQueries: jest.fn(async () => {}), |
| 12 | + writeOutRequires: jest.fn(async () => {}), |
| 13 | + calculateDirtyQueries: jest.fn( |
| 14 | + async (): Promise<{ |
| 15 | + queryIds: IGroupedQueryIds |
| 16 | + }> => { |
| 17 | + return { |
| 18 | + queryIds: { |
| 19 | + pageQueryIds: [], |
| 20 | + staticQueryIds: [], |
| 21 | + }, |
| 22 | + } |
| 23 | + } |
| 24 | + ), |
| 25 | +} |
| 26 | + |
| 27 | +const machine = queryRunningMachine.withConfig( |
| 28 | + { |
| 29 | + actions: queryActions, |
| 30 | + services, |
| 31 | + }, |
| 32 | + { |
| 33 | + program: {} as IProgram, |
| 34 | + store, |
| 35 | + reporter, |
| 36 | + pendingQueryRuns: new Set([`/`]), |
| 37 | + } |
| 38 | +) |
| 39 | + |
| 40 | +const resetMocks = (mocks: Record<string, jest.Mock>): void => |
| 41 | + Object.values(mocks).forEach(mock => mock.mockClear()) |
| 42 | + |
| 43 | +const resetAllMocks = (): void => { |
| 44 | + resetMocks(services) |
| 45 | +} |
| 46 | + |
| 47 | +const finished = async ( |
| 48 | + service: Interpreter<any, any, any, any, any> |
| 49 | +): Promise<void> => |
| 50 | + new Promise(resolve => { |
| 51 | + service.onDone(() => resolve()) |
| 52 | + }) |
| 53 | + |
| 54 | +function debug(service: Interpreter<any, any, any, any, any>): void { |
| 55 | + let last: any |
| 56 | + |
| 57 | + service.onTransition(state => { |
| 58 | + if (!last) { |
| 59 | + last = state |
| 60 | + } else if (!state.changed) { |
| 61 | + return |
| 62 | + } |
| 63 | + |
| 64 | + reporter.info( |
| 65 | + `---onTransition---\n${require(`util`).inspect( |
| 66 | + { |
| 67 | + stateValue: state.value, |
| 68 | + event: state.event, |
| 69 | + pendingQueryRuns: state.context.pendingQueryRuns, |
| 70 | + changedStateValue: state.value !== last.value, |
| 71 | + }, |
| 72 | + { depth: Infinity } |
| 73 | + )}` |
| 74 | + ) |
| 75 | + last = state |
| 76 | + }) |
| 77 | +} |
| 78 | + |
| 79 | +expect.extend({ |
| 80 | + toHaveInSet(received, item) { |
| 81 | + if (received.has(item)) { |
| 82 | + return { |
| 83 | + pass: true, |
| 84 | + message: (): string => |
| 85 | + `Expected ${Array.from(received)} not to contain ${item}`, |
| 86 | + } |
| 87 | + } else { |
| 88 | + return { |
| 89 | + pass: false, |
| 90 | + message: (): string => |
| 91 | + `Expected ${Array.from(received)} not to contain ${item}`, |
| 92 | + } |
| 93 | + } |
| 94 | + }, |
| 95 | +}) |
| 96 | + |
| 97 | +/* eslint-disable @typescript-eslint/no-namespace */ |
| 98 | +declare global { |
| 99 | + namespace jest { |
| 100 | + // eslint-disable-next-line @typescript-eslint/naming-convention |
| 101 | + interface Expect { |
| 102 | + toHaveInSet(item: any): any |
| 103 | + } |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +describe(`query-running state machine`, () => { |
| 108 | + beforeEach(() => { |
| 109 | + resetAllMocks() |
| 110 | + }) |
| 111 | + |
| 112 | + it(`initialises`, async () => { |
| 113 | + const service = interpret(machine) |
| 114 | + // debug(service) |
| 115 | + |
| 116 | + service.start() |
| 117 | + expect(service.state.value).toBe(`extractingQueries`) |
| 118 | + }) |
| 119 | + |
| 120 | + it(`doesn't drop pendingQueryRuns that were added during calculation of dirty queries`, async () => { |
| 121 | + const deferred = pDefer<{ |
| 122 | + queryIds: IGroupedQueryIds |
| 123 | + }>() |
| 124 | + const waitForExecutionOfCalcDirtyQueries = pDefer() |
| 125 | + |
| 126 | + services.calculateDirtyQueries.mockImplementation( |
| 127 | + async (): Promise<{ |
| 128 | + queryIds: IGroupedQueryIds |
| 129 | + }> => { |
| 130 | + waitForExecutionOfCalcDirtyQueries.resolve() |
| 131 | + |
| 132 | + // allow test to execute some code before resuming service |
| 133 | + |
| 134 | + await deferred.promise |
| 135 | + |
| 136 | + return { |
| 137 | + queryIds: { |
| 138 | + pageQueryIds: [], |
| 139 | + staticQueryIds: [], |
| 140 | + }, |
| 141 | + } |
| 142 | + } |
| 143 | + ) |
| 144 | + |
| 145 | + const service = interpret(machine) |
| 146 | + // debug(service) |
| 147 | + |
| 148 | + service.send({ |
| 149 | + type: `QUERY_RUN_REQUESTED`, |
| 150 | + payload: { |
| 151 | + pagePath: `/bar/`, |
| 152 | + }, |
| 153 | + }) |
| 154 | + |
| 155 | + service.start() |
| 156 | + |
| 157 | + await waitForExecutionOfCalcDirtyQueries.promise |
| 158 | + |
| 159 | + // we are in middle of execution of calcDirtyQueries service |
| 160 | + // let's dispatch QUERY_RUN_REQUESTED for page /foo/ |
| 161 | + service.send({ |
| 162 | + type: `QUERY_RUN_REQUESTED`, |
| 163 | + payload: { |
| 164 | + pagePath: `/foo/`, |
| 165 | + }, |
| 166 | + }) |
| 167 | + |
| 168 | + deferred.resolve() |
| 169 | + |
| 170 | + // let state machine reach final state |
| 171 | + await finished(service) |
| 172 | + |
| 173 | + // let's make sure that we called calculateDirtyQueries service |
| 174 | + // with every page that was requested, even if page was requested |
| 175 | + // while we were executing calcDirtyQueries service |
| 176 | + expect(services.calculateDirtyQueries).toHaveBeenCalledWith( |
| 177 | + expect.objectContaining({ |
| 178 | + currentlyHandledPendingQueryRuns: expect.toHaveInSet(`/`), |
| 179 | + }), |
| 180 | + expect.anything(), |
| 181 | + expect.anything() |
| 182 | + ) |
| 183 | + |
| 184 | + expect(services.calculateDirtyQueries).toHaveBeenCalledWith( |
| 185 | + expect.objectContaining({ |
| 186 | + currentlyHandledPendingQueryRuns: expect.toHaveInSet(`/bar/`), |
| 187 | + }), |
| 188 | + expect.anything(), |
| 189 | + expect.anything() |
| 190 | + ) |
| 191 | + |
| 192 | + expect(services.calculateDirtyQueries).toHaveBeenCalledWith( |
| 193 | + expect.objectContaining({ |
| 194 | + currentlyHandledPendingQueryRuns: expect.toHaveInSet(`/foo/`), |
| 195 | + }), |
| 196 | + expect.anything(), |
| 197 | + expect.anything() |
| 198 | + ) |
| 199 | + }) |
| 200 | +}) |
0 commit comments