From b48cba686e481182eab5f298d7a88fc49d199919 Mon Sep 17 00:00:00 2001 From: alexm Date: Fri, 20 Sep 2024 14:14:17 +0200 Subject: [PATCH] test: add front test --- src/composables/useStateQuery.js | 6 +++ .../composables/useStateQuery.spec.js | 42 +++++++++++++++++++ .../InvoiceIn/InvoiceInIntrastat.spec.js | 2 +- .../pages/InvoiceIn/InvoiceInVat.spec.js | 3 +- 4 files changed, 51 insertions(+), 2 deletions(-) create mode 100644 test/vitest/__tests__/composables/useStateQuery.spec.js diff --git a/src/composables/useStateQuery.js b/src/composables/useStateQuery.js index e377ad564..09232cc67 100644 --- a/src/composables/useStateQuery.js +++ b/src/composables/useStateQuery.js @@ -19,9 +19,15 @@ export function useStateQuery() { queries.value.delete(hash); } + function reset() { + queries.value = new Set(); + } + return { add, isLoading, remove, + queries, + reset, }; } diff --git a/test/vitest/__tests__/composables/useStateQuery.spec.js b/test/vitest/__tests__/composables/useStateQuery.spec.js new file mode 100644 index 000000000..8424a9bea --- /dev/null +++ b/test/vitest/__tests__/composables/useStateQuery.spec.js @@ -0,0 +1,42 @@ +import { describe, expect, it, beforeEach } from 'vitest'; +import { useStateQuery } from 'src/composables/useStateQuery'; + +describe('useStateQuery', () => { + const { add, isLoading, remove, reset, queries } = useStateQuery(); + beforeEach(() => { + reset(); + expect(queries.value.size).toBeFalsy(); + }); + + it('should add two queries', async () => { + expect(queries.value.size).toBeFalsy(); + const hash = add(); + expect(queries.value.size).toBeTruthy(); + expect(queries.value.has(hash)).toBeTruthy(); + + add(); + expect(queries.value.size).toBe(2); + }); + + it('should add and remove loading state', async () => { + expect(isLoading().value).toBeFalsy(); + const hash = add(); + expect(isLoading().value).toBeTruthy(); + remove(hash); + expect(isLoading().value).toBeFalsy(); + }); + + it('should add and remove hash', async () => { + add(); + add(); + add(); + + const beforeCount = queries.value.size; + const hash = add(); + expect(queries.value.has(hash)).toBeTruthy(); + + remove(hash); + expect(queries.value.has(hash)).toBeFalsy(); + expect(queries.value.size).toBe(beforeCount); + }); +}); diff --git a/test/vitest/__tests__/pages/InvoiceIn/InvoiceInIntrastat.spec.js b/test/vitest/__tests__/pages/InvoiceIn/InvoiceInIntrastat.spec.js index adfb054c6..2851ca3d9 100644 --- a/test/vitest/__tests__/pages/InvoiceIn/InvoiceInIntrastat.spec.js +++ b/test/vitest/__tests__/pages/InvoiceIn/InvoiceInIntrastat.spec.js @@ -6,6 +6,7 @@ describe('InvoiceInIntrastat', () => { let vm; beforeAll(() => { + vi.spyOn(axios, 'get').mockResolvedValue({ data: [{}] }); vm = createWrapper(InvoiceInIntrastat, { global: { stubs: ['vnPaginate'], @@ -14,7 +15,6 @@ describe('InvoiceInIntrastat', () => { }, }, }).vm; - vi.spyOn(axios, 'get').mockResolvedValue({ data: [{}] }); }); describe('getTotal()', () => { diff --git a/test/vitest/__tests__/pages/InvoiceIn/InvoiceInVat.spec.js b/test/vitest/__tests__/pages/InvoiceIn/InvoiceInVat.spec.js index 76453f65a..0cdda8492 100644 --- a/test/vitest/__tests__/pages/InvoiceIn/InvoiceInVat.spec.js +++ b/test/vitest/__tests__/pages/InvoiceIn/InvoiceInVat.spec.js @@ -1,11 +1,12 @@ import { vi, describe, expect, it, beforeAll } from 'vitest'; -import { createWrapper } from 'app/test/vitest/helper'; +import { createWrapper, axios } from 'app/test/vitest/helper'; import InvoiceInVat from 'src/pages/InvoiceIn/Card/InvoiceInVat.vue'; describe('InvoiceInVat', () => { let vm; beforeAll(() => { + vi.spyOn(axios, 'get').mockResolvedValue({ data: [{}] }); vm = createWrapper(InvoiceInVat, { global: { stubs: [],