Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EES-5159 hook up data set preview, variables and footnotes sections #4921

Merged
merged 1 commit into from
Jun 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import DataSetFileDetails from '@frontend/modules/data-catalogue/components/Data
import DataSetFilePageNav from '@frontend/modules/data-catalogue/components/DataSetFilePageNav';
import DataSetFilePreview from '@frontend/modules/data-catalogue/components/DataSetFilePreview';
import DataSetFileUsage from '@frontend/modules/data-catalogue/components/DataSetFileUsage';
import DataSetFileVariables from '@frontend/modules/data-catalogue/components/DataSetFileVariables';
import DataSetFileFootnotes from '@frontend/modules/data-catalogue/components/DataSetFileFootnotes';
import styles from '@frontend/modules/data-catalogue/DataSetPage.module.scss';
import NotFoundPage from '@frontend/modules/NotFoundPage';
import apiDataSetQueries from '@frontend/queries/apiDataSetQueries';
Expand All @@ -30,21 +32,14 @@ import classNames from 'classnames';
import { GetServerSideProps } from 'next';
import { useRouter } from 'next/router';
import React, { useEffect, useState } from 'react';

// TODO EES-4856
export const pageHiddenSections = {
dataSetPreview: 'Data set preview',
dataSetVariables: 'Variables in this data set',
footnotes: 'Footnotes',
} as const;
import omit from 'lodash/omit';

export const pageBaseSections = {
dataSetDetails: 'Data set details',
dataSetPreview: 'Data set preview',
dataSetVariables: 'Variables in this data set',
dataSetFootnotes: 'Footnotes',
dataSetUsage: 'Using this data',
// TODO EES-4856
// dataSetPreview: 'Data set preview',
// dataSetVariables: 'Variables in this data set',
// footnotes: 'Footnotes',
} as const;

export const pageApiSections = {
Expand All @@ -57,10 +52,7 @@ export const pageSections = {
...pageApiSections,
} as const;

export type PageHiddenSection = typeof pageHiddenSections;
export type PageSection = typeof pageSections;

export type PageHiddenSectionId = keyof PageHiddenSection;
export type PageSectionId = keyof PageSection;

interface Props {
Expand All @@ -77,7 +69,6 @@ export default function DataSetFilePage({
const [activeSection, setActiveSection] =
useState<PageSectionId>('dataSetDetails');
const [fullScreenPreview, toggleFullScreenPreview] = useToggle(false);
const [showAllPreview, toggleShowAllPreview] = useToggle(false);
const router = useRouter();

const handleDownload = async () => {
Expand Down Expand Up @@ -114,17 +105,18 @@ export default function DataSetFilePage({
pageSections[pageSectionId]
) {
setActiveSection(pageSectionId);

router.push(
{
pathname: `/data-catalogue/data-set/${dataSetFile.id}`,
hash: pageSectionId,
},
undefined,
{
shallow: true,
},
);
if (router.isReady) {
router.push(
{
pathname: `/data-catalogue/data-set/${dataSetFile.id}`,
hash: pageSectionId,
},
undefined,
{
shallow: true,
},
);
}
}
});
}, 10);
Expand All @@ -143,6 +135,11 @@ export default function DataSetFilePage({

const { file, release, summary, title } = dataSetFile;

const allNavSections = apiDataSet ? pageSections : pageBaseSections;
const navSections = dataSetFile.footnotes.length
? allNavSections
: omit(allNavSections, 'dataSetFootnotes');

return (
<Page
title={title}
Expand All @@ -152,10 +149,9 @@ export default function DataSetFilePage({
>
{fullScreenPreview ? (
<DataSetFilePreview
dataCsvPreview={dataSetFile.file.dataCsvPreview}
fullScreen={fullScreenPreview}
showAll={showAllPreview}
onToggleFullScreen={toggleFullScreenPreview}
onToggleShowAll={toggleShowAllPreview}
/>
) : (
<>
Expand Down Expand Up @@ -204,29 +200,27 @@ export default function DataSetFilePage({
<div className="govuk-grid-row">
<DataSetFilePageNav
activeSection={activeSection}
sections={apiDataSet ? pageSections : pageBaseSections}
sections={navSections}
onClickItem={setActiveSection}
/>

<div className="govuk-grid-column-two-thirds">
<DataSetFileDetails dataSetFile={dataSetFile} />

{/* TODO EES-4856 */}
{/* <DataSetFilePreview
<DataSetFilePreview
dataCsvPreview={dataSetFile.file.dataCsvPreview}
fullScreen={fullScreenPreview}
showAll={showAllPreview}
onToggleFullScreen={() => {
toggleFullScreenPreview();
window.scrollTo(0, 0);
}}
onToggleShowAll={toggleShowAllPreview}
/> */}
/>

{/* TODO EES-4856 */}
{/* <DataSetFileVariables /> */}
<DataSetFileVariables variables={dataSetFile.file.variables} />

{/* TODO EES-4856 */}
{/* <DataSetFileFootnotes /> */}
{!!dataSetFile.footnotes.length && (
<DataSetFileFootnotes footnotes={dataSetFile.footnotes} />
)}

<DataSetFileUsage
hasApiDataSet={!!apiDataSet}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,62 @@ import {
ApiDataSetVersion,
} from '@frontend/services/apiDataSetService';
import {
DataSetCsvPreview,
DataSetFile,
DataSetFootnote,
DataSetFileSummary,
DataSetVariable,
} from '@frontend/services/dataSetFileService';

export const testDataSetCsvPreview: DataSetCsvPreview = {
headers: ['time_period', 'geographic_level', 'filter_1', 'indicator_1'],
rows: [
['201819', 'National', 'filter_1_value', '100'],
['201920', 'National', 'filter_1_value', '101'],
['202021', 'National', 'filter_1_value', '102'],
['202122', 'National', 'filter_1_value', '103'],
['202223', 'National', 'filter_1_value', '104'],
],
};

export const testDataSetVariables: DataSetVariable[] = [
{
label: 'Filter 1 label',
value: 'filter_1',
},
{
label: 'Filter 2 label',
value: 'filter_2',
},
{
label: 'Indicator 1 label',
value: 'indicator_1',
},
{
label: 'Indicator 2 label',
value: 'indicator_2',
},
{
label: 'Indicator 3 label',
value: 'indicator_3',
},
{
label: 'Indicator 4 label',
value: 'indicator_4',
},
];

export const testDataSetFootnotes: DataSetFootnote[] = [
{
id: 'footnote-1',
label: 'Footnote 1',
},
{
id: 'footnote-2',
label: 'Footnote 2',
},
];

export const testDataSetFileSummaries: DataSetFileSummary[] = [
{
api: {
Expand Down Expand Up @@ -129,8 +181,8 @@ export const testDataSetFile: DataSetFile = {
geographicLevels: ['Local authority', 'National'],
indicators: ['Indicator 1', 'Indicator 2'],
},
dataCsvPreview: { headers: ['column_1'], rows: [['1']] },
variables: [{ value: 'column_1', label: 'Column 1 is for something' }],
dataCsvPreview: testDataSetCsvPreview,
variables: testDataSetVariables,
subjectId: 'subject-id',
},
release: {
Expand All @@ -150,7 +202,7 @@ export const testDataSetFile: DataSetFile = {
},
summary: 'Data set 1 summary',
title: 'Data set 1',
footnotes: [{ id: 'footnote-1', label: 'Footnote 1' }],
footnotes: testDataSetFootnotes,
};

export const testDataSetWithApi: DataSetFile = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ const downloadService = _downloadService as jest.Mocked<
>;

describe('DataSetFilePage', () => {
const testDataSetFileWithoutFootnotes = { ...testDataSetFile, footnotes: [] };

test('renders the data set file heading, summary and info', async () => {
render(<DataSetFilePage dataSetFile={testDataSetFile} />);

Expand Down Expand Up @@ -90,6 +92,63 @@ describe('DataSetFilePage', () => {
).toBeInTheDocument();
});

test('renders the data set preview section', async () => {
render(<DataSetFilePage dataSetFile={testDataSetFile} />);

expect(await screen.findByText('On this page')).toBeInTheDocument();

expect(
screen.getByRole('heading', { name: 'Data set preview' }),
).toBeInTheDocument();
});

test('renders the data set variables section', async () => {
render(<DataSetFilePage dataSetFile={testDataSetFile} />);

expect(await screen.findByText('On this page')).toBeInTheDocument();

expect(
screen.getByRole('heading', { name: 'Variables in this data set' }),
).toBeInTheDocument();
});

test('renders the data set footnotes section', async () => {
render(<DataSetFilePage dataSetFile={testDataSetFile} />);

expect(await screen.findByText('On this page')).toBeInTheDocument();

expect(
screen.getByRole('heading', { name: 'Footnotes' }),
).toBeInTheDocument();
});

test('does not render the data set footnotes section when there are no footnotes', async () => {
render(<DataSetFilePage dataSetFile={testDataSetFileWithoutFootnotes} />);

expect(await screen.findByText('On this page')).toBeInTheDocument();

expect(
screen.queryByRole('heading', { name: 'Footnotes' }),
).not.toBeInTheDocument();
});

test('renders the data set usage section', async () => {
render(<DataSetFilePage dataSetFile={testDataSetFile} />);

expect(await screen.findByText('On this page')).toBeInTheDocument();

expect(
screen.getByRole('heading', { name: 'Using this data' }),
).toBeInTheDocument();

expect(
screen.getByRole('link', { name: 'View or create your own tables' }),
).toHaveAttribute(
'href',
'/data-tables/publication-slug/release-slug?subjectId=subject-id',
);
});

describe('non-API data set', () => {
test('does not render API version info', async () => {
render(<DataSetFilePage dataSetFile={testDataSetFile} />);
Expand All @@ -109,19 +168,28 @@ describe('DataSetFilePage', () => {
screen.getByRole('navigation', { name: 'On this page' }),
);
const navLinks = nav.getAllByRole('link');
expect(navLinks).toHaveLength(2);
expect(navLinks).toHaveLength(5);
expect(navLinks[0]).toHaveAttribute('href', '#dataSetDetails');
expect(navLinks[1]).toHaveAttribute('href', '#dataSetUsage');
expect(navLinks[1]).toHaveAttribute('href', '#dataSetPreview');
expect(navLinks[2]).toHaveAttribute('href', '#dataSetVariables');
expect(navLinks[3]).toHaveAttribute('href', '#dataSetFootnotes');
expect(navLinks[4]).toHaveAttribute('href', '#dataSetUsage');
});

test('renders the data set usage section', async () => {
render(<DataSetFilePage dataSetFile={testDataSetFile} />);
test('renders the page navigation correctly when there are no footnotes', async () => {
render(<DataSetFilePage dataSetFile={testDataSetFileWithoutFootnotes} />);

expect(await screen.findByText('On this page')).toBeInTheDocument();

expect(
screen.getByRole('heading', { name: 'Using this data' }),
).toBeInTheDocument();
const nav = within(
screen.getByRole('navigation', { name: 'On this page' }),
);
const navLinks = nav.getAllByRole('link');
expect(navLinks).toHaveLength(4);
expect(navLinks[0]).toHaveAttribute('href', '#dataSetDetails');
expect(navLinks[1]).toHaveAttribute('href', '#dataSetPreview');
expect(navLinks[2]).toHaveAttribute('href', '#dataSetVariables');
expect(navLinks[3]).toHaveAttribute('href', '#dataSetUsage');
});

test('does not render the API version history section', async () => {
Expand Down Expand Up @@ -178,34 +246,39 @@ describe('DataSetFilePage', () => {
);

const navLinks = nav.getAllByRole('link');
expect(navLinks).toHaveLength(4);
expect(navLinks).toHaveLength(7);
expect(navLinks[0]).toHaveAttribute('href', '#dataSetDetails');
expect(navLinks[1]).toHaveAttribute('href', '#dataSetUsage');
expect(navLinks[2]).toHaveAttribute('href', '#apiVersionHistory');
expect(navLinks[3]).toHaveAttribute('href', '#apiQuickStart');
expect(navLinks[1]).toHaveAttribute('href', '#dataSetPreview');
expect(navLinks[2]).toHaveAttribute('href', '#dataSetVariables');
expect(navLinks[3]).toHaveAttribute('href', '#dataSetFootnotes');
expect(navLinks[4]).toHaveAttribute('href', '#dataSetUsage');
expect(navLinks[5]).toHaveAttribute('href', '#apiVersionHistory');
expect(navLinks[6]).toHaveAttribute('href', '#apiQuickStart');
});

test('renders the data set usage section', async () => {
test('renders the page navigation correctly when there are no footnotes', async () => {
render(
<DataSetFilePage
apiDataSet={testApiDataSet}
apiDataSetVersion={testApiDataSetVersion}
dataSetFile={testDataSetFile}
dataSetFile={testDataSetFileWithoutFootnotes}
/>,
);

expect(await screen.findByText('On this page')).toBeInTheDocument();

expect(
screen.getByRole('heading', { name: 'Using this data' }),
).toBeInTheDocument();

expect(
screen.getByRole('link', { name: 'View or create your own tables' }),
).toHaveAttribute(
'href',
'/data-tables/publication-slug/release-slug?subjectId=subject-id',
const nav = within(
screen.getByRole('navigation', { name: 'On this page' }),
);

const navLinks = nav.getAllByRole('link');
expect(navLinks).toHaveLength(6);
expect(navLinks[0]).toHaveAttribute('href', '#dataSetDetails');
expect(navLinks[1]).toHaveAttribute('href', '#dataSetPreview');
expect(navLinks[2]).toHaveAttribute('href', '#dataSetVariables');
expect(navLinks[3]).toHaveAttribute('href', '#dataSetUsage');
expect(navLinks[4]).toHaveAttribute('href', '#apiVersionHistory');
expect(navLinks[5]).toHaveAttribute('href', '#apiQuickStart');
});

test('renders the API version history section', async () => {
Expand Down
Loading