From 7bb988aa541b8841f8be8ee6aab7da9873e91c86 Mon Sep 17 00:00:00 2001 From: Yanick Champoux Date: Thu, 2 Mar 2023 11:42:33 -0500 Subject: [PATCH] initialState --- src/Updux.ts | 18 +++++++++++++++++- src/initial.test.ts | 15 +++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 src/initial.test.ts diff --git a/src/Updux.ts b/src/Updux.ts index eeb5984..e77f598 100644 --- a/src/Updux.ts +++ b/src/Updux.ts @@ -1 +1,17 @@ -export default class Updux { } +export default class Updux> { + #local_initial: T_Local_State; + + constructor( + config: Partial<{ + initial: T_Local_State; + }>, + ) { + // TODO test that we default to {} + this.#local_initial = config.initial ?? ({} as T_Local_State); + } + + // TODO memoize? + get initial() { + return this.#local_initial; + } +} diff --git a/src/initial.test.ts b/src/initial.test.ts new file mode 100644 index 0000000..082a40e --- /dev/null +++ b/src/initial.test.ts @@ -0,0 +1,15 @@ +import Updux from './Updux.js'; + +test('default', () => { + const { initial } = new Updux({}); + + expect(initial).toBeTypeOf('object'); + expect(initial).toEqual({}); +}); + +test('number', () => { + const { initial } = new Updux({ initial: 3 }); + + expect(initial).toBeTypeOf('number'); + expect(initial).toEqual(3); +});