Skip to content

Commit

Permalink
refactor test (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
dai-shi authored Apr 21, 2024
1 parent 354385c commit e3b6d41
Show file tree
Hide file tree
Showing 3 changed files with 215 additions and 251 deletions.
93 changes: 41 additions & 52 deletions tests/01_basic.spec.tsx
Original file line number Diff line number Diff line change
@@ -1,59 +1,48 @@
import { describe, expect, it } from 'vitest';
import { expect, test } from 'vitest';
import { create } from 'zustand';
import { createSlice, withSlices } from 'zustand-slices';

describe('basic spec', () => {
it('should export functions', () => {
expect(createSlice).toBeDefined();
expect(withSlices).toBeDefined();
});
describe('createSlice', () => {
it('should return a slice config', () => {
const slice = createSlice({
name: 'counter',
value: 0,
actions: {
increment: () => (prev) => prev + 1,
},
});
// returns the input
expect(createSlice(slice)).toBe(slice);
});
});
describe('withSlices', () => {
it('should combine slices and nest state', () => {
const countSlice = createSlice({
name: 'count',
value: 0,
actions: {
inc: () => (prev) => prev + 1,
reset: () => () => 0,
},
});

const textSlice = createSlice({
name: 'text',
value: 'Hello',
actions: {
updateText: (newText: string) => () => newText,
reset: () => () => 'Hello',
},
});

const combinedConfig = withSlices(countSlice, textSlice);

expect(combinedConfig).toBeInstanceOf(Function);

const store = create(combinedConfig);

const state = store.getState();
test('should export functions', () => {
expect(createSlice).toBeDefined();
expect(withSlices).toBeDefined();
});

expect(state.count).toBe(countSlice.value);
expect(state.text).toBe(textSlice.value);
test('createSlice', () => {
const slice = createSlice({
name: 'counter',
value: 0,
actions: {
increment: () => (prev) => prev + 1,
},
});
// returns the input
expect(createSlice(slice)).toBe(slice);
});

expect(state.inc).toBeInstanceOf(Function);
expect(state.reset).toBeInstanceOf(Function);
expect(state.updateText).toBeInstanceOf(Function);
});
test('withSlices', () => {
const countSlice = createSlice({
name: 'count',
value: 0,
actions: {
inc: () => (prev) => prev + 1,
reset: () => () => 0,
},
});
const textSlice = createSlice({
name: 'text',
value: 'Hello',
actions: {
updateText: (newText: string) => () => newText,
reset: () => () => 'Hello',
},
});
const combinedConfig = withSlices(countSlice, textSlice);
expect(combinedConfig).toBeInstanceOf(Function);
const store = create(combinedConfig);
const state = store.getState();
expect(state.count).toBe(countSlice.value);
expect(state.text).toBe(textSlice.value);
expect(state.inc).toBeInstanceOf(Function);
expect(state.reset).toBeInstanceOf(Function);
expect(state.updateText).toBeInstanceOf(Function);
});
250 changes: 122 additions & 128 deletions tests/02_type.spec.tsx
Original file line number Diff line number Diff line change
@@ -1,149 +1,143 @@
import { describe, it } from 'vitest';
import { test } from 'vitest';
import { expectType } from 'ts-expect';
import type { TypeEqual } from 'ts-expect';

import { createSlice, withSlices } from 'zustand-slices';

describe('slice type', () => {
it('single slice', () => {
const countSlice = createSlice({
name: 'count',
value: 0,
actions: {
inc: () => (prev) => prev + 1,
},
});
expectType<
TypeEqual<
{
name: 'count';
value: number;
actions: {
inc: () => (prev: number) => number;
};
},
typeof countSlice
>
>(true);
test('slice type: single slice', () => {
const countSlice = createSlice({
name: 'count',
value: 0,
actions: {
inc: () => (prev) => prev + 1,
},
});
expectType<
TypeEqual<
{
name: 'count';
value: number;
actions: {
inc: () => (prev: number) => number;
};
},
typeof countSlice
>
>(true);
});

describe('name collisions', () => {
it('same slice names', () => {
const countSlice = createSlice({
name: 'count',
value: 0,
actions: {
inc: () => (prev) => prev + 1,
},
});
const anotherCountSlice = createSlice({
name: 'count',
value: 0,
actions: {
inc: () => (prev) => prev + 1,
},
});
expectType<never>(withSlices(countSlice, anotherCountSlice));
test('name collisions: same slice names', () => {
const countSlice = createSlice({
name: 'count',
value: 0,
actions: {
inc: () => (prev) => prev + 1,
},
});
const anotherCountSlice = createSlice({
name: 'count',
value: 0,
actions: {
inc: () => (prev) => prev + 1,
},
});
expectType<never>(withSlices(countSlice, anotherCountSlice));
});

it('slice name and action name', () => {
const countSlice = createSlice({
name: 'count',
value: 0,
actions: {
inc: () => (prev) => prev + 1,
},
});
const anotherCountSlice = createSlice({
name: 'anotherCount',
value: 0,
actions: {
count: () => (prev) => prev + 1,
},
});
expectType<never>(withSlices(countSlice, anotherCountSlice));
test('name collisions: slice name and action name', () => {
const countSlice = createSlice({
name: 'count',
value: 0,
actions: {
inc: () => (prev) => prev + 1,
},
});
const anotherCountSlice = createSlice({
name: 'anotherCount',
value: 0,
actions: {
count: () => (prev) => prev + 1,
},
});
expectType<never>(withSlices(countSlice, anotherCountSlice));
});

it('slice name and action name (overlapping case)', () => {
const countSlice = createSlice({
name: 'count',
value: 0,
actions: {
inc: () => (prev) => prev + 1,
},
});
const anotherCountSlice = createSlice({
name: 'anotherCount',
value: 0,
actions: {
count: () => (prev) => prev + 1,
dec: () => (prev) => prev - 1,
},
});
expectType<never>(withSlices(countSlice, anotherCountSlice));
test('name collisions: slice name and action name (overlapping case)', () => {
const countSlice = createSlice({
name: 'count',
value: 0,
actions: {
inc: () => (prev) => prev + 1,
},
});
const anotherCountSlice = createSlice({
name: 'anotherCount',
value: 0,
actions: {
count: () => (prev) => prev + 1,
dec: () => (prev) => prev - 1,
},
});
expectType<never>(withSlices(countSlice, anotherCountSlice));
});

describe('args collisions', () => {
it('different args', () => {
const countSlice = createSlice({
name: 'count',
value: 0,
actions: {
inc: (s: string) => (prev) => prev + (s ? 2 : 1),
},
});
const anotherCountSlice = createSlice({
name: 'anotherCount',
value: 0,
actions: {
inc: (n: number) => (prev) => prev + n,
},
});
expectType<never>(withSlices(countSlice, anotherCountSlice));
expectType<never>(withSlices(anotherCountSlice, countSlice));
test('args collisions: different args', () => {
const countSlice = createSlice({
name: 'count',
value: 0,
actions: {
inc: (s: string) => (prev) => prev + (s ? 2 : 1),
},
});
const anotherCountSlice = createSlice({
name: 'anotherCount',
value: 0,
actions: {
inc: (n: number) => (prev) => prev + n,
},
});
expectType<never>(withSlices(countSlice, anotherCountSlice));
expectType<never>(withSlices(anotherCountSlice, countSlice));
});

it('same args', () => {
const countSlice = createSlice({
name: 'count',
value: 0,
actions: {
inc: () => (prev) => prev + 1,
},
});
const anotherCountSlice = createSlice({
name: 'anotherCount',
value: 0,
actions: {
anotherInc: (n: number) => (prev) => prev + n,
},
});
expectType<(...args: never[]) => unknown>(
withSlices(countSlice, anotherCountSlice),
);
expectType<(...args: never[]) => unknown>(
withSlices(anotherCountSlice, countSlice),
);
test('args collisions: same args', () => {
const countSlice = createSlice({
name: 'count',
value: 0,
actions: {
inc: () => (prev) => prev + 1,
},
});
const anotherCountSlice = createSlice({
name: 'anotherCount',
value: 0,
actions: {
anotherInc: (n: number) => (prev) => prev + n,
},
});
expectType<(...args: never[]) => unknown>(
withSlices(countSlice, anotherCountSlice),
);
expectType<(...args: never[]) => unknown>(
withSlices(anotherCountSlice, countSlice),
);
});

it('overload case', () => {
const countSlice = createSlice({
name: 'count',
value: 0,
actions: {
inc: () => (prev) => prev + 1,
},
});
const anotherCountSlice = createSlice({
name: 'anotherCount',
value: 0,
actions: {
inc: (n: number) => (prev) => prev + n,
},
});
expectType<never>(withSlices(countSlice, anotherCountSlice));
expectType<never>(withSlices(anotherCountSlice, countSlice));
test('args collisions: overload case', () => {
const countSlice = createSlice({
name: 'count',
value: 0,
actions: {
inc: () => (prev) => prev + 1,
},
});
const anotherCountSlice = createSlice({
name: 'anotherCount',
value: 0,
actions: {
inc: (n: number) => (prev) => prev + n,
},
});
expectType<never>(withSlices(countSlice, anotherCountSlice));
expectType<never>(withSlices(anotherCountSlice, countSlice));
});
Loading

0 comments on commit e3b6d41

Please sign in to comment.