1+ import { QueryClientProvider } from "@tanstack/react-query" ;
2+ import { render , screen } from '@testing-library/react' ;
3+ import userEvent from "@testing-library/user-event" ;
4+ import { BaseConfig } from 'components/WorkoutRoutines/models/BaseConfig' ;
5+
6+ import { ConfigDetailsField } from 'components/WorkoutRoutines/widgets/forms/BaseConfigForm' ;
7+ import React from 'react' ;
8+ import { testQueryClient } from "tests/queryClient" ;
9+
10+
11+ jest . mock ( 'utils/consts' , ( ) => {
12+ return {
13+ DEBOUNCE_ROUTINE_FORMS : '5'
14+ } ;
15+ } ) ;
16+
17+
18+ jest . mock ( 'components/WorkoutRoutines/queries' , ( ) => ( {
19+ useEditWeightConfigQuery : jest . fn ( ( ) => ( { mutate : editMutation } ) ) ,
20+ useAddWeightConfigQuery : jest . fn ( ( ) => ( { mutate : addMutation } ) ) ,
21+ useDeleteWeightConfigQuery : jest . fn ( ( ) => ( { mutate : deleteMutation } ) ) ,
22+ useEditMaxWeightConfigQuery : jest . fn ( ( ) => ( { mutate : editMutation } ) ) ,
23+ useAddMaxWeightConfigQuery : jest . fn ( ( ) => ( { mutate : addMutation } ) ) ,
24+ useDeleteMaxWeightConfigQuery : jest . fn ( ( ) => ( { mutate : deleteMutation } ) ) ,
25+ useEditRepsConfigQuery : jest . fn ( ( ) => ( { mutate : editMutation } ) ) ,
26+ useAddRepsConfigQuery : jest . fn ( ( ) => ( { mutate : addMutation } ) ) ,
27+ useDeleteRepsConfigQuery : jest . fn ( ( ) => ( { mutate : deleteMutation } ) ) ,
28+ useEditMaxRepsConfigQuery : jest . fn ( ( ) => ( { mutate : editMutation } ) ) ,
29+ useAddMaxRepsConfigQuery : jest . fn ( ( ) => ( { mutate : addMutation } ) ) ,
30+ useDeleteMaxRepsConfigQuery : jest . fn ( ( ) => ( { mutate : deleteMutation } ) ) ,
31+ useEditNrOfSetsConfigQuery : jest . fn ( ( ) => ( { mutate : editMutation } ) ) ,
32+ useAddNrOfSetsConfigQuery : jest . fn ( ( ) => ( { mutate : addMutation } ) ) ,
33+ useDeleteNrOfSetsConfigQuery : jest . fn ( ( ) => ( { mutate : deleteMutation } ) ) ,
34+ useEditRestConfigQuery : jest . fn ( ( ) => ( { mutate : editMutation } ) ) ,
35+ useAddRestConfigQuery : jest . fn ( ( ) => ( { mutate : addMutation } ) ) ,
36+ useDeleteRestConfigQuery : jest . fn ( ( ) => ( { mutate : deleteMutation } ) ) ,
37+ useEditMaxRestConfigQuery : jest . fn ( ( ) => ( { mutate : editMutation } ) ) ,
38+ useAddMaxRestConfigQuery : jest . fn ( ( ) => ( { mutate : addMutation } ) ) ,
39+ useDeleteMaxRestConfigQuery : jest . fn ( ( ) => ( { mutate : deleteMutation } ) ) ,
40+ useEditRiRConfigQuery : jest . fn ( ( ) => ( { mutate : editMutation } ) ) ,
41+ useAddRiRConfigQuery : jest . fn ( ( ) => ( { mutate : addMutation } ) ) ,
42+ useDeleteRiRConfigQuery : jest . fn ( ( ) => ( { mutate : deleteMutation } ) ) ,
43+ } ) ) ;
44+
45+
46+ const editMutation = jest . fn ( ) ;
47+ const addMutation = jest . fn ( ) ;
48+ const deleteMutation = jest . fn ( ) ;
49+
50+ const DEBOUNCE_WAIT = 10 ;
51+
52+ describe ( 'ConfigDetailsField Component' , ( ) => {
53+ const routineId = 1 ;
54+ const slotId = 2 ;
55+
56+ beforeEach ( ( ) => {
57+ jest . clearAllMocks ( ) ;
58+ } ) ;
59+
60+ afterEach ( ( ) => {
61+ jest . restoreAllMocks ( ) ;
62+ } ) ;
63+
64+ const testConfigTypes = [ 'weight' , 'max-weight' , 'reps' , 'max-reps' , 'sets' , 'rest' , 'max-rest' , 'rir' ] as const ;
65+
66+ testConfigTypes . forEach ( ( type ) => {
67+ describe ( `for type ${ type } ` , ( ) => {
68+ test ( 'calls editQuery.mutate with correct data when entry exists' , async ( ) => {
69+
70+ const mockConfig = new BaseConfig ( 123 , 10 , 1 , null , 5 , '+' , null , true , false ) ;
71+ const user = userEvent . setup ( ) ;
72+
73+ render (
74+ < QueryClientProvider client = { testQueryClient } >
75+ < ConfigDetailsField config = { mockConfig } routineId = { routineId } slotId = { slotId } type = { type } />
76+ </ QueryClientProvider >
77+ ) ;
78+
79+ await user . type ( screen . getByTestId ( `${ type } -field` ) , '2' ) ;
80+
81+
82+ await new Promise ( ( resolve ) => setTimeout ( resolve , DEBOUNCE_WAIT ) ) ;
83+
84+ expect ( addMutation ) . toHaveBeenCalledTimes ( 0 ) ;
85+ expect ( editMutation ) . toHaveBeenCalledTimes ( 1 ) ;
86+ expect ( editMutation ) . toHaveBeenCalledWith ( {
87+ id : mockConfig . id ,
88+ slot_config : slotId ,
89+ value : 52 ,
90+ iteration : 1 ,
91+ operation : null ,
92+ replace : true ,
93+ need_log_to_apply : false ,
94+ } ) ;
95+ expect ( deleteMutation ) . toHaveBeenCalledTimes ( 0 ) ;
96+ } ) ;
97+
98+ test ( 'calls addQuery.mutate with correct data when creating a new entry' , async ( ) => {
99+ const user = userEvent . setup ( ) ;
100+
101+ render (
102+ < QueryClientProvider client = { testQueryClient } >
103+ < ConfigDetailsField routineId = { routineId } slotId = { slotId } type = { type } />
104+ </ QueryClientProvider >
105+ ) ;
106+
107+ await user . type ( screen . getByTestId ( `${ type } -field` ) , '8' ) ;
108+ await new Promise ( ( resolve ) => setTimeout ( resolve , DEBOUNCE_WAIT ) ) ;
109+
110+ expect ( addMutation ) . toHaveBeenCalledTimes ( 1 ) ;
111+ expect ( addMutation ) . toHaveBeenCalledWith ( {
112+ slot : slotId ,
113+ slot_config : 2 ,
114+ value : 8 ,
115+ iteration : 1 ,
116+ operation : null ,
117+ replace : true ,
118+ need_log_to_apply : false ,
119+ } ) ;
120+ expect ( editMutation ) . toHaveBeenCalledTimes ( 0 ) ;
121+ expect ( deleteMutation ) . toHaveBeenCalledTimes ( 0 ) ;
122+ } ) ;
123+
124+ test ( 'calls deleteQuery.mutate when value is deleted' , async ( ) => {
125+ const user = userEvent . setup ( ) ;
126+
127+ const mockConfig = new BaseConfig ( 123 , 10 , 1 , null , 5 , '+' , null , true , false ) ;
128+ render (
129+ < QueryClientProvider client = { testQueryClient } >
130+ < ConfigDetailsField config = { mockConfig } routineId = { routineId } slotId = { slotId } type = { type } />
131+ </ QueryClientProvider >
132+ ) ;
133+
134+ await user . clear ( screen . getByTestId ( `${ type } -field` ) ) ;
135+ await new Promise ( ( resolve ) => setTimeout ( resolve , DEBOUNCE_WAIT ) ) ;
136+
137+ expect ( addMutation ) . toHaveBeenCalledTimes ( 0 ) ;
138+ expect ( editMutation ) . toHaveBeenCalledTimes ( 0 ) ;
139+ expect ( deleteMutation ) . toHaveBeenCalledTimes ( 1 ) ;
140+ expect ( deleteMutation ) . toHaveBeenCalledWith ( mockConfig . id ) ;
141+ } ) ;
142+ } ) ;
143+ } ) ;
144+ } ) ;
0 commit comments