1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00
lens/src/common/test-utils/use-fake-time.ts
Janne Savolainen 5f57213179
Fix stuff happening based on timers not being run correctly in unit tests (#5764)
* Introduce helper for advancing fake time

Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>

* Introduce reactive now to kludge around global shared state in library

Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>

* Replace all usages of "now" from mobx-utils with our own kludge to get rid of shared global state between unit tests

Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>

* Consolidate all usages of advanceTimersByTime to make sure things happening based on timers are run correctly

Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>

* Fix incorrect expect in test

Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>

* Enable skipped unit test since prerequisites are done

Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>
2022-07-01 19:40:34 +03:00

26 lines
625 B
TypeScript

/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import { act } from "@testing-library/react";
let usingFakeTime = false;
export const advanceFakeTime = (milliseconds: number) => {
if (!usingFakeTime) {
throw new Error("Tried to advance fake time but it was not enabled. Call useFakeTime() first.");
}
act(() => {
jest.advanceTimersByTime(milliseconds);
});
};
export const useFakeTime = (dateTime: string) => {
usingFakeTime = true;
jest.useFakeTimers();
jest.setSystemTime(new Date(dateTime));
};