From 3ea44440b7c18a9238e1de8211b7006b889d90e2 Mon Sep 17 00:00:00 2001 From: Bram Reyniers Date: Tue, 21 May 2024 13:06:54 +0200 Subject: [PATCH] add throttle tests --- .../tests/components/user/SearchTable.spec.ts | 21 ++++++++ frontend/tests/utils.spec.ts | 48 +++++++++++++++++++ frontend/tests/views/AdminView.spec.ts | 10 ---- 3 files changed, 69 insertions(+), 10 deletions(-) create mode 100644 frontend/tests/components/user/SearchTable.spec.ts create mode 100644 frontend/tests/utils.spec.ts diff --git a/frontend/tests/components/user/SearchTable.spec.ts b/frontend/tests/components/user/SearchTable.spec.ts new file mode 100644 index 00000000..f6dce9ac --- /dev/null +++ b/frontend/tests/components/user/SearchTable.spec.ts @@ -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) + }) +}); diff --git a/frontend/tests/utils.spec.ts b/frontend/tests/utils.spec.ts new file mode 100644 index 00000000..b14ea46e --- /dev/null +++ b/frontend/tests/utils.spec.ts @@ -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); + }); +}); diff --git a/frontend/tests/views/AdminView.spec.ts b/frontend/tests/views/AdminView.spec.ts index ac506059..ccd77700 100644 --- a/frontend/tests/views/AdminView.spec.ts +++ b/frontend/tests/views/AdminView.spec.ts @@ -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) - }); - });