Skip to content

Commit

Permalink
Merge branch 'EAR-2109-list-collector-additional-pages' of github.com…
Browse files Browse the repository at this point in the history
…:ONSdigital/eq-author-app into EAR-2109-list-collector-additional-pages
  • Loading branch information
farres1 committed Nov 6, 2023
2 parents 1ce4e2a + 8451100 commit 6fc9ffd
Show file tree
Hide file tree
Showing 7 changed files with 152 additions and 45 deletions.
26 changes: 26 additions & 0 deletions eq-author-api/schema/resolvers/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,32 @@ const Resolvers = {
};
}
},
supplementaryDataSurveyIdList: async () => {
const url = `${process.env.SUPPLEMENTARY_DATA_GATEWAY}survey_list`;

try {
const response = await authorisedRequest(
url,
process.env.SUPPLEMENTARY_DATA_GATEWAY_AUDIENCE,
{
headers: { "Content-Type": "application/json" },
}
);
if (response.status !== 200) {
throw new Error(
`unable to get survey list. status(${response.status})`
);
}
return {
surveyIdList: response.data,
};
} catch (err) {
logger.error(err.message);
return {
surveyIdList: [],
};
}
},
supplementaryData: (_, args, ctx) => ctx.questionnaire.supplementaryData,
listNames: (_, args, ctx) => {
const listNames = [];
Expand Down
34 changes: 34 additions & 0 deletions eq-author-api/schema/tests/SupplementaryDataSurveyIdList.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/* eslint-disable camelcase */
const executeQuery = require("../../tests/utils/executeQuery");

jest.mock("node-fetch");

const fetch = require("node-fetch");
const query = `
query GetSupplementaryDataSurveyIdList {
supplementaryDataSurveyIdList {
surveyIdList
}
}`;

fetch.mockImplementation(() =>
Promise.resolve({
status: 200,
json: () => ["121", "122", "123"],
})
);

describe("SupplementaryDataSurveyIdList", () => {
it("should query supplementary data survey id list schema", async () => {
const expectedResponse = {
data: {
supplementaryDataSurveyIdList: {
surveyIdList: ["121", "122", "123"],
},
},
};

const response = await executeQuery(query);
expect(response).toMatchObject(expectedResponse);
});
});
5 changes: 5 additions & 0 deletions eq-author-api/schema/typeDefs.js
Original file line number Diff line number Diff line change
Expand Up @@ -849,6 +849,10 @@ type SupplementaryDataVersions {
versions: [Version!]!
}
type SupplementaryDataSurveyIdList {
surveyIdList: [String]!
}
type SupplementaryDataField {
id: ID,
type: String,
Expand Down Expand Up @@ -908,6 +912,7 @@ type Query {
collectionLists: CollectionLists
list(input: QueryInput!): List
supplementaryDataVersions(id: ID!): SupplementaryDataVersions
supplementaryDataSurveyIdList: SupplementaryDataSurveyIdList
supplementaryData: SupplementaryData
publishHistory: [PublishHistoryEvent]
listNames: [ListName]
Expand Down
15 changes: 13 additions & 2 deletions eq-author/src/App/dataSettings/SupplementaryDataPage/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import styled from "styled-components";
import { useQuery, useMutation } from "@apollo/react-hooks";
import { withRouter, useParams } from "react-router-dom";

import GET_SUPPLEMENTARY_DATA_SURVEY_ID_LIST from "graphql/getSupplementaryDataSurveyIdList.graphql";
import UPDATE_SUPPLEMENTARY_DATA from "graphql/updateSupplementaryData.graphql";
import GET_SUPPLEMENTARY_DATA from "graphql/getSupplementaryData.graphql";
import UNLINK_SUPPLEMENTARY_DATA from "graphql/unlinkSupplementaryData.graphql";
Expand All @@ -19,7 +20,6 @@ import Loading from "components/Loading";
import Error from "components/Error";

import Theme from "contexts/themeContext";
import { SURVEY_IDS } from "constants/surveyIDs";
import { colors, radius } from "constants/theme";
import Icon from "assets/icon-select.svg";
import {
Expand Down Expand Up @@ -143,6 +143,13 @@ const SupplementaryDataPage = () => {
fetchPolicy: "cache-and-network",
});

const { data: supplementaryDataSurveyIdList } = useQuery(
GET_SUPPLEMENTARY_DATA_SURVEY_ID_LIST,
{
fetchPolicy: "network-only",
}
);

const buildData = () => {
let schemaData;
if (supplementaryData?.supplementaryData?.data) {
Expand All @@ -153,13 +160,17 @@ const SupplementaryDataPage = () => {
if (schemaData) {
schemaData.surveyId = supplementaryData.supplementaryData?.surveyId;
}

return schemaData;
};

const tableData = buildData();

const schemaData = tableData?.data;

const surveyIdList =
supplementaryDataSurveyIdList?.supplementaryDataSurveyIdList?.surveyIdList;

const handleUnlinkClick = () => {
setShowUnlinkModal(true);
};
Expand Down Expand Up @@ -233,7 +244,7 @@ const SupplementaryDataPage = () => {
>
Survey ID
</Option>
{SURVEY_IDS.map((surveyID) => (
{surveyIdList?.map((surveyID) => (
<Option key={surveyID} value={surveyID}>
{surveyID}
</Option>
Expand Down
111 changes: 69 additions & 42 deletions eq-author/src/App/dataSettings/SupplementaryDataPage/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,44 +8,14 @@ import { MeContext } from "App/MeContext";
import { publishStatusSubscription } from "components/EditorLayout/Header";
import QuestionnaireContext from "components/QuestionnaireContext";
import unlinkSupplementaryDataMutation from "graphql/unlinkSupplementaryData.graphql";
import GET_SUPPLEMENTARY_DATA_SURVEY_ID_LIST from "graphql/getSupplementaryDataSurveyIdList.graphql";
import GET_SUPPLEMENTARY_DATA_VERSIONS_QUERY from "graphql/getSupplementaryDataVersions.graphql";

jest.mock("@apollo/react-hooks", () => ({
...jest.requireActual("@apollo/react-hooks"),
useQuery: jest.fn(),
useMutation: jest.fn(() => [() => null]),
}));

useQuery.mockImplementation(() => ({
loading: false,
error: false,
data: {
supplementaryDataVersions: {
surveyId: "068",
versions: [
{
guid: "123-111-789",
// eslint-disable-next-line camelcase
sds_schema_version: "1",
// eslint-disable-next-line camelcase
sds_published_at: "2023-01-12T13:37:27+00:00",
},
{
guid: "123-222-789",
// eslint-disable-next-line camelcase
sds_schema_version: "2",
// eslint-disable-next-line camelcase
sds_published_at: "2023-02-08T12:37:27+00:00",
},
{
guid: "123-333-789",
// eslint-disable-next-line camelcase
sds_schema_version: "3",
// eslint-disable-next-line camelcase
sds_published_at: "2023-03-23T08:37:27+00:00",
},
],
},
},
useMutation: jest.fn(() => [() => null]),
}));

const renderSupplementaryDatasetPage = (questionnaire, props, user, mocks) => {
Expand Down Expand Up @@ -105,6 +75,60 @@ describe("Supplementary dataset page", () => {
}),
},
];
useQuery.mockImplementation((key) => {
if (key === GET_SUPPLEMENTARY_DATA_VERSIONS_QUERY) {
return {
loading: false,
error: false,
data: {
supplementaryDataVersions: {
surveyId: "121",
versions: [
{
guid: "123-111-789",
// eslint-disable-next-line camelcase
sds_schema_version: "1",
// eslint-disable-next-line camelcase
sds_published_at: "2023-01-12T13:37:27+00:00",
},
{
guid: "123-222-789",
// eslint-disable-next-line camelcase
sds_schema_version: "2",
// eslint-disable-next-line camelcase
sds_published_at: "2023-02-08T12:37:27+00:00",
},
{
guid: "123-333-789",
// eslint-disable-next-line camelcase
sds_schema_version: "3",
// eslint-disable-next-line camelcase
sds_published_at: "2023-03-23T08:37:27+00:00",
},
],
},
},
};
} else if (key === GET_SUPPLEMENTARY_DATA_SURVEY_ID_LIST) {
return {
data: {
supplementaryDataSurveyIdList: {
surveyIdList: ["121", "122"],
},
},
};
} else {
return {
loading: false,
error: false,
data: {},
};
}
});

afterEach(() => {
jest.clearAllMocks();
});
});

it("should display heading and basic text", () => {
Expand All @@ -119,6 +143,7 @@ describe("Supplementary dataset page", () => {
getByText("Only one dataset can be linked per questionnaire.")
).toBeTruthy();
});

describe("survey picker", () => {
it("should display a select picker with all options", () => {
const { getByText, getByTestId } = renderSupplementaryDatasetPage(
Expand All @@ -129,21 +154,23 @@ describe("Supplementary dataset page", () => {
);
expect(getByText("Select a survey ID")).toBeTruthy();
expect(getByTestId("list-select")).toBeTruthy();
expect(getByText(/068/)).toBeTruthy();
expect(getByText(/121/)).toBeTruthy();
expect(getByText(/122/)).toBeTruthy();
});

it("should select a survey id", async () => {
const { getByText, getByTestId, getAllByTestId, findAllByText } =
renderSupplementaryDatasetPage(questionnaire, props, user, mocks);

const select = getByTestId("list-select");
fireEvent.change(select, { target: { value: "068" } });

expect(getByText("Datasets for survey ID 068")).toBeTruthy();
expect(getByTestId("datasets-table")).toBeTruthy();
expect(findAllByText("Date created")).toBeTruthy();
expect(getAllByTestId("dataset-row")).toBeTruthy();
expect(getByText("23/03/2023")).toBeTruthy();
fireEvent.change(select, { target: { value: "121" } });
await waitFor(() => {
expect(getByText("Datasets for survey ID 121")).toBeTruthy();
expect(getByTestId("datasets-table")).toBeTruthy();
expect(findAllByText("Date created")).toBeTruthy();
expect(getAllByTestId("dataset-row")).toBeTruthy();
expect(getByText("23/03/2023")).toBeTruthy();
});
});

it("should link a dataset", async () => {
Expand All @@ -157,7 +184,7 @@ describe("Supplementary dataset page", () => {
);

const select = getByTestId("list-select");
fireEvent.change(select, { target: { value: "068" } });
fireEvent.change(select, { target: { value: "121" } });
await act(async () => {
await fireEvent.click(getAllByTestId("btn-link")[0]);
});
Expand Down
1 change: 0 additions & 1 deletion eq-author/src/constants/surveyIDs.js

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
query GetSupplementaryDataSurveyIdList {
supplementaryDataSurveyIdList {
surveyIdList
}
}

0 comments on commit 6fc9ffd

Please sign in to comment.