From 0febecb979efe60d70bb98889c3cf5c25c70cf28 Mon Sep 17 00:00:00 2001 From: vanilla-wave Date: Wed, 25 Dec 2024 17:58:20 +0100 Subject: [PATCH] feat: add customDefaultState option --- src/controller.ts | 6 ++++-- src/tests/controller.test.ts | 40 ++++++++++++++++++++++++++++++++++++ src/tests/utils.ts | 12 +++++++++-- src/types.ts | 1 + 4 files changed, 55 insertions(+), 4 deletions(-) diff --git a/src/controller.ts b/src/controller.ts index b6120d7..ee0894a 100644 --- a/src/controller.ts +++ b/src/controller.ts @@ -19,13 +19,14 @@ let instanceCounter = 0; const defaultLoggerOptions = { context: 'Onboarding', }; -const getDefaultBaseState = (): BaseState => ({ +const base = { availablePresets: [], activePresets: [], suggestedPresets: [], wizardState: 'hidden' as const, enabled: false, -}); +}; +const getDefaultBaseState = (): BaseState => base; const getDefaultProgressState = () => ({ presetPassedSteps: {}, @@ -88,6 +89,7 @@ export class Controller { + it('can use empty custom state', () => { + const options = getOptions(); + // @ts-ignore + options.baseState = {}; + options.customDefaultState = {}; + + const controller = new Controller(options); + expect(controller.state.base).toEqual({ + wizardState: 'hidden', + enabled: false, + activePresets: [], + availablePresets: [], + suggestedPresets: [], + }); + }); + + it('can apply custom state', () => { + const options = getOptions(); + options.customDefaultState = { + wizardState: 'visible', + enabled: true, + }; + + const controller = new Controller(options); + expect(controller.state.base.wizardState).toBe('visible'); + expect(controller.state.base.enabled).toBe(true); + }); + + it('should applies before saved value', () => { + const options = getOptions({wizardState: 'collapsed'}); + options.customDefaultState = { + wizardState: 'visible', + }; + + const controller = new Controller(options); + expect(controller.state.base.wizardState).toBe('collapsed'); + }); +}); diff --git a/src/tests/utils.ts b/src/tests/utils.ts index 85a0d1f..adb98ab 100644 --- a/src/tests/utils.ts +++ b/src/tests/utils.ts @@ -1,4 +1,11 @@ -import {BaseState, CombinedPreset, OnboardingPlugin, PresetStep, ProgressState} from '../types'; +import { + BaseState, + CombinedPreset, + InitOptions, + OnboardingPlugin, + PresetStep, + ProgressState, +} from '../types'; import {PromoPresetsPlugin} from '../plugins/promo-presets'; export const getOptions = ( @@ -65,7 +72,8 @@ export const getOptions = ( }, }, plugins: [] as OnboardingPlugin[], - }; + customDefaultState: {} as Partial, + } satisfies InitOptions; }; export const getOptionsWithCombined = ( diff --git a/src/types.ts b/src/types.ts index a378749..163f796 100644 --- a/src/types.ts +++ b/src/types.ts @@ -134,6 +134,7 @@ export type InitOptions) => void; logger?: LoggerOptions; debugMode?: boolean; + customDefaultState?: Partial; plugins?: OnboardingPlugin[]; hooks?: { [K in keyof EventsMap]?: (