Skip to content

Commit

Permalink
_currentStep is now by default set to -1 + fixing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Meena committed Aug 5, 2023
1 parent 7950cf8 commit f2b6f27
Show file tree
Hide file tree
Showing 11 changed files with 88 additions and 68 deletions.
2 changes: 1 addition & 1 deletion src/core/dontShowAgain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,5 @@ export function setDontShowAgain(intro: IntroJs, dontShowAgain: boolean) {
*/
export function getDontShowAgain(intro: IntroJs): boolean {
const dontShowCookie = getCookie(intro._options.dontShowAgainCookie);
return dontShowCookie && dontShowCookie === dontShowAgainCookieValue;
return dontShowCookie !== "" && dontShowCookie === dontShowAgainCookieValue;
}
4 changes: 2 additions & 2 deletions src/core/fetchIntroSteps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export default function fetchIntroSteps(
);
let introItems: IntroStep[] = [];

if (intro._options.steps) {
if (intro._options.steps && intro._options.steps.length) {
//use steps passed programmatically
for (const step of intro._options.steps) {
const currentItem = cloneObject(step);
Expand Down Expand Up @@ -65,7 +65,7 @@ export default function fetchIntroSteps(
}

if (currentItem.element !== null) {
introItems.push(currentItem);
introItems.push(currentItem as IntroStep);
}
}
} else {
Expand Down
4 changes: 2 additions & 2 deletions src/core/hint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import setHelperLayerPosition from "./setHelperLayerPosition";
import placeTooltip from "./placeTooltip";
import createElement from "../util/createElement";
import debounce from "../util/debounce";
import { HintPosition, TooltipPosition } from "./steps";
import { HintPosition, HintStep, TooltipPosition } from "./steps";
import { IntroJs } from "src/intro";

/**
Expand Down Expand Up @@ -442,7 +442,7 @@ export async function populateHints(
currentItem.hintAnimation || intro._options.hintAnimation;

if (currentItem.element !== null) {
intro._hintItems.push(currentItem);
intro._hintItems.push(currentItem as HintStep);
}
}
} else {
Expand Down
4 changes: 2 additions & 2 deletions src/core/steps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@ export type IntroStep = {
intro: string;
tooltipClass?: string;
highlightClass?: string;
element?: HTMLElement | string;
element?: HTMLElement | string | null;
position: TooltipPosition;
scrollTo: ScrollTo;
disableInteraction?: boolean;
};

export type HintStep = {
element?: HTMLElement | string;
element?: HTMLElement | string | null;
tooltipClass?: string;
position: TooltipPosition;
hint?: string;
Expand Down
4 changes: 2 additions & 2 deletions src/option.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import {
} from "./core/steps";

export interface Options {
steps: IntroStep[];
hints: HintStep[];
steps: Partial<IntroStep>[];
hints: Partial<HintStep>[];
/* Is this tour instance active? Don't show the tour again if this flag is set to false */
isActive: boolean;
/* Next button label in tooltip box */
Expand Down
6 changes: 3 additions & 3 deletions tests/jest/core/exitIntro.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ describe("exitIntro", () => {

intro.exit(false);

expect(intro._currentStep).toBeUndefined();
expect(intro._currentStep).toBe(-1);
});

test("should call the onexit and onbeforeexit callbacks", async () => {
Expand All @@ -30,7 +30,7 @@ describe("exitIntro", () => {
steps: [
{
intro: "step one",
element: document.querySelector("h1"),
element: document.querySelector("h1") as HTMLElement,
},
],
})
Expand Down Expand Up @@ -110,7 +110,7 @@ describe("exitIntro", () => {
steps: [
{
intro: "step one",
element: document.querySelector("h1"),
element: document.querySelector("h1") as HTMLElement,
},
],
})
Expand Down
104 changes: 61 additions & 43 deletions tests/jest/core/placeTooltip.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as getOffset from "../../../src/util/getOffset";
import * as getWindowSize from "../../../src/util/getWindowSize";
import placeTooltip from "../../../src/core/placeTooltip";
import { IntroJs } from "src/intro";
import { IntroStep } from "src/core/steps";

describe("placeTooltip", () => {
test("should automatically place the tooltip position when there is enough space", () => {
Expand Down Expand Up @@ -29,25 +30,27 @@ describe("placeTooltip", () => {
right: 300,
});

const targetElement = document.createElement("div");
const currentStep: IntroStep = {
step: 0,
intro: "hello",
title: "hello",
position: "top",
element: document.createElement("div"),
scrollTo: "element",
};
const tooltipLayer = document.createElement("div");
const arrowLayer = document.createElement("div");

placeTooltip(
{
_currentStep: 0,
_introItems: [
{
intro: "hello",
position: "top",
},
],
_introItems: [currentStep],
_options: {
positionPrecedence: ["top", "bottom", "left", "right"],
autoPosition: true,
},
} as IntroJs,
targetElement,
currentStep,
tooltipLayer,
arrowLayer,
false
Expand All @@ -59,7 +62,14 @@ describe("placeTooltip", () => {
});

test("should skip auto positioning when autoPosition is false", () => {
const targetElement = document.createElement("div");
const currentStep: IntroStep = {
step: 0,
intro: "hello",
title: "hello",
position: "top",
element: document.createElement("div"),
scrollTo: "element",
};
const tooltipLayer = document.createElement("div");
const arrowLayer = document.createElement("div");

Expand All @@ -77,7 +87,7 @@ describe("placeTooltip", () => {
autoPosition: false,
},
} as IntroJs,
targetElement,
currentStep,
tooltipLayer,
arrowLayer,
false
Expand Down Expand Up @@ -111,25 +121,27 @@ describe("placeTooltip", () => {
right: 0,
});

const targetElement = document.createElement("div");
const currentStep: IntroStep = {
step: 0,
intro: "hello",
title: "hello",
position: "left",
element: document.createElement("div"),
scrollTo: "element",
};
const tooltipLayer = document.createElement("div");
const arrowLayer = document.createElement("div");

placeTooltip(
{
_currentStep: 0,
_introItems: [
{
intro: "hello",
position: "left",
},
],
_introItems: [currentStep],
_options: {
positionPrecedence: ["top", "bottom", "left", "right"],
autoPosition: true,
},
} as IntroJs,
targetElement,
currentStep,
tooltipLayer,
arrowLayer,
false
Expand Down Expand Up @@ -163,25 +175,27 @@ describe("placeTooltip", () => {
right: 0,
});

const targetElement = document.createElement("div");
const currentStep: IntroStep = {
step: 0,
intro: "hello",
title: "hello",
position: "left",
element: document.createElement("div"),
scrollTo: "element",
};
const tooltipLayer = document.createElement("div");
const arrowLayer = document.createElement("div");

placeTooltip(
{
_currentStep: 0,
_introItems: [
{
intro: "hello",
position: "left",
},
],
_introItems: [currentStep],
_options: {
positionPrecedence: ["top", "bottom", "left", "right"],
autoPosition: true,
},
} as IntroJs,
targetElement,
currentStep,
tooltipLayer,
arrowLayer,
false
Expand All @@ -193,26 +207,28 @@ describe("placeTooltip", () => {
});

test("should attach the global custom tooltip css class", () => {
const targetElement = document.createElement("div");
const currentStep: IntroStep = {
step: 0,
intro: "hello",
title: "hello",
position: "left",
element: document.createElement("div"),
scrollTo: "element",
};
const tooltipLayer = document.createElement("div");
const arrowLayer = document.createElement("div");

placeTooltip(
{
_currentStep: 0,
_introItems: [
{
intro: "hello",
position: "left",
},
],
_introItems: [currentStep],
_options: {
positionPrecedence: ["top", "bottom", "left", "right"],
autoPosition: true,
tooltipClass: "newclass",
},
} as IntroJs,
targetElement,
currentStep,
tooltipLayer,
arrowLayer,
false
Expand All @@ -224,26 +240,28 @@ describe("placeTooltip", () => {
});

test("should attach the step custom tooltip css class", () => {
const targetElement = document.createElement("div");
const currentStep: IntroStep = {
step: 0,
intro: "hello",
title: "hello",
position: "left",
element: document.createElement("div"),
scrollTo: "element",
tooltipClass: "myclass",
};
const tooltipLayer = document.createElement("div");
const arrowLayer = document.createElement("div");

placeTooltip(
{
_currentStep: 0,
_introItems: [
{
intro: "hello",
position: "left",
tooltipClass: "myclass",
},
],
_introItems: [currentStep],
_options: {
positionPrecedence: ["top", "bottom", "left", "right"],
autoPosition: true,
},
} as IntroJs,
targetElement,
currentStep,
tooltipLayer,
arrowLayer,
false
Expand Down
8 changes: 2 additions & 6 deletions tests/jest/core/steps.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,7 @@ jest.mock("../../../src/core/showElement");
jest.mock("../../../src/core/exitIntro");

describe("steps", () => {
let context: IntroJs = {
_currentStep: 0,
_introItems: [],
_introBeforeChangeCallback: undefined,
} as IntroJs;
let context: IntroJs;

beforeEach(() => {
context = {
Expand Down Expand Up @@ -98,7 +94,7 @@ describe("steps", () => {
(_showElement as jest.Mock).mockImplementation(showElementMock);

const onBeforeChangeMock = jest.fn();
const sideEffect = [];
const sideEffect: number[] = [];

context._introBeforeChangeCallback = async () => {
return new Promise<boolean>((res) => {
Expand Down
8 changes: 4 additions & 4 deletions tests/jest/helper.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export function find(selector: string | HTMLElement): HTMLElement {
if (typeof selector === "string") {
return document.querySelector(selector);
return document.querySelector(selector) as HTMLElement;
}

if (selector instanceof Element) {
Expand All @@ -10,7 +10,7 @@ export function find(selector: string | HTMLElement): HTMLElement {
throw Error("invalid selector");
}

export function content(selector: string | HTMLElement): string | undefined {
export function content(selector: string | HTMLElement): string | null {
const el = find(selector);

if (el) {
Expand All @@ -20,7 +20,7 @@ export function content(selector: string | HTMLElement): string | undefined {
return null;
}

export function className(selector: string | HTMLElement): string {
export function className(selector: string | HTMLElement): string | null {
const el = find(selector);

if (el) {
Expand Down Expand Up @@ -57,7 +57,7 @@ export function appendDummyElement(
): HTMLElement {
const el = document.createElement(name || "p");
el.innerHTML = text || "hello world";
el.setAttribute("style", style);
el.setAttribute("style", style || "");

document.body.appendChild(el);

Expand Down
11 changes: 8 additions & 3 deletions tests/jest/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -259,10 +259,15 @@ describe("intro", () => {
const intro2 = introJs();
const intro3 = introJs();

expect(intro1["introjs-instance"]).not.toBeNull();
expect(intro2["introjs-instance"]).not.toBeNull();
expect(intro3["introjs-instance"]).not.toBeNull();
//@ts-ignore
expect(intro1["introjs-instance"]).toBeNumber();
//@ts-ignore
expect(intro2["introjs-instance"]).toBeNumber();
//@ts-ignore
expect(intro3["introjs-instance"]).toBeNumber();
//@ts-ignore
expect(intro1["introjs-instance"]).not.toBe(intro2["introjs-instance"]);
//@ts-ignore
expect(intro2["introjs-instance"]).not.toBe(intro3["introjs-instance"]);
});

Expand Down
1 change: 1 addition & 0 deletions tests/jest/setup.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import "regenerator-runtime/runtime";
//@ts-ignore
import { JSDOM } from "jsdom";
//import * as matchers from "jest-extended";
//import expect from "expect";
Expand Down

0 comments on commit f2b6f27

Please sign in to comment.