|
| 1 | +import { describe, expect, it } from 'vitest'; |
| 2 | +import { Ref } from '@w5s/core'; |
| 3 | +import { useConfiguration } from './useConfiguration.js'; |
| 4 | + |
| 5 | +describe(useConfiguration, () => { |
| 6 | + it('should initialize configuration and expose initial', () => { |
| 7 | + const store = Ref({}); |
| 8 | + const meta = { name: 'app' } as const; |
| 9 | + const initial = { mode: 'light', retries: 2 }; |
| 10 | + |
| 11 | + const configuration = useConfiguration(meta, initial, store); |
| 12 | + |
| 13 | + expect(configuration.current).toEqual(initial); |
| 14 | + expect(configuration.initial).toEqual(initial); |
| 15 | + expect(store.current).toEqual({ |
| 16 | + app: { |
| 17 | + configuration: initial, |
| 18 | + }, |
| 19 | + }); |
| 20 | + }); |
| 21 | + |
| 22 | + it('should support get, update, and modify', () => { |
| 23 | + const store = Ref({}); |
| 24 | + const meta = { name: 'app' } as const; |
| 25 | + const initial = { mode: 'light', retries: 2 }; |
| 26 | + |
| 27 | + const configuration = useConfiguration(meta, initial, store); |
| 28 | + |
| 29 | + expect(configuration.get('mode')).toBe('light'); |
| 30 | + |
| 31 | + const previous = configuration.current; |
| 32 | + configuration.update({ retries: 3 }); |
| 33 | + expect(configuration.current).toEqual({ mode: 'light', retries: 3 }); |
| 34 | + expect(configuration.current).not.toBe(previous); |
| 35 | + |
| 36 | + configuration.modify((current) => ({ ...current, mode: 'dark' })); |
| 37 | + expect(configuration.current).toEqual({ mode: 'dark', retries: 3 }); |
| 38 | + }); |
| 39 | + |
| 40 | + it('should not override existing configuration in store', () => { |
| 41 | + const store = Ref({ |
| 42 | + app: { |
| 43 | + configuration: { mode: 'dark', retries: 5 }, |
| 44 | + other: true, |
| 45 | + }, |
| 46 | + }); |
| 47 | + const meta = { name: 'app' } as const; |
| 48 | + const initial = { mode: 'light', retries: 1 }; |
| 49 | + |
| 50 | + const configuration = useConfiguration(meta, initial, store); |
| 51 | + |
| 52 | + expect(configuration.current).toEqual({ mode: 'dark', retries: 5 }); |
| 53 | + expect(store.current.app).toEqual({ |
| 54 | + configuration: { mode: 'dark', retries: 5 }, |
| 55 | + other: true, |
| 56 | + }); |
| 57 | + }); |
| 58 | +}); |
0 commit comments