Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/TR-1207/Add locale format for DateTime in UTC #106

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 19 additions & 18 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
"@oat-sa/expr-eval": "^1.3.0",
"@oat-sa/prettier-config": "^0.1.1",
"@oat-sa/tao-core-libs": "^0.4.3",
"@oat-sa/tao-qunit-testrunner": "^0.1.3",
"@oat-sa/tao-qunit-testrunner": "1.0.3",
"async": "^0.2.10",
"decimal.js": "10.1.1",
"dompurify": "1.0.11",
Expand Down
12 changes: 7 additions & 5 deletions src/util/locale.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* Copyright (c) 2016-2019 (original work) Open Assessment Technologies SA;
* Copyright (c) 2016-2021 (original work) Open Assessment Technologies SA;
*
*/

Expand Down Expand Up @@ -115,11 +115,13 @@ export default {

/**
* Parse unix timestamp
* Note that user's (browser's) timezone will be used.
* @param {Number} timestamp
* Note that user's (browser's) timezone will be used by default, unless the utc parameter is set to true.
* @param {Number} timestamp - The timestamp to format. It is considered as in the target timezone.
* @param {Boolean} [utc=false] - For the UTC timezone. By default the user's timezone will be used.
* @return string
*/
formatDateTime: function(timestamp) {
return moment(timestamp, 'X').format(this.getDateTimeFormat());
formatDateTime(timestamp, utc = false) {
const datetime = utc ? moment.utc(timestamp, 'X') : moment(timestamp, 'X');
return datetime.format(this.getDateTimeFormat());
}
};
54 changes: 54 additions & 0 deletions test/util/locale/momentMock.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/**
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; under version 2
* of the License (non-upgradable).
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* Copyright (c) 2021 Open Assessment Technologies SA ;
*/
/**
* @author Jean-Sébastien Conan <jean-sebastien@taotesting.com>
*/
define([
'core/eventifier'
], function (eventifier) {
'use strict';

const mockValues = {};

const momentMock = eventifier(function (...args) {
momentMock.trigger('moment', ...args);
return momentMockFactory(...args);
});

function momentMockFactory(...factoryArgs) {
momentMock.trigger('factory', ...factoryArgs);

return {
format(...args) {
momentMock.trigger('format', ...args);
return mockValues.format;
}
};
}

return Object.assign(momentMock, {
utc(...args) {
momentMock.trigger('utc', ...args);
return momentMockFactory(...args);
},

mockFormat(value) {
mockValues.format = value;
}
});
});
6 changes: 6 additions & 0 deletions test/util/locale/test.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@
<script type="text/javascript">
require(['/environment/config.js'], function() {
require(['qunitEnv'], function() {
//some mocks
requirejs.config({
paths: {
'moment': 'test/util/locale/momentMock'
}
});
require(['test/util/locale/test'], function() {
QUnit.start();
});
Expand Down
95 changes: 92 additions & 3 deletions test/util/locale/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,19 @@
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* Copyright (c) 2016 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT);
* Copyright (c) 2016-2021 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT);
*
* @author Ivan Klimchuk <klimchuk@1pt.com>
*/
define(['module', 'util/locale'], function(module, locale) {
define([
'module',
'moment',
'util/locale'
], function (module, moment, locale) {
// All tests are grouped in one module because global state is changed during them
QUnit.module('API');

QUnit.test('util api, different locales', function(assert) {
QUnit.test('util api, different locales', assert => {
// American style
locale.setConfig({
decimalSeparator: '.',
Expand Down Expand Up @@ -92,4 +96,89 @@ define(['module', 'util/locale'], function(module, locale) {
'the valid integer value with dot as decimal separator and comma as thousands separator'
);
});

QUnit.test('util/formatDateTime', assert => {
const ready = assert.async();
jsconan marked this conversation as resolved.
Show resolved Hide resolved
const expectedTimestamp = 1621641600;
const expectedOptions = 'X';

assert.expect(10);

Promise.resolve()
.then(() => {
const expectedOutput = '05/21/2021';

moment.off('.test');

const promises = [
new Promise(resolve => {
moment.on('moment.test', (ts, options) => {
assert.ok(true, 'Local timezone applied!');
assert.equal(ts, expectedTimestamp, 'The expected timestamp has been supplied');
assert.equal(options, expectedOptions, 'The expected options have been supplied');
resolve();
});

moment.on('utc.test', (ts, options) => {
assert.ok(false, 'UTC timezone should not be applied!');
assert.equal(ts, expectedTimestamp, 'The expected timestamp has been supplied');
assert.equal(options, expectedOptions, 'The expected options have been supplied');
resolve();
});
}),
new Promise(resolve => {
moment.on('format.test', () => {
assert.ok(true, 'Local format applied');
resolve();
});
})
];

moment.mockFormat(expectedOutput);
assert.equal(locale.formatDateTime(expectedTimestamp), expectedOutput, 'Format date/time to locale using user timezone');

return promises;
})
.then(() => {
const expectedOutput = '05/22/2021';

moment.off('.test');

const promises = [
new Promise(resolve => {
moment.on('moment.test', (ts, options) => {
assert.ok(false, 'Local timezone should not be applied!');
assert.equal(ts, expectedTimestamp, 'The expected timestamp has been supplied');
assert.equal(options, expectedOptions, 'The expected options have been supplied');
resolve();
});

moment.on('utc.test', (ts, options) => {
assert.ok(true, 'UTC timezone applied!');
assert.equal(ts, expectedTimestamp, 'The expected timestamp has been supplied');
assert.equal(options, expectedOptions, 'The expected options have been supplied');
resolve();
});
}),
new Promise(resolve => {
moment.on('format.test', () => {
assert.ok(true, 'Local format applied');
resolve();
});
})
];

moment.mockFormat(expectedOutput);
assert.equal(locale.formatDateTime(expectedTimestamp, true), expectedOutput, 'Format date/time to locale using UTC timezone');

return promises;
})
.catch(err => {
assert.pushResult({
result: false,
message: err
});
})
.then(ready);
});
});