|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const path = require('path'); |
| 4 | +const fs = require('fs'); |
| 5 | +const helper = require('./helper'); |
| 6 | +const config = require('./fixtures/contentbase-config/webpack.config'); |
| 7 | + |
| 8 | +const contentBasePublic = path.join( |
| 9 | + __dirname, |
| 10 | + 'fixtures/contentbase-config/public' |
| 11 | +); |
| 12 | + |
| 13 | +describe('liveReload', () => { |
| 14 | + let server; |
| 15 | + describe('Test disabling live reloading', () => { |
| 16 | + const nestedFile = path.join(contentBasePublic, 'assets/example.txt'); |
| 17 | + |
| 18 | + jest.setTimeout(30000); |
| 19 | + |
| 20 | + beforeAll((done) => { |
| 21 | + server = helper.start( |
| 22 | + config, |
| 23 | + { |
| 24 | + contentBase: contentBasePublic, |
| 25 | + watchContentBase: true, |
| 26 | + liveReload: false, |
| 27 | + }, |
| 28 | + done |
| 29 | + ); |
| 30 | + }); |
| 31 | + |
| 32 | + afterAll((done) => { |
| 33 | + helper.close(() => { |
| 34 | + done(); |
| 35 | + }); |
| 36 | + fs.truncateSync(nestedFile); |
| 37 | + }); |
| 38 | + |
| 39 | + it('Should not reload on changing files', (done) => { |
| 40 | + let reloaded = false; |
| 41 | + |
| 42 | + server.contentBaseWatchers[0].on('change', () => { |
| 43 | + // it means that file has changed |
| 44 | + |
| 45 | + // simulating server behaviour |
| 46 | + if (server.options.liveReload !== false) { |
| 47 | + Object.defineProperty(window.location, 'reload', { |
| 48 | + configurable: true, |
| 49 | + }); |
| 50 | + window.location.reload = jest.fn(); |
| 51 | + window.location.reload(); |
| 52 | + reloaded = true; |
| 53 | + } |
| 54 | + expect(reloaded).toBe(false); |
| 55 | + |
| 56 | + done(); |
| 57 | + }); |
| 58 | + |
| 59 | + // change file content |
| 60 | + setTimeout(() => { |
| 61 | + fs.writeFileSync(nestedFile, 'Heyo', 'utf8'); |
| 62 | + }, 1000); |
| 63 | + }); |
| 64 | + }); |
| 65 | + |
| 66 | + describe('Testing live reloading', () => { |
| 67 | + const nestedFile = path.join(contentBasePublic, 'assets/example.txt'); |
| 68 | + |
| 69 | + jest.setTimeout(30000); |
| 70 | + |
| 71 | + beforeAll((done) => { |
| 72 | + server = helper.start( |
| 73 | + config, |
| 74 | + { |
| 75 | + contentBase: contentBasePublic, |
| 76 | + watchContentBase: true, |
| 77 | + liveReload: true, |
| 78 | + }, |
| 79 | + done |
| 80 | + ); |
| 81 | + }); |
| 82 | + |
| 83 | + afterAll((done) => { |
| 84 | + helper.close(() => { |
| 85 | + done(); |
| 86 | + }); |
| 87 | + fs.truncateSync(nestedFile); |
| 88 | + }); |
| 89 | + |
| 90 | + it('Should reload on changing files', (done) => { |
| 91 | + let reloaded = false; |
| 92 | + |
| 93 | + server.contentBaseWatchers[0].on('change', () => { |
| 94 | + // it means that files has changed |
| 95 | + |
| 96 | + // simulating server behaviour |
| 97 | + if (server.options.liveReload !== false) { |
| 98 | + Object.defineProperty(window.location, 'reload', { |
| 99 | + configurable: true, |
| 100 | + }); |
| 101 | + window.location.reload = jest.fn(); |
| 102 | + window.location.reload(); |
| 103 | + reloaded = true; |
| 104 | + } |
| 105 | + expect(reloaded).toBe(true); |
| 106 | + |
| 107 | + done(); |
| 108 | + }); |
| 109 | + |
| 110 | + // change file content |
| 111 | + setTimeout(() => { |
| 112 | + fs.writeFileSync(nestedFile, 'Heyo', 'utf8'); |
| 113 | + }, 1000); |
| 114 | + }); |
| 115 | + }); |
| 116 | +}); |
0 commit comments