Skip to content

Commit ac8a214

Browse files
authored
fix: Survey crash caused by fully answered survey (#1273)
* fix: Fully answered survey causes crash * chore: Release 12.0.4
1 parent 05a1036 commit ac8a214

3 files changed

Lines changed: 105 additions & 5 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@equinor/amplify-component-lib",
3-
"version": "12.0.3",
3+
"version": "12.0.4",
44
"description": "Frontend Typescript components for the Amplify team",
55
"main": "dist/index.js",
66
"types": "dist/index.d.ts",

src/providers/SurveyProvider/SurveyProvider.test.tsx

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
import { ReactNode } from 'react';
22

3+
import {
4+
QuestionType,
5+
SurveyResponseStatus,
6+
SurveyType,
7+
UserSurveyVm,
8+
} from '@equinor/subsurface-app-management';
39
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
410

511
import { useSurvey } from './hooks/useSurvey';
@@ -76,3 +82,98 @@ test("'SurveyQuestion' throws error when there is no active survey", () => {
7682
)
7783
).toThrow("SurveyQuestion couldn't find active survey or question");
7884
});
85+
86+
test('Sets activeQuestionIndex to 0 if all questions have answers', async ({
87+
worker,
88+
}) => {
89+
const survey: UserSurveyVm = {
90+
surveyId: {
91+
value: 'some-id',
92+
},
93+
status: SurveyResponseStatus.IN_PROGRESS,
94+
surveyType: SurveyType.DEFAULT,
95+
applicationId: 'some-app',
96+
title: 'This is the title',
97+
description: '',
98+
startAt: '',
99+
endAt: '',
100+
showConfettiOnComplete: false,
101+
questions: [
102+
{
103+
questionId: { value: 'id1' },
104+
questionText: 'Hei',
105+
type: QuestionType.TEXT,
106+
order: 1,
107+
answer: {
108+
answerId: { value: 'id2' },
109+
textAnswer: 'Something or other',
110+
},
111+
},
112+
],
113+
};
114+
worker.use(
115+
http.get('*/api/v1/surveys/applications/:applicationName/me', () =>
116+
HttpResponse.json(survey)
117+
)
118+
);
119+
120+
const { result } = renderHook(() => useSurvey(), { wrapper: TestProviders });
121+
122+
await waitFor(() =>
123+
expect(result.current.activeSurvey?.surveyId.value).toBe(
124+
survey.surveyId.value
125+
)
126+
);
127+
await waitFor(() => expect(result.current.activeQuestionIndex).toBe(0));
128+
});
129+
130+
test('Sets activeQuestionIndex to the last unanswered question if some questions have answers', async ({
131+
worker,
132+
}) => {
133+
const survey: UserSurveyVm = {
134+
surveyId: {
135+
value: 'some-id',
136+
},
137+
status: SurveyResponseStatus.IN_PROGRESS,
138+
surveyType: SurveyType.DEFAULT,
139+
applicationId: 'some-app',
140+
title: 'This is the title',
141+
description: '',
142+
startAt: '',
143+
endAt: '',
144+
showConfettiOnComplete: false,
145+
questions: [
146+
{
147+
questionId: { value: 'id1' },
148+
questionText: 'Hei',
149+
type: QuestionType.TEXT,
150+
order: 1,
151+
answer: {
152+
answerId: { value: 'id2' },
153+
textAnswer: 'Something or other',
154+
},
155+
},
156+
{
157+
questionId: { value: 'id2' },
158+
questionText: 'Hei',
159+
type: QuestionType.TEXT,
160+
order: 2,
161+
answer: undefined,
162+
},
163+
],
164+
};
165+
worker.use(
166+
http.get('*/api/v1/surveys/applications/:applicationName/me', () =>
167+
HttpResponse.json(survey)
168+
)
169+
);
170+
171+
const { result } = renderHook(() => useSurvey(), { wrapper: TestProviders });
172+
173+
await waitFor(() =>
174+
expect(result.current.activeSurvey?.surveyId.value).toBe(
175+
survey.surveyId.value
176+
)
177+
);
178+
await waitFor(() => expect(result.current.activeQuestionIndex).toBe(1));
179+
});

src/providers/SurveyProvider/SurveyProvider.tsx

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,10 @@ export const SurveyProvider: FC<SurveyProviderProps> = ({ children }) => {
7070
initializedQuestionIndex.current !== activeSurvey.surveyId.value
7171
) {
7272
initializedQuestionIndex.current = activeSurvey.surveyId.value;
73-
setActiveQuestionIndex(
74-
activeSurvey.questions.findIndex(
75-
(question) => question.answer === undefined
76-
)
73+
const questionIndex = activeSurvey.questions.findIndex(
74+
(question) => question.answer === undefined
7775
);
76+
setActiveQuestionIndex(questionIndex >= 0 ? questionIndex : 0);
7877
}
7978
}, [activeSurvey]);
8079

0 commit comments

Comments
 (0)