Skip to content

Commit 0990567

Browse files
committed
add src/mock-editors and src/support/components
1 parent f72370d commit 0990567

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+3094
-501
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,6 @@ node_modules
77
gitlab-ui
88

99
/src/demo.js
10-
/src/demo.html
10+
/src/demo.html
11+
12+
/npm-publish

src/mock-editor/TestingExplorer/TestingExplorerDemo.tsx

Lines changed: 53 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
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";
44
import { AddCaseRequest, addDir, buildTestingItemV2, DeleteCaseRequest, deleteDir, ListCaseResp, renameDir, TestingItem } from "./testing-api";
5-
import { ExtensionData, useTestingExplorerEditorController } from "./TestingExplorerEditor";
5+
import { useTestingExplorerEditorController } from "./TestingExplorerEditor";
66
import { demoAPI as listDemoAPI } from "./TestingList/TestingListDemo";
77

8+
import TestingExplorer from ".";
89
import { useCurrent } from "../react-hooks";
910
import { stringifyData } from "../util/format";
1011
import { demoAPI } from "./TestingExplorerEditor/TestingExplorerEditorDemo";
11-
import { Options } from "./TestingList";
12-
import TestingExplorer from ".";
1312
import { patchResponse } from "./TestingExplorerEditor/util";
13+
import { Options } from "./TestingList";
14+
import { ExtensionData } from "./TestingExplorerEditor/TraceList/trace-types";
1415

1516
const listCaseURL = 'http://localhost:16000/api/case/listAll?noCaseList=true'
1617
const updateSummaryURL = 'http://localhost:16000/api/summary/update'
@@ -21,54 +22,66 @@ export interface TestingExplorerDemoProps {
2122
topElement?: any
2223
}
2324

24-
export default function (props: TestingExplorerDemoProps) {
25+
export default function TestingExplorerDemo(props: TestingExplorerDemoProps) {
2526
const editorControllerRef = useTestingExplorerEditorController()
2627
const [testingItems, setTestingItems] = useState<TestingItem[]>()
2728

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 () => {
2938
const e = await axios({ url: listCaseURL, method: "GET" })
3039
const resp: ListCaseResp = e.data?.data
3140
const item = buildTestingItemV2(resp.root)
3241
setTestingItems(item ? [item] : [])
3342
}
3443
useEffect(() => {
35-
refresh()
44+
refreshTreeList()
3645
}, [])
3746

3847
// const [curItem, setItem] = useState<TestingItem>()
3948

40-
type ItemBundle = {
41-
item: TestingItem,
42-
action?: (caseData: TestingCase) => void
43-
}
44-
const [itemBundle, setItemBundle] = useState<ItemBundle>()
45-
4649
const [mockInfo, setMockInfo] = useState<MockInfo>()
4750
const [caseData, setCaseData] = useState<TestingCase>()
4851

4952
const curItem = itemBundle?.item
5053

54+
const caseDataRef = useCurrent(caseData)
55+
5156
// get the case
5257
// ping localhost:16000 first
5358
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) => {
5560
// console.log("reload case:", curItem)
5661
let caseData: TestingCase
5762
if (curItem?.kind === "case") {
5863
caseData = await demoAPI.loadCase(curItem.method as string, curItem.path as string, curItem.id as number)
5964
}
60-
editorControllerRef.current?.clearResponse?.()
65+
if (clearResponse) {
66+
editorControllerRef.current?.clearResponse?.()
67+
}
6168
setCaseData(caseData)
6269
action?.(caseData)
6370
return caseData
6471
}
6572

73+
// refresh the data, without clear response
74+
const refreshItemData = () => {
75+
setItemBundle(v => ({ ...v, version: v.version + 1, clearResponse: false }))
76+
}
77+
78+
6679
useEffect(() => {
6780
// console.log("reload case onChange:", itemBundle?.item)
68-
reloadItem(itemBundle?.item, itemBundle?.action)
81+
reloadItem(itemBundle?.item, itemBundle?.action, itemBundle?.clearResponse)
6982
},
7083
// 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]
7285
)
7386

7487
useEffect(() => {
@@ -93,7 +106,12 @@ export default function (props: TestingExplorerDemoProps) {
93106
listProps={{
94107
data: testingItems,
95108
onTreeChangeRequested() {
96-
refresh()
109+
refreshTreeList()
110+
refreshItemData()
111+
},
112+
onRefreshRoot() {
113+
refreshTreeList()
114+
refreshItemData()
97115
},
98116
style: {
99117
// width: "400px",
@@ -104,8 +122,9 @@ export default function (props: TestingExplorerDemoProps) {
104122
},
105123
async onClickCaseRun(item, root, index, update) {
106124
// console.log("reload case onClick:", item)
107-
setItemBundle({
125+
setItemBundle(v => ({
108126
item,
127+
clearResponse: true,
109128
action: async (caseData: TestingCase) => {
110129
// avoid loading items twice
111130
const resp = await editorControllerRef.current?.request?.(caseData)
@@ -115,7 +134,7 @@ export default function (props: TestingExplorerDemoProps) {
115134
status: status,
116135
}))
117136
}
118-
})
137+
}))
119138
// console.log("clickCaseRun:", item)
120139
},
121140

@@ -167,7 +186,7 @@ export default function (props: TestingExplorerDemoProps) {
167186
id: id + 1,
168187
dir: item.path as string,
169188
name: `${item.name} Copy`,
170-
data: { ...caseData },
189+
data: { ...caseDataRef.current },
171190
}
172191
await axios({
173192
url: "http://localhost:16000/api/case/add",
@@ -236,7 +255,7 @@ export default function (props: TestingExplorerDemoProps) {
236255
},
237256
onSelectChange(item, root, index) {
238257
// console.log("onSelectChange:", item)
239-
setItemBundle({ item })
258+
setItemBundle(v => ({ item, clearResponse: true }))
240259
// setItem(item)
241260
},
242261
}}
@@ -249,31 +268,36 @@ export default function (props: TestingExplorerDemoProps) {
249268
async save(caseName, caseData: TestingCase) {
250269
if (curItemRef.current?.kind === "case") {
251270
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()
253273
})
254274
return
255275
} else {
256276
await renameDir(curItemRef?.current?.method as string,
257277
curItemRef?.current?.path as string,
258278
curItemRef.current?.name as string,
259279
caseName,
260-
).then(() => refresh())
280+
).then(() => refreshTreeList())
261281
}
262282
},
263283
async request(req: TestingRequestV2) {
264284
if (!curItem?.method) {
265285
return undefined
266286
}
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 })
271288
},
272289
}}
273290
/>
274291
</div>
275292
}
276293

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+
277301
function maxID(children?: TestingItem[]): number {
278302
let id = 0
279303
for (let child of (children || [])) {

0 commit comments

Comments
 (0)