Skip to content

Commit

Permalink
test: unused describe()
Browse files Browse the repository at this point in the history
  • Loading branch information
johnsoncodehk committed Oct 7, 2024
1 parent bbade5e commit 8da5dac
Showing 1 changed file with 95 additions and 97 deletions.
192 changes: 95 additions & 97 deletions tests/computedArray.spec.ts
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);
});

0 comments on commit 8da5dac

Please sign in to comment.