1
1
import axios from "axios" ;
2
- import React , { CSSProperties , useEffect , useState } from "react" ;
3
- import { getRespStatus , MockInfo , RunStatus , TestingCase , TestingRequestV2 , TestingResponseV2 } from "./testing" ;
2
+ import { CSSProperties , useEffect , useState } from "react" ;
3
+ import { getRespStatus , MockInfo , TestingCase , TestingRequestV2 , TestingResponseV2 } from "./testing" ;
4
4
import { AddCaseRequest , addDir , buildTestingItemV2 , DeleteCaseRequest , deleteDir , ListCaseResp , renameDir , TestingItem } from "./testing-api" ;
5
- import { ExtensionData , useTestingExplorerEditorController } from "./TestingExplorerEditor" ;
5
+ import { useTestingExplorerEditorController } from "./TestingExplorerEditor" ;
6
6
import { demoAPI as listDemoAPI } from "./TestingList/TestingListDemo" ;
7
7
8
+ import TestingExplorer from "." ;
8
9
import { useCurrent } from "../react-hooks" ;
9
10
import { stringifyData } from "../util/format" ;
10
11
import { demoAPI } from "./TestingExplorerEditor/TestingExplorerEditorDemo" ;
11
- import { Options } from "./TestingList" ;
12
- import TestingExplorer from "." ;
13
12
import { patchResponse } from "./TestingExplorerEditor/util" ;
13
+ import { Options } from "./TestingList" ;
14
+ import { ExtensionData } from "./TestingExplorerEditor/TraceList/trace-types" ;
14
15
15
16
const listCaseURL = 'http://localhost:16000/api/case/listAll?noCaseList=true'
16
17
const updateSummaryURL = 'http://localhost:16000/api/summary/update'
@@ -21,54 +22,66 @@ export interface TestingExplorerDemoProps {
21
22
topElement ?: any
22
23
}
23
24
24
- export default function ( props : TestingExplorerDemoProps ) {
25
+ export default function TestingExplorerDemo ( props : TestingExplorerDemoProps ) {
25
26
const editorControllerRef = useTestingExplorerEditorController ( )
26
27
const [ testingItems , setTestingItems ] = useState < TestingItem [ ] > ( )
27
28
28
- const refresh = async ( ) => {
29
+ type ItemBundle = {
30
+ item ?: TestingItem ,
31
+ version ?: number ,
32
+ clearResponse ?: boolean // clear response before action
33
+ action ?: ( caseData : TestingCase ) => void
34
+ }
35
+ const [ itemBundle , setItemBundle ] = useState < ItemBundle > ( { version : 0 } as ItemBundle )
36
+
37
+ const refreshTreeList = async ( ) => {
29
38
const e = await axios ( { url : listCaseURL , method : "GET" } )
30
39
const resp : ListCaseResp = e . data ?. data
31
40
const item = buildTestingItemV2 ( resp . root )
32
41
setTestingItems ( item ? [ item ] : [ ] )
33
42
}
34
43
useEffect ( ( ) => {
35
- refresh ( )
44
+ refreshTreeList ( )
36
45
} , [ ] )
37
46
38
47
// const [curItem, setItem] = useState<TestingItem>()
39
48
40
- type ItemBundle = {
41
- item : TestingItem ,
42
- action ?: ( caseData : TestingCase ) => void
43
- }
44
- const [ itemBundle , setItemBundle ] = useState < ItemBundle > ( )
45
-
46
49
const [ mockInfo , setMockInfo ] = useState < MockInfo > ( )
47
50
const [ caseData , setCaseData ] = useState < TestingCase > ( )
48
51
49
52
const curItem = itemBundle ?. item
50
53
54
+ const caseDataRef = useCurrent ( caseData )
55
+
51
56
// get the case
52
57
// ping localhost:16000 first
53
58
const curItemRef = useCurrent ( curItem )
54
- const reloadItem = async ( curItem : TestingItem , action : ( caseData : TestingCase ) => void ) => {
59
+ const reloadItem = async ( curItem : TestingItem , action : ( caseData : TestingCase ) => void , clearResponse : boolean ) => {
55
60
// console.log("reload case:", curItem)
56
61
let caseData : TestingCase
57
62
if ( curItem ?. kind === "case" ) {
58
63
caseData = await demoAPI . loadCase ( curItem . method as string , curItem . path as string , curItem . id as number )
59
64
}
60
- editorControllerRef . current ?. clearResponse ?.( )
65
+ if ( clearResponse ) {
66
+ editorControllerRef . current ?. clearResponse ?.( )
67
+ }
61
68
setCaseData ( caseData )
62
69
action ?.( caseData )
63
70
return caseData
64
71
}
65
72
73
+ // refresh the data, without clear response
74
+ const refreshItemData = ( ) => {
75
+ setItemBundle ( v => ( { ...v , version : v . version + 1 , clearResponse : false } ) )
76
+ }
77
+
78
+
66
79
useEffect ( ( ) => {
67
80
// console.log("reload case onChange:", itemBundle?.item)
68
- reloadItem ( itemBundle ?. item , itemBundle ?. action )
81
+ reloadItem ( itemBundle ?. item , itemBundle ?. action , itemBundle ?. clearResponse )
69
82
} ,
70
83
// destruct basic data so change won't load twice
71
- [ itemBundle ?. item ?. kind , itemBundle ?. item ?. method , itemBundle ?. item ?. path , itemBundle ?. item ?. id ]
84
+ [ itemBundle ?. item ?. kind , itemBundle ?. item ?. method , itemBundle ?. item ?. path , itemBundle ?. item ?. id , itemBundle ?. clearResponse , itemBundle . version ]
72
85
)
73
86
74
87
useEffect ( ( ) => {
@@ -93,7 +106,12 @@ export default function (props: TestingExplorerDemoProps) {
93
106
listProps = { {
94
107
data : testingItems ,
95
108
onTreeChangeRequested ( ) {
96
- refresh ( )
109
+ refreshTreeList ( )
110
+ refreshItemData ( )
111
+ } ,
112
+ onRefreshRoot ( ) {
113
+ refreshTreeList ( )
114
+ refreshItemData ( )
97
115
} ,
98
116
style : {
99
117
// width: "400px",
@@ -104,8 +122,9 @@ export default function (props: TestingExplorerDemoProps) {
104
122
} ,
105
123
async onClickCaseRun ( item , root , index , update ) {
106
124
// console.log("reload case onClick:", item)
107
- setItemBundle ( {
125
+ setItemBundle ( v => ( {
108
126
item,
127
+ clearResponse : true ,
109
128
action : async ( caseData : TestingCase ) => {
110
129
// avoid loading items twice
111
130
const resp = await editorControllerRef . current ?. request ?.( caseData )
@@ -115,7 +134,7 @@ export default function (props: TestingExplorerDemoProps) {
115
134
status : status ,
116
135
} ) )
117
136
}
118
- } )
137
+ } ) )
119
138
// console.log("clickCaseRun:", item)
120
139
} ,
121
140
@@ -167,7 +186,7 @@ export default function (props: TestingExplorerDemoProps) {
167
186
id : id + 1 ,
168
187
dir : item . path as string ,
169
188
name : `${ item . name } Copy` ,
170
- data : { ...caseData } ,
189
+ data : { ...caseDataRef . current } ,
171
190
}
172
191
await axios ( {
173
192
url : "http://localhost:16000/api/case/add" ,
@@ -236,7 +255,7 @@ export default function (props: TestingExplorerDemoProps) {
236
255
} ,
237
256
onSelectChange ( item , root , index ) {
238
257
// console.log("onSelectChange:", item)
239
- setItemBundle ( { item } )
258
+ setItemBundle ( v => ( { item, clearResponse : true } ) )
240
259
// setItem(item)
241
260
} ,
242
261
} }
@@ -249,31 +268,36 @@ export default function (props: TestingExplorerDemoProps) {
249
268
async save ( caseName , caseData : TestingCase ) {
250
269
if ( curItemRef . current ?. kind === "case" ) {
251
270
await demoAPI . saveCase ( curItemRef ?. current ?. method as string , curItemRef ?. current ?. path as string , curItemRef . current ?. id as number , caseName , caseData ) . finally ( ( ) => {
252
- refresh ( )
271
+ refreshTreeList ( )
272
+ refreshItemData ( )
253
273
} )
254
274
return
255
275
} else {
256
276
await renameDir ( curItemRef ?. current ?. method as string ,
257
277
curItemRef ?. current ?. path as string ,
258
278
curItemRef . current ?. name as string ,
259
279
caseName ,
260
- ) . then ( ( ) => refresh ( ) )
280
+ ) . then ( ( ) => refreshTreeList ( ) )
261
281
}
262
282
} ,
263
283
async request ( req : TestingRequestV2 ) {
264
284
if ( ! curItem ?. method ) {
265
285
return undefined
266
286
}
267
- const data : TestingResponseV2 < ExtensionData > = await demoAPI . requestTest ( { ...req , method : curItem . method } ) . catch ( e => {
268
- return { Error : e . message } as TestingResponseV2 < ExtensionData >
269
- } ) as TestingResponseV2 < ExtensionData >
270
- return patchResponse ( data )
287
+ return await requestAndPatch ( { ...req , method : curItem . method } )
271
288
} ,
272
289
} }
273
290
/>
274
291
</ div >
275
292
}
276
293
294
+ export async function requestAndPatch ( req : TestingRequestV2 ) {
295
+ const data : TestingResponseV2 < ExtensionData > = await demoAPI . requestTest ( req ) . catch ( e => {
296
+ return { Error : e . message } as TestingResponseV2 < ExtensionData >
297
+ } ) as TestingResponseV2 < ExtensionData >
298
+ return patchResponse ( data )
299
+ }
300
+
277
301
function maxID ( children ?: TestingItem [ ] ) : number {
278
302
let id = 0
279
303
for ( let child of ( children || [ ] ) ) {
0 commit comments