diff --git a/webapp/packages/core-blocks/src/Cell.test.tsx b/webapp/packages/core-blocks/src/Cell.test.tsx index 25b249cc71..eab88c1fc9 100644 --- a/webapp/packages/core-blocks/src/Cell.test.tsx +++ b/webapp/packages/core-blocks/src/Cell.test.tsx @@ -5,8 +5,8 @@ * Licensed under the Apache License, Version 2.0. * you may not use this file except in compliance with the License. */ -import { describe, expect, it } from '@jest/globals'; -import { waitFor } from '@testing-library/react'; +import { afterEach, describe, expect, it } from '@jest/globals'; +import { cleanup, waitFor } from '@testing-library/react'; import { createApp, renderInApp } from '@cloudbeaver/tests-runner'; @@ -15,6 +15,10 @@ import { Cell } from './Cell'; const app = createApp(); describe('Cell', () => { + afterEach(() => { + cleanup(); + }); + it('should render children correctly', async () => { const { getByText } = renderInApp(Test Children, app); const text = await waitFor(() => getByText('Test Children')); diff --git a/webapp/packages/core-blocks/src/Icon.test.tsx b/webapp/packages/core-blocks/src/Icon.test.tsx index 19739703c3..549821f235 100644 --- a/webapp/packages/core-blocks/src/Icon.test.tsx +++ b/webapp/packages/core-blocks/src/Icon.test.tsx @@ -5,28 +5,34 @@ * Licensed under the Apache License, Version 2.0. * you may not use this file except in compliance with the License. */ -import { expect, test } from '@jest/globals'; -import { render, screen } from '@testing-library/react'; +import { afterEach, describe, expect, test } from '@jest/globals'; +import { cleanup, render, screen } from '@testing-library/react'; import { Icon } from './Icon'; -test('icons.svg#name', () => { - (globalThis as any)._ROOT_URI_ = undefined; +describe('Icon', () => { + afterEach(() => { + cleanup(); + }); - render(); - expect(screen.getByTestId('Icon').querySelector('use')).toHaveAttribute('href', '/icons/icons.svg#test'); -}); + test('icons.svg#name', () => { + (globalThis as any)._ROOT_URI_ = undefined; -test('/image.jpg', () => { - (globalThis as any)._ROOT_URI_ = undefined; + render(); + expect(screen.getByTestId('Icon').querySelector('use')).toHaveAttribute('href', '/icons/icons.svg#test'); + }); - render(); - expect(screen.getByTestId('Icon').querySelector('use')).toHaveAttribute('href', '/image.jpg'); -}); + test('/image.jpg', () => { + (globalThis as any)._ROOT_URI_ = undefined; + + render(); + expect(screen.getByTestId('Icon').querySelector('use')).toHaveAttribute('href', '/image.jpg'); + }); -test('{_ROOT_URI_}/icons.svg#name', () => { - (globalThis as any)._ROOT_URI_ = '/path/'; + test('{_ROOT_URI_}/icons.svg#name', () => { + (globalThis as any)._ROOT_URI_ = '/path/'; - render(); - expect(screen.getByTestId('Icon').querySelector('use')).toHaveAttribute('href', '/path/icons/icons.svg#test'); + render(); + expect(screen.getByTestId('Icon').querySelector('use')).toHaveAttribute('href', '/path/icons/icons.svg#test'); + }); }); diff --git a/webapp/packages/core-blocks/src/Link.test.tsx b/webapp/packages/core-blocks/src/Link.test.tsx index 212d34b300..85dd0708a2 100644 --- a/webapp/packages/core-blocks/src/Link.test.tsx +++ b/webapp/packages/core-blocks/src/Link.test.tsx @@ -5,8 +5,8 @@ * Licensed under the Apache License, Version 2.0. * you may not use this file except in compliance with the License. */ -import { describe, expect, it, jest } from '@jest/globals'; -import { fireEvent, queryByAttribute, waitFor } from '@testing-library/react'; +import { afterEach, describe, expect, it, jest } from '@jest/globals'; +import { cleanup, fireEvent, queryByAttribute, waitFor } from '@testing-library/react'; import { createApp, renderInApp } from '@cloudbeaver/tests-runner'; @@ -15,6 +15,10 @@ import { Link } from './Link'; const app = createApp(); describe('Link', () => { + afterEach(() => { + cleanup(); + }); + it('should render link and children correctly', async () => { const { getByText } = renderInApp(Test Link, app); const linkElement = await waitFor(() => getByText('Test Link')); diff --git a/webapp/packages/core-blocks/src/useControlledScroll.test.ts b/webapp/packages/core-blocks/src/useControlledScroll.test.ts index cfcb568fd4..64f703b374 100644 --- a/webapp/packages/core-blocks/src/useControlledScroll.test.ts +++ b/webapp/packages/core-blocks/src/useControlledScroll.test.ts @@ -16,6 +16,7 @@ describe('useControlledScroll', () => { afterEach(() => { element.remove(); + jest.useRealTimers(); }); beforeEach(() => { diff --git a/webapp/packages/core-blocks/src/useStateDelay.test.ts b/webapp/packages/core-blocks/src/useStateDelay.test.ts index 5144abc4cc..f205d8263c 100644 --- a/webapp/packages/core-blocks/src/useStateDelay.test.ts +++ b/webapp/packages/core-blocks/src/useStateDelay.test.ts @@ -5,8 +5,8 @@ * Licensed under the Apache License, Version 2.0. * you may not use this file except in compliance with the License. */ -import { beforeAll, describe, expect, jest, test } from '@jest/globals'; -import { renderHook, waitFor } from '@testing-library/react'; +import { afterEach, beforeEach, describe, expect, jest, test } from '@jest/globals'; +import { act, renderHook } from '@testing-library/react'; import { useStateDelay } from './useStateDelay'; @@ -19,25 +19,30 @@ interface IHookProps { const useStateDelayWrapper = ({ value, delay, callback }: IHookProps) => useStateDelay(value, delay, callback); describe('useStateDelay', () => { - beforeAll(() => { + afterEach(() => { + jest.useRealTimers(); + }); + + beforeEach(() => { jest.useFakeTimers(); }); test("should return initial state during whole hook's lifecycle", async () => { - const { result } = renderHook(() => useStateDelay(true, 100)); + const { result, unmount } = renderHook(() => useStateDelay(true, 100)); expect(result.current).toBe(true); - await waitFor(async () => { - await new Promise(resolve => setTimeout(resolve, 50)); + act(() => { + jest.advanceTimersByTime(50); }); expect(result.current).toBe(true); - await waitFor(async () => { - await new Promise(resolve => setTimeout(resolve, 60)); + act(() => { + jest.advanceTimersByTime(60); }); expect(result.current).toBe(true); + unmount(); }); test('should return updated state after delay if it was updated', async () => { - const { result, rerender } = renderHook(({ value, delay }: IHookProps) => useStateDelayWrapper({ value, delay }), { + const { result, rerender, unmount } = renderHook(({ value, delay }: IHookProps) => useStateDelayWrapper({ value, delay }), { initialProps: { value: false, delay: 100, @@ -48,19 +53,20 @@ describe('useStateDelay', () => { value: true, delay: 100, }); - await waitFor(async () => { - await new Promise(resolve => setTimeout(resolve, 50)); + act(() => { + jest.advanceTimersByTime(50); }); expect(result.current).toBe(false); - await waitFor(async () => { - await new Promise(resolve => setTimeout(resolve, 60)); + act(() => { + jest.advanceTimersByTime(60); }); expect(result.current).toBe(true); + unmount(); }); test('should execute callback on state change', async () => { const callback = jest.fn(); - const { rerender } = renderHook(({ value, delay }: IHookProps) => useStateDelayWrapper({ value, delay, callback }), { + const { rerender, unmount } = renderHook(({ value, delay }: IHookProps) => useStateDelayWrapper({ value, delay, callback }), { initialProps: { value: false, delay: 100, @@ -68,8 +74,8 @@ describe('useStateDelay', () => { }, }); expect(callback).toHaveBeenCalledTimes(0); - await waitFor(async () => { - await new Promise(resolve => setTimeout(resolve, 50)); + act(() => { + jest.advanceTimersByTime(50); }); expect(callback).toHaveBeenCalledTimes(0); rerender({ @@ -77,15 +83,16 @@ describe('useStateDelay', () => { delay: 100, callback, }); - await waitFor(async () => { - await new Promise(resolve => setTimeout(resolve, 60)); + act(() => { + jest.advanceTimersByTime(500); }); expect(callback).toHaveBeenCalledTimes(1); + unmount(); }); test('should not call callback', async () => { const callback = jest.fn(); - const { result, rerender } = renderHook(({ value, delay }: IHookProps) => useStateDelayWrapper({ value, delay, callback }), { + const { result, rerender, unmount } = renderHook(({ value, delay }: IHookProps) => useStateDelayWrapper({ value, delay, callback }), { initialProps: { value: false, delay: 100, @@ -94,8 +101,8 @@ describe('useStateDelay', () => { }); expect(result.current).toBe(false); expect(callback).toHaveBeenCalledTimes(0); - await waitFor(async () => { - await new Promise(resolve => setTimeout(resolve, 50)); + act(() => { + jest.advanceTimersByTime(50); }); expect(callback).toHaveBeenCalledTimes(0); rerender({ @@ -103,34 +110,36 @@ describe('useStateDelay', () => { delay: 100, callback, }); - await waitFor(async () => { - await new Promise(resolve => setTimeout(resolve, 60)); + act(() => { + jest.advanceTimersByTime(60); }); expect(callback).toHaveBeenCalledTimes(0); + unmount(); }); test("should prolong delay if was updated as hook's argument", async () => { - const { result, rerender } = renderHook(({ value, delay }: IHookProps) => useStateDelayWrapper({ value, delay }), { + const { result, rerender, unmount } = renderHook(({ value, delay }: IHookProps) => useStateDelayWrapper({ value, delay }), { initialProps: { value: false, delay: 100, }, }); expect(result.current).toBe(false); - await waitFor(async () => { - await new Promise(resolve => setTimeout(resolve, 50)); + act(() => { + jest.advanceTimersByTime(50); }); rerender({ value: true, delay: 200, }); - await waitFor(async () => { - await new Promise(resolve => setTimeout(resolve, 60)); + act(() => { + jest.advanceTimersByTime(60); }); expect(result.current).toBe(false); - await waitFor(async () => { - await new Promise(resolve => setTimeout(resolve, 100)); + act(() => { + jest.advanceTimersByTime(500); }); expect(result.current).toBe(true); + unmount(); }); }); diff --git a/webapp/packages/core-utils/src/Promises/cancellableTimeout.test.ts b/webapp/packages/core-utils/src/Promises/cancellableTimeout.test.ts index 084d4b4e4a..8b4714853c 100644 --- a/webapp/packages/core-utils/src/Promises/cancellableTimeout.test.ts +++ b/webapp/packages/core-utils/src/Promises/cancellableTimeout.test.ts @@ -5,7 +5,7 @@ * Licensed under the Apache License, Version 2.0. * you may not use this file except in compliance with the License. */ -import { describe, expect, it, jest } from '@jest/globals'; +import { afterEach, beforeEach, describe, expect, it, jest } from '@jest/globals'; import { cancellableTimeout } from './cancellableTimeout'; @@ -16,7 +16,13 @@ jest.mock('./CancellablePromise', () => ({ })); describe('cancellableTimeout', () => { - jest.useFakeTimers(); + beforeEach(() => { + jest.useFakeTimers(); + }); + + afterEach(() => { + jest.useRealTimers(); + }); it('resolves after the specified timeout', async () => { const timeout = 0; diff --git a/webapp/packages/core-utils/src/blobToBase64.test.ts b/webapp/packages/core-utils/src/blobToBase64.test.ts index c881ffb43e..235c1cfe0e 100644 --- a/webapp/packages/core-utils/src/blobToBase64.test.ts +++ b/webapp/packages/core-utils/src/blobToBase64.test.ts @@ -39,5 +39,6 @@ describe('blobToBase64', () => { blobToBase64(blob); expect(readAsDataURL).toHaveBeenCalledWith(blob); + jest.useRealTimers(); }); }); diff --git a/webapp/packages/core-utils/src/debounce.test.ts b/webapp/packages/core-utils/src/debounce.test.ts index b395a14503..8b7e082e13 100644 --- a/webapp/packages/core-utils/src/debounce.test.ts +++ b/webapp/packages/core-utils/src/debounce.test.ts @@ -5,15 +5,18 @@ * Licensed under the Apache License, Version 2.0. * you may not use this file except in compliance with the License. */ -import { describe, expect, jest, test } from '@jest/globals'; +import { afterEach, beforeEach, describe, expect, jest, test } from '@jest/globals'; import { debounce, debounceAsync } from './debounce'; -// https://jestjs.io/docs/timer-mocks -// Tell Jest to mock all timeout functions -jest.useFakeTimers(); - describe('Debounce', () => { + beforeEach(() => { + jest.useFakeTimers(); + }); + afterEach(() => { + jest.useRealTimers(); + }); + test('function should be executed just once', () => { const func = jest.fn(); const debouncedFunction = debounce(func, 1000); @@ -30,6 +33,13 @@ describe('Debounce', () => { }); describe('DebounceAsync', () => { + beforeEach(() => { + jest.useFakeTimers(); + }); + afterEach(() => { + jest.useRealTimers(); + }); + test('function should be executed just once', async () => { const func = jest.fn(() => Promise.resolve(true)); const debouncedFunction = debounceAsync(func, 1000); diff --git a/webapp/packages/core-utils/src/throttle.test.ts b/webapp/packages/core-utils/src/throttle.test.ts index b9f006eca0..a7e4d0f823 100644 --- a/webapp/packages/core-utils/src/throttle.test.ts +++ b/webapp/packages/core-utils/src/throttle.test.ts @@ -5,12 +5,19 @@ * Licensed under the Apache License, Version 2.0. * you may not use this file except in compliance with the License. */ -import { describe, expect, it, jest } from '@jest/globals'; +import { afterEach, beforeEach, describe, expect, it, jest } from '@jest/globals'; import { throttle } from './throttle'; describe('throttle', () => { - jest.useFakeTimers(); + beforeEach(() => { + jest.useFakeTimers(); + }); + + afterEach(() => { + jest.useRealTimers(); + }); + it('should throttle', () => { const callback = jest.fn(); const throttled = throttle(callback, 100, false);