Skip to content

Commit

Permalink
test(server): move live reload tests to new file
Browse files Browse the repository at this point in the history
  • Loading branch information
EslamHiko committed May 16, 2019
1 parent 8483c03 commit 0523cfd
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 103 deletions.
103 changes: 0 additions & 103 deletions test/ContentBase.test.js
Expand Up @@ -18,109 +18,6 @@ const contentBaseOther = path.join(
describe('ContentBase', () => {
let server;
let req;
describe('Test disabling live reloading', () => {
const nestedFile = path.join(contentBasePublic, 'assets/example.txt');

jest.setTimeout(30000);

beforeAll((done) => {
server = helper.start(
config,
{
contentBase: contentBasePublic,
watchContentBase: true,
liveReload: false,
},
done
);
req = request(server.app);
});

afterAll((done) => {
helper.close(() => {
done();
});
fs.truncateSync(nestedFile);
});

it('Should not reload on changing files', (done) => {
let reloaded = false;

server.contentBaseWatchers[0].on('change', () => {
// it means that file has changed

// simulating server behaviour
if (server.options.liveReload !== false) {
Object.defineProperty(window.location, 'reload', {
configurable: true,
});
window.location.reload = jest.fn();
window.location.reload();
reloaded = true;
}
expect(reloaded).toBe(false);

done();
});

// change file content
setTimeout(() => {
fs.writeFileSync(nestedFile, 'Heyo', 'utf8');
}, 1000);
});
});

describe('Testing live reloading', () => {
const nestedFile = path.join(contentBasePublic, 'assets/example.txt');

jest.setTimeout(30000);

beforeAll((done) => {
server = helper.start(
config,
{
contentBase: contentBasePublic,
watchContentBase: true,
liveReload: true,
},
done
);
req = request(server.app);
});

afterAll((done) => {
helper.close(() => {
done();
});
fs.truncateSync(nestedFile);
});

it('Should reload on changing files', (done) => {
let reloaded = false;

server.contentBaseWatchers[0].on('change', () => {
// it means that files has changed

// simulating server behaviour
if (server.options.liveReload !== false) {
Object.defineProperty(window.location, 'reload', {
configurable: true,
});
window.location.reload = jest.fn();
window.location.reload();
reloaded = true;
}
expect(reloaded).toBe(true);

done();
});

// change file content
setTimeout(() => {
fs.writeFileSync(nestedFile, 'Heyo', 'utf8');
}, 1000);
});
});

describe('to directory', () => {
const nestedFile = path.join(contentBasePublic, 'assets/example.txt');
Expand Down
116 changes: 116 additions & 0 deletions test/LiveReload.test.js
@@ -0,0 +1,116 @@
'use strict';

const path = require('path');
const fs = require('fs');
const helper = require('./helper');
const config = require('./fixtures/contentbase-config/webpack.config');

const contentBasePublic = path.join(
__dirname,
'fixtures/contentbase-config/public'
);

describe('liveReload', () => {
let server;
describe('Test disabling live reloading', () => {
const nestedFile = path.join(contentBasePublic, 'assets/example.txt');

jest.setTimeout(30000);

beforeAll((done) => {
server = helper.start(
config,
{
contentBase: contentBasePublic,
watchContentBase: true,
liveReload: false,
},
done
);
});

afterAll((done) => {
helper.close(() => {
done();
});
fs.truncateSync(nestedFile);
});

it('Should not reload on changing files', (done) => {
let reloaded = false;

server.contentBaseWatchers[0].on('change', () => {
// it means that file has changed

// simulating server behaviour
if (server.options.liveReload !== false) {
Object.defineProperty(window.location, 'reload', {
configurable: true,
});
window.location.reload = jest.fn();
window.location.reload();
reloaded = true;
}
expect(reloaded).toBe(false);

done();
});

// change file content
setTimeout(() => {
fs.writeFileSync(nestedFile, 'Heyo', 'utf8');
}, 1000);
});
});

describe('Testing live reloading', () => {
const nestedFile = path.join(contentBasePublic, 'assets/example.txt');

jest.setTimeout(30000);

beforeAll((done) => {
server = helper.start(
config,
{
contentBase: contentBasePublic,
watchContentBase: true,
liveReload: true,
},
done
);
});

afterAll((done) => {
helper.close(() => {
done();
});
fs.truncateSync(nestedFile);
});

it('Should reload on changing files', (done) => {
let reloaded = false;

server.contentBaseWatchers[0].on('change', () => {
// it means that files has changed

// simulating server behaviour
if (server.options.liveReload !== false) {
Object.defineProperty(window.location, 'reload', {
configurable: true,
});
window.location.reload = jest.fn();
window.location.reload();
reloaded = true;
}
expect(reloaded).toBe(true);

done();
});

// change file content
setTimeout(() => {
fs.writeFileSync(nestedFile, 'Heyo', 'utf8');
}, 1000);
});
});
});

0 comments on commit 0523cfd

Please sign in to comment.