Skip to content

Commit

Permalink
#1533 - add unit tests for my DOIs view
Browse files Browse the repository at this point in the history
  • Loading branch information
louise-davies committed Jul 21, 2023
1 parent a8cddae commit 629f81c
Show file tree
Hide file tree
Showing 2 changed files with 312 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,9 @@ const DLSRoutes = {
datasets: '/browse/proposal/INVESTIGATION 1/investigation/1/dataset',
datafiles:
'/browse/proposal/INVESTIGATION 1/investigation/1/dataset/1/datafile',
dataPublicationLanding: '/browse/dataPublication/1',
mydata: '/my-data/DLS',
mydois: '/my-dois/DLS',
};

describe('PageTable', () => {
Expand Down Expand Up @@ -1155,6 +1157,44 @@ describe('PageTable', () => {
expect(history.location.pathname).toBe('/login');
});

it('renders DLSMyDOIsTable for DLS my dois route', async () => {
history.push(DLSRoutes.mydois);

render(
<PageRouting
view="table"
location={history.location}
loggedInAnonymously={false}
/>,
{ wrapper: Wrapper }
);

expect(
await findColumnHeaderByName('datapublications.title')
).toBeInTheDocument();
expect(
await findColumnHeaderByName('datapublications.pid')
).toBeInTheDocument();
expect(
await findColumnHeaderByName('datapublications.publication_date')
).toBeInTheDocument();
});

it('redirects to login page when not signed in (DLSMyDOIsTable) ', () => {
history.push(DLSRoutes.mydois);

render(
<PageRouting
loggedInAnonymously
view="table"
location={history.location}
/>,
{ wrapper: Wrapper }
);

expect(history.location.pathname).toBe('/login');
});

it('renders DLSProposalTable for DLS proposal route', async () => {
history.push(DLSRoutes.proposals);

Expand Down Expand Up @@ -1365,4 +1405,21 @@ describe('PageTable', () => {
expect(await screen.findByText('loading.oops')).toBeInTheDocument();
});
});

it('renders DLSDataPublicationLanding for DLS dataPublications route', async () => {
history.push(DLSRoutes.dataPublicationLanding);

render(
<PageRouting
view={null}
location={history.location}
loggedInAnonymously={false}
/>,
{ wrapper: Wrapper }
);

expect(
await screen.findByTestId('dls-dataPublication-landing')
).toBeInTheDocument();
});
});
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');
});
});

0 comments on commit 629f81c

Please sign in to comment.