Skip to content

Commit

Permalink
Restructure test harness to actually evaluate script.
Browse files Browse the repository at this point in the history
  • Loading branch information
rwjblue committed Oct 15, 2018
1 parent 5bab3c2 commit a5f3062
Showing 1 changed file with 76 additions and 95 deletions.
171 changes: 76 additions & 95 deletions tests/inject-live-reload-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,127 +23,108 @@ describe('contenFor returns', () => {
});

describe('dynamicScript returns right script when', hooks => {
let options;
let options, location;

function evaluate(script) {
let functionBody = `
'use strict';
let script = null;
let location = ${JSON.stringify(location)};
let window = {};
let document = {
createElement() {
return { };
},
getElementsByTagName() {
return [
{
appendChild(_script) {
script = _script;
}
}
];
}
};
${script}
return { script, LiveReloadOptions: window.LiveReloadOptions };
`;

try {
return new Function(functionBody)();
} catch(e) {
QUnit.config.current.assert.ok(false, `${e.message} while evaluating\n\n${functionBody}\n`);
}
}

function getScriptSrc(script) {
return evaluate(script).script.src;
}

hooks.beforeEach(() => {
options = {
port: 4200,
liveReloadPort: 4200,
isLatestEmber: true
isLatestEmber: true,
liveReloadPrefix: '_lr',
liveReloadJsUrl: null,
liveReloadOptions: null,
};

location = {
hostname: 'localhost',
protocol: 'http:',
port: 4200,
};
});

it('livereload port and port are same', assert => {
it('liveReloadPort and port are same', (assert) => {
let script = InjectLiveReload.dynamicScript(options);
assert.equal(script, `(function() {
var srcUrl = null;
var host= location.hostname || 'localhost';
var liveReloadPort = undefined;
var defaultPort = location.protocol === 'https:' ? 443 : 80;
var port = liveReloadPort || location.port || defaultPort;
var path = '';
var prefixURL = '';
var src = srcUrl || prefixURL + '/livereload.js?port=' + port + '&host=' + host + path;
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = src;
document.getElementsByTagName('head')[0].appendChild(script);
}());`);
let actual = getScriptSrc(script);

assert.strictEqual(actual, '/_lr/livereload.js?port=4200&host=localhost&path=_lr/livereload');
});

it('livereload port and port are different', assert => {
it('liveReloadPort and port are different', assert => {
options.liveReloadPort = '35729';
let script = InjectLiveReload.dynamicScript(options);
assert.equal(script, `(function() {
var srcUrl = null;
var host= location.hostname || 'localhost';
var liveReloadPort = 35729;
var defaultPort = location.protocol === 'https:' ? 443 : 80;
var port = liveReloadPort || location.port || defaultPort;
var path = '';
var prefixURL = '(location.protocol || 'http:') + '//' + host + ':' + 35729';
var src = srcUrl || prefixURL + '/livereload.js?port=' + port + '&host=' + host + path;
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = src;
document.getElementsByTagName('head')[0].appendChild(script);
}());`);
let actual = getScriptSrc(script);

assert.strictEqual(actual, 'http://localhost:35729/_lr/livereload.js?port=35729&host=localhost&path=_lr/livereload');
});

it('livereload prefix is provided', assert => {
options.liveReloadPrefix = 'test';
it('liveReloadPrefix is provided', assert => {
options.liveReloadPrefix = 'other-lr-path';
let script = InjectLiveReload.dynamicScript(options);
assert.equal(script, `(function() {
var srcUrl = null;
var host= location.hostname || 'localhost';
var liveReloadPort = undefined;
var defaultPort = location.protocol === 'https:' ? 443 : 80;
var port = liveReloadPort || location.port || defaultPort;
var path = '&path=test/livereload';
var prefixURL = '';
var src = srcUrl || prefixURL + '/test/livereload.js?port=' + port + '&host=' + host + path;
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = src;
document.getElementsByTagName('head')[0].appendChild(script);
}());`)
let actual = getScriptSrc(script);

assert.strictEqual(actual, '/other-lr-path/livereload.js?port=4200&host=localhost&path=other-lr-path/livereload');
});

it('livereloadJs file URL is explicitly provided', assert => {
it('liveReloadJs file URL is explicitly provided', assert => {
options.liveReloadJsUrl = 'test.com';
let script = InjectLiveReload.dynamicScript(options);
assert.equal(script, `(function() {
var srcUrl = 'test.com';
var host= location.hostname || 'localhost';
var liveReloadPort = undefined;
var defaultPort = location.protocol === 'https:' ? 443 : 80;
var port = liveReloadPort || location.port || defaultPort;
var path = '';
var prefixURL = '';
var src = srcUrl || prefixURL + '/livereload.js?port=' + port + '&host=' + host + path;
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = src;
document.getElementsByTagName('head')[0].appendChild(script);
}());`)
let actual = getScriptSrc(script);

assert.strictEqual(actual, 'test.com');
});

it('livereloadOptions are provided', assert => {
options.liveReloadOptions = { test: true};
it('liveReloadOptions are provided', assert => {
options.liveReloadOptions = { test: "wowza" };
let script = InjectLiveReload.dynamicScript(options);
assert.equal(script, `(function() {
window.LiveReloadOptions = {"test":true,"snipver":1};
var srcUrl = null;
var host= location.hostname || 'localhost';
var liveReloadPort = undefined;
var defaultPort = location.protocol === 'https:' ? 443 : 80;
var port = liveReloadPort || location.port || defaultPort;
var path = '';
var prefixURL = '';
var src = srcUrl || prefixURL + '/livereload.js?port=' + port + '&host=' + host + path;
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = src;
document.getElementsByTagName('head')[0].appendChild(script);
}());`)
let actual = evaluate(script);

assert.deepEqual(actual.LiveReloadOptions, Object.assign({ snipver: 1 }, options.liveReloadOptions));
});

it('provide compatiblity for older ember version', assert => {
options.isLatestEmber = false;
let script = InjectLiveReload.dynamicScript(options);
assert.equal(script, `(function() {
var srcUrl = null;
var host= location.hostname || 'localhost';
var liveReloadPort = undefined;
var defaultPort = location.protocol === 'https:' ? 443 : 80;
var port = liveReloadPort || location.port || defaultPort;
var path = '';
var prefixURL = '';
var src = srcUrl || prefixURL + '/livereload.js?port=' + port + '&host=' + host + path;
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = src;
document.getElementsByTagName('head')[0].appendChild(script);
}());`)
let actual = getScriptSrc(script);

assert.strictEqual(actual, '/_lr/livereload.js?port=4200&host=localhost');
});
});

Expand Down Expand Up @@ -196,4 +177,4 @@ describe('serverMiddleware', hooks => {
})
})
})
});
});

0 comments on commit a5f3062

Please sign in to comment.