Skip to content

Commit

Permalink
Merge pull request #564 from ONSdigital/BLAIS5-3514
Browse files Browse the repository at this point in the history
Promote from BLAIS5-3514 to main
  • Loading branch information
motalm authored Oct 19, 2023
2 parents 997eeb5 + 839af93 commit b78b9d4
Show file tree
Hide file tree
Showing 17 changed files with 442 additions and 361 deletions.
29 changes: 23 additions & 6 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
{
"ignorePatterns": ["node_modules/*","dist/*","build/*"],
"ignorePatterns": [
".eslintrc.js",
"node_modules/*",
"dist/*",
"build/*",
"coverage/*"
],
"env": {
"browser": true,
"es2021": true,
Expand All @@ -20,7 +26,8 @@
},
"plugins": [
"react",
"@typescript-eslint"
"@typescript-eslint",
"import"
],
"rules": {
"linebreak-style": [
Expand All @@ -35,11 +42,21 @@
"error",
"always"
],
"import/no-extraneous-dependencies": [
"error", {"devDependencies": true}
],
"@typescript-eslint/no-explicit-any": "error",
"react/react-in-jsx-scope": "off",
"react/require-default-props": "off",
"no-spaced-func": "off",
"max-len": ["error", { "code": 180 }],
"no-undef": "off"
},
"overrides": [
{
"files": ["*.js"],
"files": [
"*.js"
],
"rules": {
"@typescript-eslint/no-var-requires": "off"
}
Expand All @@ -49,13 +66,13 @@
"react": {
"createClass": "createReactClass", // Regex for Component Factory to use,
// default to "createReactClass"
"pragma": "React", // Pragma to use, default to "React"
"fragment": "Fragment", // Fragment to use (may be a property of <pragma>), default to "Fragment"
"pragma": "React", // Pragma to use, default to "React"
"fragment": "Fragment", // Fragment to use (may be a property of <pragma>), default to "Fragment"
"version": "detect", // React version. "detect" automatically picks the version you have installed.
// You can also use `16.0`, `16.3`, etc, if you want to override the detected value.
// default to latest and warns if missing
// It will default to "detect" in the future
"flowVersion": "0.53" // Flow version
}
}
}
}
14 changes: 14 additions & 0 deletions .github/workflows/test.js.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,17 @@ jobs:
token: ${{ secrets.CODECOV_TOKEN }}
file: ./coverage/coverage-final.json
fail_ci_if_error: true

lint:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [ 16.x ]
steps:
- uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
- run: yarn install
- run: yarn run lint
13 changes: 12 additions & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,16 @@ module.exports = {
],
"setupFilesAfterEnv": [
"jest-extended"
]
],
"preset": "ts-jest",
"moduleNameMapper": {
"\\.(css|less|scss)$": "identity-obj-proxy"
},
"testEnvironmentOptions": {
"html": "<html lang=\"zh-cmn-Hant\"></html>",
"url": "https://jestjs.io/",
"userAgent": "Agent/007"
},
transformIgnorePatterns: ["<rootDir>/node_modules/(?!crypto-random-string/)"],
modulePathIgnorePatterns: ["<rootDir>/dist/"]
};
20 changes: 16 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
"start-react": "react-scripts start",
"build-react": "react-scripts build",
"test": "jest --coverage --watchAll=false",
"lint": "yarn eslint .",
"lint-fix": "yarn eslint . --fix",
"gcp-build": "react-scripts build && tsc --project tsconfig.server.json",
"dev": "concurrently --kill-others-on-fail \"yarn start-server\" \"yarn start-react\""
},
Expand Down Expand Up @@ -38,32 +40,42 @@
"express": "^4.17.1",
"formik": "^2.2.9",
"google-auth-library": "^8.7.0",
"history": "^4.9.0",
"jest-cucumber": "^3.0.0",
"jsonwebtoken": "^9.0.0",
"lodash": "^4.17.20",
"node-fetch": "^3.3.2",
"pino": "^8.7.0",
"pino-http": "^8.1.1",
"react": "^17.0.0",
"react-dom": "^17.0.0",
"react-router-dom": "^5.2.0",
"react-scripts": "5.0.1",
"typescript": "^4.9.4"
"typescript": "~4.3.5"
},
"devDependencies": {
"@babel/core": "^7.20.5",
"@babel/plugin-proposal-private-property-in-object": "^7.21.11",
"@babel/plugin-transform-react-jsx": "^7.12.1",
"@babel/preset-typescript": "^7.12.1",
"@types/ejs": "^3.0.5",
"@types/lodash": "^4.14.165",
"@types/react-router-dom": "^5.1.6",
"@types/supertest": "^2.0.12",
"@typescript-eslint/eslint-plugin": "^4.6.1",
"@typescript-eslint/eslint-plugin": "^5.43.0",
"@typescript-eslint/parser": "^4.6.1",
"axios-mock-adapter": "^1.21.1",
"eslint": "^7.13.0",
"eslint-plugin-react": "^7.21.5",
"eslint": "^8.0.1",
"eslint-config-airbnb": "^19.0.4",
"eslint-config-airbnb-typescript": "^17.0.0",
"eslint-plugin-import": "^2.28.1",
"eslint-plugin-n": "^15.0.0",
"eslint-plugin-promise": "^6.0.0",
"eslint-plugin-react": "^7.32.2",
"eslint-plugin-react-hooks": "^4.6.0",
"jest-extended": "^0.11.5",
"supertest": "^6.3.3",
"ts-jest": "^29.1.1",
"typemoq": "^2.1.0"
},
"browserslist": {
Expand Down
4 changes: 2 additions & 2 deletions server/Functions.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
function isNumber(n: any) {
return !isNaN(parseFloat(n)) && isFinite(n);
function isNumber(n: number | string) {
return !isNaN(parseFloat(String(n))) && isFinite(Number(n));
}

function fieldPeriodFromInstrument(instrument_name: string): string {
Expand Down
3 changes: 3 additions & 0 deletions server/Logger.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import pino from "pino-http";
module.exports = pino({});

import { P } from "pino";

export interface Logger {
Expand Down
72 changes: 36 additions & 36 deletions server/Questionnaires/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,25 +26,25 @@ describe("QuestionnaireRouter", () => {
};

app.use((req: Request, res: Response, next: NextFunction) => {
req["log"] = log as P.Logger<P.LoggerOptions>;
req.log = log as P.Logger<P.LoggerOptions>;
next();
});

const environmentVariables: EnvironmentVariables = {
VM_EXTERNAL_WEB_URL: "vm.com",
BIMS_CLIENT_ID: "bims-id",
VM_EXTERNAL_WEB_URL: "vm.com",
BIMS_CLIENT_ID: "bims-id",
BIMS_API_URL: "http://bims.com",
VM_EXTERNAL_CLIENT_URL: "",
BLAISE_API_URL: "",
CATI_DASHBOARD_URL: ""
}
};

app.use(QuestionnaireRouter(
environmentVariables,
blaiseApiMock.object
));

const request = supertest(app);
const request = supertest(app);

const today = new Date();
const tomorrow = new Date();
Expand All @@ -61,10 +61,10 @@ describe("QuestionnaireRouter", () => {
it("returns 500 when questionnaires endpoint errors", async () => {
// aarnge
blaiseApiMock.setup((api) => api.getAllQuestionnairesWithCatiData()).throws(new Error());

// act
const response = await request.get("/questionnaires");

// assert
expect(response.status).toBe(500);
expect(log.error).toHaveBeenCalledWith("Failed to retrieve instrument list");
Expand All @@ -88,7 +88,7 @@ describe("QuestionnaireRouter", () => {
sentHeaders = headers;
return [200, { tostartdate: today.toISOString() }];
});

// act
await request.get("/questionnaires");

Expand Down Expand Up @@ -117,7 +117,7 @@ describe("QuestionnaireRouter", () => {

// act
const response = await request.get("/questionnaires");

// assert
expect(response.status).toBe(200);

Expand Down Expand Up @@ -148,7 +148,7 @@ describe("QuestionnaireRouter", () => {

// act
const response = await request.get("/questionnaires");

// assert
expect(response.status).toBe(200);
expect(response.body).toEqual([]);
Expand Down Expand Up @@ -176,14 +176,14 @@ describe("QuestionnaireRouter", () => {
activeToday: true
};

blaiseApiMock.setup((api) => api.getAllQuestionnairesWithCatiData()).returns(async () => [questionnaire]);
blaiseApiMock.setup((api) => api.getAllQuestionnairesWithCatiData()).returns(async () => [questionnaire]);
mockHttp
.onGet(`http://bims.com/tostartdate/${questionnaire.name}`)
.reply(200, {}, { "content-type": "text/html" });

// act
const response = await request.get("/questionnaires");

// assert
expect(response.status).toBe(200);
expect(response.body).toEqual([
Expand Down Expand Up @@ -218,10 +218,10 @@ describe("QuestionnaireRouter", () => {
activeToday: true
};

blaiseApiMock.setup((api) => api.getAllQuestionnairesWithCatiData()).returns(async () => [questionnaire]);
blaiseApiMock.setup((api) => api.getAllQuestionnairesWithCatiData()).returns(async () => [questionnaire]);
mockHttp
.onGet(`http://bims.com/tostartdate/${questionnaire.name}`)
.reply(200, { tostartdate: today.toISOString() }, { "content-type": "application/json" });
.reply(200, { tostartdate: today.toISOString() }, { "content-type": "application/json" });

// act
const response = await request.get("/questionnaires");
Expand Down Expand Up @@ -251,20 +251,20 @@ describe("QuestionnaireRouter", () => {
activeToday: false
};

blaiseApiMock.setup((api) => api.getAllQuestionnairesWithCatiData()).returns(async () => [questionnaire]);
blaiseApiMock.setup((api) => api.getAllQuestionnairesWithCatiData()).returns(async () => [questionnaire]);
mockHttp
.onGet(`http://bims.com/tostartdate/${questionnaire.name}`)
.reply(200, { tostartdate: today.toISOString() }, { "content-type": "application/json" });
.onGet(`http://bims.com/tostartdate/${questionnaire.name}`)
.reply(200, { tostartdate: today.toISOString() }, { "content-type": "application/json" });

// act
const response = await request.get("/questionnaires");

// assert
expect(response.status).toBe(200);
expect(response.body).toEqual([]);
expect(log.debug).toHaveBeenCalledWith("The BIMS request responded with a status of 200 and a body of [object Object]");
expect(log.debug).toHaveBeenCalledWith(expect.stringMatching(/the instrument OPN2211A is live for TO \(TO start date = .*\) \(Active today = false\)/));
expect(log.info).toHaveBeenCalledWith( "Retrieved active instruments, 0 item/s");
expect(log.info).toHaveBeenCalledWith("Retrieved active instruments, 0 item/s");
expect(log.warn).not.toHaveBeenCalled();
expect(log.error).not.toHaveBeenCalled();
});
Expand All @@ -280,14 +280,14 @@ describe("QuestionnaireRouter", () => {
activeToday: true
};

blaiseApiMock.setup((api) => api.getAllQuestionnairesWithCatiData()).returns(async () => [questionnaire]);
blaiseApiMock.setup((api) => api.getAllQuestionnairesWithCatiData()).returns(async () => [questionnaire]);
mockHttp
.onGet(`http://bims.com/tostartdate/${questionnaire.name}`)
.reply(200, { tostartdate: tomorrow.toISOString() }, { "content-type": "application/json" });
.onGet(`http://bims.com/tostartdate/${questionnaire.name}`)
.reply(200, { tostartdate: tomorrow.toISOString() }, { "content-type": "application/json" });

// act
const response = await request.get("/questionnaires");

// assert
expect(response.status).toBe(200);
expect(response.body).toEqual([]);
Expand All @@ -307,14 +307,14 @@ describe("QuestionnaireRouter", () => {
activeToday: false
};

blaiseApiMock.setup((api) => api.getAllQuestionnairesWithCatiData()).returns(async () => [questionnaire]);
blaiseApiMock.setup((api) => api.getAllQuestionnairesWithCatiData()).returns(async () => [questionnaire]);
mockHttp
.onGet(`http://bims.com/tostartdate/${questionnaire.name}`)
.onGet(`http://bims.com/tostartdate/${questionnaire.name}`)
.reply(200, { tostartdate: tomorrow.toISOString() }, { "content-type": "application/json" });

// act
const response = await request.get("/questionnaires");

// assert
expect(response.status).toBe(200);
expect(response.body).toEqual([]);
Expand Down Expand Up @@ -344,9 +344,9 @@ describe("QuestionnaireRouter", () => {
activeToday: true
};

blaiseApiMock.setup((api) => api.getAllQuestionnairesWithCatiData()).returns(async () => [questionnaire]);
blaiseApiMock.setup((api) => api.getAllQuestionnairesWithCatiData()).returns(async () => [questionnaire]);
mockHttp
.onGet(`http://bims.com/tostartdate/${questionnaire.name}`)
.onGet(`http://bims.com/tostartdate/${questionnaire.name}`)
.reply(200, { tostartdate: today.toISOString() }, { "content-type": "application/json" });

// act
Expand Down Expand Up @@ -377,14 +377,14 @@ describe("QuestionnaireRouter", () => {
activeToday: false
};

blaiseApiMock.setup((api) => api.getAllQuestionnairesWithCatiData()).returns(async () => [questionnaire]);
blaiseApiMock.setup((api) => api.getAllQuestionnairesWithCatiData()).returns(async () => [questionnaire]);
mockHttp
.onGet(`http://bims.com/tostartdate/${questionnaire.name}`)
.onGet(`http://bims.com/tostartdate/${questionnaire.name}`)
.reply(200, { tostartdate: today.toISOString() }, { "content-type": "application/json" });

// act
const response = await request.get("/questionnaires");

// assert
expect(response.status).toBe(200);
expect(response.body).toEqual([]);
Expand All @@ -404,9 +404,9 @@ describe("QuestionnaireRouter", () => {
activeToday: true
};

blaiseApiMock.setup((api) => api.getAllQuestionnairesWithCatiData()).returns(async () => [questionnaire]);
blaiseApiMock.setup((api) => api.getAllQuestionnairesWithCatiData()).returns(async () => [questionnaire]);
mockHttp
.onGet(`http://bims.com/tostartdate/${questionnaire.name}`)
.onGet(`http://bims.com/tostartdate/${questionnaire.name}`)
.reply(200, { tostartdate: today.toISOString() }, { "content-type": "application/json" });

// act
Expand All @@ -428,5 +428,5 @@ describe("QuestionnaireRouter", () => {
survey: "XXX"
}
]);
});
});
});
Loading

0 comments on commit b78b9d4

Please sign in to comment.