-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#1533 - add unit tests for my DOIs view
- Loading branch information
1 parent
a8cddae
commit 629f81c
Showing
2 changed files
with
312 additions
and
0 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
255 changes: 255 additions & 0 deletions
255
packages/datagateway-dataview/src/views/table/dls/dlsMyDOIsTable.component.test.tsx
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,255 @@ | ||
import { | ||
render, | ||
type RenderResult, | ||
screen, | ||
within, | ||
} from '@testing-library/react'; | ||
import { | ||
dGCommonInitialState, | ||
type DataPublication, | ||
readSciGatewayToken, | ||
useDataPublicationCount, | ||
useDataPublicationsInfinite, | ||
} from 'datagateway-common'; | ||
import { createMemoryHistory, type MemoryHistory } from 'history'; | ||
import * as React from 'react'; | ||
import { QueryClient, QueryClientProvider } from 'react-query'; | ||
import { Provider } from 'react-redux'; | ||
import { Router } from 'react-router-dom'; | ||
import configureStore from 'redux-mock-store'; | ||
import thunk from 'redux-thunk'; | ||
import { | ||
applyDatePickerWorkaround, | ||
cleanupDatePickerWorkaround, | ||
findAllRows, | ||
findCellInRow, | ||
findColumnHeaderByName, | ||
findColumnIndexByName, | ||
findRowAt, | ||
} from '../../../setupTests'; | ||
import type { StateType } from '../../../state/app.types'; | ||
import { initialState as dgDataViewInitialState } from '../../../state/reducers/dgdataview.reducer'; | ||
import DLSMyDOIsTable from './dlsMyDOIsTable.component'; | ||
import userEvent from '@testing-library/user-event'; | ||
import type { UserEvent } from '@testing-library/user-event/setup/setup'; | ||
|
||
jest.mock('datagateway-common', () => { | ||
const originalModule = jest.requireActual('datagateway-common'); | ||
|
||
return { | ||
__esModule: true, | ||
...originalModule, | ||
useDataPublicationCount: jest.fn(), | ||
useDataPublicationsInfinite: jest.fn(), | ||
readSciGatewayToken: jest.fn(), | ||
}; | ||
}); | ||
|
||
describe('DLS MyData table component', () => { | ||
const mockStore = configureStore([thunk]); | ||
let state: StateType; | ||
let rowData: DataPublication[]; | ||
let history: MemoryHistory; | ||
let user: UserEvent; | ||
|
||
const renderComponent = (): RenderResult => { | ||
const store = mockStore(state); | ||
return render( | ||
<Provider store={store}> | ||
<Router history={history}> | ||
<QueryClientProvider client={new QueryClient()}> | ||
<DLSMyDOIsTable /> | ||
</QueryClientProvider> | ||
</Router> | ||
</Provider> | ||
); | ||
}; | ||
|
||
beforeEach(() => { | ||
history = createMemoryHistory(); | ||
user = userEvent.setup(); | ||
|
||
state = JSON.parse( | ||
JSON.stringify({ | ||
dgdataview: dgDataViewInitialState, | ||
dgcommon: dGCommonInitialState, | ||
}) | ||
); | ||
rowData = [ | ||
{ | ||
id: 7, | ||
pid: 'doi 1', | ||
description: 'foo bar', | ||
title: 'Data Publication Title', | ||
modTime: '2023-07-21', | ||
createTime: '2023-07-21', | ||
publicationDate: '2023-07-21', | ||
users: [ | ||
{ | ||
id: 1, | ||
contributorType: 'minter', | ||
fullName: 'John Smith', | ||
}, | ||
], | ||
content: { | ||
id: 7, | ||
dataCollectionInvestigations: [ | ||
{ | ||
id: 8, | ||
investigation: { | ||
id: 1, | ||
title: 'Title 1', | ||
name: 'Name 1', | ||
summary: 'foo bar', | ||
visitId: '1', | ||
doi: 'doi 1', | ||
size: 1, | ||
investigationInstruments: [ | ||
{ | ||
id: 1, | ||
instrument: { | ||
id: 3, | ||
name: 'Beamline 1', | ||
}, | ||
}, | ||
], | ||
startDate: '2023-07-21', | ||
endDate: '2023-07-22', | ||
}, | ||
dataCollection: { id: 9 }, | ||
}, | ||
], | ||
}, | ||
}, | ||
]; | ||
|
||
(useDataPublicationCount as jest.Mock).mockReturnValue({ | ||
data: 0, | ||
}); | ||
(useDataPublicationsInfinite as jest.Mock).mockReturnValue({ | ||
data: { pages: [rowData] }, | ||
fetchNextPage: jest.fn(), | ||
}); | ||
(readSciGatewayToken as jest.Mock).mockReturnValue({ | ||
username: 'testUser', | ||
}); | ||
global.Date.now = jest.fn(() => 1); | ||
}); | ||
|
||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('renders correctly', async () => { | ||
renderComponent(); | ||
|
||
const rows = await findAllRows(); | ||
expect(rows).toHaveLength(1); | ||
|
||
expect( | ||
await findColumnHeaderByName('datapublications.title') | ||
).toBeInTheDocument(); | ||
expect( | ||
await findColumnHeaderByName('datapublications.pid') | ||
).toBeInTheDocument(); | ||
expect( | ||
await findColumnHeaderByName('datapublications.publication_date') | ||
).toBeInTheDocument(); | ||
|
||
const row = rows[0]; | ||
|
||
expect( | ||
within( | ||
findCellInRow(row, { | ||
columnIndex: await findColumnIndexByName('datapublications.title'), | ||
}) | ||
).getByText('Data Publication Title') | ||
).toBeInTheDocument(); | ||
expect( | ||
within( | ||
findCellInRow(row, { | ||
columnIndex: await findColumnIndexByName('datapublications.pid'), | ||
}) | ||
).getByText('doi 1') | ||
).toBeInTheDocument(); | ||
expect( | ||
within( | ||
findCellInRow(row, { | ||
columnIndex: await findColumnIndexByName( | ||
'datapublications.publication_date' | ||
), | ||
}) | ||
).getByText('2023-07-21') | ||
).toBeInTheDocument(); | ||
}); | ||
|
||
it('updates filter query params on text filter', async () => { | ||
renderComponent(); | ||
|
||
const filterInput = await screen.findByRole('textbox', { | ||
name: 'Filter by datapublications.title', | ||
hidden: true, | ||
}); | ||
|
||
await user.type(filterInput, 'test'); | ||
|
||
expect(history.location.search).toBe( | ||
`?filters=${encodeURIComponent( | ||
'{"title":{"value":"test","type":"include"}}' | ||
)}` | ||
); | ||
|
||
await user.clear(filterInput); | ||
|
||
expect(history.location.search).toBe('?'); | ||
}); | ||
|
||
it('updates filter query params on date filter', async () => { | ||
applyDatePickerWorkaround(); | ||
|
||
renderComponent(); | ||
|
||
const filterInput = await screen.findByRole('textbox', { | ||
name: 'datapublications.publication_date filter to', | ||
}); | ||
|
||
await user.type(filterInput, '2023-07-21'); | ||
|
||
expect(history.location.search).toBe( | ||
`?filters=${encodeURIComponent( | ||
'{"publicationDate":{"endDate":"2023-07-21"}}' | ||
)}` | ||
); | ||
|
||
await user.clear(filterInput); | ||
|
||
expect(history.location.search).toBe('?'); | ||
|
||
cleanupDatePickerWorkaround(); | ||
}); | ||
|
||
it('updates sort query params on sort', async () => { | ||
renderComponent(); | ||
|
||
await user.click( | ||
await screen.findByRole('button', { name: 'datapublications.title' }) | ||
); | ||
|
||
expect(history.location.search).toBe( | ||
`?sort=${encodeURIComponent('{"title":"asc"}')}` | ||
); | ||
}); | ||
|
||
it('renders title and DOI as a links', async () => { | ||
renderComponent(); | ||
|
||
const row = await findRowAt(0); | ||
|
||
expect( | ||
await within(row).findByRole('link', { name: 'Data Publication Title' }) | ||
).toHaveAttribute('href', '/browse/dataPublication/7'); | ||
expect( | ||
await within(row).findByRole('link', { name: 'doi 1' }) | ||
).toHaveAttribute('href', 'https://doi.org/doi 1'); | ||
}); | ||
}); |