-
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 #632 from systemli/Improve-background-loading-for-…
…Tickers ⚡️ Improve background loading for Tickers
- Loading branch information
Showing
12 changed files
with
377 additions
and
208 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
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
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
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 |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { useQuery } from '@tanstack/react-query' | ||
import { GetTickersQueryParams, useTickerApi } from '../api/Ticker' | ||
|
||
interface Props { | ||
token: string | ||
params: GetTickersQueryParams | ||
} | ||
|
||
const useTickersQuery = ({ token, params }: Props) => { | ||
const { getTickers } = useTickerApi(token) | ||
|
||
return useQuery({ | ||
queryKey: ['tickers', params], | ||
queryFn: () => getTickers(params), | ||
placeholderData: previousData => previousData, | ||
}) | ||
} | ||
|
||
export default useTickersQuery |
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 |
---|---|---|
@@ -0,0 +1,93 @@ | ||
import { QueryClient, QueryClientProvider } from '@tanstack/react-query' | ||
import { render, screen } from '@testing-library/react' | ||
import sign from 'jwt-encode' | ||
import { MemoryRouter } from 'react-router' | ||
import { AuthProvider } from '../contexts/AuthContext' | ||
import HomeView from './HomeView' | ||
|
||
describe('HomeView', () => { | ||
function setup() { | ||
const client = new QueryClient({ | ||
defaultOptions: { | ||
queries: { | ||
retry: false, | ||
}, | ||
}, | ||
}) | ||
|
||
return render( | ||
<QueryClientProvider client={client}> | ||
<MemoryRouter> | ||
<AuthProvider> | ||
<HomeView /> | ||
</AuthProvider> | ||
</MemoryRouter> | ||
</QueryClientProvider> | ||
) | ||
} | ||
|
||
it('should render error when query fails', async () => { | ||
fetchMock.mockReject(new Error('Failed to fetch')) | ||
|
||
const token = sign( | ||
{ | ||
id: 1, | ||
email: '[email protected]', | ||
roles: ['user', 'admin'], | ||
exp: new Date().getTime() / 1000 + 600, | ||
}, | ||
'secret' | ||
) | ||
vi.spyOn(window.localStorage.__proto__, 'getItem').mockReturnValue(token) | ||
|
||
setup() | ||
|
||
expect(fetchMock).toHaveBeenCalledTimes(1) | ||
|
||
expect(screen.getByText(/loading/i)).toBeInTheDocument() | ||
expect(await screen.findByText('Unable to fetch tickers from server.')).toBeInTheDocument() | ||
}) | ||
|
||
it('should render tickers list', async () => { | ||
fetchMock.mockResponseOnce(JSON.stringify({ data: { tickers: [] } })) | ||
|
||
const token = sign( | ||
{ | ||
id: 1, | ||
email: '[email protected]', | ||
roles: ['user', 'admin'], | ||
exp: new Date().getTime() / 1000 + 600, | ||
}, | ||
'secret' | ||
) | ||
|
||
vi.spyOn(window.localStorage.__proto__, 'getItem').mockReturnValue(token) | ||
|
||
setup() | ||
|
||
expect(fetchMock).toHaveBeenCalledTimes(2) | ||
|
||
expect(screen.getByText(/loading/i)).toBeInTheDocument() | ||
expect(await screen.findByText('Tickers')).toBeInTheDocument() | ||
}) | ||
|
||
it('should redirect to ticker when user has only one ticker', async () => { | ||
fetchMock.mockResponseOnce(JSON.stringify({ data: { tickers: [{ id: 1 }] } })) | ||
|
||
const token = sign( | ||
{ | ||
id: 1, | ||
email: '[email protected]', | ||
roles: ['user'], | ||
exp: new Date().getTime() / 1000 + 600, | ||
}, | ||
'secret' | ||
) | ||
|
||
vi.spyOn(window.localStorage.__proto__, 'getItem').mockReturnValue(token) | ||
|
||
setup() | ||
|
||
expect(screen.getByText(/loading/i)).toBeInTheDocument() | ||
}) | ||
}) |
Oops, something went wrong.