Skip to content

Commit

Permalink
Merge pull request #154 from gravity-ui/sync-progress-load
Browse files Browse the repository at this point in the history
feat(onboarding): allow synchronous progress state init
  • Loading branch information
vanilla-wave authored Feb 4, 2025
2 parents 32ff354 + e423daa commit 4232a1c
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 11 deletions.
28 changes: 18 additions & 10 deletions src/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -570,27 +570,35 @@ export class Controller<HintParams, Presets extends string, Steps extends string
return;
}

const progressStateFromOptions = this.options.progressState;
if (progressStateFromOptions) {
this.initProgressState(progressStateFromOptions);
return;
}

if (!this.progressLoadingPromise) {
this.progressLoadingPromise = this.options.getProgressState();
}

this.logger.debug('Loading onboarding progress data');
try {
const newProgressState = await this.progressLoadingPromise;

this.state.progress = {
...getDefaultProgressState(),
...newProgressState,
};
this.status = 'active';
this.emitStateChange();

this.logger.debug('Onboarding progress data loaded');
this.initProgressState(await this.progressLoadingPromise);
} catch (e) {
this.logger.error('progress data loading error');
}
}

initProgressState(state: Partial<ProgressState>) {
this.state.progress = {
...getDefaultProgressState(),
...state,
};
this.status = 'active';
this.emitStateChange();

this.logger.debug('Onboarding progress data initialized');
}

async resetToDefaultState() {
this.state = {
base: this.getDefaultBaseState(),
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 @@ -628,3 +628,43 @@ describe('custom default state', () => {
expect(controller.state.base.enabled).toBe(true);
});
});

describe('progressState init', function () {
it('init -> call getProgressState ', async function () {
const options = getOptions();

const controller = new Controller(options);
await controller.ensureRunning();

expect(options.getProgressState).toHaveBeenCalled();
});

it('has progressState in options -> dont call getProgressState ', async function () {
const options = getOptions();
// @ts-ignore
options.progressState = {
presetPassedSteps: {},
finishedPresets: [],
};

const controller = new Controller(options);
await controller.ensureRunning();

expect(options.getProgressState).not.toHaveBeenCalled();
});

it('has progressState in options -> use progressState value', async function () {
const options = getOptions();
const progressState = {
presetPassedSteps: {},
finishedPresets: [],
};
// @ts-ignore
options.progressState = progressState;

const controller = new Controller(options);
await controller.ensureRunning();

expect(controller.state.progress).toEqual(progressState);
});
});
2 changes: 1 addition & 1 deletion src/tests/steps.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ describe('pass step', function () {
expect(mock).toHaveBeenCalled();
});

it('passedStep -> dont call hook', async function () {
it('passed step -> dont call hook', async function () {
const options = getOptions();
const mock = jest.fn();

Expand Down
1 change: 1 addition & 0 deletions src/tests/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ export const getOptions = (
},
plugins: [] as OnboardingPlugin[],
customDefaultState: {} as Partial<BaseState>,
progressState: undefined,
} satisfies InitOptions<any, any, any>;
};

Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ export type ShowHintParams<HintParams, Presets extends string, Steps extends str
export type InitOptions<HintParams, Presets extends string, Steps extends string> = {
config: InitConfig<HintParams, Presets, Steps>;
baseState: Partial<BaseState> | undefined;
progressState?: Partial<ProgressState>;
getProgressState: () => Promise<Partial<ProgressState>>;
onSave: {
state: (state: BaseState) => Promise<any>;
Expand Down

0 comments on commit 4232a1c

Please sign in to comment.