Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

containers.svelte.test.ts

246 lines6.2 KB

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​
80 delete ref.b
81 expect('b' in ref).toBe(false)
82​
83 update({})
84 expect('a' in ref).toBe(false)
85​
86 update({ a: 1, b: 2 })
87 expect('b' in ref).toBe(true)
88 expect('a' in ref).toBe(true)
89 }),
90 )
91​
92 it('should correctly trap calls to `ownKeys`', () => {
93 const [ref, update] = createRawRef<Record<string, number>>({
94 a: 1,
95 b: 2,
96 })
97​
98 expect(Object.keys(ref)).toEqual(['a', 'b'])
99​
100 delete ref.b
101 expect(Reflect.ownKeys(ref)).toEqual(['a'])
102​
103 update({})
104 expect(Object.keys(ref)).toEqual([])
105​
106 update({ a: 1, b: 2 })
107 expect(Object.keys(ref)).toEqual(['a', 'b'])
108 })
109​
110 it('should correctly trap calls to `getOwnPropertyDescriptor`', () => {
111 const [ref, update] = createRawRef<Record<string, number>>({
112 a: 1,
113 b: 2,
114 })
115​
116 expect(Reflect.getOwnPropertyDescriptor(ref, 'b')).toEqual({
117 configurable: true,
118 enumerable: true,
119 get: expect.any(Function),
120 set: expect.any(Function),
121 })
122​
123 delete ref.b
124 expect(Reflect.getOwnPropertyDescriptor(ref, 'b')).toEqual(undefined)
125​
126 update({})
127 expect(Reflect.getOwnPropertyDescriptor(ref, 'a')).toEqual(undefined)
128​
129 update({ a: 1, b: 2 })
130 expect(Reflect.getOwnPropertyDescriptor(ref, 'a')).toEqual({
131 configurable: true,
132 enumerable: true,
133 get: expect.any(Function),
134 set: expect.any(Function),
135 })
136 expect(Reflect.getOwnPropertyDescriptor(ref, 'b')).toEqual({
137 configurable: true,
138 enumerable: true,
139 get: expect.any(Function),
140 set: expect.any(Function),
141 })
142 })
143​
144 it('should lazily access values when using `update`', () => {
145 let aAccessed = false
146 let bAccessed = false
147 const [ref, update] = createRawRef({
148 get a() {
149 aAccessed = true
150 return 1
151 },
152 get b() {
153 bAccessed = true
154 return 2
155 },
156 })
157​
158 expect(aAccessed).toBe(false)
159 expect(bAccessed).toBe(false)
160​
161 expect(ref.a).toBe(1)
162​
163 expect(aAccessed).toBe(true)
164 expect(bAccessed).toBe(false)
165​
166 aAccessed = false
167 bAccessed = false
168​
169 update({
170 get a() {
171 aAccessed = true
172 return 2
173 },
174 get b() {
175 bAccessed = true
176 return 3
177 },
178 })
179 expect(aAccessed).toBe(false)
180 expect(bAccessed).toBe(false)
181​
182 expect(ref.a).toBe(2)
183​
184 expect(aAccessed).toBe(true)
185 expect(bAccessed).toBe(false)
186 })
187​
188 it('should handle arrays', () => {
189 const [ref, update] = createRawRef([1, 2, 3])
190​
191 expect(ref).toEqual([1, 2, 3])
192​
193 ref[0] = 4
194 expect(ref).toEqual([4, 2, 3])
195​
196 update([5, 6])
197 expect(ref).toEqual([5, 6])
198​
199 update([7, 8, 9])
200 expect(ref).toEqual([7, 8, 9])
201 })
202​
203 it('should return `false` when deleting a property that does not exist', () => {
204 const [ref] = createRawRef<Record<string, number>>({ a: 1, b: 2 })
205​
206 expect(Reflect.deleteProperty(ref, 'c')).toBe(false)
207 expect(ref).toEqual({ a: 1, b: 2 })
208 })
209​
210 it('should handle shrinking an array by more than one entry at once', () => {
211 // Regression for #10341: createQueries crashed with
212 // `TypeError: can't delete property 'N': proxy deleteProperty handler returned false`
213 // when two or more items were removed from the reactive array in a single update.
214 const [ref, update] = createRawRef([1, 2, 3, 4, 5])
215​
216 expect(ref).toEqual([1, 2, 3, 4, 5])
217​
218 update([1, 2])
219 expect(ref).toEqual([1, 2])
220​
221 update([1, 2, 3, 4])
222 expect(ref).toEqual([1, 2, 3, 4])
223​
224 update([])
225 expect(ref).toEqual([])
226 })
227​
228 it('should behave like a regular object when not using `update`', () => {
229 const [ref] = createRawRef<Record<string, unknown>>({ a: 1, b: 2 })
230​
231 expect(ref).toEqual({ a: 1, b: 2 })
232​
233 ref.a = 3
234 expect(ref).toEqual({ a: 3, b: 2 })
235​
236 ref.b = 4
237 expect(ref).toEqual({ a: 3, b: 4 })
238​
239 ref.c = 5
240 expect(ref).toEqual({ a: 3, b: 4, c: 5 })
241​
242 ref.fn = () => 6
243 expect(ref).toEqual({ a: 3, b: 4, c: 5, fn: expect.any(Function) })
244 expect((ref.fn as () => number)()).toBe(6)
245 })
246})