Skip to content

Commit cd18182

Browse files
committed
test(types): custom response
1 parent 1e289d7 commit cd18182

File tree

2 files changed

+95
-9
lines changed

2 files changed

+95
-9
lines changed

tests/useRequest.test.ts

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,4 +244,61 @@ describe("useRequest", () => {
244244
_MyRes<MockDataUserItem> | undefined
245245
>();
246246
});
247+
248+
test("custom response data (The first value of the array returned)", async () => {
249+
const Component = defineComponent({
250+
setup() {
251+
const TARGET_ID = "1";
252+
const mockItem = MOCK_DATA_USER_LIST.find((i) => i.id === TARGET_ID);
253+
254+
const _getResponseItem = (r: AxiosResponse<any>) => r.data?.name;
255+
const _instance = axios.create({
256+
baseURL: BASE_URL,
257+
});
258+
const myrequest = <T extends { name?: string }>(
259+
config: AxiosRequestConfig,
260+
) => _request<AxiosResponse<T>, any, "data", "name">(config);
261+
262+
const [createRequest] = useRequest(
263+
(id: string) =>
264+
myrequest<MockDataUserItem>({
265+
method: "get",
266+
url: `/user/${id}`,
267+
}),
268+
{
269+
instance: _instance,
270+
getResponseItem: _getResponseItem,
271+
onCompleted: (d, r) => {
272+
// custom `data` value
273+
expect(d).toStrictEqual(mockItem?.name);
274+
expectTypeOf(d).toMatchTypeOf<string | undefined>();
275+
expect(r.data).toStrictEqual(mockItem);
276+
expectTypeOf(r).toMatchTypeOf<
277+
AxiosResponse<MockDataUserItem> | undefined
278+
>();
279+
},
280+
},
281+
);
282+
283+
createRequest(TARGET_ID)
284+
.ready()
285+
.then(([data, response]) => {
286+
// custom `data` value
287+
expect(data).toStrictEqual(mockItem?.name);
288+
expectTypeOf(data).toMatchTypeOf<string | undefined>();
289+
expect(response.data).toStrictEqual(mockItem);
290+
expectTypeOf(response).toMatchTypeOf<
291+
AxiosResponse<MockDataUserItem> | undefined
292+
>();
293+
})
294+
.catch(() => {
295+
expect(2).toBe(1);
296+
});
297+
298+
return () => h("div");
299+
},
300+
});
301+
302+
mount(Component);
303+
});
247304
});

tests/useResource.test.ts

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@ import { computed, defineComponent, h, ref, unref, reactive } from "vue";
55

66
import { useResource, _request } from "../src";
77
import type { MockDataUserItem } from "./setup/mock-request";
8-
import { getAPIFuncs, MOCK_DATA_USER_LIST } from "./setup/mock-request";
8+
import {
9+
BASE_URL,
10+
getAPIFuncs,
11+
MOCK_DATA_USER_LIST,
12+
} from "./setup/mock-request";
913

1014
describe("useResource", () => {
1115
test("should be defined", () => {
@@ -17,7 +21,16 @@ describe("useResource", () => {
1721
setup() {
1822
const id = ref("1");
1923
const params = computed(() => ({ id: unref(id) }));
20-
const [res] = useResource(getAPIFuncs(true).user.get, [params]);
24+
const [res] = useResource(getAPIFuncs(true).user.get, [params], {
25+
onCompleted: (d, r) => {
26+
expectTypeOf(d).toEqualTypeOf<MockDataUserItem | undefined>();
27+
expectTypeOf(r).toEqualTypeOf<AxiosResponse<MockDataUserItem>>();
28+
29+
const _item = MOCK_DATA_USER_LIST.find((i) => i.id === unref(id));
30+
expect(d).toStrictEqual(_item);
31+
expect(r.data).toStrictEqual(_item);
32+
},
33+
});
2134

2235
const _unref_res = unref(res);
2336
expect(_unref_res.isLoading).toBeTruthy();
@@ -247,24 +260,40 @@ describe("useResource", () => {
247260
test("types: custom response type", async () => {
248261
const Component = defineComponent({
249262
setup() {
250-
const id = ref("1");
251-
252-
const [res] = useResource(
263+
const [res01] = useResource(
253264
(i: string) =>
254265
_request<MockDataUserItem>({
266+
baseURL: BASE_URL,
255267
method: "get",
256268
url: `/user/${i}`,
257269
}),
258-
[id],
270+
false,
259271
);
260272

261-
const _unref_res = unref(res);
273+
const _unref_res01 = unref(res01);
262274

263-
expectTypeOf(_unref_res.data).toEqualTypeOf<undefined>();
264-
expectTypeOf(_unref_res.response).toEqualTypeOf<
275+
expectTypeOf(_unref_res01.data).toEqualTypeOf<undefined>();
276+
expectTypeOf(_unref_res01.response).toEqualTypeOf<
265277
MockDataUserItem | undefined
266278
>();
267279

280+
const [res02] = useResource(
281+
(i: string) =>
282+
_request<AxiosResponse<MockDataUserItem>, any, "data", "name">({
283+
baseURL: BASE_URL,
284+
method: "get",
285+
url: `/user/${i}`,
286+
}),
287+
false,
288+
);
289+
290+
const _unref_res02 = unref(res02);
291+
292+
expectTypeOf(_unref_res02.data).toEqualTypeOf<string | undefined>();
293+
expectTypeOf(_unref_res02.response).toEqualTypeOf<
294+
AxiosResponse<MockDataUserItem> | undefined
295+
>();
296+
268297
return () => h("div");
269298
},
270299
});

0 commit comments

Comments
 (0)