Skip to content

Commit

Permalink
chore: update for eta v3
Browse files Browse the repository at this point in the history
  • Loading branch information
nebrelbug committed May 29, 2023
1 parent 6268397 commit 6eeffe9
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 125 deletions.
9 changes: 4 additions & 5 deletions test/compile-string.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/* global it, expect, describe */

import { Eta } from "../src/index";
import { compileToString } from "../src/compile-string";

const eta = new Eta();

Expand All @@ -13,7 +12,7 @@ const complexTemplate = fs.readFileSync(filePath, "utf8");

describe("Compile to String test", () => {
it("compiles a simple template", () => {
const str = compileToString.call(eta, "hi <%= it.name %>");
const str = eta.compileToString("hi <%= it.name %>");
expect(str).toEqual(`
let include = (template, data) => this.render(template, data, options);
let includeAsync = (template, data) => this.renderAsync(template, data, options);
Expand Down Expand Up @@ -42,7 +41,7 @@ return __eta.res;
});

it("compiles a simple template with a raw tag", () => {
const str = compileToString.call(eta, "hi <%~ it.name %>");
const str = eta.compileToString("hi <%~ it.name %>");
expect(str).toEqual(`
let include = (template, data) => this.render(template, data, options);
let includeAsync = (template, data) => this.renderAsync(template, data, options);
Expand Down Expand Up @@ -71,7 +70,7 @@ return __eta.res;
});

it("works with whitespace trimming", () => {
const str = compileToString.call(eta, "hi\n<%- = it.firstname-%>\n<%_ = it.lastname_%>");
const str = eta.compileToString("hi\n<%- = it.firstname-%>\n<%_ = it.lastname_%>");
expect(str).toEqual(`
let include = (template, data) => this.render(template, data, options);
let includeAsync = (template, data) => this.renderAsync(template, data, options);
Expand Down Expand Up @@ -101,7 +100,7 @@ return __eta.res;
});

it("compiles complex template", () => {
const str = compileToString.call(eta, complexTemplate);
const str = eta.compileToString(complexTemplate);
expect(str).toEqual(`
let include = (template, data) => this.render(template, data, options);
let includeAsync = (template, data) => this.renderAsync(template, data, options);
Expand Down
23 changes: 11 additions & 12 deletions test/parse.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/* global it, expect, describe */

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

const eta = new Eta();

Expand All @@ -14,32 +13,32 @@ const complexTemplate = fs.readFileSync(filePath, "utf8");

describe("parse test", () => {
it("parses a simple template", () => {
const buff = parse.call(eta, "hi <%= hey %>");
const buff = eta.parse("hi <%= hey %>");
expect(buff).toEqual(["hi ", { val: "hey", t: "i" }]);
});

it("parses a raw tag", () => {
const buff = parse.call(eta, "hi <%~ hey %>");
const buff = eta.parse("hi <%~ hey %>");
expect(buff).toEqual(["hi ", { val: "hey", t: "r" }]);
});

it("works with whitespace trimming", () => {
const buff = parse.call(eta, "hi\n<%- = hey-%> <%_ = hi _%>");
const buff = eta.parse("hi\n<%- = hey-%> <%_ = hi _%>");
expect(buff).toEqual(["hi", { val: "hey", t: "i" }, { val: "hi", t: "i" }]);
});

it("works with multiline comments", () => {
const buff = parse.call(eta, "hi <% /* comment contains delimiter %> */ %>");
const buff = eta.parse("hi <% /* comment contains delimiter %> */ %>");
expect(buff).toEqual(["hi ", { val: "/* comment contains delimiter %> */", t: "e" }]);
});

it("parses with simple template literal", () => {
const buff = parse.call(eta, "hi <%= `template %> ${value}` %>");
const buff = eta.parse("hi <%= `template %> ${value}` %>");
expect(buff).toEqual(["hi ", { val: "`template %> ${value}`", t: "i" }]);
});

it("compiles complex template", () => {
const buff = parse.call(eta, complexTemplate);
const buff = eta.parse(complexTemplate);
expect(buff).toEqual([
"Hi\\n",
{ t: "e", val: 'console.log("Hope you like Eta!")' },
Expand Down Expand Up @@ -72,13 +71,13 @@ describe("parse test", () => {

test("throws with unclosed tag", () => {
expect(() => {
parse.call(eta, '<%hi("hey")');
eta.parse('<%hi("hey")');
}).toThrowError("hi");
});

test("throws with unclosed single-quote string", () => {
expect(() => {
parse.call(eta, "<%= ' %>");
eta.parse("<%= ' %>");
}).toThrowError(`unclosed string at line 1 col 5:
<%= ' %>
Expand All @@ -87,7 +86,7 @@ describe("parse test", () => {

test("throws with unclosed double-quote string", () => {
expect(() => {
parse.call(eta, '<%= " %>');
eta.parse('<%= " %>');
}).toThrowError(`unclosed string at line 1 col 5:
<%= " %>
Expand All @@ -96,7 +95,7 @@ describe("parse test", () => {

test("throws with unclosed template literal", () => {
expect(() => {
parse.call(eta, "<%= ` %>");
eta.parse("<%= ` %>");
}).toThrowError(`unclosed string at line 1 col 5:
<%= \` %>
Expand All @@ -105,7 +104,7 @@ describe("parse test", () => {

test("throws with unclosed multi-line comment", () => {
expect(() => {
parse.call(eta, "<%= /* %>");
eta.parse("<%= /* %>");
}).toThrowError(`unclosed comment at line 1 col 5:
<%= /* %>
Expand Down
54 changes: 49 additions & 5 deletions test/plugins.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* global it, expect, describe */
import * as Eta from "../src/index";
import { Eta } from "../src/index";

import { EtaConfig } from "../src/config";
import { AstObject } from "../src/parse";

Expand All @@ -17,12 +18,55 @@ function myPlugin() {
};
}

const template = `<%= it.val %> <%= @@num@@ %>.`;
const emojiTransform = () => {
return {
processTemplate: function (str: string) {
return str.replace(":thumbsup:", "👍");
},
};
};

const capitalizeCool = () => {
return {
processTemplate: function (str: string) {
return str.replace("cool", "COOL");
},
};
};

const replaceThumbsUp = () => {
return {
processTemplate: function (str: string) {
return str.replace("👍", "✨");
},
};
};

describe("Plugins", () => {
it("Plugins function properly", () => {
expect(Eta.render(template, { val: "value" }, { plugins: [myPlugin()] })).toEqual(
"value 2352.3.String to append"
);
const eta = new Eta({ plugins: [myPlugin()] });
const template = `<%= it.val %> <%= @@num@@ %>.`;

expect(eta.renderString(template, { val: "value" })).toEqual("value 2352.3.String to append");
});
});

describe("processTemplate plugin", () => {
it("Simple plugin works correctly", () => {
const eta = new Eta({ plugins: [emojiTransform()] });
const template = ":thumbsup:";

const res = eta.renderString(template, {});

expect(res).toEqual("👍");
});

it("Multiple chained plugins work correctly", () => {
const eta = new Eta({ plugins: [emojiTransform(), capitalizeCool(), replaceThumbsUp()] });
const template = ":thumbsup: This is a cool template";

const res = eta.renderString(template, {});

expect(res).toEqual("✨ This is a COOL template");
});
});
144 changes: 41 additions & 103 deletions test/render.spec.ts
Original file line number Diff line number Diff line change
@@ -1,120 +1,58 @@
/* global it, expect, describe */

import render, { renderAsync } from "../src/render";
import compile from "../src/compile";
import { templates } from "../src/containers";
import { Eta } from "../src/index";

describe("Simple Render checks", () => {
describe("Render works", () => {
it("Simple template compiles", () => {
expect(render("Hi <%= it.name%>", { name: "Ada Lovelace" })).toEqual("Hi Ada Lovelace");
});
it("String trimming works", () => {
expect(render("Hi \n<%- =it.name_%> !", { name: "Ada Lovelace" })).toEqual(
"Hi Ada Lovelace!"
);
});
it("Rendering function works", () => {
expect(render(compile("Hi \n<%- =it.name_%> !"), { name: "Ada Lovelace" })).toEqual(
"Hi Ada Lovelace!"
);
});
it("Rendering function works", async () => {
const template = "Hello <%= await it.getName() %>!";
const getName = () => {
return new Promise((res) => {
setTimeout(() => {
res("Ada");
}, 20);
});
};
expect(await render(template, { getName }, { async: true })).toEqual("Hello Ada!");
});
it("Rendering async function works", async () => {
const template = "Hello <%= await it.getName() %>!";
const getName = () => {
return new Promise((res) => {
setTimeout(() => {
res("Ada");
}, 20);
});
};
expect(await renderAsync(template, { getName })).toEqual("Hello Ada!");
});
});
});
const eta = new Eta();

describe("Render caching checks", () => {
it("Simple template caches", () => {
render("Hi <%=it.name%>", { name: "Ada Lovelace" }, { cache: true, name: "template1" });
expect(templates.get("template1")).toBeTruthy();
describe("basic functionality", () => {
it("renderString: template compiles", () => {
expect(eta.renderString("Hi <%= it.name%>", { name: "Ada Lovelace" })).toEqual(
"Hi Ada Lovelace"
);
});

it("Simple template works again", () => {
expect(
render("This shouldn't show up", { name: "Ada Lovelace" }, { cache: true, name: "template1" })
).toEqual("Hi Ada Lovelace");
it("renderString: string trimming", () => {
expect(eta.renderString("Hi \n<%- =it.name_%> !", { name: "Ada Lovelace" })).toEqual(
"Hi Ada Lovelace!"
);
});
});

describe("Renders with different scopes", () => {
it("Puts `it` in global scope with env.useWith", () => {
expect(render("Hi <%=name%>", { name: "Ada Lovelace" }, { useWith: true })).toEqual(
"Hi Ada Lovelace"
it("render: passing in a template function", () => {
expect(eta.render(eta.compile("Hi \n<%- =it.name_%> !"), { name: "Ada Lovelace" })).toEqual(
"Hi Ada Lovelace!"
);
});
});

describe("processTemplate plugin", () => {
it("Simple plugin works correctly", () => {
const template = ":thumbsup:";

const emojiTransform = {
processTemplate: function (str: string) {
return str.replace(":thumbsup:", "👍");
},
it("renderStringAsync", async () => {
const template = "Hello <%= await it.getName() %>!";
const getName = () => {
return new Promise((res) => {
setTimeout(() => {
res("Ada");
}, 20);
});
};

const res = render(
template,
{},
{
plugins: [emojiTransform],
}
);

expect(res).toEqual("👍");
expect(await eta.renderStringAsync(template, { getName })).toEqual("Hello Ada!");
});
});

it("Multiple chained plugins work correctly", () => {
const template = ":thumbsup: This is a cool template";
// 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();
// });

const emojiTransform = {
processTemplate: function (str: string) {
return str.replace(":thumbsup:", "👍");
},
};
// it("Simple template works again", () => {
// expect(
// render("This shouldn't show up", { name: "Ada Lovelace" }, { cache: true, name: "template1" })
// ).toEqual("Hi Ada Lovelace");
// });
// });

const capitalizeCool = {
processTemplate: function (str: string) {
return str.replace("cool", "COOL");
},
};

const replaceThumbsUp = {
processTemplate: function (str: string) {
return str.replace("👍", "✨");
},
};
describe("Renders with different scopes", () => {
it("Puts `it` in global scope with env.useWith", () => {
const etaWithUseWith = new Eta({ useWith: true });

const res = render(
template,
{},
{
plugins: [emojiTransform, capitalizeCool, replaceThumbsUp],
}
expect(etaWithUseWith.renderString("Hi <%=name%>", { name: "Ada Lovelace" })).toEqual(
"Hi Ada Lovelace"
);

expect(res).toEqual("✨ This is a COOL template");
});
});

0 comments on commit 6eeffe9

Please sign in to comment.