1616 */
1717
1818import type { Meta , StoryObj } from '@storybook/react-vite' ;
19+ import { expect , waitFor , within } from 'storybook/test' ;
1920import BuildFormWrapperForStorybook from '../storybookWrappers/BuildFormWrapperForStorybook' ;
2021import {
2122 qEnableBehaviorAll ,
@@ -31,6 +32,17 @@ import {
3132 qText
3233} from '../assets/questionnaires' ; // More on how to set up stories at: https://storybook.js.org/docs/react/writing-stories/introduction#default-export
3334import { createStory } from '../storybookWrappers/createStory' ;
35+ import {
36+ checkCheckBox ,
37+ checkCheckboxOption ,
38+ checkRadioOption ,
39+ findByLinkIdOrLabel ,
40+ getAnswers ,
41+ getGroupAnswers ,
42+ inputInteger ,
43+ inputText ,
44+ queryByLinkIdOrLabel
45+ } from '../testUtils' ;
3446
3547// More on how to set up stories at: https://storybook.js.org/docs/react/writing-stories/introduction#default-export
3648const meta = {
@@ -48,60 +60,214 @@ type Story = StoryObj<typeof meta>;
4860export const InitialSingle : Story = createStory ( {
4961 args : {
5062 questionnaire : qInitialSingle
63+ } ,
64+ play : async ( ) => {
65+ // Wait for the answers to be updated in the store
66+ const answers = await getAnswers ( 'patient-age' ) ;
67+ expect ( answers ) . toEqual ( [ expect . objectContaining ( { valueInteger : 30 } ) ] ) ;
5168 }
5269} ) as Story ;
5370
5471export const InitialRepeats : Story = createStory ( {
5572 args : {
5673 questionnaire : qInitialRepeats
74+ } ,
75+ play : async ( ) => {
76+ // Wait for the answers to be updated in the store
77+ await new Promise ( ( resolve ) => setTimeout ( resolve , 500 ) ) ;
78+ const answers = await getGroupAnswers ( 'container' , 'visited-states' ) ;
79+ expect ( answers ) . toHaveLength ( 5 ) ;
80+ expect ( answers ) . toEqual (
81+ expect . arrayContaining ( [
82+ expect . objectContaining ( {
83+ valueCoding : expect . objectContaining ( {
84+ code : 'ACT' ,
85+ display : 'Australian Capital Territory'
86+ } )
87+ } ) ,
88+ expect . objectContaining ( {
89+ valueCoding : expect . objectContaining ( { code : 'NSW' , display : 'New South Wales' } )
90+ } )
91+ ] )
92+ ) ;
5793 }
5894} ) as Story ;
5995
6096export const EnableWhen : Story = createStory ( {
6197 args : {
6298 questionnaire : qEnableWhen
99+ } ,
100+ play : async ( { canvasElement } ) => {
101+ const visibilityExpectations : Array < { option : string ; shouldBeVisible : boolean } > = [
102+ { option : 'No' , shouldBeVisible : false } ,
103+ { option : 'N/A' , shouldBeVisible : false } ,
104+ { option : 'Yes' , shouldBeVisible : true }
105+ ] ;
106+
107+ for ( const { option, shouldBeVisible } of visibilityExpectations ) {
108+ await checkRadioOption ( canvasElement , 'registered-for-my-aged-care' , option ) ;
109+
110+ const dependentField = queryByLinkIdOrLabel ( canvasElement , 'my-aged-care-number' ) ;
111+ if ( shouldBeVisible ) {
112+ expect ( dependentField ) . toBeTruthy ( ) ;
113+ } else {
114+ expect ( dependentField ) . toBeNull ( ) ;
115+ }
116+ }
63117 }
64118} ) as Story ;
65119
66120export const EnableWhenMultiCheckbox : Story = createStory ( {
67121 args : {
68122 questionnaire : qEnableWhenMultiCheckbox
123+ } ,
124+ play : async ( { canvasElement } ) => {
125+ expect ( queryByLinkIdOrLabel ( canvasElement , 'clinical-guidance-a' ) ) . toBeNull ( ) ;
126+ expect ( queryByLinkIdOrLabel ( canvasElement , 'clinical-guidance-b' ) ) . toBeNull ( ) ;
127+ expect ( queryByLinkIdOrLabel ( canvasElement , 'clinical-guidance-c' ) ) . toBeNull ( ) ;
128+
129+ await checkCheckboxOption (
130+ canvasElement ,
131+ 'select-conditions-list' ,
132+ 'Condition A (Displays Clinical guidance: Condition A question)'
133+ ) ;
134+ await checkCheckboxOption (
135+ canvasElement ,
136+ 'select-conditions-list' ,
137+ 'Condition B (Displays Clinical guidance: Condition B question)'
138+ ) ;
139+
140+ expect ( queryByLinkIdOrLabel ( canvasElement , 'clinical-guidance-a' ) ) . toBeTruthy ( ) ;
141+ expect ( queryByLinkIdOrLabel ( canvasElement , 'clinical-guidance-b' ) ) . toBeTruthy ( ) ;
142+ expect ( queryByLinkIdOrLabel ( canvasElement , 'clinical-guidance-c' ) ) . toBeNull ( ) ;
69143 }
70144} ) as Story ;
71145
72146export const EnableBehaviorAll : Story = createStory ( {
73147 args : {
74148 questionnaire : qEnableBehaviorAll
149+ } ,
150+ play : async ( { canvasElement } ) => {
151+ expect ( queryByLinkIdOrLabel ( canvasElement , 'has-heart-condition' ) ) . toBeNull ( ) ;
152+ expect ( queryByLinkIdOrLabel ( canvasElement , 'medication-list' ) ) . toBeNull ( ) ;
153+
154+ await checkCheckBox ( canvasElement , 'has-heart-disease' ) ;
155+ expect ( queryByLinkIdOrLabel ( canvasElement , 'has-heart-condition' ) ) . toBeTruthy ( ) ;
156+ expect ( queryByLinkIdOrLabel ( canvasElement , 'medication-list' ) ) . toBeNull ( ) ;
157+
158+ await checkCheckBox ( canvasElement , 'has-heart-condition' ) ;
159+ expect ( queryByLinkIdOrLabel ( canvasElement , 'medication-list' ) ) . toBeTruthy ( ) ;
75160 }
76161} ) as Story ;
77162
78163export const EnableBehaviorAny : Story = createStory ( {
79164 args : {
80165 questionnaire : qEnableBehaviorAny
166+ } ,
167+ play : async ( { canvasElement } ) => {
168+ const visibilityExpectations : Array < { option : string ; shouldBeVisible : boolean } > = [
169+ { option : 'Never smoked' , shouldBeVisible : false } ,
170+ { option : 'Smoker' , shouldBeVisible : true } ,
171+ { option : 'Ex-smoker' , shouldBeVisible : true } ,
172+ { option : 'Exposure to second hand tobacco smoke' , shouldBeVisible : false } ,
173+ { option : 'Wants to quit' , shouldBeVisible : true } ,
174+ { option : 'Other tobacco use' , shouldBeVisible : false }
175+ ] ;
176+
177+ for ( const { option, shouldBeVisible } of visibilityExpectations ) {
178+ await checkRadioOption ( canvasElement , 'smoking-status' , option ) ;
179+
180+ const dependentField = queryByLinkIdOrLabel ( canvasElement , 'how-long-as-a-smoker' ) ;
181+ if ( shouldBeVisible ) {
182+ expect ( dependentField ) . toBeTruthy ( ) ;
183+ } else {
184+ expect ( dependentField ) . toBeNull ( ) ;
185+ }
186+ }
81187 }
82188} ) as Story ;
83189
84190export const EnableWhenExpressionSimple : Story = createStory ( {
85191 args : {
86192 questionnaire : qEnableWhenExpressionSimple
193+ } ,
194+ play : async ( { canvasElement } ) => {
195+ await inputInteger ( canvasElement , 'patient-age' , 4 ) ;
196+ expect ( queryByLinkIdOrLabel ( canvasElement , 'patient-priorities-less-than-6' ) ) . toBeTruthy ( ) ;
197+ expect ( queryByLinkIdOrLabel ( canvasElement , 'patient-priorities-6-to-12' ) ) . toBeNull ( ) ;
198+ expect ( queryByLinkIdOrLabel ( canvasElement , 'patient-priorities-more-than-12' ) ) . toBeNull ( ) ;
199+
200+ await inputInteger ( canvasElement , 'patient-age' , 20 ) ;
201+ expect ( queryByLinkIdOrLabel ( canvasElement , 'patient-priorities-less-than-6' ) ) . toBeNull ( ) ;
202+ expect ( queryByLinkIdOrLabel ( canvasElement , 'patient-priorities-more-than-12' ) ) . toBeTruthy ( ) ;
87203 }
88204} ) as Story ;
89205
90206export const EnableWhenExpressionTabs : Story = createStory ( {
91207 args : {
92208 questionnaire : qEnableWhenExpressionTabs
209+ } ,
210+ play : async ( { canvasElement } ) => {
211+ await inputInteger ( canvasElement , 'patient-age' , 4 ) ;
212+ expect ( queryByLinkIdOrLabel ( canvasElement , 'red-flags-early-identification' ) ) . toBeTruthy ( ) ;
213+ expect ( queryByLinkIdOrLabel ( canvasElement , 'substance-use' ) ) . toBeNull ( ) ;
214+ expect ( queryByLinkIdOrLabel ( canvasElement , 'sexual-health' ) ) . toBeNull ( ) ;
215+ expect ( queryByLinkIdOrLabel ( canvasElement , 'genitourinary-and-sexual-health' ) ) . toBeNull ( ) ;
216+
217+ await inputInteger ( canvasElement , 'patient-age' , 20 ) ;
218+ expect ( queryByLinkIdOrLabel ( canvasElement , 'red-flags-early-identification' ) ) . toBeNull ( ) ;
219+ expect ( queryByLinkIdOrLabel ( canvasElement , 'substance-use' ) ) . toBeTruthy ( ) ;
220+ expect ( queryByLinkIdOrLabel ( canvasElement , 'sexual-health' ) ) . toBeTruthy ( ) ;
221+ expect ( queryByLinkIdOrLabel ( canvasElement , 'genitourinary-and-sexual-health' ) ) . toBeNull ( ) ;
222+
223+ await inputInteger ( canvasElement , 'patient-age' , 55 ) ;
224+ expect ( queryByLinkIdOrLabel ( canvasElement , 'red-flags-early-identification' ) ) . toBeNull ( ) ;
225+ expect ( queryByLinkIdOrLabel ( canvasElement , 'substance-use' ) ) . toBeTruthy ( ) ;
226+ expect ( queryByLinkIdOrLabel ( canvasElement , 'sexual-health' ) ) . toBeNull ( ) ;
227+ expect ( queryByLinkIdOrLabel ( canvasElement , 'genitourinary-and-sexual-health' ) ) . toBeTruthy ( ) ;
93228 }
94229} ) as Story ;
95230
96231export const TargetConstraintSimple : Story = createStory ( {
97232 args : {
98233 questionnaire : qTargetConstraintSimple
234+ } ,
235+ play : async ( { canvasElement } ) => {
236+ const constraintMessage =
237+ 'Systolic blood pressure should not be less than diastolic blood pressure.' ;
238+
239+ await inputInteger ( canvasElement , '1.1' , 80 ) ;
240+ await inputInteger ( canvasElement , '1.2' , 120 ) ;
241+ const systolicAnswer = await findByLinkIdOrLabel ( canvasElement , '1.1' ) ;
242+ await waitFor ( ( ) => {
243+ expect ( within ( systolicAnswer ) . queryByText ( constraintMessage ) ) . toBeTruthy ( ) ;
244+ } ) ;
245+
246+ await inputInteger ( canvasElement , '1.1' , 120 ) ;
247+ await inputInteger ( canvasElement , '1.2' , 80 ) ;
248+ await waitFor ( ( ) => {
249+ expect ( within ( systolicAnswer ) . queryByText ( constraintMessage ) ) . toBeNull ( ) ;
250+ } ) ;
99251 }
100252} ) as Story ;
101253
102254export const TargetConstraintMulti : Story = createStory ( {
103255 args : {
104256 questionnaire : qTargetConstraintMultiple
257+ } ,
258+ play : async ( { canvasElement } ) => {
259+ const constraintMessage = 'Text and special characters, i.e. +, -, () are not allowed' ;
260+
261+ await inputText ( canvasElement , 'aus-contact' , '04(12)34+abc' ) ;
262+ const ausContactAnswer = await findByLinkIdOrLabel ( canvasElement , 'aus-contact' ) ;
263+ await waitFor ( ( ) => {
264+ expect ( within ( ausContactAnswer ) . queryByText ( constraintMessage ) ) . toBeTruthy ( ) ;
265+ } ) ;
266+
267+ await inputText ( canvasElement , 'aus-contact' , '0412345678' ) ;
268+ await waitFor ( ( ) => {
269+ expect ( within ( ausContactAnswer ) . queryByText ( constraintMessage ) ) . toBeNull ( ) ;
270+ } ) ;
105271 }
106272} ) as Story ;
107273
0 commit comments