11import gql from 'graphql-tag' ;
22import { beforeAll , describe , expect , it } from 'vitest' ;
3- import { adminQuery } from '../../utils/testQuery ' ;
3+ import { queryAsAdmin } from '../../utils/testQueryHelper ' ;
44
55const WORKFLOW_DEFINITION_ADD_MUTATION = gql `
66 mutation WorkflowDefinitionSet($entityType: String!, $definition: String!) {
@@ -102,7 +102,7 @@ describe('Workflow Resolver', () => {
102102 } ) ;
103103
104104 beforeAll ( async ( ) => {
105- const result = await adminQuery ( {
105+ const result = await queryAsAdmin ( {
106106 query : CREATE_DRAFT_WORKSPACE_QUERY ,
107107 variables : {
108108 input : { name : 'Workflow Test Workspace' } ,
@@ -111,118 +111,118 @@ describe('Workflow Resolver', () => {
111111 if ( result . errors ) {
112112 console . error ( 'DraftWorkspaceAdd Error:' , JSON . stringify ( result . errors , null , 2 ) ) ;
113113 }
114- draftWorkspaceId = result . data . draftWorkspaceAdd . id ;
114+ draftWorkspaceId = result . data ? .draftWorkspaceAdd . id ;
115115 } ) ;
116116
117117 it ( 'should create a workflow definition' , async ( ) => {
118- const result = await adminQuery ( {
118+ const result = await queryAsAdmin ( {
119119 query : WORKFLOW_DEFINITION_ADD_MUTATION ,
120120 variables : {
121121 entityType : 'DraftWorkspace' ,
122122 definition : workflowDefinition ,
123123 } ,
124124 } ) ;
125- expect ( result . data . workflowDefinitionSet . target_type ) . toBe ( 'DraftWorkspace' ) ;
126- expect ( result . data . workflowDefinitionSet . workflow_id ) . toBeDefined ( ) ;
125+ expect ( result . data ? .workflowDefinitionSet . target_type ) . toBe ( 'DraftWorkspace' ) ;
126+ expect ( result . data ? .workflowDefinitionSet . workflow_id ) . toBeDefined ( ) ;
127127 } ) ;
128128
129129 it ( 'should query a workflow definition' , async ( ) => {
130- const result = await adminQuery ( {
130+ const result = await queryAsAdmin ( {
131131 query : WORKFLOW_DEFINITION_QUERY ,
132132 variables : {
133133 entityType : 'DraftWorkspace' ,
134134 } ,
135135 } ) ;
136- expect ( result . data . workflowDefinition . initialState ) . toBe ( 'open' ) ;
137- expect ( result . data . workflowDefinition . states . length ) . toBe ( 2 ) ;
138- expect ( result . data . workflowDefinition . transitions [ 0 ] . event ) . toBe ( 'validate_event' ) ;
136+ expect ( result . data ? .workflowDefinition . initialState ) . toBe ( 'open' ) ;
137+ expect ( result . data ? .workflowDefinition . states . length ) . toBe ( 2 ) ;
138+ expect ( result . data ? .workflowDefinition . transitions [ 0 ] . event ) . toBe ( 'validate_event' ) ;
139139 } ) ;
140140
141141 it ( 'should query a workflow instance' , async ( ) => {
142- const instanceResult = await adminQuery ( {
142+ const instanceResult = await queryAsAdmin ( {
143143 query : WORKFLOW_INSTANCE_QUERY ,
144144 variables : {
145145 entityId : draftWorkspaceId ,
146146 } ,
147147 } ) ;
148- expect ( instanceResult . data . workflowInstance . currentState ) . toBe ( 'open' ) ;
149- expect ( instanceResult . data . workflowInstance . allowedTransitions . length ) . toBe ( 1 ) ;
150- expect ( instanceResult . data . workflowInstance . allowedTransitions [ 0 ] . event ) . toBe ( 'validate_event' ) ;
151- expect ( instanceResult . data . workflowInstance . allowedTransitions [ 0 ] . actions ) . toContain ( 'validateDraft' ) ;
148+ expect ( instanceResult . data ? .workflowInstance . currentState ) . toBe ( 'open' ) ;
149+ expect ( instanceResult . data ? .workflowInstance . allowedTransitions . length ) . toBe ( 1 ) ;
150+ expect ( instanceResult . data ? .workflowInstance . allowedTransitions [ 0 ] . event ) . toBe ( 'validate_event' ) ;
151+ expect ( instanceResult . data ? .workflowInstance . allowedTransitions [ 0 ] . actions ) . toContain ( 'validateDraft' ) ;
152152 } ) ;
153153
154154 it ( 'should query a workflow instance via nested draftWorkspace' , async ( ) => {
155- const instanceResult = await adminQuery ( {
155+ const instanceResult = await queryAsAdmin ( {
156156 query : WORKFLOW_INSTANCE_NESTED_QUERY ,
157157 variables : {
158158 entityId : draftWorkspaceId ,
159159 } ,
160160 } ) ;
161- expect ( instanceResult . data . draftWorkspace . workflowInstance . currentState ) . toBe ( 'open' ) ;
162- expect ( instanceResult . data . draftWorkspace . workflowInstance . allowedTransitions . length ) . toBe ( 1 ) ;
163- expect ( instanceResult . data . draftWorkspace . workflowInstance . allowedTransitions [ 0 ] . event ) . toBe ( 'validate_event' ) ;
161+ expect ( instanceResult . data ? .draftWorkspace . workflowInstance . currentState ) . toBe ( 'open' ) ;
162+ expect ( instanceResult . data ? .draftWorkspace . workflowInstance . allowedTransitions . length ) . toBe ( 1 ) ;
163+ expect ( instanceResult . data ? .draftWorkspace . workflowInstance . allowedTransitions [ 0 ] . event ) . toBe ( 'validate_event' ) ;
164164 } ) ;
165165
166166 it ( 'should trigger a workflow event' , async ( ) => {
167- const result = await adminQuery ( {
167+ const result = await queryAsAdmin ( {
168168 query : TRIGGER_WORKFLOW_EVENT_MUTATION ,
169169 variables : {
170170 entityId : draftWorkspaceId ,
171171 eventName : 'validate_event' ,
172172 } ,
173173 } ) ;
174- expect ( result . data . triggerWorkflowEvent . success ) . toBe ( true ) ;
175- expect ( result . data . triggerWorkflowEvent . newState ) . toBe ( 'validated' ) ;
174+ expect ( result . data ? .triggerWorkflowEvent . success ) . toBe ( true ) ;
175+ expect ( result . data ? .triggerWorkflowEvent . newState ) . toBe ( 'validated' ) ;
176176
177177 // Check if the entity was actually updated
178- const instanceResult = await adminQuery ( {
178+ const instanceResult = await queryAsAdmin ( {
179179 query : WORKFLOW_INSTANCE_QUERY ,
180180 variables : {
181181 entityId : draftWorkspaceId ,
182182 } ,
183183 } ) ;
184- expect ( instanceResult . data . workflowInstance . currentState ) . toBe ( 'validated' ) ;
185- expect ( instanceResult . data . workflowInstance . allowedTransitions . length ) . toBe ( 0 ) ;
184+ expect ( instanceResult . data ? .workflowInstance . currentState ) . toBe ( 'validated' ) ;
185+ expect ( instanceResult . data ? .workflowInstance . allowedTransitions . length ) . toBe ( 0 ) ;
186186 } ) ;
187187
188188 it ( 'should fail to trigger an invalid event' , async ( ) => {
189- const result = await adminQuery ( {
189+ const result = await queryAsAdmin ( {
190190 query : TRIGGER_WORKFLOW_EVENT_MUTATION ,
191191 variables : {
192192 entityId : draftWorkspaceId ,
193193 eventName : 'invalid_event' ,
194194 } ,
195195 } ) ;
196- expect ( result . data . triggerWorkflowEvent . success ) . toBe ( false ) ;
197- expect ( result . data . triggerWorkflowEvent . reason ) . toContain ( 'No transition found' ) ;
196+ expect ( result . data ? .triggerWorkflowEvent . success ) . toBe ( false ) ;
197+ expect ( result . data ? .triggerWorkflowEvent . reason ) . toContain ( 'No transition found' ) ;
198198 } ) ;
199199
200200 it ( 'should delete a workflow definition' , async ( ) => {
201201 // 1. Delete the workflow definition
202- const deleteResult = await adminQuery ( {
202+ const deleteResult = await queryAsAdmin ( {
203203 query : WORKFLOW_DEFINITION_DELETE_MUTATION ,
204204 variables : {
205205 entityType : 'DraftWorkspace' ,
206206 } ,
207207 } ) ;
208- expect ( deleteResult . data . workflowDefinitionDelete . workflow_id ) . toBeNull ( ) ;
208+ expect ( deleteResult . data ? .workflowDefinitionDelete . workflow_id ) . toBeNull ( ) ;
209209
210210 // 2. Check if the definition is gone
211- const queryResult = await adminQuery ( {
211+ const queryResult = await queryAsAdmin ( {
212212 query : WORKFLOW_DEFINITION_QUERY ,
213213 variables : {
214214 entityType : 'DraftWorkspace' ,
215215 } ,
216216 } ) ;
217- expect ( queryResult . data . workflowDefinition ) . toBeNull ( ) ;
217+ expect ( queryResult . data ? .workflowDefinition ) . toBeNull ( ) ;
218218
219219 // 3. Check if instance now returns null
220- const instanceResult = await adminQuery ( {
220+ const instanceResult = await queryAsAdmin ( {
221221 query : WORKFLOW_INSTANCE_QUERY ,
222222 variables : {
223223 entityId : draftWorkspaceId ,
224224 } ,
225225 } ) ;
226- expect ( instanceResult . data . workflowInstance ) . toBeNull ( ) ;
226+ expect ( instanceResult . data ? .workflowInstance ) . toBeNull ( ) ;
227227 } ) ;
228228} ) ;
0 commit comments