Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion shepherd.js/src/tour.ts
Original file line number Diff line number Diff line change
Expand Up @@ -400,10 +400,14 @@ export class Tour extends Evented {
* @private
*/
_showStep(step: Step) {
// `currentStep` is `undefined` until a step has shown, so normalize to
// `null` to keep `previous` consistent across every path into `show`
const previous = this.currentStep ?? null;

this.currentStep = step;
this.trigger('show', {
step,
previous: this.currentStep
previous
});

step.show();
Expand Down
45 changes: 45 additions & 0 deletions shepherd.js/test/unit/tour.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,51 @@ describe('Tour | Top-Level Class', function () {
).toBeFalsy();
});

it('triggers show with the step that was showing before as `previous`', function () {
const shows = [];
instance.on('show', ({ step, previous }) =>
shows.push({ step, previous })
);

instance.start();
instance.next();

const [firstShow, secondShow] = shows;

expect(firstShow.step.id).toBe('test');
expect(
firstShow.previous,
'there is no previous step on the first show'
).toBeNull();

expect(secondShow.step.id).toBe('test2');
expect(
secondShow.previous.id,
'`previous` is the step that was showing before'
).toBe('test');
expect(
secondShow.previous,
'`previous` is not the same object as `step`'
).not.toBe(secondShow.step);
});

it('reports `previous` as null when no step was showing before', function () {
// `removeStep` clears `currentStep` to `undefined` before showing the
// next step, so `previous` has to be normalized on that path too
instance.start();

let previousStep;
instance.on('show', ({ previous }) => (previousStep = previous));

instance.removeStep('test');

expect(instance.getCurrentStep().id).toBe('test2');
expect(
previousStep,
'`previous` is null rather than undefined when nothing was showing'
).toBeNull();
});

it('showOn determines which steps to skip', function () {
instance.start();
expect(instance.getCurrentStep().id).toBe('test');
Expand Down
Loading