-
Notifications
You must be signed in to change notification settings - Fork 50
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 #852 from Ekep-Obasi/feat/skip-cache-see-latest
feat: add skip_cache param when tapping `See Latest` button
- Loading branch information
Showing
6 changed files
with
109 additions
and
25 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 |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import { fireEvent, render, waitFor } from '@testing-library/react' | ||
import * as React from 'react' | ||
import { LatestView } from '..' | ||
import { useDataStore } from '../../../../../stores/useDataStore' | ||
import { useUserStore } from '../../../../../stores/useUserStore' | ||
|
||
const mockedUseDataStore = useDataStore as jest.MockedFunction<typeof useDataStore> | ||
const mockedUseUserStore = useUserStore as jest.MockedFunction<typeof useUserStore> | ||
|
||
jest.mock('~/stores/useDataStore') | ||
jest.mock('~/stores/useUserStore') | ||
|
||
Object.defineProperty(window, 'matchMedia', { | ||
writable: true, | ||
value: jest.fn().mockImplementation((query) => ({ | ||
matches: false, | ||
media: query, | ||
onchange: null, | ||
addListener: jest.fn(), | ||
removeListener: jest.fn(), | ||
addEventListener: jest.fn(), | ||
removeEventListener: jest.fn(), | ||
dispatchEvent: jest.fn(), | ||
})), | ||
}) | ||
|
||
describe('LatestView Component', () => { | ||
beforeEach(() => { | ||
jest.clearAllMocks() | ||
mockedUseDataStore.mockReturnValue([jest.fn()]) | ||
mockedUseUserStore.mockReturnValue([0, jest.fn(), jest.fn()]) | ||
}) | ||
|
||
test('renders button correctly when new data added', () => { | ||
const { getByText } = render(<LatestView isSearchResult={false} />) | ||
const galleryIcon = document.querySelector('.heading__icon') as Node | ||
|
||
expect(getByText('Latest')).toBeInTheDocument() | ||
expect(galleryIcon).toBeInTheDocument() | ||
}) | ||
|
||
test('does not show the latest button when there are no nodes', () => { | ||
mockedUseUserStore.mockReturnValue([0, jest.fn(), jest.fn()]) | ||
|
||
const { queryByText } = render(<LatestView isSearchResult={false} />) | ||
|
||
expect(queryByText('See Latest (0)')).toBeNull() | ||
}) | ||
|
||
test('calls latest endpoint with param on button click', async () => { | ||
const fetchDataMock = jest.fn() | ||
|
||
mockedUseDataStore.mockReturnValue([fetchDataMock]) | ||
|
||
const setNodeCountMock = jest.fn() | ||
|
||
const setBudgetMock = jest.fn() | ||
|
||
mockedUseUserStore.mockReturnValue([5, setNodeCountMock, setBudgetMock]) | ||
|
||
const { getByText } = render(<LatestView isSearchResult={false} />) | ||
|
||
fireEvent.click(getByText('See Latest (5)')) | ||
|
||
await waitFor(() => { | ||
expect(fetchDataMock).toHaveBeenCalledWith(setBudgetMock, { skip_cache: 'true' }) | ||
expect(setNodeCountMock).toHaveBeenCalledWith('CLEAR') | ||
}) | ||
}) | ||
}) |
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