|
1 | | -import { state, createState } from '../../'; |
| 1 | +import { state, createState, Config } from '../../'; |
| 2 | + |
| 3 | +test('function overloads and type correctness', () => { |
| 4 | + const s1 = state('s1'); |
| 5 | + expect(s1.get().trim()).toBe('s1'); |
| 6 | + |
| 7 | + const s2 = state<string>(); |
| 8 | + expect(s2.get()?.trim()).toBeUndefined(); |
| 9 | + |
| 10 | + const s2ctx = state<string, number>(); |
| 11 | + expect(s2ctx.get()?.trim()).toBeUndefined(); |
| 12 | + s2ctx.set('context', 1); |
| 13 | + |
| 14 | + const s3 = state([] as string[]); |
| 15 | + expect(s3.get()[0]?.trim()).toBeUndefined(); |
| 16 | + |
| 17 | + const s4 = state([] as string[], (set, get) => ({ |
| 18 | + append: (newValue: string) => set([...get(), newValue]) |
| 19 | + })); |
| 20 | + s4.append('s4'); |
| 21 | + expect(s4.get()[0].trim()).toBe('s4'); |
| 22 | + |
| 23 | + // Custom actions must not override built-in state properties |
| 24 | + const s5 = state([] as string[], (set, get) => ({ |
| 25 | + append: (newValue: string) => set([...get(), newValue]), |
| 26 | + set: () => set([]) |
| 27 | + })); |
| 28 | + s5.append('tmp'); |
| 29 | + expect(s5.get()).toEqual(['tmp']); |
| 30 | + s5.set(['s5']); |
| 31 | + expect(s5.get()[0].trim()).toBe('s5'); |
| 32 | + |
| 33 | + const s6 = state<string[], { append: (newValue: string) => void }>([], (set, get) => ({ |
| 34 | + append: (newValue) => set([...get(), newValue]) |
| 35 | + })); |
| 36 | + s6.append('s6'); |
| 37 | + expect(s6.get()[0].trim()).toBe('s6'); |
| 38 | + |
| 39 | + const s6ctx = state<string[], { append: (newValue: string) => void }, number>([], (set, get) => ({ |
| 40 | + append: (newValue) => set([...get(), newValue]) |
| 41 | + })); |
| 42 | + s6ctx.append('s6'); |
| 43 | + expect(s6ctx.get()[0].trim()).toBe('s6'); |
| 44 | + s6ctx.set(['context'], 1); |
| 45 | + |
| 46 | + const s7 = state<string[]>([], null, { key: 's7' }); |
| 47 | + expect(s7.get()[0]?.trim()).toBeUndefined(); |
| 48 | + |
| 49 | + const s7ctx = state<string[], number>([], null, { key: 's7' }); |
| 50 | + expect(s7ctx.get()[0]?.trim()).toBeUndefined(); |
| 51 | + s7ctx.set(['context'], 1); |
| 52 | + |
| 53 | + const s8 = state<string | undefined>(undefined, undefined, { key: 's8' }); |
| 54 | + expect(s8.get()?.trim()).toBeUndefined(); |
| 55 | + |
| 56 | + const s8ctx = state<string | undefined, number>(undefined, undefined, { key: 's8' }); |
| 57 | + expect(s8ctx.get()?.trim()).toBeUndefined(); |
| 58 | + s8ctx.set('context', 1); |
| 59 | + |
| 60 | + const s9 = state<string[], { append: (newValue: string) => void }>( |
| 61 | + [], |
| 62 | + (set, get) => ({ |
| 63 | + append: (newValue) => set([...get(), newValue]) |
| 64 | + }), |
| 65 | + { key: 's9' } |
| 66 | + ); |
| 67 | + s9.append('s9'); |
| 68 | + expect(s9.get()[0].trim()).toBe('s9'); |
| 69 | + |
| 70 | + const s9ctx = state<string[], { append: (newValue: string) => void }, number>( |
| 71 | + [], |
| 72 | + (set, get) => ({ |
| 73 | + append: (newValue) => set([...get(), newValue]) |
| 74 | + }), |
| 75 | + { key: 's9' } |
| 76 | + ); |
| 77 | + s9ctx.append('s9'); |
| 78 | + expect(s9ctx.get()[0].trim()).toBe('s9'); |
| 79 | + s9ctx.set(['context'], 1); |
| 80 | + |
| 81 | + const s10 = state('s10', undefined); |
| 82 | + expect(s10.get().trim()).toBe('s10'); |
| 83 | +}); |
2 | 84 |
|
3 | 85 | test('state should notify listeners when updated', () => { |
4 | 86 | const listener = jest.fn(); |
@@ -66,7 +148,7 @@ test('state can bind actions', () => { |
66 | 148 |
|
67 | 149 | test('state can be enhanced with middleware', () => { |
68 | 150 | const middleware = jest.fn(); |
69 | | - const state = createState({ |
| 151 | + const state = createState<Config>({ |
70 | 152 | middleware: |
71 | 153 | ({ set, get }, config) => |
72 | 154 | (...arg) => { |
|
0 commit comments