-
Notifications
You must be signed in to change notification settings - Fork 8
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
Showing
3 changed files
with
215 additions
and
251 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,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); | ||
}); |
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,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)); | ||
}); |
Oops, something went wrong.