Skip to content

Commit

Permalink
update tests (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
gurgunday authored Jan 20, 2024
1 parent 1fb3d47 commit b9b2122
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 22 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:

strategy:
matrix:
node-version: [^18, ^20]
node-version: [^18, lts/*]

steps:
- uses: actions/checkout@v4
Expand Down
34 changes: 13 additions & 21 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,24 @@ import test from "node:test";
import assert from "node:assert";
import { html } from "../src/index.js";

const username = "G";
const username = "Paul";
const descriptionSafe = "This is a safe description.";
const descriptionUnsafe =
"<script>alert('This is an unsafe description.')</script>";
const array1 = [1, 2, 3, 4, 5];
const conditionTrue = true;
const conditionFalse = false;
const empty = "";
const emptyString = "";

test("renders correctly", () => {
assert.strictEqual(html({ raw: [] }, []), "");
test("renders empty input", () => {
assert.strictEqual(html({ raw: [] }), "");
});

test("renders correctly", () => {
assert.strictEqual(html`${empty}`, "");
test("renders empty input", () => {
assert.strictEqual(html`${emptyString}`, "");
});

test("renders correctly", () => {
test("renders normal input", () => {
assert.strictEqual(html`Hey, ${username}!`, `Hey, ${username}!`);
});

Expand All @@ -37,13 +37,6 @@ test("escapes unsafe output", () => {
);
});

test("escapes unsafe output", () => {
assert.strictEqual(
html`<p>${descriptionUnsafe}</p>`,
`<p>&lt;script&gt;alert(&apos;This is an unsafe description.&apos;)&lt;/script&gt;</p>`,
);
});

test("renders arrays", () => {
assert.strictEqual(
html`<p>${[descriptionSafe, descriptionUnsafe]}</p>`,
Expand All @@ -59,8 +52,8 @@ test("bypass escaping", () => {
});

test("renders wrapped html calls", () => {
// prettier-ignore
assert.strictEqual(
// prettier-ignore
html`<p>!${conditionTrue ? html`<strong>${descriptionUnsafe}</strong>` : ""}</p>`,
"<p><strong>&lt;script&gt;alert(&apos;This is an unsafe description.&apos;)&lt;/script&gt;</strong></p>",
);
Expand All @@ -75,7 +68,6 @@ test("renders multiple html calls", () => {
!${conditionFalse ? html`<em> ${array1} </em>` : ""}
</p>
`,
// it should be formatted
`
<p>
<strong> This is a safe description. </strong>
Expand All @@ -89,26 +81,26 @@ test("renders multiple html calls", () => {
test("renders multiple html calls with different expression types", () => {
const obj = {};
obj.toString = () => {
return "Description of the object.";
return "description of the object";
};

// prettier-ignore
assert.strictEqual(
html`
<p>
!${conditionTrue ? html`<strong> ${descriptionSafe} </strong>` : ""}
!${conditionFalse
? ""
: // prettier-ignore
:
html`<em> ${array1.map((i) => {return i + 1;})} </em>`}<br />
And also, ${false} ${null}${undefined}${obj}
And also, ${false} ${null}${undefined}${obj} is ${true}
</p>
`,
// it should be formatted
`
<p>
<strong> This is a safe description. </strong>
<em> 23456 </em><br />
And also, false Description of the object.
And also, false description of the object is true
</p>
`,
);
Expand Down

0 comments on commit b9b2122

Please sign in to comment.