-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bbade5e
commit 8da5dac
Showing
1 changed file
with
95 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,114 +1,112 @@ | ||
import { describe, expect, it, vi } from 'vitest'; | ||
import { expect, test, vi } from 'vitest'; | ||
import { effect, signal } from '..'; | ||
import { computedArray } from '../unstable/computedArray'; | ||
|
||
describe('computedArray', () => { | ||
it('should get updated item value', () => { | ||
const src = signal([1]); | ||
const arr = computedArray(src, item => { | ||
return () => item.get() + 1; | ||
}); | ||
expect(arr[0]).toBe(2); | ||
test('should get updated item value', () => { | ||
const src = signal([1]); | ||
const arr = computedArray(src, item => { | ||
return () => item.get() + 1; | ||
}); | ||
expect(arr[0]).toBe(2); | ||
}); | ||
|
||
it('should watch item value change', () => { | ||
const spy = vi.fn(); | ||
const src = signal([1]); | ||
const arr = computedArray(src, item => { | ||
return () => item.get() + 1; | ||
}); | ||
effect(() => { | ||
spy(); | ||
arr[0]; | ||
}); | ||
expect(spy).toHaveBeenCalledTimes(1); | ||
src.set([2]); | ||
expect(spy).toHaveBeenCalledTimes(2); | ||
test('should watch item value change', () => { | ||
const spy = vi.fn(); | ||
const src = signal([1]); | ||
const arr = computedArray(src, item => { | ||
return () => item.get() + 1; | ||
}); | ||
effect(() => { | ||
spy(); | ||
arr[0]; | ||
}); | ||
expect(spy).toHaveBeenCalledTimes(1); | ||
src.set([2]); | ||
expect(spy).toHaveBeenCalledTimes(2); | ||
}); | ||
|
||
it('should not trigger if item value did not change', () => { | ||
const spy = vi.fn(); | ||
const src = signal([1]); | ||
const arr = computedArray(src, item => { | ||
return () => item.get() + 1; | ||
}); | ||
effect(() => { | ||
spy(); | ||
arr[0]; | ||
}); | ||
expect(spy).toHaveBeenCalledTimes(1); | ||
src.set([1]); | ||
expect(spy).toHaveBeenCalledTimes(1); | ||
test('should not trigger if item value did not change', () => { | ||
const spy = vi.fn(); | ||
const src = signal([1]); | ||
const arr = computedArray(src, item => { | ||
return () => item.get() + 1; | ||
}); | ||
effect(() => { | ||
spy(); | ||
arr[0]; | ||
}); | ||
expect(spy).toHaveBeenCalledTimes(1); | ||
src.set([1]); | ||
expect(spy).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
test('should not trigger first item computed if source item did not change', () => { | ||
const spy = vi.fn(); | ||
const src = signal([1]); | ||
const arr = computedArray(src, (item, i) => { | ||
return () => { | ||
if (i === 0) { | ||
spy(); | ||
} | ||
return item.get() + 1; | ||
}; | ||
}); | ||
effect(() => arr[0]); | ||
expect(spy).toHaveBeenCalledTimes(1); | ||
src.set([1, 2]); | ||
expect(spy).toHaveBeenCalledTimes(1); | ||
src.set([2, 2, 3]); | ||
expect(spy).toHaveBeenCalledTimes(2); | ||
}); | ||
|
||
it('should not trigger first item computed if source item did not change', () => { | ||
const spy = vi.fn(); | ||
const src = signal([1]); | ||
const arr = computedArray(src, (item, i) => { | ||
return () => { | ||
if (i === 0) { | ||
spy(); | ||
} | ||
return item.get() + 1; | ||
}; | ||
}); | ||
effect(() => arr[0]); | ||
expect(spy).toHaveBeenCalledTimes(1); | ||
src.set([1, 2]); | ||
expect(spy).toHaveBeenCalledTimes(1); | ||
src.set([2, 2, 3]); | ||
expect(spy).toHaveBeenCalledTimes(2); | ||
test('should watch length change', () => { | ||
const spy = vi.fn(); | ||
const src = signal([1]); | ||
const arr = computedArray(src, item => { | ||
return () => item.get() + 1; | ||
}); | ||
effect(() => { | ||
spy(); | ||
arr.length; | ||
}); | ||
expect(spy).toHaveBeenCalledTimes(1); | ||
src.set([2]); | ||
expect(spy).toHaveBeenCalledTimes(1); | ||
src.set([2, 3]); | ||
expect(spy).toHaveBeenCalledTimes(2); | ||
}); | ||
|
||
it('should watch length change', () => { | ||
const spy = vi.fn(); | ||
const src = signal([1]); | ||
const arr = computedArray(src, item => { | ||
return () => item.get() + 1; | ||
}); | ||
effect(() => { | ||
spy(); | ||
arr.length; | ||
}); | ||
expect(spy).toHaveBeenCalledTimes(1); | ||
src.set([2]); | ||
expect(spy).toHaveBeenCalledTimes(1); | ||
src.set([2, 3]); | ||
expect(spy).toHaveBeenCalledTimes(2); | ||
test('should watch item remove', () => { | ||
const spy = vi.fn(); | ||
const src = signal([1, 2]); | ||
const arr = computedArray(src, item => { | ||
return () => item.get() + 1; | ||
}); | ||
effect(() => { | ||
spy(); | ||
arr[0]; | ||
}); | ||
expect(spy).toHaveBeenCalledTimes(1); | ||
src.set([1]); | ||
expect(spy).toHaveBeenCalledTimes(1); | ||
src.set([]); | ||
expect(spy).toHaveBeenCalledTimes(2); | ||
}); | ||
|
||
it('should watch item remove', () => { | ||
const spy = vi.fn(); | ||
const src = signal([1, 2]); | ||
const arr = computedArray(src, item => { | ||
return () => item.get() + 1; | ||
}); | ||
effect(() => { | ||
test('should only trigger access items', () => { | ||
const spy = vi.fn(); | ||
const src = signal([1, 2, 3, 4]); | ||
const arr = computedArray(src, item => { | ||
return () => { | ||
spy(); | ||
arr[0]; | ||
}); | ||
expect(spy).toHaveBeenCalledTimes(1); | ||
src.set([1]); | ||
expect(spy).toHaveBeenCalledTimes(1); | ||
src.set([]); | ||
expect(spy).toHaveBeenCalledTimes(2); | ||
return item.get() + 1; | ||
}; | ||
}); | ||
|
||
it('should only trigger access items', () => { | ||
const spy = vi.fn(); | ||
const src = signal([1, 2, 3, 4]); | ||
const arr = computedArray(src, item => { | ||
return () => { | ||
spy(); | ||
return item.get() + 1; | ||
}; | ||
}); | ||
effect(() => { | ||
arr[0]; | ||
arr[1]; | ||
}); | ||
expect(spy).toHaveBeenCalledTimes(2); | ||
src.set([2, 3, 4, 5]); | ||
expect(spy).toHaveBeenCalledTimes(4); | ||
effect(() => { | ||
arr[0]; | ||
arr[1]; | ||
}); | ||
expect(spy).toHaveBeenCalledTimes(2); | ||
src.set([2, 3, 4, 5]); | ||
expect(spy).toHaveBeenCalledTimes(4); | ||
}); |