Skip to content

Commit

Permalink
Optimize loop. (#384)
Browse files Browse the repository at this point in the history
* Optimize loop.

* Update test.yml
  • Loading branch information
alexmacarthur authored Jul 25, 2024
1 parent 70eb30a commit 2a35d4a
Show file tree
Hide file tree
Showing 33 changed files with 291 additions and 195 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
- uses: actions/setup-node@v4
with:
node-version: 20
- run: npm install && npm run bootstrap
- run: npm install && cd packages/typeit && npm install && cd ../typeit-react && npm install && cd ../../ && npm run build
- run: npm test
- uses: preactjs/compressed-size-action@v2
with:
Expand Down
1 change: 1 addition & 0 deletions lerna.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"packages/*"
],
"version": "independent",
"useNx": false,
"command": {
"publish": {
"ignoreChanges": [
Expand Down
4 changes: 2 additions & 2 deletions packages/typeit-react/src/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ describe("rendering as specific element", () => {
}}
>
A span tag.
</TypeIt>
</TypeIt>,
);
});

Expand All @@ -39,7 +39,7 @@ describe("rendering as specific element", () => {
}}
>
An H1 tag.
</TypeIt>
</TypeIt>,
);
});

Expand Down
2 changes: 1 addition & 1 deletion packages/typeit-react/src/setup.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import '@testing-library/jest-dom/vitest';
import "@testing-library/jest-dom/vitest";
import { beforeEach } from "vitest";
import "vitest-dom/extend-expect";

Expand Down
10 changes: 5 additions & 5 deletions packages/typeit/__tests__/TypeIt.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ describe("go()", () => {
let element = document.getElementById("element");

expect(element.dataset.typeitId).toEqual(
cursorNode.dataset.tiAnimationId
cursorNode.dataset.tiAnimationId,
);
expect(cursorNode.style.visibility).toEqual("");
expect(cursorNode).not.toBeNull();
Expand Down Expand Up @@ -220,7 +220,7 @@ describe("type()", () => {
let functionalInstance = new TypeIt("#element").type(() => "a");

expect(instance.getQueue().getItems()).toHaveLength(
functionalInstance.getQueue().getItems().length
functionalInstance.getQueue().getItems().length,
);
});
});
Expand Down Expand Up @@ -262,7 +262,7 @@ describe("move()", () => {
let functionalInstance = new TypeIt("#element").move(() => 9);

expect(instance.getQueue().getItems()).toHaveLength(
functionalInstance.getQueue().getItems().length
functionalInstance.getQueue().getItems().length,
);
});
});
Expand Down Expand Up @@ -418,8 +418,8 @@ describe("reset()", () => {
afterComplete: () => {
expect(element.innerHTML).toEqual(
expect.stringMatching(
/Hi.<span class="ti-cursor" data-ti-animation-id=".+">|<\/span>/
)
/Hi.<span class="ti-cursor" data-ti-animation-id=".+">|<\/span>/,
),
);
instance = instance.reset();
expect(element.innerHTML).toEqual("");
Expand Down
16 changes: 8 additions & 8 deletions packages/typeit/__tests__/chainable-methods/delete.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ describe("deleting fires correctly", () => {
afterComplete: () => {
expect(el.innerHTML).toEqual(
expect.stringMatching(
/abc<span class="ti-cursor" data-ti-animation-id=".+">|<\/span>/
)
/abc<span class="ti-cursor" data-ti-animation-id=".+">|<\/span>/,
),
);
done();
},
Expand All @@ -36,8 +36,8 @@ describe("deleting fires correctly", () => {
afterComplete: () => {
expect(el.innerHTML).toEqual(
expect.stringMatching(
/abc<strong>def<\/strong><span class="ti-cursor" data-ti-animation-id=".+">|<\/span>/
)
/abc<strong>def<\/strong><span class="ti-cursor" data-ti-animation-id=".+">|<\/span>/,
),
);
done();
},
Expand Down Expand Up @@ -112,8 +112,8 @@ describe("cursor has been moved", () => {
afterComplete: () => {
expect(document.body.querySelector("#element").innerHTML).toEqual(
expect.stringMatching(
/abc <strong>def<\/strong> g<em><span class="ti-cursor" data-ti-animation-id=".+">|<\/span>jkl<\/em> mno/
)
/abc <strong>def<\/strong> g<em><span class="ti-cursor" data-ti-animation-id=".+">|<\/span>jkl<\/em> mno/,
),
);
done();
},
Expand All @@ -130,8 +130,8 @@ describe("cursor has been moved", () => {
afterComplete: () => {
expect(document.body.querySelector("#element").innerHTML).toEqual(
expect.stringMatching(
/^abc <span class=\"ti-cursor\" data-ti-animation-id=\".+\">|<\/span> mno/
)
/^abc <span class=\"ti-cursor\" data-ti-animation-id=\".+\">|<\/span> mno/,
),
);
done();
},
Expand Down
2 changes: 1 addition & 1 deletion packages/typeit/__tests__/flush.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ describe("deleting queue items", () => {
instance.type("goodbye").flush(() => {
expect(el.textContent).toEqual("hellogoodbye|");
expect(instance.getQueue().getItems(true).length).toBeGreaterThan(
0
0,
);
resolve();
});
Expand Down
16 changes: 8 additions & 8 deletions packages/typeit/__tests__/helpers/chunkStrings.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,39 +17,39 @@ test("Parses normal string correctly.", () => {

test("Parses single HTML tag.", () => {
let result = chunkStringAsHtml(
"Hello, this is some <strong>bold</strong> text."
"Hello, this is some <strong>bold</strong> text.",
);

expect(result).toMatchSnapshot();
});

test("Parses multiple HTML tags.", () => {
let result = chunkStringAsHtml(
"Hello, this is some <strong>bold</strong> text, and some <i>italicized</i> text."
"Hello, this is some <strong>bold</strong> text, and some <i>italicized</i> text.",
);

expect(result).toMatchSnapshot();
});

test("Parses HTML tag at beginning of string.", () => {
let result = chunkStringAsHtml(
"<strong>Hello!</strong> This is some text with HTML at the beginning."
"<strong>Hello!</strong> This is some text with HTML at the beginning.",
);

expect(result).toMatchSnapshot();
});

test("Parses HTML tag at end of string.", () => {
let result = chunkStringAsHtml(
"This is some text with HTML at the <em>end.</em>"
"This is some text with HTML at the <em>end.</em>",
);

expect(result).toMatchSnapshot();
});

test("Parses HTML tag with attributes.", () => {
let result = chunkStringAsHtml(
'This string has an <strong class="strong-class" id="strong-id" data-whatever="data-att">element</strong> with attributes.'
'This string has an <strong class="strong-class" id="strong-id" data-whatever="data-att">element</strong> with attributes.',
);

expect(result).toMatchSnapshot();
Expand All @@ -58,13 +58,13 @@ test("Parses HTML tag with attributes.", () => {
describe("maybeChunkStringAsHtml()", () => {
test("Should return noderized string when setting is enabled.", () => {
expect(
maybeChunkStringAsHtml("A <em>fancy</em> string.")
maybeChunkStringAsHtml("A <em>fancy</em> string."),
).toMatchSnapshot();
});

test("Should correctly transform non-HTML string as character objects.", () => {
expect(
maybeChunkStringAsHtml("A <em>fancy</em> string.", false)
maybeChunkStringAsHtml("A <em>fancy</em> string.", false),
).toMatchSnapshot();
});
});
Expand All @@ -79,7 +79,7 @@ describe("walkElementNodes()", () => {

// "B" should have an original parent of the <span>.
expect(thirdNode.originalParent).toMatchObject(
document.querySelector("span")
document.querySelector("span"),
);

// Includes each text node, and separate nodes for the parent elements themselves.
Expand Down
89 changes: 89 additions & 0 deletions packages/typeit/__tests__/helpers/cleanUpSkipped.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import { expect, it, vi } from "vitest";
import cleanUpSkipped from "../../src/helpers/cleanUpSkipped";
import { QueueMapPair } from "../../src/types";

it("should clean up skipped items", () => {
const cleanUp = vi.fn();
const queueItems = [
[Symbol("1"), vi.fn()],
[Symbol("2"), vi.fn()],
[Symbol("3"), vi.fn()],
[Symbol("4"), vi.fn()],
[Symbol("5"), vi.fn()],
] as QueueMapPair[];

cleanUpSkipped({
index: 1,
newIndex: 3,
queueItems,
cleanUp,
});

expect(cleanUp).toHaveBeenCalledTimes(2);
expect(cleanUp).toHaveBeenCalledWith(queueItems[2][0]);
expect(cleanUp).toHaveBeenCalledWith(queueItems[3][0]);
});

it("should clean up skipped items... again", () => {
const cleanUp = vi.fn();
const queueItems = [
[Symbol("1"), vi.fn()],
[Symbol("2"), vi.fn()],
[Symbol("3"), vi.fn()],
[Symbol("4"), vi.fn()],
[Symbol("5"), vi.fn()],
] as QueueMapPair[];

cleanUpSkipped({
index: 0,
newIndex: 4,
queueItems,
cleanUp,
});

expect(cleanUp).toHaveBeenCalledTimes(4);
expect(cleanUp).toHaveBeenCalledWith(queueItems[1][0]);
expect(cleanUp).toHaveBeenCalledWith(queueItems[2][0]);
expect(cleanUp).toHaveBeenCalledWith(queueItems[3][0]);
expect(cleanUp).toHaveBeenCalledWith(queueItems[4][0]);
});

it("should clean up skipped items... again", () => {
const cleanUp = vi.fn();
const queueItems = [
[Symbol("1"), vi.fn()],
[Symbol("2"), vi.fn()],
[Symbol("3"), vi.fn()],
[Symbol("4"), vi.fn()],
[Symbol("5"), vi.fn()],
] as QueueMapPair[];

cleanUpSkipped({
index: 0,
newIndex: 0,
queueItems,
cleanUp,
});

expect(cleanUp).toHaveBeenCalledTimes(0);
});

it("should clean up skipped items... again", () => {
const cleanUp = vi.fn();
const queueItems = [
[Symbol("1"), vi.fn()],
[Symbol("2"), vi.fn()],
[Symbol("3"), vi.fn()],
[Symbol("4"), vi.fn()],
[Symbol("5"), vi.fn()],
] as QueueMapPair[];

cleanUpSkipped({
index: 4,
newIndex: 4,
queueItems,
cleanUp,
});

expect(cleanUp).toHaveBeenCalledTimes(0);
});
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ describe("moving toward the end", () => {

it("calculates correctly with nested HTML", () => {
let queue = buildQueueFromString(
`23<strong>goodbye<em>ok, then!</em>haha.</strong>whatever.`
`23<strong>goodbye<em>ok, then!</em>haha.</strong>whatever.`,
);

const result = countStepsToSelector({
Expand Down
2 changes: 1 addition & 1 deletion packages/typeit/__tests__/helpers/expandTextNodes.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ describe("nested HTML", () => {
expect(getTextNodes(element.querySelector("strong"))).toHaveLength(7);
expect(getTextNodes(element.querySelector("strong em"))).toHaveLength(4);
expect(element.innerHTML).toEqual(
"hello, <strong>there, <em>pal!</em></strong>"
"hello, <strong>there, <em>pal!</em></strong>",
);
});
});
Expand Down
4 changes: 2 additions & 2 deletions packages/typeit/__tests__/helpers/fireItem.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ describe("cursor should be paused", () => {
options: {
delay: 500,
},
})
}),
);

expect(mockAnimation.cancel).toHaveBeenCalledTimes(1);
Expand Down Expand Up @@ -121,7 +121,7 @@ describe("cursor should NOT be paused", () => {
frames: ["frame1", "frame2"],
},
},
})
}),
);

expect(mockAnimation.cancel).not.toHaveBeenCalledTimes(1);
Expand Down
12 changes: 6 additions & 6 deletions packages/typeit/__tests__/helpers/insertIntoElement.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ describe("plain text", () => {
insertIntoElement(el, document.createTextNode("x"));

expect(document.body.innerHTML).toEqual(
'<span id="el">x<i class="ti-cursor">|</i></span>'
'<span id="el">x<i class="ti-cursor">|</i></span>',
);
});

Expand All @@ -48,12 +48,12 @@ describe("plain text", () => {

insertIntoElement(el, em);
expect(document.body.innerHTML).toEqual(
'<span id="el"><em></em><i class="ti-cursor">|</i></span>'
'<span id="el"><em></em><i class="ti-cursor">|</i></span>',
);

insertIntoElement(el, text);
expect(document.body.innerHTML).toEqual(
'<span id="el"><em>a</em><i class="ti-cursor">|</i></span>'
'<span id="el"><em>a</em><i class="ti-cursor">|</i></span>',
);
});

Expand All @@ -62,7 +62,7 @@ describe("plain text", () => {
const el = document.querySelector("#top");

const spanEl = getParsedBody(
'a<em id="middle">b<strong id="bottom">c</strong></em>'
'a<em id="middle">b<strong id="bottom">c</strong></em>',
);
const nodes = walkElementNodes(expandTextNodes(spanEl));

Expand All @@ -71,7 +71,7 @@ describe("plain text", () => {
});

expect(document.body.innerHTML).toEqual(
'<span id="top">a<em id="middle">b<strong id="bottom">c</strong></em><i class="ti-cursor">|</i></span>'
'<span id="top">a<em id="middle">b<strong id="bottom">c</strong></em><i class="ti-cursor">|</i></span>',
);
});

Expand All @@ -91,7 +91,7 @@ describe("plain text", () => {
});

expect(document.body.innerHTML).toEqual(
'<span id="top">a<br>b<br>c<i class="ti-cursor">|</i></span>'
'<span id="top">a<br>b<br>c<i class="ti-cursor">|</i></span>',
);
});
});
Loading

0 comments on commit 2a35d4a

Please sign in to comment.