Skip to content
Snippets Groups Projects
Unverified Commit 2daf2dda authored by Paul Slaughter's avatar Paul Slaughter :two:
Browse files

Add fake_date test helper and use it in spec

parent ecc3943d
No related branches found
No related tags found
No related merge requests found
......@@ -90,7 +90,7 @@ exports[`NetworkPolicyList component renders policies table 1`] = `
>
<div>
4 months ago
2 months ago
</div>
</td>
......
......@@ -5,11 +5,14 @@ import { GlTable } from '@gitlab/ui';
import { convertObjectPropsToCamelCase } from '~/lib/utils/common_utils';
import { mockPoliciesResponse } from '../mock_data';
import { useFakeDate } from 'helpers/fake_date';
import { PREDEFINED_NETWORK_POLICIES } from 'ee/threat_monitoring/constants';
const mockData = mockPoliciesResponse.map(policy => convertObjectPropsToCamelCase(policy));
describe('NetworkPolicyList component', () => {
useFakeDate();
let store;
let wrapper;
......
import { memoize } from 'lodash';
// Frida Kahlo's birthday (6 = July)
const DEFAULT_PARAM = [2020, 6, 6];
const createFakeDateClass = memoize(ctorDefault => {
class FakeDate extends Date {
constructor(...ctorArgs) {
const args = ctorArgs.length ? ctorArgs : ctorDefault;
super(...args);
}
static now() {
return new FakeDate().getTime();
}
}
return FakeDate;
});
let isUsingFake = false;
let origDate = Date;
export const useFakeDate = (...args) => {
if (isUsingFake) {
throw new Error('Unexpected call to useFakeDate! Already using fake date.');
}
const FakeDate = createFakeDateClass(args.length ? args : DEFAULT_PARAM);
origDate = global.Date;
global.Date = FakeDate;
isUsingFake = true;
};
export const useRealDate = () => {
if (!isUsingFake) {
throw new Error('Unexpected call to useRealDate! Already using real date.');
}
global.Date = origDate;
isUsingFake = false;
};
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment