Skip to content

Commit 821599f

Browse files
committed
add tests
1 parent ced8cd9 commit 821599f

File tree

2 files changed

+61
-3
lines changed

2 files changed

+61
-3
lines changed

src/Serilog.Ui.Web/src/__tests__/hooks/useAuthProperties.spec.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { act, renderHook } from '__tests__/_setup/testing-utils';
22
import { useAuthProperties } from 'app/hooks/useAuthProperties';
33
import { IAuthPropertiesStorageKeys } from 'app/util/auth';
4-
import { describe, expect, it } from 'vitest';
4+
import { DispatchedCustomEvents } from 'types/types';
5+
import { describe, expect, it, vi } from 'vitest';
56

67
describe('useAuthProperties', () => {
78
it('renders with default values', () => {
@@ -52,6 +53,8 @@ describe('useAuthProperties', () => {
5253
});
5354

5455
it('clears auth state', () => {
56+
const mock = vi.fn()
57+
document.addEventListener(DispatchedCustomEvents.RemoveTableKey, mock)
5558
sessionStorage.setItem(IAuthPropertiesStorageKeys.jwt_bearerToken, 'token');
5659
sessionStorage.setItem(IAuthPropertiesStorageKeys.basic_user, 'user');
5760

@@ -66,5 +69,6 @@ describe('useAuthProperties', () => {
6669
expect(result.current.basic_user).toBe('');
6770
expect(result.current.basic_pwd).toBe('');
6871
expect(result.current.jwt_bearerToken).toBe('');
72+
expect(mock).toHaveBeenCalledOnce()
6973
});
7074
});

src/Serilog.Ui.Web/src/__tests__/hooks/useSearchForm.spec.ts

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import { dbKeysMock } from '__tests__/_setup/mocks/samples';
2-
import { renderHook, waitFor } from '__tests__/_setup/testing-utils';
2+
import { act, renderHook, waitFor } from '__tests__/_setup/testing-utils';
33
import { useSearchForm } from 'app/hooks/useSearchForm';
44
import { IAuthPropertiesStorageKeys } from 'app/util/auth';
55
import { AuthType } from 'types/types';
66
import { describe, expect, it } from 'vitest';
77

88
describe('useSearchForm', () => {
9-
it('renders and sets default table key on reset', async () => {
9+
it('sets default table key on reset', async () => {
1010
sessionStorage.setItem(IAuthPropertiesStorageKeys.jwt_bearerToken, 'token');
1111

1212
const { result } = renderHook(() => useSearchForm(), { authType: AuthType.Jwt });
@@ -17,6 +17,60 @@ describe('useSearchForm', () => {
1717
});
1818
});
1919

20+
it('sets NULL table key on reset', async () => {
21+
sessionStorage.setItem(IAuthPropertiesStorageKeys.jwt_bearerToken, 'token');
22+
23+
const { result } = renderHook(() => useSearchForm(), { authType: AuthType.Jwt });
24+
25+
await waitFor(() => {
26+
result.current.reset(true);
27+
expect(result.current.getValues('table')).toBe(null);
28+
});
29+
});
30+
31+
type Properties = Parameters<ReturnType<typeof useSearchForm>['setValue']>['0'];
32+
it.each([{
33+
property: 'level' as Properties, resetResult: true,
34+
},
35+
{
36+
property: 'search' as Properties, resetResult: true,
37+
},
38+
{
39+
property: 'startDate' as Properties, resetResult: true,
40+
},
41+
{
42+
property: 'endDate' as Properties, resetResult: true,
43+
},
44+
{
45+
property: 'entriesPerPage' as Properties, resetResult: false
46+
},
47+
{
48+
property: 'page' as Properties, resetResult: false
49+
},
50+
{
51+
property: 'sortBy' as Properties, resetResult: false
52+
},
53+
{
54+
property: 'table' as Properties, resetResult: false
55+
},
56+
{
57+
property: 'sortOn' as Properties, resetResult: false
58+
}
59+
])('hints for refetch, returning $resetResult on reset, for property $property', async ({ property, resetResult }: { property: Properties, resetResult: boolean }) => {
60+
sessionStorage.setItem(IAuthPropertiesStorageKeys.jwt_bearerToken, 'token');
61+
62+
const { result } = renderHook(() => useSearchForm(), { authType: AuthType.Jwt });
63+
64+
act(() => {
65+
result.current.setValue(property, 'test')
66+
})
67+
68+
act(() => {
69+
const shouldRefetch = result.current.reset();
70+
expect(shouldRefetch).toBe(resetResult);
71+
})
72+
});
73+
2074
it('renders and leaves default table key undefined, if internal query was not successful', async () => {
2175
sessionStorage.removeItem(IAuthPropertiesStorageKeys.jwt_bearerToken);
2276

0 commit comments

Comments
 (0)