Skip to content

Commit

Permalink
refresh unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
binrysearch committed Sep 9, 2024
1 parent d68ed0a commit 6fd5727
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 24 deletions.
3 changes: 1 addition & 2 deletions src/packages/tour/components/TourTooltip.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Tooltip, type TooltipProps } from "../../tooltip/tooltip";
import van, { PropValueOrDerived, State } from "../../dom/van";
import van, { PropValueOrDerived } from "../../dom/van";
import {
activeClassName,
bulletsClassName,
Expand All @@ -20,7 +20,6 @@ import {
} from "../classNames";
import { TourStep } from "../steps";
import { dataStepNumberAttribute } from "../dataAttributes";
import getOffset from "../../../util/getOffset";
import scrollParentToElement from "../../../util/scrollParentToElement";
import scrollTo from "../../../util/scrollTo";

Expand Down
12 changes: 7 additions & 5 deletions src/packages/tour/mock.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import createElement from "../../util/createElement";
import van from "../dom/van"
import { TourStep } from "./steps";
import { Tour } from "./tour";
import {
Expand All @@ -7,22 +7,24 @@ import {
dataStepAttribute,
} from "./dataAttributes";

const { div, b, a, h1 } = van.tags;

export const appendMockSteps = (targetElement: HTMLElement = document.body) => {
const mockElementOne = createElement("div");
const mockElementOne = div();
mockElementOne.setAttribute(dataIntroAttribute, "Mock element");

const mockElementTwo = createElement("b");
const mockElementTwo = b();
mockElementTwo.setAttribute(dataIntroAttribute, "Mock element left position");
mockElementTwo.setAttribute(dataPosition, "left");

const mockElementThree = createElement("h1");
const mockElementThree = h1();
mockElementThree.setAttribute(
dataIntroAttribute,
"Mock element second to last"
);
mockElementThree.setAttribute(dataStepAttribute, "10");

const mockElementFour = createElement("a");
const mockElementFour = a();
mockElementFour.setAttribute(dataIntroAttribute, "Mock element last");
mockElementFour.setAttribute(dataStepAttribute, "20");

Expand Down
40 changes: 23 additions & 17 deletions src/packages/tour/refresh.test.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,37 @@
import * as tooltip from "../../packages/tooltip";
import { getMockTour } from "./mock";
import { Tour } from "./tour";
import van from "../dom/van";
import {
sleep,
waitMsForDerivations,
waitMsForExitTransition,
} from "../../util/sleep";

const { div } = van.tags;

describe("refresh", () => {
test("should not refetch the steps when refreshStep is false", async () => {
// Arrange
jest.spyOn(tooltip, "placeTooltip");
let mockTour: Tour;
let targetElement: HTMLElement;

const targetElement = document.createElement("div");
document.body.appendChild(targetElement);
beforeEach(() => {
mockTour = getMockTour();
targetElement = div();
van.add(document.body, targetElement);
});

const mockTour = getMockTour();
afterEach(async () => {
await mockTour.exit();
await sleep(waitMsForExitTransition);
});

test("should not refetch the steps when refreshStep is false", async () => {
// Arrange
mockTour.addStep({
intro: "first",
});

await mockTour.start();
await sleep(waitMsForDerivations);

// Act
mockTour.setOptions({
Expand All @@ -35,20 +51,10 @@ describe("refresh", () => {
expect(mockTour.getSteps()).toHaveLength(1);
expect(mockTour.getStep(0).intro).toBe("first");
expect(document.querySelectorAll(".introjs-bullets ul li").length).toBe(1);

// cleanup
await mockTour.exit();
});

test("should fetch the steps when refreshStep is true", async () => {
// Arrange
jest.spyOn(tooltip, "placeTooltip");

const targetElement = document.createElement("div");
document.body.appendChild(targetElement);

const mockTour = getMockTour();

mockTour.addStep({
intro: "first",
});
Expand Down
8 changes: 8 additions & 0 deletions src/util/sleep.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export const sleep = (ms: number) =>
new Promise((resolve) => setTimeout(resolve, ms));

// time to wait for the derivations to update
export const waitMsForDerivations = 5;

// time to wait for the exit transition to complete
export const waitMsForExitTransition = 260;

0 comments on commit 6fd5727

Please sign in to comment.