Skip to content

Commit

Permalink
all tests passing
Browse files Browse the repository at this point in the history
  • Loading branch information
alexmacarthur committed Nov 25, 2023
1 parent fc77108 commit 1123821
Show file tree
Hide file tree
Showing 14 changed files with 226 additions and 206 deletions.
272 changes: 142 additions & 130 deletions packages/typeit/__tests__/TypeIt.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import TypeIt from "../src";
import TypeIt from "../src/TypeIt.ts";

let instance;
let args;
Expand Down Expand Up @@ -33,10 +33,6 @@ test("Initial queue only contains startDelay pause.", () => {
expect(instance.getQueue().getItems()).toMatchSnapshot();
});

test("Returns an object with expected properties.", () => {
expect(Object.keys(instance).sort()).toMatchSnapshot();
});

describe("hard-coded strings", () => {
test("prepends hard-coded strings.", () => {
setHTML`
Expand Down Expand Up @@ -82,22 +78,24 @@ describe("hard-coded strings", () => {
});
});

test("Will not begin until explicitly called.", (done) => {
test("Will not begin until explicitly called.", () => {
setHTML`<div>
<span id="element"></span>
</div>`;

const instance = new TypeIt("#element", {
strings: "hello!",
afterComplete: () => {
expect(instance.is("started")).toBe(true);
done();
},
});
return new Promise((resolve) => {
const instance = new TypeIt("#element", {
strings: "hello!",
afterComplete: () => {
expect(instance.is("started")).toBe(true);
resolve();
},
});

expect(instance.is("started")).toBe(false);
expect(instance.is("started")).toBe(false);

instance.go();
instance.go();
});
});

test("Clears out remnants of previous instances correctly.", () => {
Expand All @@ -117,20 +115,22 @@ test("Clears out remnants of previous instances correctly.", () => {
expect(!instance.getOptions().strings[0].includes("ti-cursor")).toEqual(true);
});

test("Typing doesn't end with a break tag.", (done) => {
test("Typing doesn't end with a break tag.", () => {
setHTML`<div>
<span id="element"></span>
</div>`;

const element = document.querySelector("#element");
new TypeIt("#element", {
...args[1],
strings: ["One string.", "Two string.", "Three string."],
afterComplete: () => {
expect(element.innerHTML.endsWith("<br>")).not.toBe(true);
done();
},
}).go();
return new Promise((resolve) => {
const element = document.querySelector("#element");
new TypeIt("#element", {
...args[1],
strings: ["One string.", "Two string.", "Three string."],
afterComplete: () => {
expect(element.innerHTML.endsWith("<br>")).not.toBe(true);
resolve();
},
}).go();
});
});

test("Should skip over empty strings.", () => {
Expand All @@ -142,39 +142,43 @@ test("Should skip over empty strings.", () => {
});

describe("go()", () => {
test("Attaches cursor correctly.", (done) => {
args[1].afterComplete = () => {
let cursorNode = document.querySelector(".ti-cursor");
let element = document.getElementById("element");

expect(element.dataset.typeitId).toEqual(
cursorNode.dataset.tiAnimationId
);
expect(cursorNode.style.visibility).toEqual("");
expect(cursorNode).not.toBeNull();
done();
};
test("Attaches cursor correctly.", () => {
return new Promise((resolve) => {
args[1].afterComplete = () => {
let cursorNode = document.querySelector(".ti-cursor");
let element = document.getElementById("element");

expect(element.dataset.typeitId).toEqual(
cursorNode.dataset.tiAnimationId
);
expect(cursorNode.style.visibility).toEqual("");
expect(cursorNode).not.toBeNull();
resolve();
};

instance = new TypeIt(...args);
instance = new TypeIt(...args);

expect(document.querySelector(".ti-cursor")).toBeNull();
instance.go();
expect(document.querySelector(".ti-cursor")).toBeNull();
instance.go();
});
});

test("Attaches hidden cursor when option is disabled.", (done) => {
args[1].cursor = false;
args[1].afterComplete = () => {
let cursorNode = document.querySelector(".ti-cursor");
expect(cursorNode).not.toBeNull();
expect(cursorNode.style.visibility).toEqual("hidden");
test("Attaches hidden cursor when option is disabled.", () => {
return new Promise((resolve) => {
args[1].cursor = false;
args[1].afterComplete = () => {
let cursorNode = document.querySelector(".ti-cursor");
expect(cursorNode).not.toBeNull();
expect(cursorNode.style.visibility).toEqual("hidden");

done();
};
instance = new TypeIt(...args);
resolve();
};
instance = new TypeIt(...args);

let cursorNode = document.querySelector(".ti-cursor");
expect(cursorNode).toBeNull();
instance.go();
let cursorNode = document.querySelector(".ti-cursor");
expect(cursorNode).toBeNull();
instance.go();
});
});
});

Expand Down Expand Up @@ -269,7 +273,7 @@ describe("delete()", () => {
const items = instance.getQueue().getItems();

expect(items).toHaveLength(4);
expect(items[2].func.name).toEqual("_delete");
expect(items[2].func.name).toEqual("bound #delete");
});

test("Should temporarily update options when specified.", () => {
Expand Down Expand Up @@ -314,17 +318,19 @@ describe("break()", () => {
});

describe("empty()", () => {
test("Should empty out element when called with no cursor.", (done) => {
args[1].cursor = false;
element.innerHTML = "existing text";

args[1].afterComplete = function () {
expect(element.childNodes).toHaveLength(1);
done();
};

const instance = new TypeIt(...args);
instance.empty().go();
test("Should empty out element when called with no cursor.", () => {
return new Promise((resolve) => {
args[1].cursor = false;
element.innerHTML = "existing text";

args[1].afterComplete = function () {
expect(element.childNodes).toHaveLength(1);
resolve();
};

const instance = new TypeIt(...args);
instance.empty().go();
});
});

describe("addSplitPause()", () => {
Expand Down Expand Up @@ -359,16 +365,18 @@ describe("empty()", () => {
});
});

test("Should leave cursor alone when it empties element.", (done) => {
element.innerHTML = "existing text";
test("Should leave cursor alone when it empties element.", () => {
return new Promise((resolve) => {
element.innerHTML = "existing text";

args[1].afterComplete = function () {
expect(element.childNodes).toHaveLength(1);
done();
};
args[1].afterComplete = function () {
expect(element.childNodes).toHaveLength(1);
resolve();
};

const instance = new TypeIt(...args);
instance.empty().go();
const instance = new TypeIt(...args);
instance.empty().go();
});
});
});

Expand All @@ -388,81 +396,85 @@ describe("reset()", () => {

instance = instance.reset();

// console.log(instance.getOptions());

//-- Ensure the arguments that define these properties were passed.
expect(instance.getOptions()).toMatchSnapshot();

expect(instance.is("completed")).toBe(false);
expect(instance.is("destroyed")).toBe(false);
});

test("Wipes out element contents.", (done) => {
setHTML`<div>
<span id="element"></span>
</div>`;

let instance;
let element = document.querySelector("#element");
test("Wipes out element contents.", () => {
return new Promise((resolve) => {
setHTML`<div>
<span id="element"></span>
</div>`;

instance = new TypeIt("#element", {
speed: 0,
strings: "Hi.",
afterComplete: () => {
expect(element.innerHTML).toEqual(
expect.stringMatching(
/Hi.<span class="ti-cursor" data-ti-animation-id=".+">|<\/span>/
)
);
instance = instance.reset();
expect(element.innerHTML).toEqual("");
done();
},
}).go();
});
let instance;
let element = document.querySelector("#element");

test("Wipes out contents when it's an input.", (done) => {
setHTML`<div>
<input id="anInput">
</div>`;

let el = document.querySelector("#anInput");
let inputInstance = new TypeIt("#anInput", {
speed: 0,
strings: "Hi.",
afterComplete: () => {
expect(el.value).toEqual("Hi.");
inputInstance.reset();
expect(el.value).toEqual("");
done();
},
}).go();
instance = new TypeIt("#element", {
speed: 0,
strings: "Hi.",
afterComplete: () => {
expect(element.innerHTML).toEqual(
expect.stringMatching(
/Hi.<span class="ti-cursor" data-ti-animation-id=".+">|<\/span>/
)
);
instance = instance.reset();
expect(element.innerHTML).toEqual("");
resolve();
},
}).go();
});
});

describe("rebuilder function is provided", () => {
test("Rebuilds queue and executes it if specified.", (done) => {
test("Wipes out contents when it's an input.", () => {
return new Promise((resolve) => {
setHTML`<div>
<span id="element"></span>
<input id="anInput">
</div>`;

let hasCompletedOnce = false;
let el = document.querySelector("#element");
let instance = new TypeIt("#element", {
let el = document.querySelector("#anInput");
let inputInstance = new TypeIt("#anInput", {
speed: 0,
strings: "Hi.",
afterComplete: () => {
// After the second "run," the HTML should contain whatever the
// second rebuilt queue instructed to run.
if (hasCompletedOnce) {
expect(el.innerHTML).toMatch(/^second<span/);
return done();
}

instance.reset((i) => i.type("second")).go();
hasCompletedOnce = true;
expect(el.value).toEqual("Hi.");
inputInstance.reset();
expect(el.value).toEqual("");
resolve();
},
})
.type("first")
.go();
}).go();
});
});

describe("rebuilder function is provided", () => {
test("Rebuilds queue and executes it if specified.", () => {
return new Promise((resolve) => {
setHTML`<div>
<span id="element"></span>
</div>`;

let hasCompletedOnce = false;
let el = document.querySelector("#element");
let instance = new TypeIt("#element", {
speed: 0,
afterComplete: () => {
// After the second "run," the HTML should contain whatever the
// second rebuilt queue instructed to run.
if (hasCompletedOnce) {
expect(el.innerHTML).toMatch(/^second<span/);
return resolve();
}

instance.reset((i) => i.type("second")).go();
hasCompletedOnce = true;
},
})
.type("first")
.go();
});
});
});
});
Expand Down
2 changes: 1 addition & 1 deletion packages/typeit/__tests__/callback-methods.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import TypeIt from "../src";
import TypeIt from "../src/TypeIt.ts";

beforeEach(() => {
vi.clearAllMocks();
Expand Down
2 changes: 1 addition & 1 deletion packages/typeit/__tests__/chainable-methods/delete.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import TypeIt from "../../src";
import TypeIt from "../../src/TypeIt.ts";
import * as wait from "../../src/helpers/wait";

beforeEach(() => {
Expand Down
2 changes: 1 addition & 1 deletion packages/typeit/__tests__/chainable-methods/exec.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import TypeIt from "../../src";
import TypeIt from "../../src/TypeIt.ts";
import * as wait from "../../src/helpers/wait";

beforeEach(() => {
Expand Down
2 changes: 1 addition & 1 deletion packages/typeit/__tests__/chainable-methods/move.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import TypeIt from "../../src";
import TypeIt from "../../src/TypeIt.ts";
import * as wait from "../../src/helpers/wait";
import * as repositionCursor from "../../src/helpers/repositionCursor";
import * as countStepsToSelector from "../../src/helpers/countStepsToSelector";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import TypeIt from "../../src";
import TypeIt from "../../src/TypeIt.ts";

describe(".options()", () => {
beforeEach(() => {
Expand Down
Loading

0 comments on commit 1123821

Please sign in to comment.