|
1 | 1 | import { ReactNode } from 'react'; |
2 | 2 |
|
| 3 | +import { |
| 4 | + QuestionType, |
| 5 | + SurveyResponseStatus, |
| 6 | + SurveyType, |
| 7 | + UserSurveyVm, |
| 8 | +} from '@equinor/subsurface-app-management'; |
3 | 9 | import { QueryClient, QueryClientProvider } from '@tanstack/react-query'; |
4 | 10 |
|
5 | 11 | import { useSurvey } from './hooks/useSurvey'; |
@@ -76,3 +82,98 @@ test("'SurveyQuestion' throws error when there is no active survey", () => { |
76 | 82 | ) |
77 | 83 | ).toThrow("SurveyQuestion couldn't find active survey or question"); |
78 | 84 | }); |
| 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 | +}); |
0 commit comments