Skip to content

Commit

Permalink
CB-5340 fix unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sergeyteleshev committed Aug 6, 2024
1 parent 5d7702d commit f26ac1b
Show file tree
Hide file tree
Showing 9 changed files with 107 additions and 59 deletions.
8 changes: 6 additions & 2 deletions webapp/packages/core-blocks/src/Cell.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -15,6 +15,10 @@ import { Cell } from './Cell';
const app = createApp();

describe('Cell', () => {
afterEach(() => {
cleanup();
});

it('should render children correctly', async () => {
const { getByText } = renderInApp(<Cell>Test Children</Cell>, app);
const text = await waitFor(() => getByText('Test Children'));
Expand Down
38 changes: 22 additions & 16 deletions webapp/packages/core-blocks/src/Icon.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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(<Icon data-testid="Icon" name="test" />);
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(<Icon data-testid="Icon" name="test" />);
expect(screen.getByTestId('Icon').querySelector('use')).toHaveAttribute('href', '/icons/icons.svg#test');
});

render(<Icon data-testid="Icon" name="/image.jpg" />);
expect(screen.getByTestId('Icon').querySelector('use')).toHaveAttribute('href', '/image.jpg');
});
test('/image.jpg', () => {
(globalThis as any)._ROOT_URI_ = undefined;

render(<Icon data-testid="Icon" name="/image.jpg" />);
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(<Icon data-testid="Icon" name="test" />);
expect(screen.getByTestId('Icon').querySelector('use')).toHaveAttribute('href', '/path/icons/icons.svg#test');
render(<Icon data-testid="Icon" name="test" />);
expect(screen.getByTestId('Icon').querySelector('use')).toHaveAttribute('href', '/path/icons/icons.svg#test');
});
});
8 changes: 6 additions & 2 deletions webapp/packages/core-blocks/src/Link.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -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(<Link href="#">Test Link</Link>, app);
const linkElement = await waitFor(() => getByText('Test Link'));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ describe('useControlledScroll', () => {

afterEach(() => {
element.remove();
jest.useRealTimers();
});

beforeEach(() => {
Expand Down
69 changes: 39 additions & 30 deletions webapp/packages/core-blocks/src/useStateDelay.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -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,
Expand All @@ -48,44 +53,46 @@ 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,
callback,
},
});
expect(callback).toHaveBeenCalledTimes(0);
await waitFor(async () => {
await new Promise(resolve => setTimeout(resolve, 50));
act(() => {
jest.advanceTimersByTime(50);
});
expect(callback).toHaveBeenCalledTimes(0);
rerender({
value: true,
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,
Expand All @@ -94,43 +101,45 @@ 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({
value: false,
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();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -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;
Expand Down
1 change: 1 addition & 0 deletions webapp/packages/core-utils/src/blobToBase64.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,6 @@ describe('blobToBase64', () => {
blobToBase64(blob);

expect(readAsDataURL).toHaveBeenCalledWith(blob);
jest.useRealTimers();
});
});
20 changes: 15 additions & 5 deletions webapp/packages/core-utils/src/debounce.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down
11 changes: 9 additions & 2 deletions webapp/packages/core-utils/src/throttle.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit f26ac1b

Please sign in to comment.