Skip to content

Commit 3e11243

Browse files
committed
test(reactiveArray): add raw identity methods have been call times #11328
1 parent a07f1b2 commit 3e11243

File tree

1 file changed

+74
-0
lines changed

1 file changed

+74
-0
lines changed

packages/reactivity/__tests__/reactiveArray.spec.ts

+74
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,80 @@ describe('reactivity/reactive/Array', () => {
8888
expect(index).toBe(1)
8989
})
9090

91+
// only non-existent reactive will try to search by using its raw value
92+
describe('Array identity methods have been called times', () => {
93+
const identityMethods = ['includes', 'indexOf', 'lastIndexOf'] as const
94+
function instrumentArr(rawTarget: any[]) {
95+
identityMethods.forEach(key => {
96+
const spy = vi.fn(rawTarget[key] as any)
97+
rawTarget[key] = spy
98+
})
99+
}
100+
function searchValue(target: any[], ...args: unknown[]) {
101+
return identityMethods.map(key => (target[key] as any)(...args))
102+
}
103+
function unInstrumentArr(rawTarget: any[]) {
104+
identityMethods.forEach(key => {
105+
;(rawTarget[key] as any).mockClear()
106+
// relink to prototype method
107+
rawTarget[key] = Array.prototype[key] as any
108+
})
109+
}
110+
function expectHaveBeenCalledTimes(rawTarget: any[], times: number) {
111+
identityMethods.forEach(key => {
112+
expect(rawTarget[key]).toHaveBeenCalledTimes(times)
113+
})
114+
}
115+
116+
test('should be called once with a non-existent raw value', () => {
117+
const reactiveArr = reactive([])
118+
instrumentArr(toRaw(reactiveArr))
119+
const searchResult = searchValue(reactiveArr, {})
120+
121+
expectHaveBeenCalledTimes(toRaw(reactiveArr), 1)
122+
expect(searchResult).toStrictEqual([false, -1, -1])
123+
124+
unInstrumentArr(toRaw(reactiveArr))
125+
})
126+
127+
test('should be called once with an existent reactive value', () => {
128+
const existReactiveValue = reactive({})
129+
const reactiveArr = reactive([existReactiveValue, existReactiveValue])
130+
131+
instrumentArr(toRaw(reactiveArr))
132+
const searchResult = searchValue(reactiveArr, existReactiveValue)
133+
134+
expectHaveBeenCalledTimes(toRaw(reactiveArr), 1)
135+
expect(searchResult).toStrictEqual([true, 0, 1])
136+
137+
unInstrumentArr(toRaw(reactiveArr))
138+
})
139+
140+
test('should be called twice with a non-existent reactive value', () => {
141+
const reactiveArr = reactive([])
142+
instrumentArr(toRaw(reactiveArr))
143+
const searchResult = searchValue(reactiveArr, reactive({}))
144+
145+
expectHaveBeenCalledTimes(toRaw(reactiveArr), 2)
146+
expect(searchResult).toStrictEqual([false, -1, -1])
147+
148+
unInstrumentArr(toRaw(reactiveArr))
149+
})
150+
151+
test('should be called twice with a non-existent reactive value, but the raw value exists', () => {
152+
const existRaw = {}
153+
const reactiveArr = reactive([existRaw, existRaw])
154+
155+
instrumentArr(toRaw(reactiveArr))
156+
const searchResult = searchValue(reactiveArr, reactive(existRaw))
157+
158+
expectHaveBeenCalledTimes(toRaw(reactiveArr), 2)
159+
expect(searchResult).toStrictEqual([true, 0, 1])
160+
161+
unInstrumentArr(toRaw(reactiveArr))
162+
})
163+
})
164+
91165
test('delete on Array should not trigger length dependency', () => {
92166
const arr = reactive([1, 2, 3])
93167
const fn = vi.fn()

0 commit comments

Comments
 (0)