[IMP] web:tests: patch Date.now

Before this commit, when we use `testUtils.patchDate`,
the function `Date.now` was not using the fake date for reference.

This is an issue with the lib momentjs, which instantiate the
current date as follow:

```
new Date(Date.now())
```

Because of this, we were handling patched date and unpatched moment
objects.

Task-ID 1902848
This commit is contained in:
Alexandre Kühn
2019-08-13 07:57:10 +00:00
committed by Mathieu Duckerts-Antoine
parent 02903076c9
commit 7d8977182b
3 changed files with 45 additions and 1 deletions
@@ -446,7 +446,13 @@ function patchDate(year, month, day, hours, minutes, seconds) {
}
// Copy "native" methods explicitly; they may be non-enumerable
Date.now = NativeDate.now;
// exception: 'now' uses fake date as reference
Date.now = function () {
var date = new NativeDate();
var time = date.getTime();
time -= timeInterval;
return time;
};
Date.UTC = NativeDate.UTC;
Date.prototype = NativeDate.prototype;
Date.prototype.constructor = Date;
@@ -0,0 +1,36 @@
odoo.define('web.testUtilsTests', function (require) {
"use strict";
var testUtils = require('web.test_utils');
QUnit.module('web', {}, function () {
QUnit.module('testUtils', {}, function () {
QUnit.module('patch date');
QUnit.test('new date', function (assert) {
assert.expect(5);
const unpatchDate = testUtils.mock.patchDate(2018, 9, 23, 14, 50, 0);
const date = new Date();
assert.strictEqual(date.getFullYear(), 2018);
assert.strictEqual(date.getMonth(), 9);
assert.strictEqual(date.getDate(), 23);
assert.strictEqual(date.getHours(), 14);
assert.strictEqual(date.getMinutes(), 50);
unpatchDate();
});
QUnit.test('new moment', function (assert) {
assert.expect(1);
const unpatchDate = testUtils.mock.patchDate(2018, 9, 23, 14, 50, 0);
const m = moment();
assert.strictEqual(m.format('YYYY-MM-DD HH:mm'), '2018-10-23 14:50');
unpatchDate();
});
});
});
});
+2
View File
@@ -704,6 +704,8 @@
<script type="text/javascript" src="/web/static/tests/widgets/rainbow_man_tests.js"/>
<script type="text/javascript" src="/web/static/tests/tools/debug_manager_tests.js"/>
<script type="text/javascript" src="/web/static/tests/helpers/test_utils_tests.js"></script>
</t>
<div id="qunit"/>