Skip to content
This repository was archived by the owner on Jun 27, 2020. It is now read-only.

Commit 4b041d3

Browse files
committed
Add ie10 test & formatToParts spec
1. ie10 is crucial, beacuse this test polyfilling of intl.js polyfill. 2. Added test for formatToParts. 1. We must not implement formatToParts if not implemented in browser (or in intl.js polyfill) 2. Test 2 locales, 2 timezones and 2 formats.
1 parent 6855e9a commit 4b041d3

File tree

4 files changed

+196
-10
lines changed

4 files changed

+196
-10
lines changed

karma.conf.saucelabs.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
module.exports = function(config) {
44
const customLaunchers = {
5+
sl_ie_10: {
6+
base: 'SauceLabs',
7+
browserName: 'internet explorer',
8+
version: '10'
9+
},
510
sl_ie_11: {
611
base: 'SauceLabs',
712
browserName: 'internet explorer',
@@ -40,6 +45,6 @@ module.exports = function(config) {
4045
browserNoActivityTimeout: 100000,
4146
port: 9999,
4247
singleRun: true,
43-
browsers: ['sl_edge', 'sl_ie_11', 'sl_safari']
48+
browsers: ['sl_ie_10', 'sl_edge', 'sl_ie_11', 'sl_safari']
4449
});
4550
};

test/test-complete.js

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import assert from 'assert';
44
import timeStampTests from './test-data/time-stamp-fixtures.js';
55
import localeTests from './test-data/locale-test-fixtures.js';
6+
import formatToPartTests from './test-data/format-to-parts-fixtures.js';
67
import polyfill from '../src/code/polyfill.js';
78
import dataLoader from '../src/code/data-loader.js';
89
import tzdataMoonLanding from './test-data/tzdata-moon-nearside.js';
@@ -26,7 +27,7 @@ describe('Polyfill with complete package', () => {
2627
describe('Instanceof integrity', () => {
2728
it('nativedDateTimeFormat instanceof Intl.DateTimeFormat', () => {
2829
const nativedDateTimeFormat = new Intl.DateTimeFormat('en', {
29-
timeZone: 'America/Los_Angeles'
30+
timeZone: 'UTC'
3031
});
3132
assert.equal(nativedDateTimeFormat instanceof Intl.DateTimeFormat, true);
3233
});
@@ -39,6 +40,36 @@ describe('Polyfill with complete package', () => {
3940
});
4041
});
4142

43+
describe.skip('.formatToParts(date)', () => {
44+
it('polyfilled DateTimeFormat should implement iff native DateTimeFormat implemented it', () => {
45+
const nativeDateTimeFormat = new Intl.DateTimeFormat('en', {
46+
timeZone: 'UTC'
47+
}); // UTC is always implemented
48+
const polyfilledDateTimeFormat = new Intl.DateTimeFormat('en', {
49+
timeZone: 'Moon/Nearside'
50+
}); // Moon/Nearside must never be implemented in browser, because its fake timezone name
51+
assert(!((polyfilledDateTimeFormat.formatToParts) ^ (nativeDateTimeFormat.formatToParts)), 'formatToParts implementation mismatched');
52+
});
53+
54+
if (!new Intl.DateTimeFormat('en', {
55+
timeZone: 'Moon/Nearside'
56+
}).formatToParts) {
57+
return;
58+
}
59+
60+
formatToPartTests.forEach(test => {
61+
it(`with locale ${test.locale} timeZone ${test.timeZone} and format ${test.nameFormat}`, () => {
62+
const dateTimeFormat = new Intl.DateTimeFormat(test.locale, {
63+
timeZone: test.timeZone,
64+
timeZoneName: test.nameFormat
65+
});
66+
const parts = dateTimeFormat.formatToParts(test.date);
67+
const timeZonePart = parts.reduce((found, part) => ((part.type === 'timeZoneName') ? part : found), null);
68+
assert.equal(timeZonePart.value, test.expectedTimeZoneName);
69+
});
70+
});
71+
});
72+
4273
describe('.format(locale, option)', () => {
4374
timeStampTests.forEach(testFixture => {
4475
const param = testFixture[0].split(':'),
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
module.exports = [{
2+
// locale en
3+
date: new Date('Mon Mar 06 2017 14:50:00 GMT'),
4+
locale: 'en',
5+
timeZone: 'America/Los_Angeles',
6+
nameFormat: 'long',
7+
expectedTimeZoneName: 'Pacific Standard Time'
8+
}, {
9+
date: new Date('Sun Apr 30 2017 09:30:10 GMT'),
10+
locale: 'en',
11+
timeZone: 'America/Los_Angeles',
12+
nameFormat: 'long',
13+
expectedTimeZoneName: 'Pacific Daylight Time'
14+
},
15+
{
16+
date: new Date('Mon Mar 06 2017 14:50:00 GMT'),
17+
locale: 'en',
18+
timeZone: 'America/Los_Angeles',
19+
nameFormat: 'short',
20+
expectedTimeZoneName: 'PST'
21+
},
22+
{
23+
date: new Date('Sun Apr 30 2017 09:30:10 GMT'),
24+
locale: 'en',
25+
timeZone: 'America/Los_Angeles',
26+
nameFormat: 'short',
27+
expectedTimeZoneName: 'PDT'
28+
},
29+
{
30+
date: new Date('Mon Mar 06 2017 14:50:00 GMT'),
31+
locale: 'hi',
32+
timeZone: 'America/Los_Angeles',
33+
nameFormat: 'long',
34+
expectedTimeZoneName: 'उत्तरी अमेरिकी प्रशांत मानक समय'
35+
}, {
36+
date: new Date('Sun Apr 30 2017 09:30:10 GMT'),
37+
locale: 'hi',
38+
timeZone: 'America/Los_Angeles',
39+
nameFormat: 'long',
40+
expectedTimeZoneName: 'उत्तरी अमेरिकी प्रशांत डेलाइट समय'
41+
},
42+
{
43+
date: new Date('Sun Apr 30 2017 09:30:10 GMT'),
44+
locale: 'hi',
45+
timeZone: 'America/Los_Angeles',
46+
nameFormat: 'short',
47+
expectedTimeZoneName: 'GMT-7'
48+
},
49+
{
50+
date: new Date('Mon Mar 06 2017 14:50:00 GMT'),
51+
locale: 'hi',
52+
timeZone: 'America/Los_Angeles',
53+
nameFormat: 'short',
54+
expectedTimeZoneName: 'GMT-8'
55+
},
56+
{
57+
date: new Date('Mon Mar 06 2017 14:50:00 GMT'),
58+
locale: 'en',
59+
timeZone: 'Asia/Calcutta',
60+
nameFormat: 'long',
61+
expectedTimeZoneName: 'India Standard Time'
62+
}, {
63+
date: new Date('Sun Apr 30 2017 09:30:10 GMT'),
64+
locale: 'en',
65+
timeZone: 'Asia/Calcutta',
66+
nameFormat: 'long',
67+
expectedTimeZoneName: 'India Standard Time'
68+
},
69+
{
70+
date: new Date('Sun Apr 30 2017 09:30:10 GMT'),
71+
locale: 'en',
72+
timeZone: 'Asia/Calcutta',
73+
nameFormat: 'short',
74+
expectedTimeZoneName: 'GMT+5:30'
75+
},
76+
{
77+
date: new Date('Sun Apr 30 2017 09:30:10 GMT'),
78+
locale: 'en',
79+
timeZone: 'Asia/Calcutta',
80+
nameFormat: 'short',
81+
expectedTimeZoneName: 'GMT+5:30'
82+
},
83+
{
84+
date: new Date('Mon Mar 06 2017 14:50:00 GMT'),
85+
locale: 'hi',
86+
timeZone: 'Asia/Calcutta',
87+
nameFormat: 'long',
88+
expectedTimeZoneName: 'भारतीय मानक समय'
89+
}, {
90+
date: new Date('Sun Apr 30 2017 09:30:10 GMT'),
91+
locale: 'hi',
92+
timeZone: 'Asia/Calcutta',
93+
nameFormat: 'long',
94+
expectedTimeZoneName: 'भारतीय मानक समय'
95+
},
96+
{
97+
date: new Date('Mon Mar 06 2017 14:50:00 GMT'),
98+
locale: 'hi',
99+
timeZone: 'Asia/Calcutta',
100+
nameFormat: 'short',
101+
expectedTimeZoneName: 'IST'
102+
},
103+
{
104+
date: new Date('Sun Apr 30 2017 09:30:10 GMT'),
105+
locale: 'hi',
106+
timeZone: 'Asia/Calcutta',
107+
nameFormat: 'short',
108+
expectedTimeZoneName: 'IST'
109+
}
110+
];

test/test-saucelabs.js

Lines changed: 48 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@
33
import assert from 'assert';
44
import timeStampTests from './test-data/time-stamp-fixtures.js';
55
import localeTests from './test-data/locale-test-fixtures.js';
6+
import formatToPartTests from './test-data/format-to-parts-fixtures.js';
67
import polyfill from '../src/code/polyfill.js';
78
import dataLoader from '../src/code/data-loader.js';
89
import tzdataMoonLanding from './test-data/tzdata-moon-nearside.js';
910
import tzdata from '../src/data/tzdata.js';
1011
import localeData from '../src/data/locale.js';
1112
import metazone from '../src/data/metazone.js';
1213

13-
const isNode = (typeof global !== "undefined" && {}.toString.call(global) === '[object global]');
14+
const isNode = (typeof global !== 'undefined' && {}.toString.call(global) === '[object global]');
1415
const myGlobal = (isNode) ? global : window;
1516

1617

@@ -21,29 +22,63 @@ tzdata(myGlobal); // Loads timezone iana data in memory
2122
localeData(myGlobal); // Loads timezone CLDR data in memory
2223
tzdataMoonLanding(myGlobal);
2324

24-
function formatAssert(actual) {
25+
function formatAssert(expected, actual) {
26+
// Browsers do not follow number formating in consistent way.
27+
// This causes a mismatch between rest of the formatting.
28+
// Our test in 'test-complete.js' does test actual match,
29+
// but here we only ensure that no exception happening, while formatting.
2530
assert(actual, 'must not be a falsy value');
2631
}
2732

28-
describe('Polyfill with complete package', () => {
33+
if (myGlobal.Intl.__disableRegExpRestore) {
34+
myGlobal.Intl.__disableRegExpRestore();
35+
}
36+
37+
describe('SauceLabs# ', () => {
2938
describe('DateTimeFormat', () => {
3039
describe('Instanceof integrity', () => {
31-
it('nativedDateTimeFormat instanceof Intl.DateTimeFormat', () => {
32-
const nativedDateTimeFormat = new Intl.DateTimeFormat('en', {
40+
it('native DateTimeFormat instanceof Intl.DateTimeFormat', () => {
41+
const nativeDateTimeFormat = new Intl.DateTimeFormat('en', {
3342
timeZone: 'America/Los_Angeles'
3443
});
35-
assert.equal(nativedDateTimeFormat instanceof Intl.DateTimeFormat, true);
44+
assert.equal(nativeDateTimeFormat instanceof Intl.DateTimeFormat, true);
3645
});
3746

38-
it('polyfilledDateTimeFormat instanceof Intl.DateTimeFormat', () => {
47+
it('polyfilled DateTimeFormat instanceof Intl.DateTimeFormat', () => {
3948
const polyfilledDateTimeFormat = new Intl.DateTimeFormat('en', {
4049
timeZone: 'Moon/Nearside'
4150
});
4251
assert.equal(polyfilledDateTimeFormat instanceof Intl.DateTimeFormat, true);
4352
});
4453
});
4554

46-
describe('.format(locale, option)', () => {
55+
describe.skip('.formatToParts(date)', () => {
56+
it('polyfilled DateTimeFormat should implement iff native DateTimeFormat implemented it', () => {
57+
const nativeDateTimeFormat = new Intl.DateTimeFormat('en', {timeZone: 'UTC'}); // UTC is always implemented
58+
const polyfilledDateTimeFormat = new Intl.DateTimeFormat('en', {timeZone: 'Moon/Nearside'});// Moon/Nearside can never be implemented.
59+
assert(!((polyfilledDateTimeFormat.formatToParts) ^ (nativeDateTimeFormat.formatToParts)), 'formatToParts implementation mismatched');
60+
});
61+
62+
if (!new Intl.DateTimeFormat('en', {
63+
timeZone: 'Moon/Nearside'
64+
}).formatToParts) {
65+
return;
66+
}
67+
68+
formatToPartTests.forEach(test => {
69+
it(`with locale ${test.locale} timeZone ${test.timeZone} and format ${test.nameFormat}`, () => {
70+
const dateTimeFormat = new Intl.DateTimeFormat(test.locale, {
71+
timeZone: test.timeZone,
72+
timeZoneName: test.nameFormat
73+
});
74+
const parts = dateTimeFormat.formatToParts(test.date);
75+
const timeZonePart = parts.reduce((found, part) => ((part.type === 'timeZoneName') ? part : found), null);
76+
assert.equal(timeZonePart.value, test.expectedTimeZoneName);
77+
});
78+
});
79+
});
80+
81+
describe('.format(date, option)', () => {
4782
timeStampTests.slice(0, 1000).forEach(testFixture => {
4883
const param = testFixture[0].split(':'),
4984
locale = param[0],
@@ -127,6 +162,11 @@ describe('Polyfill with complete package', () => {
127162
});
128163
});
129164
describe('.supportedLocalesOf()', () => {
165+
if (!Intl.DateTimeFormat.supportedLocalesOf) {
166+
console.log('supportedLocalesOf is not available in this environment');
167+
return;
168+
}
169+
130170
it('should work as usual.', () => {
131171
const supportedLocales = Intl.DateTimeFormat.supportedLocalesOf('en');
132172

0 commit comments

Comments
 (0)