Skip to content

Commit b488cd7

Browse files
committed
fix: navigate after submitting ur question results, improve testability, correct page formatting
1 parent 3350da1 commit b488cd7

File tree

7 files changed

+169
-127
lines changed

7 files changed

+169
-127
lines changed

.github/workflows/gha-ci.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ jobs:
158158
id: changelog
159159
with:
160160
github-token: ${{ secrets.github_token }}
161-
output-file: 'CHANGELOG.md'
161+
output-file: "CHANGELOG.md"
162162
- name: Create Release
163163
uses: actions/create-release@v1
164164
if: ${{ steps.changelog.outputs.skipped == 'false' }}
@@ -196,6 +196,7 @@ jobs:
196196
SENTRY_PROJECT: ${{ secrets.SENTRY_PROJECT }}
197197
SENTRY_AUTH_TOKEN: ${{ secrets.SENTRY_AUTH_TOKEN }}
198198
ENABLE_SENTRY: ${{ secrets.ENABLE_SENTRY }}
199+
ENABLE_UR_QUESTION: ${{ secrets.ENABLE_UR_QUESTION }}
199200
run: |
200201
bin/write_environment.sh
201202
- name: Set up Node.js
@@ -217,4 +218,3 @@ jobs:
217218
- run: |
218219
docker build . -t ${{ secrets.ACR_SERVER }}/virtualvisits:${{ github.sha }}
219220
docker push ${{ secrets.ACR_SERVER }}/virtualvisits:${{ github.sha }}
220-

bin/write_environment.sh

+1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ SENTRY_AUTH_TOKEN=$SENTRY_AUTH_TOKEN
3131
ENABLE_SENTRY=$ENABLE_SENTRY
3232
# Feature flags
3333
SHOW_NAVIGATION_BAR=$SHOW_NAVIGATION_BAR
34+
ENABLE_UR_QUESTION=$ENABLE_UR_QUESTION
3435
EOF
3536

3637
echo "Environment file written"

package-lock.json

+6-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
"moment-duration-format": "^2.3.2",
6161
"nanoid": "^3.1.3",
6262
"next": "9.5.4",
63+
"next-absolute-url": "^1.2.2",
6364
"next-env": "^1.1.1",
6465
"nhsuk-frontend": "^3.1.0",
6566
"node-sass": "^4.14.0",
@@ -104,4 +105,4 @@
104105
"engines": {
105106
"node": "12.16.x"
106107
}
107-
}
108+
}

pageTests/visits/endUrQuestion.test.js

+20-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import React from "react";
22
import { render, screen, fireEvent, act } from "@testing-library/react";
3-
import EndUrQuestion, {
3+
import {
4+
endUrQuestionConstructor,
45
getServerSideProps,
56
} from "../../pages/visits/endUrQuestion";
67
import nock from "nock";
@@ -17,6 +18,8 @@ describe("end UR question", () => {
1718
});
1819

1920
describe("<EndUrQuestion/>", () => {
21+
const EndUrQuestion = endUrQuestionConstructor({ router: [] });
22+
2023
function stubUrQuestionApiEndpoint(desiredAnswer) {
2124
return nock("http://localhost:3001", {})
2225
.matchHeader("X-Correlation-ID", "1-ur-question")
@@ -31,7 +34,14 @@ describe("end UR question", () => {
3134
let urQuestionEndpointStub = stubUrQuestionApiEndpoint("yes");
3235

3336
it("submits the results of the ur question", async () => {
34-
render(<EndUrQuestion correlationId="1-ur-question" />);
37+
render(
38+
<EndUrQuestion
39+
correlationId="1-ur-question"
40+
router={[]}
41+
protocol="http:"
42+
host="localhost:3001"
43+
/>
44+
);
3545
await act(async () => {
3646
fireEvent.click(screen.getByTestId("ur-question-radio-yes"));
3747
fireEvent.submit(screen.getByTestId("ur-question-form"));
@@ -43,7 +53,14 @@ describe("end UR question", () => {
4353
let urQuestionEndpointStub = stubUrQuestionApiEndpoint("no");
4454

4555
it("submits the results of the ur question", async () => {
46-
render(<EndUrQuestion correlationId="1-ur-question" />);
56+
render(
57+
<EndUrQuestion
58+
correlationId="1-ur-question"
59+
router={[]}
60+
protocol="http:"
61+
host="localhost:3001"
62+
/>
63+
);
4764
await act(async () => {
4865
fireEvent.click(screen.getByTestId("ur-question-radio-no"));
4966
fireEvent.submit(screen.getByTestId("ur-question-form"));

pages/visits/endUrQuestion.js

+111-95
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import Layout from "../../src/components/Layout";
44
import propsWithContainer from "../../src/middleware/propsWithContainer";
55
import { v4 as uuidv4 } from "uuid";
66
import Router from "next/router";
7+
import absoluteUrl from "next-absolute-url";
78

89
/*
910
The end UR question is a page that should be navigated to after a call ends but
@@ -12,122 +13,137 @@ to the event logging system - this is distinct from console logging and concerns
1213
things to do with the user journey so that journeys can be tracked and we can
1314
identify potential issues with the service.
1415
*/
15-
export default function EndUrQuestion({ correlationId, callId }) {
16-
let selectedFeedback;
1716

18-
const selectFeedbackOption = (option) => (/*event*/) => {
19-
selectedFeedback = option;
20-
};
17+
export const endUrQuestionConstructor = ({ router }) =>
18+
function EndUrQuestion({ correlationId, callId, protocol, host }) {
19+
let selectedFeedback;
20+
const selectFeedbackOption = (option) => (/*event*/) => {
21+
selectedFeedback = option;
22+
};
2123

22-
const onSubmit = async (event) => {
23-
await fetchEndpointWithCorrelationId(
24-
"POST",
25-
"http://localhost:3001/api/submit-ur-answer",
26-
JSON.stringify({
27-
"would miss nhs vv": selectedFeedback,
28-
}),
29-
correlationId
30-
);
24+
const onSubmit = async (event) => {
25+
event.preventDefault();
26+
router.push(`/visits/end?callId=${callId}`);
3127

32-
Router.push(`/visits/end?callId=${callId}`);
33-
event.preventDefault();
34-
};
35-
const onClickSkip = async () => {
36-
Router.push(`/visits/end?callId=${callId}`);
37-
};
28+
try {
29+
await fetchEndpointWithCorrelationId(
30+
"POST",
31+
`${protocol}//${host}/api/submit-ur-answer`,
32+
JSON.stringify({
33+
"would miss nhs vv": selectedFeedback,
34+
}),
35+
correlationId
36+
);
37+
} catch {
38+
console.error("Could not submit UR answer");
39+
}
40+
};
41+
const onClickSkip = async () => {
42+
router.push(`/visits/end?callId=${callId}`);
43+
};
3844

39-
useEffect(() => {
40-
console.log(correlationId);
41-
}, []);
45+
useEffect(() => {
46+
console.log(correlationId);
47+
}, []);
4248

43-
return (
44-
<Layout title="Submit feedback" isBookService={false}>
45-
<div className="nhsuk-form-group">
46-
<form onSubmit={onSubmit} data-testid="ur-question-form">
47-
<div className="nhsuk-form-group">
48-
<fieldset className="nhsuk-fieldset">
49-
<legend className="nhsuk-fieldset__legend nhsuk-fieldset__legend--l">
50-
<h1 className="nhsuk-fieldset__heading">
51-
If this service was no longer available would you be
52-
dissappointed/upset?
53-
</h1>
54-
</legend>
49+
return (
50+
<Layout title="Submit feedback" isBookService={false}>
51+
<div className="nhsuk-form-group">
52+
<form onSubmit={onSubmit} data-testid="ur-question-form">
53+
<div className="nhsuk-form-group nhsuk-u-margin-bottom-5">
54+
<fieldset className="nhsuk-fieldset">
55+
<legend className="nhsuk-fieldset__legend nhsuk-fieldset__legend--l">
56+
<h1 className="nhsuk-fieldset__heading">
57+
If this service was no longer available would you be
58+
dissappointed/upset?
59+
</h1>
60+
</legend>
5561

56-
<div
57-
data-testid="ur-question-radio-buttons"
58-
className="nhsuk-radios"
59-
>
60-
<div className="nhsuk-radios__item">
61-
<input
62-
className="nhsuk-radios__input"
63-
id="ur-question-radio-yes"
64-
name="ur-question-radio"
65-
type="radio"
66-
value="yes"
67-
data-testid="ur-question-radio-yes"
68-
onClick={selectFeedbackOption("yes")}
69-
/>
70-
<label
71-
className="nhsuk-label nhsuk-radios__label"
72-
htmlFor="ur-question-radio-yes"
73-
>
74-
Yes
75-
</label>
76-
</div>
62+
<div
63+
data-testid="ur-question-radio-buttons"
64+
className="nhsuk-radios"
65+
>
66+
<div className="nhsuk-radios__item">
67+
<input
68+
className="nhsuk-radios__input"
69+
id="ur-question-radio-yes"
70+
name="ur-question-radio"
71+
type="radio"
72+
value="yes"
73+
data-testid="ur-question-radio-yes"
74+
onClick={selectFeedbackOption("yes")}
75+
/>
76+
<label
77+
className="nhsuk-label nhsuk-radios__label"
78+
htmlFor="ur-question-radio-yes"
79+
>
80+
Yes
81+
</label>
82+
</div>
7783

78-
<div className="nhsuk-radios__item">
79-
<input
80-
className="nhsuk-radios__input"
81-
id="ur-question-radio-no"
82-
name="ur-question-radio"
83-
type="radio"
84-
value="no"
85-
data-testid="ur-question-radio-no"
86-
onClick={selectFeedbackOption("no")}
87-
/>
88-
<label
89-
className="nhsuk-label nhsuk-radios__label"
90-
htmlFor="ur-question-radio-no"
91-
>
92-
No
93-
</label>
84+
<div className="nhsuk-radios__item">
85+
<input
86+
className="nhsuk-radios__input"
87+
id="ur-question-radio-no"
88+
name="ur-question-radio"
89+
type="radio"
90+
value="no"
91+
data-testid="ur-question-radio-no"
92+
onClick={selectFeedbackOption("no")}
93+
/>
94+
<label
95+
className="nhsuk-label nhsuk-radios__label"
96+
htmlFor="ur-question-radio-no"
97+
>
98+
No
99+
</label>
100+
</div>
94101
</div>
95-
</div>
96-
</fieldset>
97-
</div>
98-
<button
99-
className="nhsuk-button"
100-
data-testid="submit-ur-question-button"
101-
type="submit"
102-
>
103-
Submit feedback
104-
</button>
105-
</form>
102+
</fieldset>
103+
</div>
106104

107-
<button
108-
className="nhsuk-button nhsuk-button--secondary"
109-
onClick={onClickSkip}
110-
>
111-
Skip
112-
</button>
113-
</div>
114-
</Layout>
115-
);
116-
}
105+
<button
106+
className="nhsuk-button"
107+
data-testid="submit-ur-question-button"
108+
type="submit"
109+
>
110+
Submit feedback
111+
</button>
112+
<button
113+
className="nhsuk-button nhsuk-button--secondary nhsuk-u-margin-left-5"
114+
onClick={onClickSkip}
115+
>
116+
Skip
117+
</button>
118+
</form>
119+
</div>
120+
</Layout>
121+
);
122+
};
123+
124+
export default endUrQuestionConstructor({ router: Router });
117125

118126
export const getServerSideProps = propsWithContainer(
119-
async ({ req: { headers }, res, container, query }) => {
127+
async ({ req, res, container, query }) => {
128+
const { headers } = req;
120129
const { callId } = query;
130+
131+
if (process.env.ENABLE_UR_QUESTION === "no") {
132+
res.writeHead(302, { Location: `/visits/end?callId=${callId}` }).end();
133+
return {};
134+
}
135+
121136
const userIsAuthenticated = container.getUserIsAuthenticated();
122137
const token = await userIsAuthenticated(headers.cookie);
123138
const correlationId = `${uuidv4()}-ur-question`;
139+
const { protocol, host } = absoluteUrl(req, "localhost:3001");
124140

125141
if (token?.ward || null) {
126142
res.writeHead(302, { Location: `/visits/end?callId=${callId}` }).end();
127143
return {};
128144
} else {
129145
res.status(200);
130-
return { props: { correlationId, callId } };
146+
return { props: { correlationId, callId, protocol, host } };
131147
}
132148
}
133149
);

0 commit comments

Comments
 (0)