summaryrefslogtreecommitdiff
path: root/spec
diff options
context:
space:
mode:
authorGitLab Bot <gitlab-bot@gitlab.com>2020-09-14 15:02:52 +0000
committerGitLab Bot <gitlab-bot@gitlab.com>2020-09-14 15:02:52 +0000
commitb39170277a588c6d5c6054fbf1c703cec8b71696 (patch)
tree91e167b1a2f0fb3806d44a8baf39acc6c57055a9 /spec
parent4588bbc93a7857eb2d031a4f3e0212c0aa46b846 (diff)
downloadgitlab-ce-b39170277a588c6d5c6054fbf1c703cec8b71696.tar.gz
Add latest changes from gitlab-org/gitlab@13-3-stable-ee
Diffstat (limited to 'spec')
-rw-r--r--spec/frontend/helpers/fake_date.js49
-rw-r--r--spec/frontend/helpers/fake_date_spec.js33
2 files changed, 82 insertions, 0 deletions
diff --git a/spec/frontend/helpers/fake_date.js b/spec/frontend/helpers/fake_date.js
new file mode 100644
index 00000000000..8417b1c520a
--- /dev/null
+++ b/spec/frontend/helpers/fake_date.js
@@ -0,0 +1,49 @@
+// Frida Kahlo's birthday (6 = July)
+export const DEFAULT_ARGS = [2020, 6, 6];
+
+const RealDate = Date;
+
+const isMocked = val => Boolean(val.mock);
+
+export const createFakeDateClass = ctorDefault => {
+ const FakeDate = new Proxy(RealDate, {
+ construct: (target, argArray) => {
+ const ctorArgs = argArray.length ? argArray : ctorDefault;
+
+ return new RealDate(...ctorArgs);
+ },
+ apply: (target, thisArg, argArray) => {
+ const ctorArgs = argArray.length ? argArray : ctorDefault;
+
+ return RealDate(...ctorArgs);
+ },
+ // We want to overwrite the default 'now', but only if it's not already mocked
+ get: (target, prop) => {
+ if (prop === 'now' && !isMocked(target[prop])) {
+ return () => new RealDate(...ctorDefault).getTime();
+ }
+
+ return target[prop];
+ },
+ getPrototypeOf: target => {
+ return target.prototype;
+ },
+ // We need to be able to set props so that `jest.spyOn` will work.
+ set: (target, prop, value) => {
+ // eslint-disable-next-line no-param-reassign
+ target[prop] = value;
+ return true;
+ },
+ });
+
+ return FakeDate;
+};
+
+export const useFakeDate = (...args) => {
+ const FakeDate = createFakeDateClass(args.length ? args : DEFAULT_ARGS);
+ global.Date = FakeDate;
+};
+
+export const useRealDate = () => {
+ global.Date = RealDate;
+};
diff --git a/spec/frontend/helpers/fake_date_spec.js b/spec/frontend/helpers/fake_date_spec.js
new file mode 100644
index 00000000000..8afc8225f9b
--- /dev/null
+++ b/spec/frontend/helpers/fake_date_spec.js
@@ -0,0 +1,33 @@
+import { createFakeDateClass, DEFAULT_ARGS, useRealDate } from './fake_date';
+
+describe('spec/helpers/fake_date', () => {
+ describe('createFakeDateClass', () => {
+ let FakeDate;
+
+ beforeAll(() => {
+ useRealDate();
+ });
+
+ beforeEach(() => {
+ FakeDate = createFakeDateClass(DEFAULT_ARGS);
+ });
+
+ it('should use default args', () => {
+ expect(new FakeDate()).toEqual(new Date(...DEFAULT_ARGS));
+ expect(FakeDate()).toEqual(Date(...DEFAULT_ARGS));
+ });
+
+ it('should have deterministic now()', () => {
+ expect(FakeDate.now()).not.toBe(Date.now());
+ expect(FakeDate.now()).toBe(new Date(...DEFAULT_ARGS).getTime());
+ });
+
+ it('should be instanceof Date', () => {
+ expect(new FakeDate()).toBeInstanceOf(Date);
+ });
+
+ it('should be instanceof self', () => {
+ expect(new FakeDate()).toBeInstanceOf(FakeDate);
+ });
+ });
+});