Open History to see the last commit
1import { flushSync } from 'svelte'
2import { describe, expect, it } from 'vitest'
3import { createRawRef } from '../src/containers.svelte.js'
4import { withEffectRoot } from './utils.svelte.js'
5β
6describe('createRawRef', () => {
7 it('should create a reactive reference', () => {
8 const [ref, update] = createRawRef({ a: 1, b: 2 })
9β
10 expect(ref).toEqual({ a: 1, b: 2 })
11β
12 update({ a: 3, b: 4 })
13 expect(ref).toEqual({ a: 3, b: 4 })
14β
15 ref.a = 5
16 expect(ref).toEqual({ a: 5, b: 4 })
17 })
18β
19 it('should handle nested objects', () => {
20 const [ref, update] = createRawRef<{ a: any }>({ a: { b: { c: 1 } } })
21β
22 expect(ref).toEqual({ a: { b: { c: 1 } } })
23β
24 // update with same structure
25 update({ a: { b: { c: 2 } } })
26 expect(ref).toEqual({ a: { b: { c: 2 } } })
27β
28 ref.a.b.c = 3
29 expect(ref).toEqual({ a: { b: { c: 3 } } })
30β
31 // update with different structure should wipe out everything below the first level
32 update({ a: { b: 3 } })
33 expect(ref).toEqual({ a: { b: 3 } })
34 })
35β
36 it('should remove properties when a new object is assigned', () => {
37 const [ref, update] = createRawRef<Record<string, number>>({
38 a: 1,
39 b: 2,
40 })
41β
42 expect(ref).toEqual({ a: 1, b: 2 })
43β
44 update({ a: 3 })
45 expect(ref).toEqual({ a: 3 })
46 })
47β
48 it(
49 'should not break reactivity when removing keys',
50 withEffectRoot(() => {
51 const [ref, update] = createRawRef<Record<string, number>>({ a: 1, b: 2 })
52 const states: Array<number | undefined> = []
53 $effect(() => {
54 states.push(ref.b)
55 })
56β
57 // these flushSync calls force the effect to run and push the value to the states array
58 flushSync()
59 update({ a: 3 }) // should remove b, and should rerun the effect
60 flushSync()
61 update({ a: 3, b: 4 }) // should add b back, and should rerun the effect
62 flushSync()
63 delete ref.b // should remove b, and should rerun the effect
64 flushSync()
65 delete ref.a // should remove a, and should _not_ rerun the effect
66 expect(states).toEqual([2, undefined, 4, undefined])
67 }),
68 )
69β
70 it(
71 'should correctly trap calls to `in`',
72 withEffectRoot(() => {
73 const [ref, update] = createRawRef<Record<string, number>>({
74 a: 1,
75 b: 2,
76 })
77β
78 expect('b' in ref).toBe(true)
79 delete ref.b
80 expect('b' in ref).toBe(false)
81 update({})
82 expect('a' in ref).toBe(false)
83 update({ a: 1, b: 2 })
84 expect('b' in ref).toBe(true)
85 expect('a' in ref).toBe(true)
86 }),
87 )
88β
89 it('should correctly trap calls to `ownKeys`', () => {
90 const [ref, update] = createRawRef<Record<string, number>>({
91 a: 1,
92 b: 2,
93 })
94β
95 expect(Object.keys(ref)).toEqual(['a', 'b'])
96β
97 delete ref.b
98 expect(Reflect.ownKeys(ref)).toEqual(['a'])
99β
100 update({})
101 expect(Object.keys(ref)).toEqual([])
102β
103 update({ a: 1, b: 2 })
104 expect(Object.keys(ref)).toEqual(['a', 'b'])
105 })
106β
107 it('should correctly trap calls to `getOwnPropertyDescriptor`', () => {
108 const [ref, update] = createRawRef<Record<string, number>>({
109 a: 1,
110 b: 2,
111 })
112β
113 expect(Reflect.getOwnPropertyDescriptor(ref, 'b')).toEqual({
114 configurable: true,
115 enumerable: true,
116 get: expect.any(Function),
117 set: expect.any(Function),
118 })
119β
120 delete ref.b
121 expect(Reflect.getOwnPropertyDescriptor(ref, 'b')).toEqual(undefined)
122β
123 update({})
124 expect(Reflect.getOwnPropertyDescriptor(ref, 'a')).toEqual(undefined)
125β
126 update({ a: 1, b: 2 })
127 expect(Reflect.getOwnPropertyDescriptor(ref, 'a')).toEqual({
128 configurable: true,
129 enumerable: true,
130 get: expect.any(Function),
131 set: expect.any(Function),
132 })
133 expect(Reflect.getOwnPropertyDescriptor(ref, 'b')).toEqual({
134 configurable: true,
135 enumerable: true,
136 get: expect.any(Function),
137 set: expect.any(Function),
138 })
139 })
140β
141 it('should lazily access values when using `update`', () => {
142 let aAccessed = false
143 let bAccessed = false
144 const [ref, update] = createRawRef({
145 get a() {
146 aAccessed = true
147 return 1
148 },
149 get b() {
150 bAccessed = true
151 return 2
152 },
153 })
154β
155 expect(aAccessed).toBe(false)
156 expect(bAccessed).toBe(false)
157β
158 expect(ref.a).toBe(1)
159β
160 expect(aAccessed).toBe(true)
161 expect(bAccessed).toBe(false)
162β
163 aAccessed = false
164 bAccessed = false
165β
166 update({
167 get a() {
168 aAccessed = true
169 return 2
170 },
171 get b() {
172 bAccessed = true
173 return 3
174 },
175 })
176β
177 expect(aAccessed).toBe(false)
178 expect(bAccessed).toBe(false)
179β
180 expect(ref.a).toBe(2)
181β
182 expect(aAccessed).toBe(true)
183 expect(bAccessed).toBe(false)
184 })
185β
186 it('should handle arrays', () => {
187 const [ref, update] = createRawRef([1, 2, 3])
188β
189 expect(ref).toEqual([1, 2, 3])
190β
191 ref[0] = 4
192 expect(ref).toEqual([4, 2, 3])
193β
194 update([5, 6])
195 expect(ref).toEqual([5, 6])
196β
197 update([7, 8, 9])
198 expect(ref).toEqual([7, 8, 9])
199 })
200β
201 it('should behave like a regular object when not using `update`', () => {
202 const [ref] = createRawRef<Record<string, unknown>>({ a: 1, b: 2 })
203β
204 expect(ref).toEqual({ a: 1, b: 2 })
205β
206 ref.a = 3
207 expect(ref).toEqual({ a: 3, b: 2 })
208β
209 ref.b = 4
210 expect(ref).toEqual({ a: 3, b: 4 })
211β
212 ref.c = 5
213 expect(ref).toEqual({ a: 3, b: 4, c: 5 })
214β
215 ref.fn = () => 6
216 expect(ref).toEqual({ a: 3, b: 4, c: 5, fn: expect.any(Function) })
217 expect((ref.fn as () => number)()).toBe(6)
218 })
219})