-
Notifications
You must be signed in to change notification settings - Fork 19
/
utils.js
48 lines (39 loc) · 1.2 KB
/
utils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import fs from 'fs';
import $ from 'cheerio';
import glob from 'glob';
const FIXTURE_DIR = './test/fixtures/';
export function loadFixture (name) {
return $(fs.readFileSync(FIXTURE_DIR + name + '.html').toString());
}
export function loadPayload (name) {
try {
return JSON.parse(fs.readFileSync('./test/payloads/' + name + '.json'));
} catch (e) {
return null;
}
}
export function listFixtures (path) {
return glob.sync(FIXTURE_DIR + path + '/**/*.html')
.map((path) => path.slice(FIXTURE_DIR.length).replace(/\.[^/.]+$/, ''));
}
export class TimeWarp {
set () {
const args = Array.prototype.slice.call(arguments, 0, 6);
const offset = arguments[6] || 0;
if (!this._Date) {
this._Date = global.Date;
}
global.Date = () => {
// Thanks to http://stackoverflow.com/a/8843181/597097
const date = new (Function.prototype.bind.call(this._Date, null, ...args));
date.getTimezoneOffset = () => offset;
return date;
};
global.Date.now = () => new Date();
}
reset () {
if (this._Date) {
global.Date = this._Date;
}
}
}