|
1 | 1 | import { assertEquals } from "@std/assert"; |
2 | | -import { assertType, type IsExact } from "@std/testing/types"; |
| 2 | +import { type AssertTrue, assertType, type IsExact } from "@std/testing/types"; |
3 | 3 | import { DenopsStub } from "@denops/test/stub"; |
4 | | -import { type Action, composeActions, defineAction } from "./action.ts"; |
| 4 | +import type { DetailUnit } from "./item.ts"; |
| 5 | +import { |
| 6 | + type Action, |
| 7 | + composeActions, |
| 8 | + defineAction, |
| 9 | + type InvokeParams, |
| 10 | +} from "./action.ts"; |
5 | 11 |
|
6 | | -Deno.test("defineAction", () => { |
7 | | - const action = defineAction(async () => {}); |
8 | | - assertEquals(typeof action.invoke, "function"); |
9 | | - assertType<IsExact<typeof action, Action<unknown>>>(true); |
10 | | -}); |
| 12 | +Deno.test("defineAction", async (t) => { |
| 13 | + await t.step("without type contraint", () => { |
| 14 | + const action = defineAction((_denops, params) => { |
| 15 | + type _ = AssertTrue<IsExact<typeof params, InvokeParams<DetailUnit>>>; |
| 16 | + }); |
| 17 | + assertType<IsExact<typeof action, Action<DetailUnit>>>(true); |
| 18 | + }); |
11 | 19 |
|
12 | | -Deno.test("composeActions", async () => { |
13 | | - const results: string[] = []; |
14 | | - const action1 = defineAction(() => { |
15 | | - results.push("action1"); |
| 20 | + await t.step("with type contraint", () => { |
| 21 | + type C = { a: string }; |
| 22 | + const action = defineAction<C>((_denops, params) => { |
| 23 | + type _ = AssertTrue<IsExact<typeof params, InvokeParams<C>>>; |
| 24 | + }); |
| 25 | + assertType<IsExact<typeof action, Action<C>>>(true); |
16 | 26 | }); |
17 | | - const action2 = defineAction(() => { |
18 | | - results.push("action2"); |
| 27 | +}); |
| 28 | + |
| 29 | +Deno.test("composeActions", async (t) => { |
| 30 | + await t.step("with bear actions", async (t) => { |
| 31 | + await t.step("actions are invoked in order", async () => { |
| 32 | + const results: string[] = []; |
| 33 | + const action1 = defineAction(() => void results.push("action1")); |
| 34 | + const action2 = defineAction(() => void results.push("action2")); |
| 35 | + const action3 = defineAction(() => void results.push("action3")); |
| 36 | + const action = composeActions(action2, action1, action3); |
| 37 | + const denops = new DenopsStub(); |
| 38 | + const params = { |
| 39 | + item: undefined, |
| 40 | + selectedItems: [], |
| 41 | + filteredItems: [], |
| 42 | + }; |
| 43 | + await action.invoke(denops, params, {}); |
| 44 | + assertEquals(results, ["action2", "action1", "action3"]); |
| 45 | + }); |
| 46 | + |
| 47 | + await t.step("without type contraint", () => { |
| 48 | + const action1 = defineAction(() => {}); |
| 49 | + const action2 = defineAction(() => {}); |
| 50 | + const action3 = defineAction(() => {}); |
| 51 | + const action = composeActions(action2, action1, action3); |
| 52 | + assertType<IsExact<typeof action, Action<DetailUnit>>>(true); |
| 53 | + }); |
| 54 | + |
| 55 | + await t.step("with type contraint", () => { |
| 56 | + type C = { a: string }; |
| 57 | + const action1 = defineAction<C>(() => {}); |
| 58 | + const action2 = defineAction<C>(() => {}); |
| 59 | + const action3 = defineAction<C>(() => {}); |
| 60 | + const action = composeActions(action2, action1, action3); |
| 61 | + assertType<IsExact<typeof action, Action<C>>>(true); |
| 62 | + }); |
19 | 63 | }); |
20 | | - const action3 = defineAction(() => { |
21 | | - results.push("action3"); |
| 64 | + |
| 65 | + await t.step("with derivable actions", async (t) => { |
| 66 | + await t.step("actions are invoked in order", async () => { |
| 67 | + const results: string[] = []; |
| 68 | + const action1 = () => defineAction(() => void results.push("action1")); |
| 69 | + const action2 = () => defineAction(() => void results.push("action2")); |
| 70 | + const action3 = () => defineAction(() => void results.push("action3")); |
| 71 | + const action = composeActions(action2, action1, action3); |
| 72 | + const denops = new DenopsStub(); |
| 73 | + const params = { |
| 74 | + item: undefined, |
| 75 | + selectedItems: [], |
| 76 | + filteredItems: [], |
| 77 | + }; |
| 78 | + await action.invoke(denops, params, {}); |
| 79 | + assertEquals(results, ["action2", "action1", "action3"]); |
| 80 | + }); |
| 81 | + |
| 82 | + await t.step("without type contraint", () => { |
| 83 | + const action1 = () => defineAction(() => {}); |
| 84 | + const action2 = () => defineAction(() => {}); |
| 85 | + const action3 = () => defineAction(() => {}); |
| 86 | + const action = composeActions(action2, action1, action3); |
| 87 | + assertType<IsExact<typeof action, Action<DetailUnit>>>(true); |
| 88 | + }); |
| 89 | + |
| 90 | + await t.step("with type contraint", () => { |
| 91 | + type C = { a: string }; |
| 92 | + const action1 = () => defineAction<C>(() => {}); |
| 93 | + const action2 = () => defineAction<C>(() => {}); |
| 94 | + const action3 = () => defineAction<C>(() => {}); |
| 95 | + const action = composeActions(action2, action1, action3); |
| 96 | + assertType<IsExact<typeof action, Action<C>>>(true); |
| 97 | + }); |
22 | 98 | }); |
23 | | - const action = composeActions(action2, action1, action3); |
24 | | - const denops = new DenopsStub(); |
25 | | - const params = { |
26 | | - item: undefined, |
27 | | - selectedItems: [], |
28 | | - filteredItems: [], |
29 | | - }; |
30 | | - await action.invoke(denops, params, {}); |
31 | | - assertEquals(results, ["action2", "action1", "action3"]); |
32 | 99 | }); |
0 commit comments