Skip to content

Commit

Permalink
feat(onboarding): add customDefaultState option
Browse files Browse the repository at this point in the history
  • Loading branch information
vanilla-wave committed Dec 25, 2024
1 parent d170665 commit 5cdb0aa
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 4 deletions.
6 changes: 4 additions & 2 deletions src/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {},
Expand Down Expand Up @@ -88,6 +89,7 @@ export class Controller<HintParams, Presets extends string, Steps extends string
this.state = {
base: {
...getDefaultBaseState(),
...options.customDefaultState,
...options.baseState,
},
};
Expand Down
40 changes: 40 additions & 0 deletions src/tests/controller.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -532,3 +532,43 @@ describe('goNextStep and goNextStep', function () {
});
});
});

describe('custom default state', () => {
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');
});
});
12 changes: 10 additions & 2 deletions src/tests/utils.ts
Original file line number Diff line number Diff line change
@@ -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 = (
Expand Down Expand Up @@ -65,7 +72,8 @@ export const getOptions = (
},
},
plugins: [] as OnboardingPlugin[],
};
customDefaultState: {} as Partial<BaseState>,
} satisfies InitOptions<any, any, any>;
};

export const getOptionsWithCombined = (
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ export type InitOptions<HintParams, Presets extends string, Steps extends string
showHint?: (params: ShowHintParams<HintParams, Presets, Steps>) => void;
logger?: LoggerOptions;
debugMode?: boolean;
customDefaultState?: Partial<BaseState>;
plugins?: OnboardingPlugin[];
hooks?: {
[K in keyof EventsMap<HintParams, Presets, Steps>]?: (
Expand Down

0 comments on commit 5cdb0aa

Please sign in to comment.