|
| 1 | +import { createPages } from "../create-pages" |
| 2 | +import { store, emitter } from "../../redux" |
| 3 | +import { actions } from "../../redux/actions" |
| 4 | +import apiRunnerNode from "../../utils/api-runner-node" |
| 5 | +import * as path from "path" |
| 6 | + |
| 7 | +jest.mock(`../../utils/api-runner-node`) |
| 8 | + |
| 9 | +jest.mock(`../../utils/js-chunk-names`, () => { |
| 10 | + return { generateComponentChunkName: (): string => `--mocked--` } |
| 11 | +}) |
| 12 | + |
| 13 | +let mockAPIs = {} |
| 14 | + |
| 15 | +const component = path.join(process.cwd(), `wat`) |
| 16 | + |
| 17 | +const testPlugin = { |
| 18 | + name: `gatsby-source-test`, |
| 19 | + version: `1.0.0`, |
| 20 | +} |
| 21 | + |
| 22 | +describe(`createPages service cleans up not recreated pages`, () => { |
| 23 | + let RealDateNow |
| 24 | + let DateNowCallCount = 0 |
| 25 | + |
| 26 | + let createPagesRun = 1 |
| 27 | + let createPagesStatefullyRun = 1 |
| 28 | + |
| 29 | + let createPagesHook |
| 30 | + let createPagesStatefullyHook |
| 31 | + |
| 32 | + let deletePageActions |
| 33 | + |
| 34 | + function onDeletePage(deletePageAction): void { |
| 35 | + deletePageActions.push(deletePageAction) |
| 36 | + } |
| 37 | + |
| 38 | + beforeAll(() => { |
| 39 | + RealDateNow = Date.now |
| 40 | + Date.now = jest.fn(() => ++DateNowCallCount) |
| 41 | + apiRunnerNode.mockImplementation((apiName, opts = {}) => { |
| 42 | + if (mockAPIs[apiName]) { |
| 43 | + return mockAPIs[apiName]( |
| 44 | + { |
| 45 | + actions: Object.keys(actions).reduce((acc, actionName) => { |
| 46 | + acc[actionName] = (...args): any => |
| 47 | + store.dispatch(actions[actionName](...args, testPlugin, opts)) |
| 48 | + return acc |
| 49 | + }, {}), |
| 50 | + }, |
| 51 | + {} |
| 52 | + ) |
| 53 | + } |
| 54 | + return undefined |
| 55 | + }) |
| 56 | + |
| 57 | + createPagesHook = mockAPIs[`createPages`] = jest.fn( |
| 58 | + ({ actions }, _pluginOptions) => { |
| 59 | + actions.createPage({ |
| 60 | + path: `/stateless/stable`, |
| 61 | + component, |
| 62 | + }) |
| 63 | + actions.createPage({ |
| 64 | + path: `/stateless/dynamic/${createPagesRun}`, |
| 65 | + component, |
| 66 | + }) |
| 67 | + createPagesRun++ |
| 68 | + } |
| 69 | + ) |
| 70 | + |
| 71 | + createPagesStatefullyHook = mockAPIs[`createPagesStatefully`] = jest.fn( |
| 72 | + ({ actions }, _pluginOptions) => { |
| 73 | + actions.createPage({ |
| 74 | + path: `/stateful/stable`, |
| 75 | + component, |
| 76 | + }) |
| 77 | + actions.createPage({ |
| 78 | + path: `/stateful/dynamic/${createPagesStatefullyRun}`, |
| 79 | + component, |
| 80 | + }) |
| 81 | + createPagesStatefullyRun++ |
| 82 | + } |
| 83 | + ) |
| 84 | + |
| 85 | + emitter.on(`DELETE_PAGE`, onDeletePage) |
| 86 | + }) |
| 87 | + |
| 88 | + beforeEach(() => { |
| 89 | + createPagesRun = 1 |
| 90 | + createPagesStatefullyRun = 1 |
| 91 | + createPagesHook.mockClear() |
| 92 | + createPagesStatefullyHook.mockClear() |
| 93 | + deletePageActions = [] |
| 94 | + |
| 95 | + store.dispatch({ type: `DELETE_CACHE` }) |
| 96 | + }) |
| 97 | + |
| 98 | + afterAll(() => { |
| 99 | + Date.now = RealDateNow |
| 100 | + mockAPIs = {} |
| 101 | + emitter.off(`DELETE_PAGE`, onDeletePage) |
| 102 | + }) |
| 103 | + |
| 104 | + it.each([ |
| 105 | + [`From cold cache`, { cacheStatus: `COLD` }], |
| 106 | + [`From warm cache`, { cacheStatus: `WARM` }], |
| 107 | + ])(`%s`, async (_, { cacheStatus }) => { |
| 108 | + expect(deletePageActions).toEqual([]) |
| 109 | + expect(store.getState().pages.size).toEqual(0) |
| 110 | + |
| 111 | + if (cacheStatus === `WARM`) { |
| 112 | + // add some junk |
| 113 | + store.dispatch( |
| 114 | + actions.createPage( |
| 115 | + { |
| 116 | + path: `/stateless/junk`, |
| 117 | + component, |
| 118 | + context: {}, |
| 119 | + }, |
| 120 | + testPlugin |
| 121 | + ) |
| 122 | + ) |
| 123 | + store.dispatch( |
| 124 | + actions.createPage( |
| 125 | + { |
| 126 | + path: `/stateful/junk`, |
| 127 | + component, |
| 128 | + context: {}, |
| 129 | + }, |
| 130 | + testPlugin, |
| 131 | + { |
| 132 | + traceId: `initial-createPagesStatefully`, |
| 133 | + } |
| 134 | + ) |
| 135 | + ) |
| 136 | + |
| 137 | + expect(store.getState().pages.size).toEqual(2) |
| 138 | + expect(Array.from(store.getState().pages.keys())).toEqual([ |
| 139 | + `/stateless/junk`, |
| 140 | + `/stateful/junk`, |
| 141 | + ]) |
| 142 | + expect( |
| 143 | + store.getState().pages.get(`/stateless/junk`) |
| 144 | + .isCreatedByStatefulCreatePages |
| 145 | + ).toEqual(false) |
| 146 | + expect( |
| 147 | + store.getState().pages.get(`/stateful/junk`) |
| 148 | + .isCreatedByStatefulCreatePages |
| 149 | + ).toEqual(true) |
| 150 | + } else { |
| 151 | + expect(store.getState().pages.size).toEqual(0) |
| 152 | + } |
| 153 | + |
| 154 | + expect(mockAPIs[`createPages`]).toHaveBeenCalledTimes(0) |
| 155 | + expect(mockAPIs[`createPagesStatefully`]).toHaveBeenCalledTimes(0) |
| 156 | + |
| 157 | + await createPages({ store, shouldRunCreatePagesStatefully: true }) |
| 158 | + |
| 159 | + expect(mockAPIs[`createPages`]).toHaveBeenCalledTimes(1) |
| 160 | + expect(mockAPIs[`createPagesStatefully`]).toHaveBeenCalledTimes(1) |
| 161 | + expect(store.getState().pages.size).toEqual(4) |
| 162 | + expect(Array.from(store.getState().pages.keys())).toEqual( |
| 163 | + expect.arrayContaining([ |
| 164 | + `/stateless/stable`, |
| 165 | + `/stateless/dynamic/1`, |
| 166 | + `/stateful/stable`, |
| 167 | + `/stateful/dynamic/1`, |
| 168 | + ]) |
| 169 | + ) |
| 170 | + |
| 171 | + if (cacheStatus === `WARM`) { |
| 172 | + // "junk" pages were not recreated, so we expect DELETE_PAGE action to be emitted for those |
| 173 | + expect(deletePageActions).toEqual( |
| 174 | + expect.arrayContaining([ |
| 175 | + expect.objectContaining({ |
| 176 | + type: `DELETE_PAGE`, |
| 177 | + payload: expect.objectContaining({ |
| 178 | + path: `/stateless/junk`, |
| 179 | + }), |
| 180 | + }), |
| 181 | + ]) |
| 182 | + ) |
| 183 | + expect(deletePageActions).toEqual( |
| 184 | + expect.arrayContaining([ |
| 185 | + expect.objectContaining({ |
| 186 | + type: `DELETE_PAGE`, |
| 187 | + payload: expect.objectContaining({ |
| 188 | + path: `/stateful/junk`, |
| 189 | + }), |
| 190 | + }), |
| 191 | + ]) |
| 192 | + ) |
| 193 | + } |
| 194 | + |
| 195 | + await createPages({ store, shouldRunCreatePagesStatefully: false }) |
| 196 | + |
| 197 | + // createPagesStatefully should not be called and stateful pages should remain as they were before calling `createPages` service |
| 198 | + expect(mockAPIs[`createPages`]).toHaveBeenCalledTimes(2) |
| 199 | + expect(mockAPIs[`createPagesStatefully`]).toHaveBeenCalledTimes(1) |
| 200 | + expect(store.getState().pages.size).toEqual(4) |
| 201 | + |
| 202 | + expect(Array.from(store.getState().pages.keys())).toEqual( |
| 203 | + expect.arrayContaining([ |
| 204 | + `/stateless/stable`, |
| 205 | + `/stateless/dynamic/2`, |
| 206 | + `/stateful/stable`, |
| 207 | + `/stateful/dynamic/1`, |
| 208 | + ]) |
| 209 | + ) |
| 210 | + |
| 211 | + // 1st dynamic page was not recreated so we expect that we emitted DELETE_PAGE action |
| 212 | + expect(deletePageActions).toEqual( |
| 213 | + expect.arrayContaining([ |
| 214 | + expect.objectContaining({ |
| 215 | + type: `DELETE_PAGE`, |
| 216 | + payload: expect.objectContaining({ |
| 217 | + path: `/stateless/dynamic/1`, |
| 218 | + }), |
| 219 | + }), |
| 220 | + ]) |
| 221 | + ) |
| 222 | + }) |
| 223 | +}) |
0 commit comments