11import { GraphQLError } from "graphql" ;
2- import { addDays , addHours , set } from "date-fns" ;
2+ import { addDays , addHours , startOfDay } from "date-fns" ;
33import {
4- type ReservationNode ,
5- Authentication ,
6- ReservationKind ,
7- ReservationStartInterval ,
84 ReservationStateChoice ,
9- type ReservationUnitNode ,
105 UpdateStaffReservationDocument ,
116 RecurringReservationDocument ,
127 UpdateRecurringReservationDocument ,
138 ReservationTypeChoice ,
149 CustomerTypeChoice ,
1510 type RecurringReservationQuery ,
11+ ReservationQuery ,
1612} from "@gql/gql-types" ;
1713import { base64encode } from "common/src/helpers" ;
1814import { toApiDateUnsafe } from "common/src/common/util" ;
@@ -56,22 +52,21 @@ export const MUTATION_DATA = {
5652 } ,
5753} ;
5854
59- const TODAY = new Date ( ) ;
60- const getValidInterval = ( daysToAdd : number ) => {
61- const begin = set ( addDays ( TODAY , daysToAdd + 1 ) , {
62- hours : 6 ,
63- minutes : 0 ,
64- seconds : 0 ,
65- milliseconds : 0 ,
66- } ) ;
55+ function getValidInterval ( daysToAdd : number ) {
56+ const now = new Date ( ) ;
57+ const begin = addHours ( addDays ( startOfDay ( now ) , daysToAdd + 1 ) , 6 ) ;
6758 return [ begin . toISOString ( ) , addHours ( begin , 1 ) . toISOString ( ) ] ;
68- } ;
59+ }
6960
70- function createRecurringEdges (
71- startingPk : number ,
72- recurringPk : number ,
73- state : ReservationStateChoice = ReservationStateChoice . Confirmed
74- ) : NonNullable <
61+ function createReservationEdge ( {
62+ startingPk,
63+ recurringPk,
64+ state = ReservationStateChoice . Confirmed ,
65+ } : {
66+ startingPk : number ;
67+ recurringPk : number ;
68+ state ?: ReservationStateChoice ;
69+ } ) : NonNullable <
7570 RecurringReservationQuery [ "recurringReservation" ]
7671> [ "reservations" ] {
7772 const params = {
@@ -86,24 +81,36 @@ function createRecurringEdges(
8681 state,
8782 } ;
8883
84+ const begin1 = getValidInterval ( 0 ) [ 0 ] ;
85+ const end1 = getValidInterval ( 0 ) [ 1 ] ;
86+ const begin2 = getValidInterval ( 7 ) [ 0 ] ;
87+ const end2 = getValidInterval ( 7 ) [ 1 ] ;
88+ if ( begin1 == null || end1 == null || begin2 == null || end2 == null ) {
89+ throw new Error ( "Invalid dates" ) ;
90+ }
8991 return [
9092 {
9193 ...params ,
92- begin : getValidInterval ( 0 ) [ 0 ] ,
93- end : getValidInterval ( 0 ) [ 1 ] ,
94+ begin : begin1 ,
95+ end : end1 ,
9496 pk : startingPk ,
9597 id : base64encode ( `ReservationNode:${ startingPk } ` ) ,
9698 } ,
9799 {
98100 ...params ,
99- begin : getValidInterval ( 7 ) [ 0 ] ,
100- end : getValidInterval ( 7 ) [ 1 ] ,
101+ begin : begin2 ,
102+ end : end2 ,
101103 pk : startingPk + 1 ,
102104 id : base64encode ( `ReservationNode:${ startingPk + 1 } ` ) ,
103105 } ,
104106 ] ;
105107}
106108
109+ function convertDate ( str : string ) {
110+ const date = new Date ( str ) ;
111+ return toApiDateUnsafe ( date ) ;
112+ }
113+
107114function correctRecurringReservationQueryResult (
108115 startingPk : number ,
109116 recurringPk : number ,
@@ -113,17 +120,10 @@ function correctRecurringReservationQueryResult(
113120 allDenied ?: boolean ;
114121 }
115122) {
116- const convertDate = ( str : string ) => {
117- const date = new Date ( str ) ;
118- return toApiDateUnsafe ( date ) ;
119- } ;
120- const reservations = createRecurringEdges (
123+ const reservations = createReservationEdge ( {
121124 startingPk,
122125 recurringPk,
123- options ?. allDenied
124- ? ReservationStateChoice . Denied
125- : ReservationStateChoice . Confirmed
126- ) ;
126+ } ) ;
127127 const recurringReservation : NonNullable <
128128 RecurringReservationQuery [ "recurringReservation" ]
129129 > = {
@@ -219,76 +219,79 @@ function correctRecurringReservationQueryResult(
219219 ] ;
220220}
221221
222- export const mocks = [
223- // single reservation success
224- {
225- request : {
226- query : UpdateStaffReservationDocument ,
227- variables : MUTATION_DATA ,
228- } ,
229- result : {
230- data : {
231- staffReservationModify : { pk : 1 , errors : null } ,
232- updateReservationWorkingMemo : {
233- workingMemo : CHANGED_WORKING_MEMO ,
234- errors : null ,
222+ export function createMocks ( ) {
223+ return [
224+ // single reservation success
225+ {
226+ request : {
227+ query : UpdateStaffReservationDocument ,
228+ variables : MUTATION_DATA ,
229+ } ,
230+ result : {
231+ data : {
232+ staffReservationModify : { pk : 1 , errors : null } ,
233+ updateReservationWorkingMemo : {
234+ workingMemo : CHANGED_WORKING_MEMO ,
235+ errors : null ,
236+ } ,
235237 } ,
236238 } ,
237239 } ,
238- } ,
239- // single reservation Failure mocks: networkError once then succceed
240- ... [ { fail : true } , { fail : false } ] . map ( ( { fail } ) => ( {
241- request : {
242- query : UpdateStaffReservationDocument ,
243- variables : {
244- input : { ...MUTATION_DATA . input , pk : 101 } ,
245- workingMemo : { ... MUTATION_DATA . workingMemo , pk : 101 } ,
240+ // single reservation Failure mocks: networkError once then succceed
241+ ... [ { fail : true } , { fail : false } ] . map ( ( { fail } ) => ( {
242+ request : {
243+ query : UpdateStaffReservationDocument ,
244+ variables : {
245+ input : { ... MUTATION_DATA . input , pk : 101 } ,
246+ workingMemo : { ...MUTATION_DATA . workingMemo , pk : 101 } ,
247+ } ,
246248 } ,
247- } ,
248- error : fail ? new Error ( "Error" ) : undefined ,
249- result : ! fail
250- ? {
251- data : {
252- staffReservationModify : { pk : 101 , errors : null } ,
253- updateReservationWorkingMemo : {
254- workingMemo : CHANGED_WORKING_MEMO ,
255- errors : null ,
249+ error : fail ? new Error ( "Error" ) : undefined ,
250+ result : ! fail
251+ ? {
252+ data : {
253+ staffReservationModify : { pk : 101 , errors : null } ,
254+ updateReservationWorkingMemo : {
255+ workingMemo : CHANGED_WORKING_MEMO ,
256+ errors : null ,
257+ } ,
256258 } ,
257- } ,
258- }
259- : undefined ,
260- } ) ) ,
261- // networkError twice
262- ... [ 1 , 2 ] . map ( ( ) => ( {
263- request : {
264- query : UpdateStaffReservationDocument ,
265- variables : {
266- input : { ...MUTATION_DATA . input , pk : 102 } ,
267- workingMemo : { ... MUTATION_DATA . workingMemo , pk : 102 } ,
259+ }
260+ : undefined ,
261+ } ) ) ,
262+ // networkError twice
263+ ... [ 1 , 2 ] . map ( ( ) => ( {
264+ request : {
265+ query : UpdateStaffReservationDocument ,
266+ variables : {
267+ input : { ... MUTATION_DATA . input , pk : 102 } ,
268+ workingMemo : { ...MUTATION_DATA . workingMemo , pk : 102 } ,
269+ } ,
268270 } ,
269- } ,
270- error : new Error ( "Error" ) ,
271- } ) ) ,
272- // graphQLError
273- {
274- request : {
275- query : UpdateStaffReservationDocument ,
276- variables : {
277- input : { ...MUTATION_DATA . input , pk : 111 } ,
278- workingMemo : { ...MUTATION_DATA . workingMemo , pk : 111 } ,
271+ error : new Error ( "Error" ) ,
272+ } ) ) ,
273+ // graphQLError
274+ {
275+ request : {
276+ query : UpdateStaffReservationDocument ,
277+ variables : {
278+ input : { ...MUTATION_DATA . input , pk : 111 } ,
279+ workingMemo : { ...MUTATION_DATA . workingMemo , pk : 111 } ,
280+ } ,
281+ } ,
282+ result : {
283+ errors : [ new GraphQLError ( "Error" ) ] ,
279284 } ,
280285 } ,
281- result : {
282- errors : [ new GraphQLError ( "Error" ) ] ,
283- } ,
284- } ,
285- ...correctRecurringReservationQueryResult ( 21 , 1 ) ,
286- ...correctRecurringReservationQueryResult ( 31 , 2 , { shouldFailOnce : true } ) ,
287- ...correctRecurringReservationQueryResult ( 41 , 3 , { allDenied : true } ) ,
288- ...correctRecurringReservationQueryResult ( 51 , 4 , { shouldFailAll : true } ) ,
289- ] ;
286+ ...correctRecurringReservationQueryResult ( 21 , 1 ) ,
287+ ...correctRecurringReservationQueryResult ( 31 , 2 , { shouldFailOnce : true } ) ,
288+ ...correctRecurringReservationQueryResult ( 41 , 3 , { allDenied : true } ) ,
289+ ...correctRecurringReservationQueryResult ( 51 , 4 , { shouldFailAll : true } ) ,
290+ ] ;
291+ }
290292
291- export const mockReservation : ReservationNode = {
293+ type ReservationType = NonNullable < ReservationQuery [ "reservation" ] > ;
294+ export const mockReservation : ReservationType = {
292295 pk : 1 ,
293296 begin : "2024-01-01T10:00:00+00:00" ,
294297 end : "2024-01-01T14:00:00+00:00" ,
@@ -302,54 +305,19 @@ export const mockReservation: ReservationNode = {
302305 handlingDetails : "" ,
303306} ;
304307
305- const reservationUnit : ReservationUnitNode = {
306- pk : 1 ,
307- id : base64encode ( "ReservationUnitNode:1" ) ,
308- allowReservationsWithoutOpeningHours : true ,
309- applicationRoundTimeSlots : [ ] ,
310- applicationRounds : [ ] ,
311- bufferTimeAfter : 0 ,
312- bufferTimeBefore : 0 ,
313- authentication : Authentication . Weak ,
314- canApplyFreeOfCharge : false ,
315- contactInformation : "" ,
316- description : "" ,
317- equipments : [ ] ,
318- images : [ ] ,
319- isArchived : false ,
320- isDraft : false ,
321- name : "" ,
322- paymentTypes : [ ] ,
323- pricings : [ ] ,
324- purposes : [ ] ,
325- qualifiers : [ ] ,
326- requireIntroduction : false ,
327- requireReservationHandling : false ,
328- reservationBlockWholeDay : false ,
329- reservationCancelledInstructions : "" ,
330- reservationConfirmedInstructions : "" ,
331- reservationKind : ReservationKind . DirectAndSeason ,
332- reservationPendingInstructions : "" ,
333- reservationStartInterval : ReservationStartInterval . Interval_15Mins ,
334- resources : [ ] ,
335- services : [ ] ,
336- spaces : [ ] ,
337- maxPersons : 10 ,
338- uuid : "be4fa7a2-05b7-11ee-be56-0242ac120004" ,
339- } ;
340-
341- export const mockRecurringReservation : ReservationNode = {
342- ...mockReservation ,
343- pk : 21 ,
344- id : base64encode ( "ReservationNode:21" ) ,
345- recurringReservation : {
346- pk : 1 ,
347- created : "2021-09-01T10:00:00+00:00" ,
348- description : "" ,
349- reservations : [ ] ,
350- id : base64encode ( "RecurringReservationNode:1" ) ,
351- name : "recurring" ,
352- reservationUnit,
353- rejectedOccurrences : [ ] ,
354- } ,
355- } ;
308+ export function createMockRecurringReservation ( props : {
309+ pk : number ;
310+ recurringPk : number ;
311+ } ) : ReservationType {
312+ return {
313+ ...mockReservation ,
314+ pk : props . pk ,
315+ id : base64encode ( `ReservationNode:${ props . pk } ` ) ,
316+ recurringReservation : {
317+ pk : props . recurringPk ,
318+ description : "" ,
319+ id : base64encode ( `RecurringReservationNode:${ props . recurringPk } ` ) ,
320+ name : "recurring" ,
321+ } ,
322+ } ;
323+ }
0 commit comments