Skip to content

Commit

Permalink
test: add and refine tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nebrelbug committed May 29, 2023
1 parent cfb7aab commit 33f8bd7
Show file tree
Hide file tree
Showing 8 changed files with 150 additions and 84 deletions.
42 changes: 0 additions & 42 deletions test/async.spec.ts

This file was deleted.

6 changes: 6 additions & 0 deletions test/config.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ describe("Config Tests", () => {
expect(res).toEqual("<p>Hi</p>"); // not escaped
});

it("default filter function stringifies data", () => {
const eta = new Eta();

expect(eta.config.filterFunction({ a: 1 })).toEqual("[object Object]");
});

it("filter function", () => {
const template = "My favorite food is <%= it.fav %>";
const baseEta = new Eta();
Expand Down
30 changes: 30 additions & 0 deletions test/file-utils.spec.ts → test/file-handling.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,33 @@ Hi Test Runner`
});
});
});

describe("file handling errors", () => {
const eta = new Eta({ views: viewsDir });

it("throws if accessing a file outside the views directory", () => {
expect(() => {
eta.render("../../sensitive-file.json", {});
}).toThrow();
});

it("throws if accessing a partial outside the views directory", () => {
expect(() => {
eta.render("out-of-views-dir", {});
}).toThrow();
});

it("throws if template doesn't exist", () => {
expect(() => {
eta.render("nonexistent.eta", {});
}).toThrow(/Could not find template/);
});

it("throws if views directory isn't defined", () => {
const testEta = new Eta();

expect(() => {
testEta.render("simple.eta", {});
}).toThrow();
});
});
41 changes: 0 additions & 41 deletions test/layouts.spec.ts

This file was deleted.

97 changes: 96 additions & 1 deletion test/render.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
/* global it, expect, describe */

import path from "node:path";

import { Eta } from "../src/index";

describe("basic functionality", () => {
Expand Down Expand Up @@ -34,7 +36,7 @@ describe("render caching", () => {
});
});

describe("Renders with different scopes", () => {
describe("useWith", () => {
it("Puts `it` in global scope with env.useWith", () => {
const etaWithUseWith = new Eta({ useWith: true });

Expand All @@ -43,3 +45,96 @@ describe("Renders with different scopes", () => {
);
});
});

function resolveAfter2Seconds(val: string): Promise<string> {
return new Promise((resolve) => {
setTimeout(() => {
resolve(val);
}, 20);
});
}

async function asyncTest() {
const result = await resolveAfter2Seconds("HI FROM ASYNC");
return result;
}

describe("async", () => {
const eta = new Eta();

it("compiles asynchronously", async () => {
expect(await eta.renderStringAsync("Hi <%= it.name %>", { name: "Ada Lovelace" })).toEqual(
"Hi Ada Lovelace"
);
});

it("async function works", async () => {
expect(
await eta.renderStringAsync("<%= await it.asyncTest() %>", {
asyncTest: asyncTest,
})
).toEqual("HI FROM ASYNC");
});

it("Async template w/ syntax error throws", async () => {
await expect(async () => {
await eta.renderStringAsync("<%= @#$%^ %>", {});
}).rejects.toThrow();
});
});

describe("layouts", () => {
const eta = new Eta({ views: path.join(__dirname, "templates") });

it("Nested layouts work as expected", () => {
const res = eta.render("index.eta", { title: "Cool Title" });

expect(res).toEqual(`<!DOCTYPE html>
<html lang="en">
<head>
<title>Cool Title</title>
</head>
<body>
This is the template body.
</body>
</html>`);
});

it("Layouts are called with arguments if they're provided", async () => {
eta.loadTemplate(
"@my-layout",
`<%= it.title %> - <%~ it.body %> - <%~ it.content %> - <%~ it.randomNum %>`
);

const res = await eta.renderString(
`<% layout("@my-layout", { title: 'Nifty title', content: 'Nice content'}) %>
This is a layout`,
{ title: "Cool Title", randomNum: 3 }
);

// Note that layouts automatically accept the data of the template which called them,
// after it is merged with `it` and { body:__eta.res }

expect(res).toEqual("Nifty title - This is a layout - Nice content - 3");
});
});

describe("file rendering", () => {
const eta = new Eta({ views: path.join(__dirname, "templates") });

it("renders template file properly", () => {
const res = eta.render("simple.eta", { name: "friend" });

expect(res).toEqual("Hi friend");
});

it("renders async template file properly", async () => {
const res = await eta.renderAsync("async.eta", {});

expect(res).toEqual(`ASYNC CONTENT BELOW!
HI FROM ASYNC`);
});
});
14 changes: 14 additions & 0 deletions test/templates/async-partial.eta
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<% function resolveAfter2Seconds(val) {
return new Promise((resolve) => {
setTimeout(() => {
resolve(val);
}, 20);
});
} %>

<% async function asyncTest() {
const result = await resolveAfter2Seconds("HI FROM ASYNC");
return result;
} %>

<%= await asyncTest() %>
3 changes: 3 additions & 0 deletions test/templates/async.eta
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
ASYNC CONTENT BELOW!

<%= await includeAsync("./async-partial") %>
1 change: 1 addition & 0 deletions test/templates/out-of-views-dir.eta
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<% include("../../sensitive-file.json") %>

0 comments on commit 33f8bd7

Please sign in to comment.