Skip to content

Commit

Permalink
Merge pull request #653 from ONSdigital/BLAIS5-4421
Browse files Browse the repository at this point in the history
Promote from BLAIS5-4421 to main
  • Loading branch information
Riceyo authored Oct 28, 2024
2 parents 0e4a9cd + 92d0da1 commit 657fa83
Show file tree
Hide file tree
Showing 4 changed files with 299 additions and 122 deletions.
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,6 @@
"@google-cloud/logging": "^9.6.0",
"@google-cloud/profiler": "^4.1.3",
"@google-cloud/storage": "^5.8.5",
"@types/express": "^4.17.11",
"@types/jest": "^27.0.0",
"@types/node": "^15.12.2",
"@types/react": "^18.2.51",
"@types/react-dom": "^18.2.18",
"axios": "^1.7.4",
"blaise-api-node-client": "git+https://github.com/ONSdigital/blaise-api-node-client#1.1.0",
"blaise-design-system-react-components": "git+https://github.com/ONSdigital/blaise-design-system-react-components#0.14.0",
Expand Down Expand Up @@ -74,9 +69,14 @@
"@testing-library/react": "^14.2.1",
"@testing-library/user-event": "^13.2.1",
"@types/ejs": "^3.1.0",
"@types/express": "^4.17.11",
"@types/jest": "^27.0.0",
"@types/jsonwebtoken": "^8.5.5",
"@types/lodash": "^4.14.170",
"@types/node": "^15.12.2",
"@types/pino-http": "^5.7.0",
"@types/react": "^18.2.51",
"@types/react-dom": "^18.2.18",
"@types/react-router-dom": "^5.3.3",
"@types/react-timeago": "^4.1.7",
"@types/supertest": "^2.0.11",
Expand Down
214 changes: 203 additions & 11 deletions src/components/questionnaireList.test.tsx
Original file line number Diff line number Diff line change
@@ -1,51 +1,243 @@
/**
* @jest-environment jsdom
*/

import React from "react";
import "@testing-library/jest-dom";
import flushPromises from "../tests/utils";
import { render, waitFor, screen } from "@testing-library/react";
import { render, waitFor, screen, act } from "@testing-library/react";
import { MemoryRouter } from "react-router-dom";
import { act } from "react-dom/test-utils";
import React from "react";
import { Authenticate } from "blaise-login-react/blaise-login-react-client";
import axios from "axios";
import MockAdapter from "axios-mock-adapter";
import QuestionnaireList from "./questionnaireList";
import userEvent from "@testing-library/user-event";

const mock = new MockAdapter(axios);

// mock login
// Mock the blaise-login-react Authenticate component
jest.mock("blaise-login-react/blaise-login-react-client");
const { MockAuthenticate } = jest.requireActual("blaise-login-react/blaise-login-react-client");
Authenticate.prototype.render = MockAuthenticate.prototype.render;
MockAuthenticate.OverrideReturnValues(null, true);

describe("Questionnaire Details page ", () => {
const MOCK_QUESTIONNAIRE_LIST = [
{
"name": "IPS2409A",
"id": "ef8980d9-5f5c-416d-9b4b-9570c15c85c0",
"serverParkName": "gusty",
"installDate": "2024-10-16T13:14:01.7557563+01:00",
"status": "Active",
"dataRecordCount": 1,
"hasData": true,
"blaiseVersion": "5.14.4.3668",
"nodes": [
{
"nodeName": "blaise-gusty-mgmt",
"nodeStatus": "Active"
}
],
"fieldPeriod": "September 2024"
},
{
"name": "IPS2409B",
"id": "ef8980d9-5f5c-416d-9b4b-9570c15c85c0",
"serverParkName": "gusty",
"installDate": "2024-10-16T13:14:01.7557563+01:00",
"status": "Active",
"dataRecordCount": 1,
"hasData": true,
"blaiseVersion": "5.14.4.3668",
"nodes": [
{
"nodeName": "blaise-gusty-mgmt",
"nodeStatus": "Active"
}
],
"fieldPeriod": "September 2024"
},
{
"name": "IPS_ContactInfo",
"id": "10effca3-caec-4fc0-a037-20a43cf050c2",
"serverParkName": "gusty",
"installDate": "2024-10-18T12:59:23.4164608+01:00",
"status": "Active",
"dataRecordCount": 0,
"hasData": false,
"blaiseVersion": "5.14.4.3668",
"nodes": [
{
"nodeName": "blaise-gusty-mgmt",
"nodeStatus": "Active"
}
],
"fieldPeriod": "Field period unknown"
},
{
"name": "IPS_Attempts",
"id": "10effca3-caec-4fc0-a037-20a43cf050c4",
"serverParkName": "gusty",
"installDate": "2024-10-18T12:59:23.4164608+01:00",
"status": "Active",
"dataRecordCount": 0,
"hasData": false,
"blaiseVersion": "5.14.4.3668",
"nodes": [
{
"nodeName": "blaise-gusty-mgmt",
"nodeStatus": "Active"
}
],
"fieldPeriod": "Field period unknown"
},
{
"name": "DST2304Z",
"id": "10effca3-caec-4fc0-a037-20a43cf050c5",
"serverParkName": "gusty",
"installDate": "2024-10-18T12:59:23.4164608+01:00",
"status": "Active",
"dataRecordCount": 0,
"hasData": false,
"blaiseVersion": "5.14.4.3668",
"nodes": [
{
"nodeName": "blaise-gusty-mgmt",
"nodeStatus": "Active"
}
],
"fieldPeriod": "Field period unknown"
}
];

describe("Questionnaire List displays valid user questionnaires", () => {
beforeEach(() => {
mock.onGet("/api/questionnaires").reply(200, MOCK_QUESTIONNAIRE_LIST);
});

afterEach(() => {
mock.reset();
});

it("should not display any questionnaires if no questionnaires were fetched back from the server", async () => {
// Arrange
mock.onGet("/api/questionnaires").reply(200, []);
render(
<MemoryRouter initialEntries={["/questionnaire/"]}>
<QuestionnaireList setErrored={jest.fn()} />
</MemoryRouter >
);

// Act
await act(async () => {
await flushPromises();
});

// Assert
await waitFor(() => {
expect(screen.getByText(/Filter by questionnaire name/i)).toBeVisible();
expect(screen.getByText(/No installed questionnaires found/i)).toBeVisible();
});
});

it("should display a list of questionnaires containing only questionnaires that match the filter", async () => {
// Arrange
render(
<MemoryRouter initialEntries={["/questionnaire/"]}>
<QuestionnaireList setErrored={jest.fn()} />
</MemoryRouter >
);

// Act
await act(async () => {
await flushPromises();
});
const filterInput = screen.getByTestId(/filter-by-name/i);
userEvent.type(filterInput, "IPS2409A");

// Assert
await waitFor(() => {
expect(screen.getByText(/Filter by questionnaire name/i)).toBeVisible();
expect(screen.getByText(/1 results of 1/i)).toBeVisible();
expect(screen.getByText(/IPS2409A/i)).toBeVisible();
});
});

});

describe("Questionnaire List displays hidden questionnaires that match when using the search filter", () => {
beforeEach(() => {
mock.onGet("/api/questionnaires").reply(200, MOCK_QUESTIONNAIRE_LIST);
});

afterEach(() => {
mock.reset();
});

it("should redirect to the homepage when no questionnaire has been provided ", async () => {
it("should display the hidden ContactInfo questionnaire", async () => {
// Arrange
render(
<MemoryRouter initialEntries={["/questionnaire/"]}>
<QuestionnaireList setErrored={jest.fn()} />
</MemoryRouter >
);

// Act
await act(async () => {
await flushPromises();
});
const filterInput = screen.getByTestId(/filter-by-name/i);
userEvent.type(filterInput, "ContactInfo");

// Go direct to the questionnaire details page not from a link
// Assert
await waitFor(() => {
expect(screen.getByText(/Filter by questionnaire name/i)).toBeVisible();
expect(screen.getByText(/1 results of 1/i)).toBeVisible();
expect(screen.getByText(/IPS_ContactInfo/i)).toBeVisible();
});
});

it("should display the hidden Attempts questionnaire", async () => {
// Arrange
render(
<MemoryRouter initialEntries={["/questionnaire/"]}>
<QuestionnaireList setErrored={jest.fn()} />
</MemoryRouter >
);

// Act
await act(async () => {
await flushPromises();
});
const filterInput = screen.getByTestId(/filter-by-name/i);
userEvent.type(filterInput, "Attempts");

// Assert
await waitFor(() => {
expect(screen.getByText(/Filter by questionnaire name/i)).toBeVisible();
expect(screen.getByText(/1 results of 1/i)).toBeVisible();
expect(screen.getByText(/IPS_Attempts/i)).toBeVisible();
});
});

it("should display the hidden DST2304Z test questionnaire", async () => {
// Arrange
render(
<MemoryRouter initialEntries={["/questionnaire/"]}>
<QuestionnaireList setErrored={jest.fn()} />
</MemoryRouter >
);

// Act
await act(async () => {
await flushPromises();
});
const filterInput = screen.getByTestId(/filter-by-name/i);
userEvent.type(filterInput, "DST2304Z");

// Assert
await waitFor(() => {
expect(screen.queryByText("Questionnaire settings")).toEqual(null);
expect(screen.getByText(/Filter by questionnaire name/i)).toBeDefined();
expect(screen.queryByText(/Questionnaire details/i)).toEqual(null);
expect(screen.getByText(/Filter by questionnaire name/i)).toBeVisible();
expect(screen.getByText(/1 results of 1/i)).toBeVisible();
expect(screen.getByText(/DST2304Z/i)).toBeVisible();
});
});
});
Loading

0 comments on commit 657fa83

Please sign in to comment.