|
1 | 1 | import { describe, expect, test } from "vitest";
|
2 | 2 | import { mount, flushPromises } from "@vue/test-utils";
|
3 |
| -import { computed, defineComponent, h, ref, unref } from "vue"; |
| 3 | +import { computed, defineComponent, h, ref, unref, reactive } from "vue"; |
4 | 4 |
|
5 | 5 | import { useResource } from "../src";
|
6 | 6 | import { getAPIFuncs, MOCK_DATA_USER_LIST } from "./setup/mock-request";
|
@@ -145,7 +145,7 @@ describe("useResource", () => {
|
145 | 145 | expect(wrapper.get('[data-t-id="res.isLoading"]').text()).toBe("false");
|
146 | 146 | });
|
147 | 147 |
|
148 |
| - test("options: ", async () => { |
| 148 | + test("options: defaultState", async () => { |
149 | 149 | const Component = defineComponent({
|
150 | 150 | setup() {
|
151 | 151 | const [res, request] = useResource(getAPIFuncs(true).user.get, [], {
|
@@ -182,4 +182,55 @@ describe("useResource", () => {
|
182 | 182 | );
|
183 | 183 | expect(wrapper.get('[data-t-id="res.isLoading"]').text()).toBe("false");
|
184 | 184 | });
|
| 185 | + |
| 186 | + test("reactive parameter", async () => { |
| 187 | + const Component = defineComponent({ |
| 188 | + setup() { |
| 189 | + const params = reactive({ id: "1" }); |
| 190 | + const [res] = useResource(getAPIFuncs(true).user.get, [params], { |
| 191 | + filter: (p) => Boolean(p?.id), |
| 192 | + }); |
| 193 | + |
| 194 | + expect(unref(res).isLoading).toBeTruthy(); |
| 195 | + expect(unref(res).data).toBeUndefined(); |
| 196 | + expect(unref(res).response).toBeUndefined(); |
| 197 | + expect(unref(res).error).toBeUndefined(); |
| 198 | + |
| 199 | + const handleChangeId = () => { |
| 200 | + params.id = "2"; |
| 201 | + }; |
| 202 | + |
| 203 | + return () => |
| 204 | + h("div", [ |
| 205 | + h("button", { |
| 206 | + "data-t-id": "change_id", |
| 207 | + onClick: handleChangeId, |
| 208 | + }), |
| 209 | + h( |
| 210 | + "div", |
| 211 | + { "data-t-id": "res.data" }, |
| 212 | + JSON.stringify(res.value.data), |
| 213 | + ), |
| 214 | + h("div", { "data-t-id": "res.isLoading" }, res.value.isLoading), |
| 215 | + h("div", { "data-t-id": "params.id" }, params.id), |
| 216 | + ]); |
| 217 | + }, |
| 218 | + }); |
| 219 | + |
| 220 | + const wrapper = mount(Component); |
| 221 | + await flushPromises(); |
| 222 | + expect(wrapper.get('[data-t-id="res.data"]').text()).toBe( |
| 223 | + JSON.stringify(MOCK_DATA_USER_LIST.find((i) => i.id === "1")), |
| 224 | + ); |
| 225 | + expect(wrapper.get('[data-t-id="res.isLoading"]').text()).toBe("false"); |
| 226 | + expect(wrapper.get('[data-t-id="params.id"]').text()).toBe("1"); |
| 227 | + |
| 228 | + wrapper.get('[data-t-id="change_id"]').trigger("click"); |
| 229 | + await flushPromises(); |
| 230 | + expect(wrapper.get('[data-t-id="res.data"]').text()).toBe( |
| 231 | + JSON.stringify(MOCK_DATA_USER_LIST.find((i) => i.id === "2")), |
| 232 | + ); |
| 233 | + expect(wrapper.get('[data-t-id="res.isLoading"]').text()).toBe("false"); |
| 234 | + expect(wrapper.get('[data-t-id="params.id"]').text()).toBe("2"); |
| 235 | + }); |
185 | 236 | });
|
0 commit comments