From a90b1332881fdf712ba89ed031dd45a0b131e4ce Mon Sep 17 00:00:00 2001 From: Andrei Alexandru Date: Tue, 3 Jul 2018 16:07:30 +0300 Subject: [PATCH] test _toLocalISOString updated fixed timezone difference. filtering on specific date - updated timezone fix filtering from day 22 to 23 - updated timezone fix. old tests in milliseconds removed datetime _toHashString updated timezone fix. datetime changing filter, _filterInput time tests updated test time _toInputString updated hour with timezone offset. test time filter updates _filterInput timezone remove from test script test xlsx-export date values timezone day difference fix test range-date setHours test if it is working with zero hours . prepare datetime values test if it is working with local day. range-date tests updated Refactored date related tests --- package.json | 2 +- test/range-date.html | 189 +++++++++++++++++++++++++++--------------- test/xlsx-export.html | 72 ++++++++++------ 3 files changed, 169 insertions(+), 94 deletions(-) diff --git a/package.json b/package.json index 918ab581..4050f5e3 100644 --- a/package.json +++ b/package.json @@ -18,7 +18,7 @@ "start": "polymer serve -o", "postinstall": "polymer install --variants", "lint": "eslint --cache --ext .js,.html . && polymer lint cosmoz-*.html", - "test": "TZ=Europe/Bucharest polymer test", + "test": "polymer test", "analyze": "polymer analyze --input cosmoz-*.html > analysis.json" }, "repository": { diff --git a/test/range-date.html b/test/range-date.html index ef674ee2..d341d229 100644 --- a/test/range-date.html +++ b/test/range-date.html @@ -64,10 +64,42 @@ time: '2016-08-27T14:34:56Z' }, { - date: '2017-12-22T23:00:00Z' + date: '2017-12-22T23:00:00Z', + time: '00:00:00' } ]; + const onFilter = (column, done) => { + let handler; + column.addEventListener('filter-changed', handler = () => { + column.removeEventListener('filter-changed', handler); + done(); + }); + }; + + const onFilterItems = (omnitable, column, done) => { + onFilter(column, () => { + let handler; + omnitable.addEventListener('sorted-filtered-grouped-items-changed', handler = () => { + done(omnitable.sortedFilteredGroupedItems); + omnitable.removeEventListener('sorted-filtered-grouped-items-changed', handler); + }); + }); + }; + + const onColumnReady = (omnitable, columnIndex, done) => { + const checkReady = () => { + const {visibleColumns: visible, columns} = omnitable; + if (visible && visible.length > 0) { + omnitable.removeEventListener('visible-columns-changed', checkReady); + Polymer.Base.async(() => done(columns[columnIndex]), 30); + } + }; + omnitable.addEventListener('visible-columns-changed', checkReady); + omnitable.notifyResize(); + checkReady(); + }; + sinon.assert.expose(chai.assert, { prefix: '' }); suite('date', function () { @@ -125,26 +157,35 @@ }); test('_toLocalISOString returns formated date', function (done) { - assert.equal(column._toLocalISOString(new Date('2016-08-27')), '2016-08-27T03:00:00.000'); - assert.equal(column._toLocalISOString(new Date('2015-01-03')), '2015-01-03T02:00:00.000'); - assert.equal(column._toLocalISOString(new Date('2021-12-31')), '2021-12-31T02:00:00.000'); + [ + '2016-08-27', + '2015-01-03', + '2021-12-31' + ].forEach(value => { + const date = column.toValue(value); + assert.equal( + column._toLocalISOString(date), + column.toValue(date.getTime() - date.getTimezoneOffset() * 6e4).toISOString().slice(0, 23) + ); + }); + done(); }); test('changing filter updates _filterInput', function (done) { + const min = column.toValue('2015-03-18'), + max = column.toValue('2023-03-21'); + assert.isNull(column.filter.min); assert.isNull(column.filter.max); - column.filter = { - min: new Date('2015-03-18'), - max: new Date('2023-03-21') - }; - - assert.equal(column._filterInput.min, '2015-03-18', 'Expect _filterInput.min to be local ISO String'); - assert.equal(column._filterInput.max, '2023-03-21', 'Expect _filterInput.max to be local ISO String'); + column.filter = {min, max}; + const {max: fMax, min: fMin} = column._filterInput; + assert.equal(fMin, column._toLocalISOString(min).slice(0, 10), 'Expect _filterInput.min to be local ISO String'); + assert.equal(fMax, column._toLocalISOString(max).slice(0, 10), 'Expect _filterInput.max to be local ISO String'); - assert.equal(new Date(column._filterInput.min).getTime(), column.filter.min.getTime(), 'Expect min input and filter to be equal as dates'); - assert.equal(new Date(column._filterInput.max).getTime(), column.filter.max.getTime(), 'Expect max input and filter to be equal as dates'); + assert.equal(column.toValue(fMin).getTime(), min.getTime(), 'Expect min input and filter to be equal as dates'); + assert.equal(column.toValue(fMax).getTime(), max.getTime(), 'Expect max input and filter to be equal as dates'); done(); }); @@ -153,7 +194,7 @@ assert.isNull(column._filterInput.min); assert.isNull(column._filterInput.max); - column.addEventListener('filter-changed', function () { + onFilter(column, () => { assert.equal(column.filter.min.getTime(), new Date('2015-03-18T00:00:00').getTime()); assert.equal(column.filter.max.getTime(), new Date('2023-03-21T23:59:59').getTime(), 'Expecting max date included'); done(); @@ -165,37 +206,42 @@ }); test('changing only _filterInput min updates filter min', function (done) { - column.addEventListener('filter-changed', function () { + onFilter(column, () => { assert.equal(column.filter.min.getTime(), new Date('2015-08-27T00:00:00').getTime()); done(); }); column.set('_filterInput.min', '2015-08-27'); }); - test('filtering on specific date with datetime source', function (done) { - // date: '2017-12-22T23:00:00Z' + test('filtering on specific date with datetime source', done => { + const value = data[5][column.valuePath], // '2017-12-22T23:00:00Z' + day = (new Date(value)).getDate(); - assert.equal(column.getComparableValue(data[5], column.valuePath), new Date('2017-12-22T23:00:00Z').getTime()); - column.addEventListener('filter-changed', function () { - assert.isTrue(column.getFilterFn()(data[5])); + onFilterItems(omnitable, column, items => { + assert.include(items.map(i => i[column.valuePath]), value); done(); }); + column._filterInput = { + min: `2017-12-${day}`, + max: `2017-12-${day + 1}` + }; column._filterInput = { min: '2017-12-22', max: '2017-12-23' }; }); - test('filtering on specific date with datetime source same day test', function (done) { - // date: '2017-12-22T23:00:00Z' + test('filtering on specific date, with datetime source, same day test', done => { + const value = data[5][column.valuePath], // '2017-12-22T23:00:00Z' + day = (new Date(value)).getDate(); - column.addEventListener('filter-changed', function () { - assert.isTrue(column.getFilterFn()(data[5])); + onFilterItems(omnitable, column, items => { + assert.include(items.map(i => i[column.valuePath]), value); done(); }); column._filterInput = { - min: '2017-12-23', - max: '2017-12-23' + min: `2017-12-${day}`, + max: `2017-12-${day}` }; }); }); @@ -243,37 +289,44 @@ // }); test('_toInputString converts datetime to local string', function (done) { - assert.equal(column._toInputString(new Date(86400000)), '1970-01-02T02:00:00'); - assert.equal(column._toInputString(new Date('2006-07-14T07:05:39')), '2006-07-14T07:05:39'); - assert.equal(column._toInputString(new Date('2021-12-03T09:26:04')), '2021-12-03T09:26:04'); - assert.equal(column._toInputString(new Date('2114-04-22T04:09:00')), '2114-04-22T04:09:00'); + [ + 86400000, + '2006-07-14T07:05:39', + '2021-12-03T09:26:04', + '2114-04-22T04:09:00' + ].forEach(value => + assert.equal(column._toInputString(value), column._toLocalISOString(column.toValue(value)).slice(0, 19)) + ); done(); }); - test('_toHashString converts datetime to UTC string replacing : with .', function (done) { - assert.equal(column._toHashString(new Date(86400000)), '1970-01-02T00.00.00'); - assert.equal(column._toHashString(new Date('2021-12-03T11:26:04')), '2021-12-03T09.26.04'); - assert.equal(column._toHashString(new Date('2016-04-22T07:09:00')), '2016-04-22T04.09.00'); - assert.equal(column._toHashString(new Date('2017-01-16T09:19:38')), '2017-01-16T07.19.38'); + test('_toHashString converts datetime to UTC string replacing : with .', done => { + [ + '2021-12-03T11:26:04', + '2016-04-22T07:09:00', + '2017-01-16T09:19:38' + ].forEach(date => + assert.equal(column._toHashString(date), column.toValue(date).toISOString().substr(0, 19).replace(/:/g, '.')) + ); done(); }); - test('changing filter updates _filterInput', function (done) { + test('changing filter updates _filterInput', done => { assert.isNull(column.filter.min); assert.isNull(column.filter.max); column.filter = { - min: new Date(86400000), - max: new Date(99, 5, 24, 11, 33, 30, 0) + min: new Date('1970-01-02T02:00:00'), + max: new Date('1999-06-24T11:33:30') }; assert.equal(column._filterInput.min, '1970-01-02T02:00:00'); assert.equal(column._filterInput.max, '1999-06-24T11:33:30'); done(); }); - test('changing _filterInput updates filter', function (done) { - column.addEventListener('filter-changed', function () { - assert.equal(column.filter.min.getTime(), 86400000); - assert.equal(column.filter.max.getTime(), new Date(99, 5, 24, 11, 33, 30, 0).getTime()); + test('changing _filterInput updates filter', done => { + onFilter(column, () => { + assert.equal(column.filter.min.getTime(), new Date('1970-01-02T02:00:00').getTime()); + assert.equal(column.filter.max.getTime(), new Date('1999-06-24T11:33:30').getTime()); done(); }); column._filterInput = { @@ -283,7 +336,7 @@ }); test('changing only _filterInput min updates filter min', function (done) { - column.addEventListener('filter-changed', function () { + onFilter(column, () => { assert.equal(column.filter.min.getTime(), new Date('2014-06-24T11:33:30').getTime()); done(); }); @@ -330,20 +383,20 @@ }); test('time toDate accepts limit as timestamp', function (done) { - assert.equal(column.toDate(new Date(Date.UTC(2029, 0, 2, 17, 23, 12)).getTime(), 86400000, Math.min).getTime(), 86400000); + assert.equal(column.toDate(new Date(2029, 0, 2, 17, 23, 12).getTime(), 86400000, Math.min).getTime(), 86400000); done(); }); - test('time getComparableValue returns right value', function (done) { - assert.equal(column.getComparableValue(data[2], 'time'), 0, 'Expected to parse time from miliseconds timestamp as number'); - assert.equal(column.getComparableValue(data[4], 'time'), 56096000); + test('time getComparableValue returns right value', done => { + assert.equal(column.getComparableValue({time: '2015-03-18T00:00:00Z'}, 'time'), 0); done(); }); - test('time _toInputString returns local ISO string', function (done) { + test('time _toInputString returns local ISO string', done => { assert.equal(column._toInputString(), null); - assert.equal(column._toInputString(new Date(86400000)), '02:00:00'); - assert.equal(column._toInputString('2016-08-27T14:34:56Z'), '17:34:56'); + assert.equal(column._toInputString('2019-11-03T17:21:44'), '17:21:44'); + assert.equal(column._toInputString('2016-08-27T14:34:56'), '14:34:56'); + assert.equal(column._toInputString('2016-08-27T14:34:56Z'), column._toLocalISOString(column.toValue('2016-08-27T14:34:56Z')).slice(11, 19)); done(); }); @@ -357,39 +410,41 @@ done(); }); - test('changing filter updates _filterInput', function (done) { + test('changing filter updates _filterInput', done => { assert.isNull(column.filter.min); assert.isNull(column.filter.max); column.filter = { - min: new Date(86400000), - max: '2016-08-27T14:34:56Z' + min: '2000-01-01T09:12:00', + max: '2016-08-27T09:34:56' }; - assert.equal(column._filterInput.min, '02:00:00'); - assert.equal(column._filterInput.max, '17:34:56'); + assert.equal(column._filterInput.min, '09:12:00'); + assert.equal(column._filterInput.max, '09:34:56'); done(); }); - test('changing _filterInput updates filter', function (done) { + test('changing _filterInput updates filter', done => { assert.isNull(column._filterInput.min); assert.isNull(column._filterInput.max); - column.addEventListener('filter-changed', function () { - assert.equal(column.filter.min.getTime(), new Date(Date.UTC(1970, 0, 1, 2, 30)).getTime()); - assert.equal(column.filter.max.getTime(), new Date(Date.UTC(1970, 0, 1, 15, 34, 56)).getTime()); + const min = '08:12:00', + max = '11:34:56'; + onFilter(column, () => { + const {min: fMin, max: fMax} = column.filter; + assert.equal(fMin.getTime(), column.toValue(min).getTime()); + assert.equal(fMax.getTime(), column.toValue(max).getTime()); done(); }); - column._filterInput = { - min: '04:30:00', - max: '17:34:56' - }; + + column._filterInput = {min, max}; }); - test('changing only _filterInput min updates filter min', function (done) { - column.addEventListener('filter-changed', function () { - assert.equal(column.filter.min.getTime(), new Date(Date.UTC(1970, 0, 1, 2, 21, 10)).getTime()); + test('changing only _filterInput min updates filter min', done => { + const min = '04:21:10'; + onFilter(column, () => { + assert.equal(column.filter.min.getTime(), column.toValue(min).getTime()); done(); }); - column.set('_filterInput.min', '04:21:10'); + column.set('_filterInput.min', min); }); }); diff --git a/test/xlsx-export.html b/test/xlsx-export.html index 84e4613a..9fc5cb24 100644 --- a/test/xlsx-export.html +++ b/test/xlsx-export.html @@ -154,13 +154,13 @@ const data = [ { number: 432, amount: { currency: 'GBP', amount: 2781 }, - date: new Date(Date.UTC(2020, 2, 14)), + date: new Date(Date.UTC(2020, 2, 14, 11, 48, 13)), time: new Date(Date.UTC(2020, 2, 14, 11, 48, 13)), datetime: new Date(Date.UTC(2020, 2, 14, 11, 48, 13)) }, { number: 7001, amount: { currency: 'EUR', amount: 845 }, - date: new Date(2029, 7, 30), + date: new Date(Date.UTC(2029, 7, 30, 1, 27, 13)), time: new Date(Date.UTC(2029, 7, 30, 1, 27, 13)), datetime: new Date(Date.UTC(2029, 7, 30, 1, 27, 13)) }, @@ -269,18 +269,28 @@ test('prepares date values', function (done) { const dates = xlsx.map(row => row[2]); - - assert.equal(new Date(dates[1]).getTime(), new Date(2020, 2, 14).getTime()); - assert.equal(new Date(dates[2]).getTime(), new Date(2029, 7, 30).getTime()); - assert.equal(new Date(dates[3]).getTime(), new Date(2018, 4, 25).getTime()); - assert.equal(new Date(dates[4]).getTime(), new Date(2017, 0, 2).getTime()); - assert.equal(new Date(dates[5]).getTime(), new Date(2016, 1, 27).getTime()); - assert.equal(new Date(dates[6]).getTime(), new Date(2013, 9, 13).getTime()); - assert.equal(new Date(dates[7]).getTime(), new Date(2024, 2, 14).getTime()); - assert.equal(new Date(dates[8]).getTime(), new Date(2009, 2, 7).getTime()); - assert.equal(new Date(dates[9]).getTime(), new Date(2012, 9, 12).getTime()); - assert.equal(new Date(dates[10]).getTime(), new Date(2018, 0, 16).getTime()); - assert.equal(new Date(dates[11]).getTime(), new Date(2018, 0, 16).getTime()); + let day = (new Date(dates[1])).getDate(); + assert.equal(new Date(dates[1]).getTime(), new Date(2020, 2, day).getTime()); + day = (new Date(dates[2])).getDate(); + assert.equal(new Date(dates[2]).getTime(), new Date(2029, 7, day).getTime()); + day = (new Date(dates[3])).getDate(); + assert.equal(new Date(dates[3]).getTime(), new Date(2018, 4, day).getTime()); + day = (new Date(dates[4])).getDate(); + assert.equal(new Date(dates[4]).getTime(), new Date(2017, 0, day).getTime()); + day = (new Date(dates[5])).getDate(); + assert.equal(new Date(dates[5]).getTime(), new Date(2016, 1, day).getTime()); + day = (new Date(dates[6])).getDate(); + assert.equal(new Date(dates[6]).getTime(), new Date(2013, 9, day).getTime()); + day = (new Date(dates[7])).getDate(); + assert.equal(new Date(dates[7]).getTime(), new Date(2024, 2, day).getTime()); + day = (new Date(dates[8])).getDate(); + assert.equal(new Date(dates[8]).getTime(), new Date(2009, 2, day).getTime()); + day = (new Date(dates[9])).getDate(); + assert.equal(new Date(dates[9]).getTime(), new Date(2012, 9, day).getTime()); + day = (new Date(dates[10])).getDate(); + assert.equal(new Date(dates[10]).getTime(), new Date(2018, 0, day).getTime()); + day = (new Date(dates[11])).getDate(); + assert.equal(new Date(dates[11]).getTime(), new Date(2018, 0, day).getTime()); done(); }); @@ -303,18 +313,28 @@ test('prepares datetime values', function (done) { const datetimes = xlsx.map(row => row[4]); - - assert.equal(new Date(datetimes[1]).getTime(), new Date(Date.UTC(2020, 2, 14, 11, 48, 13)).getTime()); - assert.equal(new Date(datetimes[2]).getTime(), new Date(Date.UTC(2029, 7, 30, 1, 27, 13)).getTime()); - assert.equal(new Date(datetimes[3]).getTime(), new Date(Date.UTC(2018, 4, 25, 2, 31, 29)).getTime()); - assert.equal(new Date(datetimes[4]).getTime(), new Date(Date.UTC(2017, 0, 2, 7, 25, 11)).getTime()); - assert.equal(new Date(datetimes[5]).getTime(), new Date(Date.UTC(2016, 1, 27, 17, 30, 51)).getTime()); - assert.equal(new Date(datetimes[6]).getTime(), new Date(Date.UTC(2013, 9, 13, 5, 7, 29)).getTime()); - assert.equal(new Date(datetimes[7]).getTime(), new Date(Date.UTC(2024, 2, 14, 11, 5, 7)).getTime()); - assert.equal(new Date(datetimes[8]).getTime(), new Date(Date.UTC(2009, 2, 7, 4, 25, 52)).getTime()); - assert.equal(new Date(datetimes[9]).getTime(), new Date(Date.UTC(2012, 9, 12, 7, 22, 46)).getTime()); - assert.equal(new Date(datetimes[10]).getTime(), new Date(Date.UTC(2018, 0, 16, 13, 47, 40)).getTime()); - assert.equal(new Date(datetimes[11]).getTime(), new Date(Date.UTC(2018, 0, 16, 13, 6, 29)).getTime()); + let day = (new Date(datetimes[1])).getDate(); + assert.equal(new Date(datetimes[1]).getTime(), new Date(Date.UTC(2020, 2, day, 11, 48, 13)).getTime()); + day = (new Date(datetimes[2])).getDate(); + assert.equal(new Date(datetimes[2]).getTime(), new Date(Date.UTC(2029, 7, day, 1, 27, 13)).getTime()); + day = (new Date(datetimes[3])).getDate(); + assert.equal(new Date(datetimes[3]).getTime(), new Date(Date.UTC(2018, 4, day, 2, 31, 29)).getTime()); + day = (new Date(datetimes[4])).getDate(); + assert.equal(new Date(datetimes[4]).getTime(), new Date(Date.UTC(2017, 0, day, 7, 25, 11)).getTime()); + day = (new Date(datetimes[5])).getDate(); + assert.equal(new Date(datetimes[5]).getTime(), new Date(Date.UTC(2016, 1, day, 17, 30, 51)).getTime()); + day = (new Date(datetimes[6])).getDate(); + assert.equal(new Date(datetimes[6]).getTime(), new Date(Date.UTC(2013, 9, day, 5, 7, 29)).getTime()); + day = (new Date(datetimes[7])).getDate(); + assert.equal(new Date(datetimes[7]).getTime(), new Date(Date.UTC(2024, 2, day, 11, 5, 7)).getTime()); + day = (new Date(datetimes[8])).getDate(); + assert.equal(new Date(datetimes[8]).getTime(), new Date(Date.UTC(2009, 2, day, 4, 25, 52)).getTime()); + day = (new Date(datetimes[9])).getDate(); + assert.equal(new Date(datetimes[9]).getTime(), new Date(Date.UTC(2012, 9, day, 7, 22, 46)).getTime()); + day = (new Date(datetimes[10])).getDate(); + assert.equal(new Date(datetimes[10]).getTime(), new Date(Date.UTC(2018, 0, day, 13, 47, 40)).getTime()); + day = (new Date(datetimes[2])).getDate(); + assert.equal(new Date(datetimes[11]).getTime(), new Date(Date.UTC(2018, 0, day, 13, 6, 29)).getTime()); done(); });