-
-
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
6 changed files
with
89 additions
and
155 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,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(["{{", "}}"]); | ||
}); | ||
}); |
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