-
-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
105 additions
and
125 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
}); | ||
}); |