|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +/* eslint-disable |
| 4 | + no-undef |
| 5 | +*/ |
| 6 | +const fs = require('fs'); |
| 7 | +const path = require('path'); |
| 8 | +const testServer = require('../helpers/test-server'); |
| 9 | +const reloadConfig = require('../fixtures/reload-config/webpack.config'); |
| 10 | +const runBrowser = require('../helpers/run-browser'); |
| 11 | + |
| 12 | +describe('reload', () => { |
| 13 | + describe('hot', () => { |
| 14 | + const cssFilePath = path.resolve(__dirname, '../temp/main.css'); |
| 15 | + beforeAll((done) => { |
| 16 | + fs.writeFileSync( |
| 17 | + cssFilePath, |
| 18 | + 'body { background-color: rgb(0, 0, 255); }' |
| 19 | + ); |
| 20 | + const options = { |
| 21 | + port: 9000, |
| 22 | + host: '0.0.0.0', |
| 23 | + inline: true, |
| 24 | + hot: true, |
| 25 | + watchOptions: { |
| 26 | + poll: 500, |
| 27 | + }, |
| 28 | + }; |
| 29 | + testServer.startAwaitingCompilation(reloadConfig, options, done); |
| 30 | + }); |
| 31 | + |
| 32 | + afterAll((done) => { |
| 33 | + testServer.close(done); |
| 34 | + }); |
| 35 | + |
| 36 | + describe('on browser client', () => { |
| 37 | + jest.setTimeout(30000); |
| 38 | + |
| 39 | + it('should hot reload without page refresh', (done) => { |
| 40 | + runBrowser().then(({ page, browser }) => { |
| 41 | + let refreshed = false; |
| 42 | + page.waitForNavigation({ waitUntil: 'load' }).then(() => { |
| 43 | + page |
| 44 | + .evaluate(() => { |
| 45 | + const body = document.body; |
| 46 | + const bgColor = getComputedStyle(body)['background-color']; |
| 47 | + return bgColor; |
| 48 | + }) |
| 49 | + .then((color) => { |
| 50 | + expect(color).toEqual('rgb(0, 0, 255)'); |
| 51 | + |
| 52 | + page.setRequestInterception(true).then(() => { |
| 53 | + page.on('request', (req) => { |
| 54 | + if ( |
| 55 | + req.isNavigationRequest() && |
| 56 | + req.frame() === page.mainFrame() && |
| 57 | + req.url() === 'http://localhost:9000/main' |
| 58 | + ) { |
| 59 | + refreshed = true; |
| 60 | + } |
| 61 | + req.continue(); |
| 62 | + }); |
| 63 | + fs.writeFileSync( |
| 64 | + cssFilePath, |
| 65 | + 'body { background-color: rgb(255, 0, 0); }' |
| 66 | + ); |
| 67 | + page.waitFor(10000).then(() => { |
| 68 | + expect(refreshed).toBeFalsy(); |
| 69 | + |
| 70 | + page |
| 71 | + .evaluate(() => { |
| 72 | + const body = document.body; |
| 73 | + const bgColor = getComputedStyle(body)[ |
| 74 | + 'background-color' |
| 75 | + ]; |
| 76 | + return bgColor; |
| 77 | + }) |
| 78 | + .then((color2) => { |
| 79 | + expect(color2).toEqual('rgb(255, 0, 0)'); |
| 80 | + browser.close().then(done); |
| 81 | + }); |
| 82 | + }); |
| 83 | + }); |
| 84 | + }); |
| 85 | + }); |
| 86 | + |
| 87 | + page.goto('http://localhost:9000/main'); |
| 88 | + }); |
| 89 | + }); |
| 90 | + }); |
| 91 | + }); |
| 92 | + |
| 93 | + describe('inline', () => { |
| 94 | + const cssFilePath = path.resolve(__dirname, '../temp/main.css'); |
| 95 | + beforeAll((done) => { |
| 96 | + fs.writeFileSync( |
| 97 | + cssFilePath, |
| 98 | + 'body { background-color: rgb(0, 0, 255); }' |
| 99 | + ); |
| 100 | + const options = { |
| 101 | + port: 9000, |
| 102 | + host: '0.0.0.0', |
| 103 | + inline: true, |
| 104 | + hot: false, |
| 105 | + watchOptions: { |
| 106 | + poll: 500, |
| 107 | + }, |
| 108 | + }; |
| 109 | + testServer.startAwaitingCompilation(reloadConfig, options, done); |
| 110 | + }); |
| 111 | + |
| 112 | + afterAll((done) => { |
| 113 | + testServer.close(done); |
| 114 | + }); |
| 115 | + |
| 116 | + describe('on browser client', () => { |
| 117 | + jest.setTimeout(30000); |
| 118 | + |
| 119 | + it('should reload with page refresh', (done) => { |
| 120 | + runBrowser().then(({ page, browser }) => { |
| 121 | + let refreshed = false; |
| 122 | + page.waitForNavigation({ waitUntil: 'load' }).then(() => { |
| 123 | + page |
| 124 | + .evaluate(() => { |
| 125 | + const body = document.body; |
| 126 | + const bgColor = getComputedStyle(body)['background-color']; |
| 127 | + return bgColor; |
| 128 | + }) |
| 129 | + .then((color) => { |
| 130 | + expect(color).toEqual('rgb(0, 0, 255)'); |
| 131 | + |
| 132 | + page.setRequestInterception(true).then(() => { |
| 133 | + page.on('request', (req) => { |
| 134 | + if ( |
| 135 | + req.isNavigationRequest() && |
| 136 | + req.frame() === page.mainFrame() && |
| 137 | + req.url() === 'http://localhost:9000/main' |
| 138 | + ) { |
| 139 | + refreshed = true; |
| 140 | + } |
| 141 | + req.continue(); |
| 142 | + }); |
| 143 | + fs.writeFileSync( |
| 144 | + cssFilePath, |
| 145 | + 'body { background-color: rgb(255, 0, 0); }' |
| 146 | + ); |
| 147 | + page.waitFor(10000).then(() => { |
| 148 | + expect(refreshed).toBeTruthy(); |
| 149 | + |
| 150 | + page |
| 151 | + .evaluate(() => { |
| 152 | + const body = document.body; |
| 153 | + const bgColor = getComputedStyle(body)[ |
| 154 | + 'background-color' |
| 155 | + ]; |
| 156 | + return bgColor; |
| 157 | + }) |
| 158 | + .then((color2) => { |
| 159 | + expect(color2).toEqual('rgb(255, 0, 0)'); |
| 160 | + browser.close().then(done); |
| 161 | + }); |
| 162 | + }); |
| 163 | + }); |
| 164 | + }); |
| 165 | + }); |
| 166 | + |
| 167 | + page.goto('http://localhost:9000/main'); |
| 168 | + }); |
| 169 | + }); |
| 170 | + }); |
| 171 | + }); |
| 172 | +}); |
0 commit comments