|
| 1 | +/* eslint-env jest */ |
| 2 | +/* global jasmine */ |
| 3 | +import { join } from 'path' |
| 4 | +import { remove } from 'fs-extra' |
| 5 | +import { |
| 6 | + nextBuild, |
| 7 | + nextStart, |
| 8 | + waitFor, |
| 9 | + findPort, |
| 10 | + killApp, |
| 11 | + renderViaHTTP, |
| 12 | +} from 'next-test-utils' |
| 13 | +import cheerio from 'cheerio' |
| 14 | +import webdriver from 'next-webdriver' |
| 15 | + |
| 16 | +jasmine.DEFAULT_TIMEOUT_INTERVAL = 1000 * 60 * 1 |
| 17 | + |
| 18 | +const fixturesDir = join(__dirname, '../../css-fixtures') |
| 19 | + |
| 20 | +describe('CSS Module client-side navigation in Production', () => { |
| 21 | + const appDir = join(fixturesDir, 'multi-module') |
| 22 | + |
| 23 | + beforeAll(async () => { |
| 24 | + await remove(join(appDir, '.next')) |
| 25 | + }) |
| 26 | + |
| 27 | + let appPort |
| 28 | + let app |
| 29 | + beforeAll(async () => { |
| 30 | + await nextBuild(appDir) |
| 31 | + appPort = await findPort() |
| 32 | + app = await nextStart(appDir, appPort) |
| 33 | + }) |
| 34 | + afterAll(async () => { |
| 35 | + await killApp(app) |
| 36 | + }) |
| 37 | + |
| 38 | + it('should be able to client-side navigate from red to blue', async () => { |
| 39 | + let browser |
| 40 | + try { |
| 41 | + browser = await webdriver(appPort, '/red') |
| 42 | + |
| 43 | + await browser.eval(`window.__did_not_ssr = 'make sure this is set'`) |
| 44 | + |
| 45 | + const redColor = await browser.eval( |
| 46 | + `window.getComputedStyle(document.querySelector('#verify-red')).color` |
| 47 | + ) |
| 48 | + expect(redColor).toMatchInlineSnapshot(`"rgb(255, 0, 0)"`) |
| 49 | + |
| 50 | + await browser.elementByCss('#link-blue').click() |
| 51 | + |
| 52 | + await browser.waitForElementByCss('#verify-blue') |
| 53 | + |
| 54 | + const blueColor = await browser.eval( |
| 55 | + `window.getComputedStyle(document.querySelector('#verify-blue')).color` |
| 56 | + ) |
| 57 | + expect(blueColor).toMatchInlineSnapshot(`"rgb(0, 0, 255)"`) |
| 58 | + |
| 59 | + expect(await browser.eval(`window.__did_not_ssr`)).toMatchInlineSnapshot( |
| 60 | + `"make sure this is set"` |
| 61 | + ) |
| 62 | + } finally { |
| 63 | + if (browser) { |
| 64 | + await browser.close() |
| 65 | + } |
| 66 | + } |
| 67 | + }) |
| 68 | + |
| 69 | + it('should be able to client-side navigate from blue to red', async () => { |
| 70 | + const content = await renderViaHTTP(appPort, '/blue') |
| 71 | + const $ = cheerio.load(content) |
| 72 | + |
| 73 | + // Ensure only `/blue` page's CSS is preloaded |
| 74 | + const serverCssPreloads = $('link[rel="preload"][as="style"]') |
| 75 | + expect(serverCssPreloads.length).toBe(1) |
| 76 | + |
| 77 | + let browser |
| 78 | + try { |
| 79 | + browser = await webdriver(appPort, '/blue') |
| 80 | + |
| 81 | + await waitFor(2000) // Ensure hydration |
| 82 | + |
| 83 | + await browser.eval(`window.__did_not_ssr = 'make sure this is set'`) |
| 84 | + |
| 85 | + const redColor = await browser.eval( |
| 86 | + `window.getComputedStyle(document.querySelector('#verify-blue')).color` |
| 87 | + ) |
| 88 | + expect(redColor).toMatchInlineSnapshot(`"rgb(0, 0, 255)"`) |
| 89 | + |
| 90 | + // Check that Red was preloaded |
| 91 | + const result = await browser.eval( |
| 92 | + `[].slice.call(document.querySelectorAll('link[rel="preload"][as="style"]')).map(e=>({href:e.href})).sort()` |
| 93 | + ) |
| 94 | + expect(result.length).toBe(2) |
| 95 | + |
| 96 | + // Check that CSS was not loaded as script |
| 97 | + const cssPreloads = await browser.eval( |
| 98 | + `[].slice.call(document.querySelectorAll('link[rel=preload][href*=".css"]')).map(e=>e.as)` |
| 99 | + ) |
| 100 | + expect(cssPreloads.every(e => e === 'style')).toBe(true) |
| 101 | + |
| 102 | + await browser.elementByCss('#link-red').click() |
| 103 | + |
| 104 | + await browser.waitForElementByCss('#verify-red') |
| 105 | + |
| 106 | + const blueColor = await browser.eval( |
| 107 | + `window.getComputedStyle(document.querySelector('#verify-red')).color` |
| 108 | + ) |
| 109 | + expect(blueColor).toMatchInlineSnapshot(`"rgb(255, 0, 0)"`) |
| 110 | + |
| 111 | + expect(await browser.eval(`window.__did_not_ssr`)).toMatchInlineSnapshot( |
| 112 | + `"make sure this is set"` |
| 113 | + ) |
| 114 | + } finally { |
| 115 | + if (browser) { |
| 116 | + await browser.close() |
| 117 | + } |
| 118 | + } |
| 119 | + }) |
| 120 | + |
| 121 | + it('should be able to client-side navigate from none to red', async () => { |
| 122 | + let browser |
| 123 | + try { |
| 124 | + browser = await webdriver(appPort, '/none') |
| 125 | + |
| 126 | + await browser.eval(`window.__did_not_ssr = 'make sure this is set'`) |
| 127 | + |
| 128 | + await browser.elementByCss('#link-red').click() |
| 129 | + await browser.waitForElementByCss('#verify-red') |
| 130 | + |
| 131 | + const blueColor = await browser.eval( |
| 132 | + `window.getComputedStyle(document.querySelector('#verify-red')).color` |
| 133 | + ) |
| 134 | + expect(blueColor).toMatchInlineSnapshot(`"rgb(255, 0, 0)"`) |
| 135 | + |
| 136 | + expect(await browser.eval(`window.__did_not_ssr`)).toMatchInlineSnapshot( |
| 137 | + `"make sure this is set"` |
| 138 | + ) |
| 139 | + } finally { |
| 140 | + if (browser) { |
| 141 | + await browser.close() |
| 142 | + } |
| 143 | + } |
| 144 | + }) |
| 145 | + |
| 146 | + it('should be able to client-side navigate from none to blue', async () => { |
| 147 | + let browser |
| 148 | + try { |
| 149 | + browser = await webdriver(appPort, '/none') |
| 150 | + |
| 151 | + await browser.eval(`window.__did_not_ssr = 'make sure this is set'`) |
| 152 | + |
| 153 | + await browser.elementByCss('#link-blue').click() |
| 154 | + await browser.waitForElementByCss('#verify-blue') |
| 155 | + |
| 156 | + const blueColor = await browser.eval( |
| 157 | + `window.getComputedStyle(document.querySelector('#verify-blue')).color` |
| 158 | + ) |
| 159 | + expect(blueColor).toMatchInlineSnapshot(`"rgb(0, 0, 255)"`) |
| 160 | + |
| 161 | + expect(await browser.eval(`window.__did_not_ssr`)).toMatchInlineSnapshot( |
| 162 | + `"make sure this is set"` |
| 163 | + ) |
| 164 | + } finally { |
| 165 | + if (browser) { |
| 166 | + await browser.close() |
| 167 | + } |
| 168 | + } |
| 169 | + }) |
| 170 | +}) |
0 commit comments