-
Notifications
You must be signed in to change notification settings - Fork 67
/
useTestSpecsCrud.ts
170 lines (148 loc) 路 4.54 KB
/
useTestSpecsCrud.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import {useCallback} from 'react';
import {useNavigate} from 'react-router-dom';
import AssertionResults from 'models/AssertionResults.model';
import Test from 'models/Test.model';
import TestSpecs, {TTestSpecEntry} from 'models/TestSpecs.model';
import TestSpecsActions from 'redux/actions/TestSpecs.actions';
import {useAppDispatch} from 'redux/hooks';
import {
initSpecs,
resetSpecs,
addSpec,
removeSpec,
revertSpec,
updateSpec,
reset as resetAction,
setIsInitialized,
setSelectorSuggestions as setSelectorSuggestionsAction,
setPrevSelector as setPrevSelectorAction,
} from 'redux/slices/TestSpecs.slice';
import {ISuggestion} from 'types/TestSpecs.types';
import useBlockNavigation from 'hooks/useBlockNavigation';
import RouterActions from 'redux/actions/Router.actions';
import {RouterSearchFields} from 'constants/Common.constants';
import {useConfirmationModal} from 'providers/ConfirmationModal/ConfirmationModal.provider';
import {useNotification} from 'providers/Notification/Notification.provider';
interface IProps {
runId: string;
testId: string;
test: Test;
isDraftMode: boolean;
assertionResults?: AssertionResults;
}
const useTestSpecsCrud = ({runId, testId, test, isDraftMode, assertionResults}: IProps) => {
useBlockNavigation(isDraftMode);
const dispatch = useAppDispatch();
const navigate = useNavigate();
const {onOpen} = useConfirmationModal();
const {showNotification} = useNotification();
const setSelectedSpec = useCallback(
selector => {
const resultList = assertionResults?.resultList || [];
const positionIndex = resultList.findIndex(result => result.selector === selector);
dispatch(
RouterActions.updateSearch({
[RouterSearchFields.SelectedAssertion]: positionIndex >= 0 ? `${positionIndex}` : undefined,
})
);
},
[assertionResults?.resultList, dispatch]
);
const revert = useCallback(
(originalSelector: string) => {
return dispatch(revertSpec({originalSelector}));
},
[dispatch]
);
const dryRun = useCallback(
(definitionList: TTestSpecEntry[]) => {
return dispatch(TestSpecsActions.dryRun({testId, runId, definitionList})).unwrap();
},
[dispatch, runId, testId]
);
const publish = useCallback(async () => {
const {id} = await dispatch(TestSpecsActions.publish({test, testId, runId})).unwrap();
dispatch(RouterActions.updateSearch({[RouterSearchFields.SelectedAssertion]: ''}));
showNotification({
type: 'success',
title: 'Your test has been saved successfully',
description: 'A new test run has been generated.',
});
navigate(`/test/${testId}/run/${id}/test`);
}, [dispatch, navigate, runId, showNotification, test, testId]);
const cancel = useCallback(() => {
dispatch(resetSpecs());
}, [dispatch]);
const add = useCallback(
async (spec: TTestSpecEntry) => {
dispatch(addSpec({spec}));
setSelectedSpec(spec.selector);
},
[dispatch, setSelectedSpec]
);
const update = useCallback(
async (selector: string, spec: TTestSpecEntry) => {
dispatch(updateSpec({spec, selector}));
setSelectedSpec(selector);
},
[dispatch, setSelectedSpec]
);
const onConfirmRemove = useCallback(
async (selector: string) => {
dispatch(removeSpec({selector}));
},
[dispatch]
);
const remove = useCallback(
(selector: string) => {
onOpen({
title: 'Are you sure you want to remove this test spec?',
onConfirm: () => onConfirmRemove(selector),
});
},
[onConfirmRemove, onOpen]
);
const init = useCallback(
(initialAssertionResults: AssertionResults, specs: TestSpecs) => {
dispatch(initSpecs({assertionResults: initialAssertionResults, specs}));
},
[dispatch]
);
const updateIsInitialized = useCallback(
isInitialized => {
dispatch(setIsInitialized({isInitialized}));
},
[dispatch]
);
const reset = useCallback(() => {
dispatch(resetAction());
}, [dispatch]);
const setSelectorSuggestions = useCallback(
(selectorSuggestions: ISuggestion[]) => {
dispatch(setSelectorSuggestionsAction(selectorSuggestions));
},
[dispatch]
);
const setPrevSelector = useCallback(
(prevSelector: string) => {
dispatch(setPrevSelectorAction({prevSelector}));
},
[dispatch]
);
return {
revert,
init,
updateIsInitialized,
reset,
add,
remove,
update,
publish,
cancel,
dryRun,
setSelectedSpec,
setSelectorSuggestions,
setPrevSelector,
};
};
export default useTestSpecsCrud;