Skip to content

Commit

Permalink
adding more sanity check to testing the polyfilling process in browsers
Browse files Browse the repository at this point in the history
  • Loading branch information
caridy committed May 20, 2016
1 parent 2e31880 commit 4d75e49
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 6 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
"build:dist": "npm run build:dist:dev && npm run build:dist:prod",
"build": "npm run build:data && npm run build:lib && npm run build:dist",
"lint": "eslint .",
"test": "cd tests && node sanity.js && node noderunner.js && node saucelabs.js",
"test": "cd tests && node polyfilling.js && node sanity.js && node noderunner.js && node saucelabs.js",
"pretest": "npm run lint",
"preversion": "npm run clean && npm run build && npm run test",
"prepublish": "npm run clean && npm run build"
Expand Down
34 changes: 34 additions & 0 deletions tests/polyfilling.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
const vm = require('vm');

function assert(value, expected, message) {
console.log(message);
if (value !== expected) {
console.error(' > ERROR: expected value ' + expected + ' but the actual value is ' + value);
process.exit(1);
} else {
console.log(' > PASSED');
}
}

const context = new vm.createContext();
var script = new vm.Script('this');
const window = script.runInContext(context);

const fs = require('fs');
const code = fs.readFileSync(__dirname + '/../dist/Intl.js', 'utf8');

// first evaluation
window.window = window; // circular, in case the polyfill uses window
var originalIntl = window.Intl;
var script = new vm.Script(code);
script.runInContext(context);
assert(typeof global.Intl, 'object', 'for this test to function, global.Intl is required');
assert(typeof window.IntlPolyfill, 'object', 'polyfill should always add the custom global IntlPolyfill');
assert(window.Intl, originalIntl, 'validating that the polyfilling process does not touch the original Intl value');

// second evaluation
window.window = window; // circular, in case the polyfill uses window
window.Intl = undefined; // disabling Intl
var script = new vm.Script(code);
script.runInContext(context);
assert(window.Intl, window.IntlPolyfill, 'validating that the polyfilling process does patch Intl if it does not exist');
13 changes: 8 additions & 5 deletions tests/sanity.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,27 @@
var IntlPolyfill = require('../');

function assert(value, expected) {
function assert(value, expected, message) {
console.log(message);
if (value !== expected) {
console.log('expected value ' + expected + ' but the actual value is ' + value);
console.error(' > ERROR: expected value ' + expected + ' but the actual value is ' + value);
process.exit(1);
} else {
console.log(' > PASSED');
}
}

assert(new IntlPolyfill.NumberFormat('de-DE', {
minimumFractionDigits: 2,
maximumFractionDigits: 2
}).format(0.015), "0,02");
}).format(0.015), "0,02", 'fractional digits');

assert(new IntlPolyfill.NumberFormat('en-US', {
style: 'currency',
currency: 'GBP',
minimumFractionDigits: 2,
}).format(59.88), '£59.88');
}).format(59.88), '£59.88', 'currency with fragtional digits');

assert(new IntlPolyfill.DateTimeFormat('en', {
month:'numeric',
day: 'numeric'
}).format(new Date('2016/05/16')), '5/16');
}).format(new Date('2016/05/16')), '5/16', 'month and day');

0 comments on commit 4d75e49

Please sign in to comment.