Skip to content

Commit

Permalink
start and steps tests
Browse files Browse the repository at this point in the history
  • Loading branch information
binrysearch committed Sep 9, 2024
1 parent 7949b33 commit e970c32
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 18 deletions.
2 changes: 1 addition & 1 deletion src/packages/tour/mock.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import van from "../dom/van"
import van from "../dom/van";
import { TourStep } from "./steps";
import { Tour } from "./tour";
import {
Expand Down
10 changes: 6 additions & 4 deletions src/packages/tour/start.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { start } from "./start";
import * as steps from "./steps";
import * as addOverlayLayer from "./addOverlayLayer";
import * as nextStep from "./steps";
import { getMockTour } from "./mock";

Expand All @@ -10,35 +9,38 @@ describe("start", () => {
});

test("should call the onstart callback", () => {
// Arrange
jest.spyOn(steps, "fetchSteps").mockReturnValue([]);
jest.spyOn(addOverlayLayer, "default").mockReturnValue(true);
jest.spyOn(nextStep, "nextStep").mockReturnValue(Promise.resolve(true));

const onstartCallback = jest.fn();

const mockTour = getMockTour();
mockTour.onStart(onstartCallback);

// Act
start(mockTour);

// Assert
expect(onstartCallback).toBeCalledTimes(1);
expect(onstartCallback).toBeCalledWith(document.body);
});

test("should not start the tour if isActive is false", () => {
// Arrange
const fetchIntroStepsMock = jest
.spyOn(steps, "fetchSteps")
.mockReturnValue([]);
const addOverlayLayerMock = jest.spyOn(addOverlayLayer, "default");
const nextStepMock = jest.spyOn(nextStep, "nextStep");

const mockTour = getMockTour();
mockTour.setOption("isActive", false);

// Act
start(mockTour);

// Assert
expect(fetchIntroStepsMock).toBeCalledTimes(0);
expect(addOverlayLayerMock).toBeCalledTimes(0);
expect(nextStepMock).toBeCalledTimes(0);
});

Expand Down
20 changes: 11 additions & 9 deletions src/packages/tour/steps.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import van from "../dom/van";
import { fetchSteps, nextStep, previousStep } from "./steps";
import _showElement from "./showElement";
import {
appendMockSteps,
getMockPartialSteps,
getMockSteps,
getMockTour,
} from "./mock";
import createElement from "../../util/createElement";
import { showElement } from "./showElement";

const { div, h1 } = van.tags;

jest.mock("./showElement");
jest.mock("./exitIntro");
Expand Down Expand Up @@ -54,7 +56,7 @@ describe("steps", () => {
test("should call ShowElement", async () => {
// Arrange
const showElementMock = jest.fn();
(_showElement as jest.Mock).mockImplementation(showElementMock);
(showElement as jest.Mock).mockImplementation(showElementMock);
const mockTour = getMockTour();
mockTour.setSteps(getMockSteps());

Expand Down Expand Up @@ -88,7 +90,7 @@ describe("steps", () => {
// Arrange
const mockTour = getMockTour();
const showElementMock = jest.fn();
(_showElement as jest.Mock).mockImplementation(showElementMock);
(showElement as jest.Mock).mockImplementation(showElementMock);
const fnBeforeChangeCallback = jest.fn();
fnBeforeChangeCallback.mockReturnValue(false);

Expand All @@ -107,7 +109,7 @@ describe("steps", () => {
const mockTour = getMockTour();
mockTour.setSteps(getMockSteps());
const showElementMock = jest.fn();
(_showElement as jest.Mock).mockImplementation(showElementMock);
(showElement as jest.Mock).mockImplementation(showElementMock);

const onBeforeChangeMock = jest.fn();
const sideEffect: number[] = [];
Expand Down Expand Up @@ -153,7 +155,7 @@ describe("steps", () => {
// Arrange
const mockTour = getMockTour();
mockTour.addStep({
element: createElement("div"),
element: div(),
intro: "test step",
});

Expand All @@ -174,7 +176,7 @@ describe("steps", () => {
intro: "first step",
},
{
element: createElement("div"),
element: div(),
intro: "second step",
},
]);
Expand Down Expand Up @@ -214,7 +216,7 @@ describe("steps", () => {

test("should find and add elements from options.steps to the list", () => {
// Arrange
document.body.appendChild(createElement("h1"));
document.body.appendChild(h1());

const mockTour = getMockTour();
mockTour.addSteps(getMockPartialSteps());
Expand Down Expand Up @@ -252,7 +254,7 @@ describe("steps", () => {

test("should find the data-* elements from the DOM with the correct order", () => {
// Arrange
const targetElement = createElement("div");
const targetElement = div();
const [
mockElementOne,
mockElementTwo,
Expand Down
2 changes: 1 addition & 1 deletion src/util/elementInViewport.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import van from "../packages/dom/van";
import elementInViewport from "./elementInViewport";

const {div} = van.tags;
const { div } = van.tags;

describe("elementInViewport", () => {
test("should return true when element is in viewport", () => {
Expand Down
4 changes: 2 additions & 2 deletions src/util/positionRelativeTo.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { setPositionRelativeTo } from "./positionRelativeTo";
import { getBoundingClientRectSpy } from "../../tests/jest/helper";
import van from "../packages/dom/van"
import van from "../packages/dom/van";

const {div} = van.tags;
const { div } = van.tags;

describe("setPositionRelativeTo", () => {
it("should return if helperLayer or currentStep is null", () => {
Expand Down
1 change: 0 additions & 1 deletion src/util/queryElement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ export const queryElementsByClassName = (
return queryElements(`.${className}`, container);
};


export const getElement = (
selector: string,
container?: HTMLElement | null
Expand Down

0 comments on commit e970c32

Please sign in to comment.