-
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.
Merge pull request #712 from systemli/Use-the-submitting-state-when-t…
…he-ticker-is-saved 💄 Use the submitting state when the ticker is saved
- Loading branch information
Showing
3 changed files
with
89 additions
and
19 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
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 |
---|---|---|
|
@@ -15,22 +15,15 @@ describe('TickerForm', () => { | |
}) | ||
|
||
const callback = vi.fn() | ||
|
||
const ticker = () => { | ||
return { | ||
id: 1, | ||
title: 'Ticker', | ||
active: false, | ||
information: {}, | ||
location: {}, | ||
} as Ticker | ||
} | ||
const setSubmitting = vi.fn() | ||
|
||
beforeEach(() => { | ||
fetchMock.resetMocks() | ||
callback.mockClear() | ||
setSubmitting.mockClear() | ||
}) | ||
|
||
function setup(ticker: Ticker) { | ||
function setup(ticker?: Ticker) { | ||
const client = new QueryClient({ | ||
defaultOptions: { | ||
queries: { | ||
|
@@ -43,7 +36,7 @@ describe('TickerForm', () => { | |
<MemoryRouter> | ||
<AuthProvider> | ||
<div> | ||
<TickerForm ticker={ticker} id="tickerForm" callback={callback} /> | ||
<TickerForm ticker={ticker} id="tickerForm" callback={callback} setSubmitting={setSubmitting} /> | ||
<input name="Submit" type="submit" value="Submit" form="tickerForm" /> | ||
</div> | ||
</AuthProvider> | ||
|
@@ -53,14 +46,78 @@ describe('TickerForm', () => { | |
} | ||
|
||
it('should render the component', async () => { | ||
setup(ticker()) | ||
setup({ | ||
id: 1, | ||
title: 'Ticker', | ||
active: false, | ||
information: {}, | ||
location: {}, | ||
} as Ticker) | ||
|
||
expect(screen.getByRole('textbox', { name: 'Title' })).toBeInTheDocument() | ||
expect(screen.getByRole('checkbox', { name: 'Active' })).toBeInTheDocument() | ||
}) | ||
|
||
it('should submit the form', async () => { | ||
setup(ticker()) | ||
it('should submit for new ticker', async () => { | ||
setup() | ||
|
||
fetchMock.mockResponseOnce(JSON.stringify({ status: 'success' })) | ||
|
||
await userEvent.click(screen.getByRole('checkbox', { name: 'Active' })) | ||
await userEvent.type(screen.getByRole('textbox', { name: 'Title' }), 'New Ticker') | ||
await userEvent.type(screen.getByRole('textbox', { name: 'Description' }), 'Description') | ||
await userEvent.type(screen.getByRole('textbox', { name: 'Author' }), 'Author') | ||
await userEvent.type(screen.getByRole('textbox', { name: 'Homepage' }), 'https://example.org') | ||
await userEvent.type(screen.getByRole('textbox', { name: 'E-Mail' }), '[email protected]') | ||
await userEvent.type(screen.getByRole('textbox', { name: 'Twitter' }), 'account') | ||
await userEvent.type(screen.getByRole('textbox', { name: 'Facebook' }), 'account') | ||
await userEvent.type(screen.getByRole('textbox', { name: 'Threads' }), '@account') | ||
await userEvent.type(screen.getByRole('textbox', { name: 'Instagram' }), 'account') | ||
await userEvent.type(screen.getByRole('textbox', { name: 'Telegram' }), 'account') | ||
await userEvent.type(screen.getByRole('textbox', { name: 'Mastodon' }), 'https://mastodon.social/@account') | ||
await userEvent.type(screen.getByRole('textbox', { name: 'Bluesky' }), 'https://bsky.app/profile/account.bsky.social') | ||
|
||
await userEvent.click(screen.getByRole('button', { name: 'Submit' })) | ||
|
||
expect(callback).toHaveBeenCalledTimes(1) | ||
expect(setSubmitting).toHaveBeenCalledTimes(2) | ||
expect(fetchMock).toHaveBeenCalledTimes(1) | ||
expect(fetchMock).toHaveBeenCalledWith('http://localhost:8080/v1/admin/tickers', { | ||
body: JSON.stringify({ | ||
title: 'New Ticker', | ||
active: true, | ||
description: 'Description', | ||
information: { | ||
author: 'Author', | ||
email: '[email protected]', | ||
url: 'https://example.org', | ||
twitter: 'account', | ||
facebook: 'account', | ||
threads: '@account', | ||
instagram: 'account', | ||
telegram: 'account', | ||
mastodon: 'https://mastodon.social/@account', | ||
bluesky: 'https://bsky.app/profile/account.bsky.social', | ||
}, | ||
location: { lat: 0, lon: 0 }, | ||
}), | ||
headers: { | ||
Accept: 'application/json', | ||
Authorization: `Bearer ${token}`, | ||
'Content-Type': 'application/json', | ||
}, | ||
method: 'post', | ||
}) | ||
}) | ||
|
||
it('should submit for existing ticker', async () => { | ||
setup({ | ||
id: 1, | ||
title: 'Ticker', | ||
active: false, | ||
information: {}, | ||
location: {}, | ||
} as Ticker) | ||
|
||
fetchMock.mockResponseOnce(JSON.stringify({ status: 'success' })) | ||
|
||
|
@@ -82,6 +139,7 @@ describe('TickerForm', () => { | |
await userEvent.click(screen.getByRole('button', { name: 'Submit' })) | ||
|
||
expect(callback).toHaveBeenCalledTimes(1) | ||
expect(setSubmitting).toHaveBeenCalledTimes(2) | ||
expect(fetchMock).toHaveBeenCalledTimes(1) | ||
expect(fetchMock).toHaveBeenCalledWith('http://localhost:8080/v1/admin/tickers/1', { | ||
body: JSON.stringify({ | ||
|
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