Skip to content

Commit

Permalink
add throttle tests
Browse files Browse the repository at this point in the history
  • Loading branch information
reyniersbram committed May 21, 2024
1 parent 4a0a833 commit 3ea4444
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 10 deletions.
21 changes: 21 additions & 0 deletions frontend/tests/components/user/SearchTable.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { mount } from "@vue/test-utils";
import {expect, describe, it, vi} from "vitest";
import SearchTable from "@/components/user/SearchTable.vue"

describe("SearchTable", async () => {
const ResizeObserverMock = vi.fn(() => ({
observe: vi.fn(),
unobserve: vi.fn(),
disconnect: vi.fn(),
}));
vi.stubGlobal("ResizeObserver", ResizeObserverMock);
const wrapper = mount(SearchTable, {});
it("sorts booleans true first", () => {
const instance = wrapper.vm;
const sortBoolFunction = (instance as any).sortBool;
expect(sortBoolFunction(true, true)).toBe(0)
expect(sortBoolFunction(false, false)).toBe(0)
expect(sortBoolFunction(false, true)).toBe(1)
expect(sortBoolFunction(true, false)).toBe(-1)
})
});
48 changes: 48 additions & 0 deletions frontend/tests/utils.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import {afterEach, beforeEach, describe, expect, it, vi} from 'vitest';
import {throttle} from "@/utils";

describe('throttle', () => {
beforeEach(() => {
vi.useFakeTimers();
});
afterEach(() => {
vi.clearAllTimers();
});

it("should call a function immediately", () => {
const func = vi.fn();
const throttledFunc = throttle(func, 1000);
throttledFunc();
expect(func).toHaveBeenCalledTimes(1);
});

it("should call a function only once within the time limit", () => {
const func = vi.fn();
const throttledFunc = throttle(func, 1000);
throttledFunc();
throttledFunc();
throttledFunc();
expect(func).toHaveBeenCalledTimes(1);
});

it("should call the function again after the time limit", () => {
const func = vi.fn();
const throttledFunc = throttle(func, 1000);
throttledFunc();
throttledFunc();
expect(func).toHaveBeenCalledTimes(1);
vi.advanceTimersByTime(1500);
expect(func).toHaveBeenCalledTimes(2);
});

it("should omit subsequent calls within the time limit", () => {
const func = vi.fn();
const throttledFunc = throttle(func, 1000);
throttledFunc();
throttledFunc();
throttledFunc();
throttledFunc();
vi.advanceTimersByTime(2000);
expect(func).toHaveBeenCalledTimes(2);
});
});
10 changes: 0 additions & 10 deletions frontend/tests/views/AdminView.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,4 @@ describe("AdminView", async () => {
expect(VDataTableVirtual.text()).toContain("Is Lesgever")
expect(VDataTableVirtual.text()).toContain("Is Beheerder")
});

it("test sort bool function", () => {
const instance = wrapper.vm;
const sortBoolFunction = (instance as any).sortBool;
expect(sortBoolFunction(true, true)).toBe(0)
expect(sortBoolFunction(false, false)).toBe(0)
expect(sortBoolFunction(false, true)).toBe(1)
expect(sortBoolFunction(true, false)).toBe(-1)
});

});

0 comments on commit 3ea4444

Please sign in to comment.