Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test for helpers/api/general #8472

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 90 additions & 0 deletions app/helpers/api/general.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.

import {debounce} from './general';

describe('helpers/api/general', () => {
beforeEach(() => {
jest.useFakeTimers();
});

afterEach(() => {
jest.useRealTimers();
});
describe('debounce', () => {
test('should debounce function calls', () => {
const func = jest.fn();
const debouncedFunc = debounce(func, 100);

// Call debounced function multiple times
debouncedFunc();
debouncedFunc();
debouncedFunc();

// Function should not have been called yet
expect(func).not.toHaveBeenCalled();

// Fast forward time
jest.runAllTimers();

// Function should have been called once
expect(func).toHaveBeenCalledTimes(1);
});

test('should execute callback after debounced function', () => {
const func = jest.fn();
const callback = jest.fn();
const debouncedFunc = debounce(func, 100, false, callback);

debouncedFunc();

// Neither function should have been called yet
expect(func).not.toHaveBeenCalled();
expect(callback).not.toHaveBeenCalled();

// Fast forward time
jest.runAllTimers();

// Both functions should have been called once
expect(func).toHaveBeenCalledTimes(1);
expect(callback).toHaveBeenCalledTimes(1);
});

test('should execute immediately when immediate flag is true', () => {
const func = jest.fn();
const callback = jest.fn();
const debouncedFunc = debounce(func, 100, true, callback);

debouncedFunc();

// Both functions should be called immediately
expect(func).toHaveBeenCalledTimes(1);
expect(callback).toHaveBeenCalledTimes(1);

// Call it again immediately
debouncedFunc();

// Should not call again immediately
expect(func).toHaveBeenCalledTimes(1);
expect(callback).toHaveBeenCalledTimes(1);

// Fast forward time
jest.runAllTimers();

// Counts should remain the same
expect(func).toHaveBeenCalledTimes(1);
expect(callback).toHaveBeenCalledTimes(1);
});

test('should pass arguments to the debounced function', () => {
const func = jest.fn();
const debouncedFunc = debounce(func, 100);
const args = ['arg1', 'arg2'];

debouncedFunc(...args);
jest.runAllTimers();

expect(func).toHaveBeenCalledWith(...args);
});
});
});
Loading