-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
992a16f
commit c4b8705
Showing
2 changed files
with
54 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
import { QueryClient, QueryClientProvider } from '@tanstack/react-query' | ||
import { render, screen } from '@testing-library/react' | ||
import userEvent from '@testing-library/user-event' | ||
import sign from 'jwt-encode' | ||
import { MemoryRouter } from 'react-router' | ||
import { vi } from 'vitest' | ||
import { Ticker } from '../../api/Ticker' | ||
|
@@ -9,12 +10,21 @@ import { AuthProvider } from '../../contexts/AuthContext' | |
import { NotificationProvider } from '../../contexts/NotificationContext' | ||
import TickerUsersForm from './TickerUsersForm' | ||
|
||
const token = sign({ id: 1, email: '[email protected]', roles: ['user'], exp: new Date().getTime() / 1000 + 600 }, 'secret') | ||
|
||
describe('TickerUsersForm', () => { | ||
beforeAll(() => { | ||
localStorage.setItem('token', token) | ||
}) | ||
|
||
beforeEach(() => { | ||
fetchMock.resetMocks() | ||
handleSubmit.mockClear() | ||
}) | ||
|
||
function setup(defaultValue: Array<User>, ticker: Ticker, onSubmit: () => void) { | ||
const handleSubmit = vi.fn() | ||
|
||
const setup = (defaultValue: Array<User>, ticker: Ticker, onSubmit: () => void) => { | ||
const client = new QueryClient({ | ||
defaultOptions: { | ||
queries: { | ||
|
@@ -28,6 +38,7 @@ describe('TickerUsersForm', () => { | |
<AuthProvider> | ||
<NotificationProvider> | ||
<TickerUsersForm defaultValue={defaultValue} onSubmit={onSubmit} ticker={ticker} /> | ||
<input name="Submit" type="submit" value="Submit" form="tickerUsersForm" /> | ||
</NotificationProvider> | ||
</AuthProvider> | ||
</MemoryRouter> | ||
|
@@ -44,6 +55,7 @@ describe('TickerUsersForm', () => { | |
id: 1, | ||
email: '[email protected]', | ||
} as User | ||
|
||
fetchMock.mockResponseOnce( | ||
JSON.stringify({ | ||
data: { | ||
|
@@ -53,7 +65,6 @@ describe('TickerUsersForm', () => { | |
}) | ||
) | ||
|
||
const handleSubmit = vi.fn() | ||
setup([user], ticker, handleSubmit) | ||
|
||
expect(screen.getByRole('combobox')).toBeInTheDocument() | ||
|
@@ -64,5 +75,25 @@ describe('TickerUsersForm', () => { | |
expect(screen.getAllByText('[email protected]')).toHaveLength(2) | ||
|
||
await userEvent.click(screen.getAllByText('[email protected]')[1]) | ||
|
||
fetchMock.mockResponseOnce( | ||
JSON.stringify({ | ||
status: 'success', | ||
}) | ||
) | ||
|
||
await userEvent.click(screen.getByRole('button', { name: 'Submit' })) | ||
|
||
expect(handleSubmit).toHaveBeenCalledTimes(1) | ||
expect(fetchMock).toHaveBeenCalledTimes(2) | ||
expect(fetchMock).toHaveBeenCalledWith('http://localhost:8080/v1/admin/tickers/1/users', { | ||
body: JSON.stringify({ users: [] }), | ||
headers: { | ||
Accept: 'application/json', | ||
Authorization: `Bearer ${token}`, | ||
'Content-Type': 'application/json', | ||
}, | ||
method: 'put', | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters