Skip to content

Commit

Permalink
fix(promo-manager): onboarding hint cancelled -> don't trigger promo
Browse files Browse the repository at this point in the history
  • Loading branch information
vanilla-wave committed Sep 12, 2024
1 parent 345b8f7 commit 43eb5dd
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 7 deletions.
17 changes: 12 additions & 5 deletions src/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,9 +185,10 @@ export class Controller<HintParams, Presets extends string, Steps extends string
await this.updateBaseState();
};

stepElementReached = async (stepData: Omit<ReachElementParams<Presets, Steps>, 'preset'>) => {
const {stepSlug, element} = stepData;

stepElementReached = async ({
stepSlug,
element,
}: Omit<ReachElementParams<Presets, Steps>, 'preset'>) => {
this.reachedElements.set(stepSlug, element);

const preset = this.findActivePresetWithStep(stepSlug);
Expand All @@ -196,11 +197,17 @@ export class Controller<HintParams, Presets extends string, Steps extends string
return;
}

await this.processElementAppearance({
const stepData = {
preset,
stepSlug,
element,
});
};

const shouldProcessAppearance = await this.events.emit('stepElementReached', {stepData});

if (shouldProcessAppearance) {
await this.processElementAppearance(stepData);
}
};

processElementAppearance = async (stepData: ReachElementParams<Presets, Steps>) => {
Expand Down
4 changes: 2 additions & 2 deletions src/plugins/promo-presets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ export class PromoPresetsPlugin implements OnboardingPlugin {
apply: OnboardingPlugin['apply'] = ({onboarding}) => {
this.onboardingInstance = onboarding;

onboarding.events.subscribe('beforeShowHint', this.onHintShow);
onboarding.events.subscribe('stepElementReached', this.onElementReach);

onboarding.events.subscribe('beforeSuggestPreset', this.onSuggestPreset);

onboarding.events.subscribe('wizardStateChanged', this.onWizardStateChanged);
};

onHintShow = ({stepData}: EventsMap['beforeShowHint']) => {
onElementReach = ({stepData}: EventsMap['beforeShowHint']) => {
if (!this.onboardingInstance) {
return true;
}
Expand Down
39 changes: 39 additions & 0 deletions src/promo-manager/tests/onboarding-integration.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -293,3 +293,42 @@ it('should allow to show common preset', async function () {
expect(onboardingController.hintStore.state.open).toBe(true);
expect(onboardingController.hintStore.state.hint?.step.slug).toBe('openBoard');
});

it('cancelled hint -> not trigger promo run', async function () {
const onboardingController = new OnboardingController(
getOptionsWithPromo({wizardState: 'visible'}),
);

const options = {
...testOptions,
config: {
promoGroups: [
{
slug: 'hintPromos',
conditions: [],
promos: [],
},
],
},
onboarding: {
getInstance: () => onboardingController,
groupSlug: 'hintPromos',
},
debugMode: true,
logger: {
level: 'debug' as const,
context: 'Promo manager',
},
};

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

// show promo hint with visible guide
await onboardingController.stepElementReached({
stepSlug: 'showCoolFeature',
element: getAnchorElement(),
});

expect(controller.state.base.activePromo).toBe(null);
});
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ export type EventsMap<
runPreset: {preset: Presets};
finishPreset: {preset: Presets};
beforeSuggestPreset: {preset: string};
stepElementReached: {stepData: ReachElementParams<Presets, Steps>};
beforeShowHint: {stepData: ReachElementParams<Presets, Steps>};
stateChange: {state: Controller<any, any, any>['state']};
hintDataChanged: {state: HintState<HintParams, Presets, Steps>};
Expand Down

0 comments on commit 43eb5dd

Please sign in to comment.