bunja 3.0.2 → 4.0.0-alpha.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/solid_test.ts DELETED
@@ -1,166 +0,0 @@
1
- import {
2
- assertEquals,
3
- assertNotStrictEquals,
4
- assertStrictEquals,
5
- } from "@std/assert";
6
- import {
7
- createComponent,
8
- createContext,
9
- createRoot,
10
- createSignal,
11
- } from "solid-js";
12
- import { bunja, createScope } from "./bunja.ts";
13
- import {
14
- bindScope,
15
- BunjaStoreProvider,
16
- createScopeFromContext,
17
- useBunja,
18
- } from "./solid.ts";
19
-
20
- const settle = () => new Promise((resolve) => setTimeout(resolve, 20));
21
-
22
- for (
23
- const mode of ["context accessor", "bindScope accessor", "explicit binding"]
24
- ) {
25
- const explicit = mode === "explicit binding";
26
- Deno.test(`Solid ${mode} lifecycle`, async () => {
27
- const Context = createContext<string | (() => string)>(() => "default");
28
- const Scope = mode === "bindScope accessor"
29
- ? createScope<string>()
30
- : createScopeFromContext(Context);
31
- if (mode === "bindScope accessor") bindScope(Scope, Context);
32
- const events: string[] = [];
33
- const model = bunja(() => {
34
- const id = bunja.use(Scope);
35
- bunja.effect(() => {
36
- events.push(`start:${id}`);
37
- return () => {
38
- events.push(`stop:${id}`);
39
- };
40
- });
41
- return { id };
42
- });
43
- // Exercise transitive scope discovery, including the first unbaked instance.
44
- const parent = bunja(() => bunja.use(model));
45
- let value!: () => { id: string };
46
- let other!: typeof value;
47
- let change!: (id: string) => void;
48
- let dispose!: () => void;
49
- let unmount!: () => void;
50
- createRoot((cleanup) => {
51
- dispose = cleanup;
52
- const [id, setId] = createSignal("a");
53
- change = setId;
54
- createComponent(BunjaStoreProvider, {
55
- get children() {
56
- return createComponent(Context.Provider, {
57
- value: id,
58
- get children() {
59
- const bindings = explicit ? () => [Scope.bind(id())] : undefined;
60
- value = useBunja(parent, bindings);
61
- createRoot((cleanup) => {
62
- unmount = cleanup;
63
- other = useBunja(parent, bindings);
64
- });
65
- return null;
66
- },
67
- });
68
- },
69
- });
70
- });
71
- try {
72
- await settle();
73
- const first = value();
74
- assertEquals(first.id, "a");
75
- assertStrictEquals(other(), first);
76
- assertEquals(events, ["start:a"]);
77
- change("a");
78
- await settle();
79
- assertStrictEquals(value(), first);
80
- for (const id of ["b", "a"]) {
81
- const previous = value();
82
- change(id);
83
- await settle();
84
- assertEquals(value().id, id);
85
- assertNotStrictEquals(value(), previous);
86
- assertStrictEquals(other(), value());
87
- }
88
- assertEquals(events, [
89
- "start:a",
90
- "start:b",
91
- "stop:a",
92
- "start:a",
93
- "stop:b",
94
- ]);
95
- unmount();
96
- await settle();
97
- assertEquals(events.length, 5);
98
- } finally {
99
- unmount();
100
- dispose();
101
- await settle();
102
- }
103
- assertEquals(events, [
104
- "start:a",
105
- "start:b",
106
- "stop:a",
107
- "start:a",
108
- "stop:b",
109
- "stop:a",
110
- ]);
111
- });
112
- }
113
-
114
- Deno.test("Solid bindScope, explicit precedence and mixed context fallback", async () => {
115
- const Context = createContext<string | null | (() => string | null)>(null);
116
- const Scope = createScope<string | null>();
117
- bindScope(Scope, Context);
118
- const Other = createContext((): string => "default");
119
- const OtherScope = createScopeFromContext(Other);
120
- const model = bunja(() => ({
121
- id: bunja.use(Scope),
122
- other: bunja.use(OtherScope),
123
- }));
124
- let value!: () => { id: string | null; other: string };
125
- let change!: (value: string) => void;
126
- let override!: (value: boolean) => void;
127
- let dispose!: () => void;
128
- createRoot((cleanup) => {
129
- dispose = cleanup;
130
- const [id, setId] = createSignal("a");
131
- const [explicit, setExplicit] = createSignal(true);
132
- change = setId;
133
- override = setExplicit;
134
- createComponent(BunjaStoreProvider, {
135
- get children() {
136
- return createComponent(Other.Provider, {
137
- value: id,
138
- get children() {
139
- value = useBunja(
140
- model,
141
- () => explicit() ? [OtherScope.bind("override")] : [],
142
- );
143
- return null;
144
- },
145
- });
146
- },
147
- });
148
- });
149
- try {
150
- await settle();
151
- const first = value();
152
- assertEquals(first, { id: null, other: "override" });
153
- change("b");
154
- await settle();
155
- assertStrictEquals(value(), first);
156
- override(false);
157
- await settle();
158
- assertEquals(value(), { id: null, other: "b" });
159
- change("c");
160
- await settle();
161
- assertEquals(value(), { id: null, other: "c" });
162
- } finally {
163
- dispose();
164
- await settle();
165
- }
166
- });