Skip to content

Commit 603267a

Browse files
committed
tests: new refresh button component
1 parent c928216 commit 603267a

File tree

4 files changed

+91
-5
lines changed

4 files changed

+91
-5
lines changed

src/Serilog.Ui.Web/src/__tests__/_setup/mocks/fetch.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import dayjs from 'dayjs';
22
import { http, HttpResponse } from 'msw';
3+
import { defaultAuthType } from '../../../app/hooks/useSerilogUiProps.tsx';
34
import {
45
AuthType,
56
EncodedSeriLogObject,
@@ -9,7 +10,6 @@ import {
910
SortPropertyOptions,
1011
} from '../../../types/types';
1112
import { dbKeysMock, fakeLogs, fakeLogs2ndTable, fakeLogs3rdTable } from './samples';
12-
import { defaultAuthType } from '../../../app/hooks/useSerilogUiProps.tsx';
1313

1414
export const developmentListenersHost = ['https://localhost:3001'];
1515

@@ -51,7 +51,9 @@ export const handlers = developmentListenersHost.flatMap((dlh) => [
5151
http.get(`${dlh}/api/keys`, ({ request }) => {
5252
const auth = request.headers.get('authorization');
5353

54-
return defaultAuthType !== AuthType.Custom && !auth ? HttpResponse.error() : HttpResponse.json(dbKeysMock);
54+
return defaultAuthType !== AuthType.Custom && !auth
55+
? HttpResponse.error()
56+
: HttpResponse.json(dbKeysMock);
5557
}),
5658
]);
5759

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
import { act, render, screen, userEvent } from '__tests__/_setup/testing-utils';
2+
import { RefreshButton } from 'app/components/Refresh/RefreshButton';
3+
import { liveRefreshOptions } from 'app/hooks/useLiveRefresh';
4+
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
5+
6+
vi.mock('react-hook-form', async () => {
7+
const actual =
8+
await vi.importActual<typeof import('react-hook-form')>('react-hook-form');
9+
10+
return { ...actual, useWatch: () => 'page' };
11+
});
12+
13+
const headers = () => {
14+
const head = new Headers();
15+
head.append('authorization', 'test');
16+
return head;
17+
};
18+
vi.mock('../../../app/hooks/useAuthProperties', () => ({
19+
useAuthProperties: () => ({
20+
fetchInfo: {
21+
headers: { headers: headers() },
22+
routePrefix: '',
23+
},
24+
isHeaderReady: true,
25+
}),
26+
}));
27+
28+
describe('RefreshButton', () => {
29+
beforeEach(() => {
30+
vi.useFakeTimers({ shouldAdvanceTime: true });
31+
});
32+
afterEach(vi.useRealTimers);
33+
34+
it('renders', async () => {
35+
render(<RefreshButton />);
36+
37+
const durationSelector = screen.getByLabelText('refresh-duration-selector');
38+
expect(durationSelector).toBeInTheDocument();
39+
40+
await userEvent.click(durationSelector);
41+
await act(vi.advanceTimersToNextTimerAsync);
42+
43+
const times = liveRefreshOptions.map((lro) => lro.value);
44+
times.forEach((time) => {
45+
expect(screen.getByLabelText('refresh-duration-' + time)).toBeInTheDocument();
46+
});
47+
});
48+
49+
it('runs live feed activies with refetch sample', async () => {
50+
const spy = vi.spyOn(global, 'fetch');
51+
render(<RefreshButton />);
52+
53+
const durationSelector = screen.getByLabelText('refresh-duration-selector');
54+
await userEvent.click(durationSelector);
55+
await act(vi.advanceTimersToNextTimerAsync);
56+
57+
const sampleOpt = liveRefreshOptions[5];
58+
const timeSelector = screen.getByLabelText('refresh-duration-' + sampleOpt.value);
59+
await userEvent.click(timeSelector);
60+
await act(vi.advanceTimersToNextTimerAsync);
61+
62+
expect(spy).toHaveBeenCalledOnce();
63+
64+
await act(async () => {
65+
await vi.advanceTimersByTimeAsync(1000 * 300 + 1);
66+
});
67+
expect(spy).toHaveBeenCalledTimes(2);
68+
69+
const durationStopper = screen.getByLabelText('refresh-duration-cancel-button');
70+
expect(screen.queryByLabelText('refresh-duration-selector')).not.toBeInTheDocument();
71+
expect(durationStopper).toBeInTheDocument();
72+
73+
await userEvent.click(durationStopper);
74+
await act(vi.advanceTimersToNextTimerAsync);
75+
76+
expect(
77+
screen.queryByLabelText('refresh-duration-cancel-button'),
78+
).not.toBeInTheDocument();
79+
expect(screen.getByLabelText('refresh-duration-selector')).toBeInTheDocument();
80+
});
81+
});

src/Serilog.Ui.Web/src/__tests__/components/ShellStructure/Header.spec.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ describe('Header', () => {
2323
await waitFor(() => {
2424
expect(screen.getByRole('button', { name: 'Filter' })).toBeInTheDocument();
2525
});
26-
await waitFor(() => {
27-
expect(screen.getByText('Serilog UI')).toBeInTheDocument();
28-
});
26+
27+
expect(screen.getByText('Serilog UI')).toBeInTheDocument();
28+
expect(screen.getByLabelText('refresh-duration-selector')).toBeInTheDocument();
2929
});
3030
});

src/Serilog.Ui.Web/src/app/components/Refresh/RefreshButton.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export const RefreshButton = () => {
1515
<Button
1616
fz={9}
1717
size="compact-md"
18+
aria-label="refresh-duration-cancel-button"
1819
color={theme.colors?.green?.[7]}
1920
onClick={stopLiveRefresh}
2021
className={classes.refreshButton}
@@ -31,6 +32,7 @@ export const RefreshButton = () => {
3132
<Button
3233
fz={9}
3334
size="compact-md"
35+
aria-label="refresh-duration-selector"
3436
color={theme.colors?.gray?.[7]}
3537
className={classes.activateRefreshButton}
3638
>
@@ -47,6 +49,7 @@ export const RefreshButton = () => {
4749
startLiveRefresh(p.value);
4850
}}
4951
variant="default"
52+
aria-label={`refresh-duration-${p.value}`}
5053
>
5154
{p.label}
5255
</Button>

0 commit comments

Comments
 (0)