|
1 | 1 | /* global cy */
|
2 | 2 |
|
| 3 | +let spy |
| 4 | +Cypress.on(`window:before:load`, win => { |
| 5 | + spy = cy.spy(win.console, `error`).as(`errorMessage`) |
| 6 | +}) |
| 7 | + |
| 8 | +Cypress.on(`uncaught:exception`, (err, runnable) => { |
| 9 | + // returning false here prevents Cypress from |
| 10 | + // failing the test |
| 11 | + return false |
| 12 | +}) |
| 13 | + |
3 | 14 | const getRandomInt = (min, max) => {
|
4 | 15 | min = Math.ceil(min)
|
5 | 16 | max = Math.floor(max)
|
6 | 17 | return Math.floor(Math.random() * (max - min)) + min
|
7 | 18 | }
|
8 | 19 |
|
9 |
| -const createMockCompilationHash = () => |
10 |
| - [...Array(20)] |
| 20 | +const createMockCompilationHash = () => { |
| 21 | + const hash = [...Array(20)] |
11 | 22 | .map(a => getRandomInt(0, 16))
|
12 | 23 | .map(k => k.toString(16))
|
13 | 24 | .join(``)
|
| 25 | + cy.log({ hash }) |
| 26 | + return hash |
| 27 | +} |
14 | 28 |
|
15 | 29 | describe(`Webpack Compilation Hash tests`, () => {
|
16 | 30 | it(`should render properly`, () => {
|
17 | 31 | cy.visit(`/`).waitForRouteChange()
|
18 | 32 | })
|
19 | 33 |
|
20 |
| - // This covers the case where a user loads a gatsby site and then |
21 |
| - // the site is changed resulting in a webpack recompile and a |
22 |
| - // redeploy. This could result in a mismatch between the page-data |
23 |
| - // and the component. To protect against this, when gatsby loads a |
24 |
| - // new page-data.json, it refreshes the page if it's webpack |
25 |
| - // compilation hash differs from the one on on the window object |
26 |
| - // (which was set on initial page load) |
27 |
| - // |
28 |
| - // Since initial page load results in all links being prefetched, we |
29 |
| - // have to navigate to a non-prefetched page to test this. Thus the |
30 |
| - // `deep-link-page`. |
31 |
| - // |
32 |
| - // We simulate a rebuild by updating all page-data.jsons and page |
33 |
| - // htmls with the new hash. It's not pretty, but it's easier than |
34 |
| - // figuring out how to perform an actual rebuild while cypress is |
35 |
| - // running. See ../plugins/compilation-hash.js for the |
36 |
| - // implementation |
37 |
| - it.skip(`should reload page if build occurs in background`, () => { |
38 |
| - cy.window().then(window => { |
39 |
| - const oldHash = window.___webpackCompilationHash |
40 |
| - expect(oldHash).to.not.eq(undefined) |
41 |
| - |
| 34 | + // Service worker is handling requests so this one is cached by previous runs |
| 35 | + if (!Cypress.env(`TEST_PLUGIN_OFFLINE`)) { |
| 36 | + // This covers the case where a user loads a gatsby site and then |
| 37 | + // the site is changed resulting in a webpack recompile and a |
| 38 | + // redeploy. This could result in a mismatch between the page-data |
| 39 | + // and the component. To protect against this, when gatsby loads a |
| 40 | + // new page-data.json, it refreshes the page if it's webpack |
| 41 | + // compilation hash differs from the one on on the window object |
| 42 | + // (which was set on initial page load) |
| 43 | + // |
| 44 | + // Since initial page load results in all links being prefetched, we |
| 45 | + // have to navigate to a non-prefetched page to test this. Thus the |
| 46 | + // `deep-link-page`. |
| 47 | + // |
| 48 | + // We simulate a rebuild by intercepting app-data request and responding with random hash |
| 49 | + it(`should reload page on navigation if build occurs in background`, () => { |
42 | 50 | const mockHash = createMockCompilationHash()
|
43 | 51 |
|
44 |
| - // Simulate a new webpack build |
45 |
| - cy.task(`overwriteWebpackCompilationHash`, mockHash).then(() => { |
46 |
| - cy.getTestElement(`compilation-hash`).click() |
47 |
| - cy.waitForRouteChange() |
| 52 | + cy.visit(`/`).waitForRouteChange() |
48 | 53 |
|
49 |
| - // Navigate into a non-prefetched page |
50 |
| - cy.getTestElement(`deep-link-page`).click() |
51 |
| - cy.waitForRouteChange() |
| 54 | + let didMock = false |
| 55 | + cy.intercept("/app-data.json", req => { |
| 56 | + if (!didMock) { |
| 57 | + req.reply({ |
| 58 | + webpackCompilationHash: mockHash, |
| 59 | + }) |
| 60 | + didMock = true |
| 61 | + } |
| 62 | + }).as(`appDataFetch`) |
52 | 63 |
|
53 |
| - // If the window compilation hash has changed, we know the |
54 |
| - // page was refreshed |
55 |
| - cy.window().its(`___webpackCompilationHash`).should(`equal`, mockHash) |
| 64 | + cy.window().then(window => { |
| 65 | + // just setting some property on the window |
| 66 | + // we will later assert that property to know wether |
| 67 | + // browser reload happened or not. |
| 68 | + window.notReloaded = true |
| 69 | + window.___navigate(`/deep-link-page/`) |
56 | 70 | })
|
57 | 71 |
|
58 |
| - // Cleanup |
59 |
| - cy.task(`overwriteWebpackCompilationHash`, oldHash) |
| 72 | + cy.waitForRouteChange() |
| 73 | + |
| 74 | + // we expect reload to happen so our window property shouldn't be set anymore |
| 75 | + cy.window().its(`notReloaded`).should(`not.equal`, true) |
| 76 | + |
| 77 | + // let's make sure we actually see the content |
| 78 | + cy.contains( |
| 79 | + `StaticQuery in wrapRootElement test (should show site title):Gatsby Default Starter` |
| 80 | + ) |
60 | 81 | })
|
61 |
| - }) |
| 82 | + |
| 83 | + // This covers the case where user user loads "outdated" html from some kind of cache |
| 84 | + // and our data files (page-data and app-data) are for newer built. |
| 85 | + // We will mock both app-data (to change the hash) as well as example page-data |
| 86 | + // to simulate changes to static query hashes between builds. |
| 87 | + it(`should force reload page if on initial load the html is not matching newest app/page-data`, () => { |
| 88 | + const mockHash = createMockCompilationHash() |
| 89 | + |
| 90 | + // trying to intercept just `/` seems to intercept all routes |
| 91 | + // so intercepting same thing just with regex |
| 92 | + cy.intercept(/^\/$/).as(`indexFetch`) |
| 93 | + |
| 94 | + // We will mock `app-data` and `page-data` json responses one time (for initial load) |
| 95 | + let shouldMockAppDataRequests = true |
| 96 | + let shouldMockPageDataRequests = true |
| 97 | + cy.intercept("/app-data.json", req => { |
| 98 | + if (shouldMockAppDataRequests) { |
| 99 | + req.reply({ |
| 100 | + webpackCompilationHash: mockHash, |
| 101 | + }) |
| 102 | + shouldMockAppDataRequests = false |
| 103 | + } |
| 104 | + }).as(`appDataFetch`) |
| 105 | + |
| 106 | + cy.readFile(`public/page-data/compilation-hash/page-data.json`).then( |
| 107 | + originalPageData => { |
| 108 | + cy.intercept("/page-data/index/page-data.json", req => { |
| 109 | + if (shouldMockPageDataRequests) { |
| 110 | + req.reply({ |
| 111 | + ...originalPageData, |
| 112 | + // setting this to empty array should break runtime with |
| 113 | + // either placeholder "Loading (StaticQuery)" (for <StaticQuery> component) |
| 114 | + // or thrown error "The result of this StaticQuery could not be fetched." (for `useStaticQuery` hook) |
| 115 | + staticQueryHashes: [], |
| 116 | + }) |
| 117 | + shouldMockPageDataRequests = false |
| 118 | + } |
| 119 | + }).as(`pageDataFetch`) |
| 120 | + } |
| 121 | + ) |
| 122 | + |
| 123 | + cy.visit(`/`) |
| 124 | + cy.wait(1500) |
| 125 | + |
| 126 | + // <StaticQuery> component case |
| 127 | + cy.contains("Loading (StaticQuery)").should("not.exist") |
| 128 | + |
| 129 | + // useStaticQuery hook case |
| 130 | + cy.get(`@errorMessage`).should(`not.called`) |
| 131 | + |
| 132 | + // let's make sure we actually see the content |
| 133 | + cy.contains( |
| 134 | + `StaticQuery in wrapRootElement test (should show site title):Gatsby Default Starter` |
| 135 | + ) |
| 136 | + |
| 137 | + cy.get("@indexFetch.all").should("have.length", 2) |
| 138 | + cy.get("@appDataFetch.all").should("have.length", 2) |
| 139 | + cy.get("@pageDataFetch.all").should("have.length", 2) |
| 140 | + }) |
| 141 | + |
| 142 | + it(`should not force reload indefinitely`, () => { |
| 143 | + const mockHash = createMockCompilationHash() |
| 144 | + |
| 145 | + // trying to intercept just `/` seems to intercept all routes |
| 146 | + // so intercepting same thing just with regex |
| 147 | + cy.intercept(/^\/$/).as(`indexFetch`) |
| 148 | + |
| 149 | + // We will mock `app-data` and `page-data` json responses permanently |
| 150 | + cy.intercept("/app-data.json", req => { |
| 151 | + req.reply({ |
| 152 | + webpackCompilationHash: mockHash, |
| 153 | + }) |
| 154 | + }).as(`appDataFetch`) |
| 155 | + |
| 156 | + cy.readFile(`public/page-data/index/page-data.json`).then( |
| 157 | + originalPageData => { |
| 158 | + cy.intercept("/page-data/index/page-data.json", req => { |
| 159 | + req.reply({ |
| 160 | + ...originalPageData, |
| 161 | + // setting this to empty array should break runtime with |
| 162 | + // either placeholder "Loading (StaticQuery)" (for <StaticQuery> component) |
| 163 | + // or thrown error "The result of this StaticQuery could not be fetched." (for `useStaticQuery` hook) |
| 164 | + staticQueryHashes: [], |
| 165 | + }) |
| 166 | + }).as(`pageDataFetch`) |
| 167 | + } |
| 168 | + ) |
| 169 | + |
| 170 | + cy.visit(`/`) |
| 171 | + |
| 172 | + cy.wait(1500) |
| 173 | + |
| 174 | + cy.get("@indexFetch.all").should("have.length", 2) |
| 175 | + cy.get("@appDataFetch.all").should("have.length", 2) |
| 176 | + cy.get("@pageDataFetch.all").should("have.length", 2) |
| 177 | + }) |
| 178 | + } |
62 | 179 | })
|
0 commit comments