Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGitLab Bot <gitlab-bot@gitlab.com>2021-02-10 18:11:19 +0300
committerGitLab Bot <gitlab-bot@gitlab.com>2021-02-10 18:11:19 +0300
commit6cffe9ea21d0974ebd3c544a3b711ffcd35649e2 (patch)
treea40fc35d2ef7a1a36a669094bf1d38d5df72265f /spec/frontend/__helpers__/fake_date
parentec0ecba05cf7712bc8095af9363ee8ff8d999654 (diff)
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'spec/frontend/__helpers__/fake_date')
-rw-r--r--spec/frontend/__helpers__/fake_date/fake_date.js60
-rw-r--r--spec/frontend/__helpers__/fake_date/fake_date_spec.js33
-rw-r--r--spec/frontend/__helpers__/fake_date/index.js2
-rw-r--r--spec/frontend/__helpers__/fake_date/jest.js41
4 files changed, 136 insertions, 0 deletions
diff --git a/spec/frontend/__helpers__/fake_date/fake_date.js b/spec/frontend/__helpers__/fake_date/fake_date.js
new file mode 100644
index 00000000000..bc088ad96b6
--- /dev/null
+++ b/spec/frontend/__helpers__/fake_date/fake_date.js
@@ -0,0 +1,60 @@
+// Frida Kahlo's birthday (6 = July)
+const DEFAULT_ARGS = [2020, 6, 6];
+
+const RealDate = Date;
+
+const isMocked = (val) => Boolean(val.mock);
+
+const createFakeDateClass = (ctorDefaultParam = []) => {
+ const ctorDefault = ctorDefaultParam.length ? ctorDefaultParam : DEFAULT_ARGS;
+
+ 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 new RealDate(...ctorArgs).toString();
+ },
+ // 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;
+};
+
+const setGlobalDateToFakeDate = (...args) => {
+ const FakeDate = createFakeDateClass(args);
+ global.Date = FakeDate;
+};
+
+const setGlobalDateToRealDate = () => {
+ global.Date = RealDate;
+};
+
+// We use commonjs so that the test environment module can pick this up
+// eslint-disable-next-line import/no-commonjs
+module.exports = {
+ setGlobalDateToFakeDate,
+ setGlobalDateToRealDate,
+ createFakeDateClass,
+ RealDate,
+};
diff --git a/spec/frontend/__helpers__/fake_date/fake_date_spec.js b/spec/frontend/__helpers__/fake_date/fake_date_spec.js
new file mode 100644
index 00000000000..730765e52d2
--- /dev/null
+++ b/spec/frontend/__helpers__/fake_date/fake_date_spec.js
@@ -0,0 +1,33 @@
+import { createFakeDateClass } from './fake_date';
+
+describe('spec/helpers/fake_date', () => {
+ describe('createFakeDateClass', () => {
+ let FakeDate;
+
+ beforeEach(() => {
+ FakeDate = createFakeDateClass();
+ });
+
+ it('should use default args', () => {
+ expect(new FakeDate()).toMatchInlineSnapshot(`2020-07-06T00:00:00.000Z`);
+ });
+
+ it('should use default args when called as a function', () => {
+ expect(FakeDate()).toMatchInlineSnapshot(
+ `"Mon Jul 06 2020 00:00:00 GMT+0000 (Greenwich Mean Time)"`,
+ );
+ });
+
+ it('should have deterministic now()', () => {
+ expect(FakeDate.now()).toMatchInlineSnapshot(`1593993600000`);
+ });
+
+ it('should be instanceof Date', () => {
+ expect(new FakeDate()).toBeInstanceOf(Date);
+ });
+
+ it('should be instanceof self', () => {
+ expect(new FakeDate()).toBeInstanceOf(FakeDate);
+ });
+ });
+});
diff --git a/spec/frontend/__helpers__/fake_date/index.js b/spec/frontend/__helpers__/fake_date/index.js
new file mode 100644
index 00000000000..3d1b124ce79
--- /dev/null
+++ b/spec/frontend/__helpers__/fake_date/index.js
@@ -0,0 +1,2 @@
+export * from './fake_date';
+export * from './jest';
diff --git a/spec/frontend/__helpers__/fake_date/jest.js b/spec/frontend/__helpers__/fake_date/jest.js
new file mode 100644
index 00000000000..65e45619049
--- /dev/null
+++ b/spec/frontend/__helpers__/fake_date/jest.js
@@ -0,0 +1,41 @@
+import { createJestExecutionWatcher } from '../jest_execution_watcher';
+import { RealDate, createFakeDateClass } from './fake_date';
+
+const throwInsideExecutionError = (fnName) => {
+ throw new Error(`Cannot call "${fnName}" during test execution (i.e. within "it", "beforeEach", "beforeAll", etc.).
+
+Instead, please move the call to "${fnName}" inside the "describe" block itself.
+
+ describe('', () => {
+ + ${fnName}();
+
+ it('', () => {
+ - ${fnName}();
+ })
+ })
+`);
+};
+
+const isExecutingTest = createJestExecutionWatcher();
+
+export const useDateInScope = (fnName, factory) => {
+ if (isExecutingTest()) {
+ throwInsideExecutionError(fnName);
+ }
+
+ let origDate;
+
+ beforeAll(() => {
+ origDate = global.Date;
+ global.Date = factory();
+ });
+
+ afterAll(() => {
+ global.Date = origDate;
+ });
+};
+
+export const useFakeDate = (...args) =>
+ useDateInScope('useFakeDate', () => createFakeDateClass(args));
+
+export const useRealDate = () => useDateInScope('useRealDate', () => RealDate);