Skip to content

Commit

Permalink
chore: update tests for v3
Browse files Browse the repository at this point in the history
  • Loading branch information
nebrelbug committed May 29, 2023
1 parent ea37ebb commit 4fff2b5
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 155 deletions.
65 changes: 13 additions & 52 deletions test/async.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
/* global it, expect, describe */

import * as Eta from "../src/index";
import { buildRegEx } from "./err.spec";
import { Eta } from "../src/index";

const eta = new Eta();

function resolveAfter2Seconds(val: string): Promise<string> {
return new Promise((resolve) => {
Expand All @@ -18,64 +19,24 @@ async function asyncTest() {

describe("Async Render checks", () => {
describe("Async works", () => {
it("Simple template compiles asynchronously", async () => {
expect(
await Eta.render("Hi <%= it.name %>", { name: "Ada Lovelace" }, { async: true })
).toEqual("Hi Ada Lovelace");
});

it("Simple template compiles asynchronously with callback", (done) => {
function cb(_err: Error | null, res?: string) {
expect(res).toEqual(res);
done();
}
Eta.render("Hi <%= it.name %>", { name: "Ada Lovelace" }, { async: true }, cb);
it("compiles asynchronously", async () => {
expect(await eta.renderStringAsync("Hi <%= it.name %>", { name: "Ada Lovelace" })).toEqual(
"Hi Ada Lovelace"
);
});

it("Async function works", async () => {
it("async function works", async () => {
expect(
await Eta.render(
"<%= await it.asyncTest() %>",
{ name: "Ada Lovelace", asyncTest: asyncTest },
{ async: true }
)
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.render("<%= @#$%^ %>", {}, { async: true });
}).rejects.toThrow(
buildRegEx(`
var tR='',__l,__lP,include=E.include.bind(E),includeFile=E.includeFile.bind(E)
function layout(p,d){__l=p;__lP=d}
tR+=E.e(@#$%^)
if(__l)tR=await includeFile(__l,Object.assign(it,{body:tR},__lP))
if(cb){cb(null,tR)} return tR
`)
);
});

it("Async template w/ syntax error passes error to callback", (done) => {
function cb(err: Error | null, _res?: string) {
expect(err).toBeTruthy();
expect((err as Error).message).toMatch(
buildRegEx(`
var tR='',__l,__lP,include=E.include.bind(E),includeFile=E.includeFile.bind(E)
function layout(p,d){__l=p;__lP=d}
tR+=E.e(@#$%^)
if(__l)tR=await includeFile(__l,Object.assign(it,{body:tR},__lP))
if(cb){cb(null,tR)} return tR
`)
);
done();
}

Eta.render("<%= @#$%^ %>", {}, { name: "Ada Lovelace", async: true }, cb);
await eta.renderStringAsync("<%= @#$%^ %>", {});
}).rejects.toThrow();
});
});
});

describe("Async Loops", () => {
// TODO
});
8 changes: 4 additions & 4 deletions test/compile-string.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ describe("Compile to String test", () => {
let include = (template, data) => this.render(template, data, options);
let includeAsync = (template, data) => this.renderAsync(template, data, options);
let __eta = {res: "", e: this.escapeFunction, f: this.filterFunction};
let __eta = {res: "", e: this.config.escapeFunction, f: this.config.filterFunction};
function layout(path, data) {
__eta.layout = path;
Expand Down Expand Up @@ -46,7 +46,7 @@ return __eta.res;
let include = (template, data) => this.render(template, data, options);
let includeAsync = (template, data) => this.renderAsync(template, data, options);
let __eta = {res: "", e: this.escapeFunction, f: this.filterFunction};
let __eta = {res: "", e: this.config.escapeFunction, f: this.config.filterFunction};
function layout(path, data) {
__eta.layout = path;
Expand Down Expand Up @@ -75,7 +75,7 @@ return __eta.res;
let include = (template, data) => this.render(template, data, options);
let includeAsync = (template, data) => this.renderAsync(template, data, options);
let __eta = {res: "", e: this.escapeFunction, f: this.filterFunction};
let __eta = {res: "", e: this.config.escapeFunction, f: this.config.filterFunction};
function layout(path, data) {
__eta.layout = path;
Expand Down Expand Up @@ -105,7 +105,7 @@ return __eta.res;
let include = (template, data) => this.render(template, data, options);
let includeAsync = (template, data) => this.renderAsync(template, data, options);
let __eta = {res: "", e: this.escapeFunction, f: this.filterFunction};
let __eta = {res: "", e: this.config.escapeFunction, f: this.config.filterFunction};
function layout(path, data) {
__eta.layout = path;
Expand Down
24 changes: 8 additions & 16 deletions test/compile.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
/* global it, expect, describe */

import { compile } from "../src/index";
import { buildRegEx } from "./err.spec";
import { Eta } from "../src/index";

const eta = new Eta();

const fs = require("fs"),
path = require("path"),
Expand All @@ -11,32 +12,23 @@ const complexTemplate = fs.readFileSync(filePath, "utf8");

describe("Compile test", () => {
it("parses a simple template", () => {
const str = compile("hi <%= hey %>");
const str = eta.compile("hi <%= hey %>");
expect(str).toBeTruthy();
});

it("works with plain string templates", () => {
const str = compile("hi this is a template");
const str = eta.compile("hi this is a template");
expect(str).toBeTruthy();
});

// TODO: Update
it("compiles complex template", () => {
const str = compile(complexTemplate);
const str = eta.compile(complexTemplate);
expect(str).toBeTruthy();
});

test("throws with bad inner JS syntax", () => {
expect(() => {
compile("<% hi (=h) %>");
}).toThrow(
buildRegEx(`
var tR='',__l,__lP,include=E.include.bind(E),includeFile=E.includeFile.bind(E)
function layout(p,d){__l=p;__lP=d}
hi (=h)
if(__l)tR=includeFile(__l,Object.assign(it,{body:tR},__lP))
if(cb){cb(null,tR)} return tR
`)
);
eta.compile("<% hi (=h) %>");
}).toThrow();
});
});
103 changes: 51 additions & 52 deletions test/config.spec.ts
Original file line number Diff line number Diff line change
@@ -1,78 +1,77 @@
/* global it, expect, describe */

import { render, defaultConfig } from "../src/index";
import { config, configure, getConfig } from "../src/config";
import { Eta } from "../src/index";

describe("Config Tests", () => {
it("Renders a simple template with default env", () => {
const res = render("hi <%= it.name %>", { name: "Ben" }, defaultConfig);
it("custom tags", () => {
const eta = new Eta({ tags: ["<<", ">>"] });
const res = eta.renderString("hi <<= it.name >>", { name: "Ben" });
expect(res).toEqual("hi Ben");
});

it("Renders a simple template with custom tags", () => {
const res = render("hi <<= it.name >>", { name: "Ben" }, { tags: ["<<", ">>"] });
expect(res).toEqual("hi Ben");
});

it("Renders a simple template with stored env", () => {
const res = render("<%= it.html %>", { html: "<p>Hi</p>" }, { autoEscape: false });
it("no autoescape", () => {
const eta = new Eta({ autoEscape: false });
const res = eta.renderString("<%= it.html %>", { html: "<p>Hi</p>" });
expect(res).toEqual("<p>Hi</p>"); // not escaped
});

it("config.filter works", () => {
it("filter function", () => {
const template = "My favorite food is <%= it.fav %>";
const baseEta = new Eta();

expect(render(template, {})).toEqual("My favorite food is undefined");
expect(baseEta.renderString(template, {})).toEqual("My favorite food is undefined");

expect(
render(
template,
{},
{
filter: function () {
return "apples";
},
}
)
).toEqual("My favorite food is apples");
const etaWithSimpleFilter = new Eta({
autoFilter: true,
// turn every value into "apples"
filterFunction: (_val) => "apples",
});

let timesFilterCalled = 0;
expect(etaWithSimpleFilter.renderString(template, {})).toEqual("My favorite food is apples");
});

expect(
render(
"<%= it.val1 %>, <%~ it.val2 %>, <%~ it.val3 %>",
{},
{
filter: function () {
timesFilterCalled++;
if (timesFilterCalled <= 1) {
return "The first";
} else {
return "another";
}
},
it("complex filter function", () => {
let timesFilterCalled = 0;
const eta = new Eta({
autoFilter: true,
filterFunction: function () {
timesFilterCalled++;
if (timesFilterCalled <= 1) {
return "The first";
} else {
return "another";
}
)
).toEqual("The first, another, another");
},
});

expect(eta.renderString("<%= it.val1 %>, <%~ it.val2 %>, <%~ it.val3 %>", {})).toEqual(
"The first, another, another"
);
});

it("Configure command works", () => {
const updatedConfig = configure({ tags: ["{{", "}}"] });
it("withConfig", () => {
const eta = new Eta();

const res = eta
.withConfig({ tags: ["{{", "}}"] })
.renderString("{{= it.name }}", { name: "John Appleseed" });

const res = render("{{= it.name }}", { name: "John Appleseed" });
expect(res).toEqual("John Appleseed");

expect(defaultConfig).toEqual(updatedConfig);
expect(defaultConfig.tags).toEqual(["{{", "}}"]);
// the original tags should remain unchanged
expect(eta.config.tags).toEqual(["<%", "%>"]);
});

it("config and defaultConfig are the same object", () => {
expect(defaultConfig).toEqual(config);
});
it("configure", () => {
const eta = new Eta();

eta.configure({ tags: ["{{", "}}"] });

const res = eta.renderString("{{= it.name }}", { name: "John Appleseed" });

expect(res).toEqual("John Appleseed");

it("getConfig creates a clone of config with no arguments", () => {
/* eslint-disable @typescript-eslint/ban-ts-comment */
// @ts-ignore (in this case, we're calling getConfig w/ 0 arguments even though it takes 1 or 2)
expect(getConfig()).toEqual(config);
// the original tags should have changed
expect(eta.config.tags).toEqual(["{{", "}}"]);
});
});
5 changes: 0 additions & 5 deletions test/err.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,6 @@

import { ParseErr } from "../src/err";

// So we can build a RegEx to test our errors against (used in other spec files)
export function buildRegEx(string: string) {
return RegExp(string.replace(/[.*+\-?^${}()|[\]\\]/g, "\\$&").replace(/\r\n|\n|\r/g, "\\n"));
}

describe("ParseErr", () => {
it("error throws correctly", () => {
try {
Expand Down
39 changes: 13 additions & 26 deletions test/render.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

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

const eta = new Eta();

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

it("renderString: template compiles", () => {
expect(eta.renderString("Hi <%= it.name%>", { name: "Ada Lovelace" })).toEqual(
"Hi Ada Lovelace"
Expand All @@ -20,32 +20,19 @@ describe("basic functionality", () => {
"Hi Ada Lovelace!"
);
});
it("renderStringAsync", async () => {
const template = "Hello <%= await it.getName() %>!";
const getName = () => {
return new Promise((res) => {
setTimeout(() => {
res("Ada");
}, 20);
});
};
expect(await eta.renderStringAsync(template, { getName })).toEqual("Hello Ada!");
});
});

// describe("render caching", () => {
// it("Simple template caches", () => {
// eta.renderString();
// render("Hi <%=it.name%>", { name: "Ada Lovelace" }, { cache: true, name: "template1" });
// expect(eta.templatesSync.get("template1")).toBeTruthy();
// });

// it("Simple template works again", () => {
// expect(
// render("This shouldn't show up", { name: "Ada Lovelace" }, { cache: true, name: "template1" })
// ).toEqual("Hi Ada Lovelace");
// });
// });
describe("render caching", () => {
const eta = new Eta({ cache: true });

eta.loadTemplate("@template1", "Hi <%=it.name%>");

it("Simple template caches", () => {
expect(eta.render("@template1", { name: "Ada Lovelace" })).toEqual("Hi Ada Lovelace");

expect(eta.templatesSync.get("@template1")).toBeTruthy();
});
});

describe("Renders with different scopes", () => {
it("Puts `it` in global scope with env.useWith", () => {
Expand Down

0 comments on commit 4fff2b5

Please sign in to comment.