-
-
Notifications
You must be signed in to change notification settings - Fork 290
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
10 changed files
with
136 additions
and
2 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
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 |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import {expect} from "chai"; | ||
import {renderFile} from "ejs"; | ||
import {dirname} from "node:path"; | ||
import {getEngineFixture} from "../../test/getEngineFixture"; | ||
import {EtaEngine} from "./EtaEngine"; | ||
|
||
describe("EtaEngine", () => { | ||
it("should render the given content", async () => { | ||
const {render, locals, $compile, template} = await getEngineFixture({ | ||
token: EtaEngine | ||
}); | ||
|
||
await render(); | ||
|
||
expect(await render()).to.eq("<p>Tobi</p>\n"); | ||
expect($compile()).to.have.been.callCount(2); | ||
expect($compile()).to.have.been.calledWithExactly(template, {...locals, cache: false}); | ||
}); | ||
|
||
it("should render the given content (by string - no cache)", async () => { | ||
const {render, locals, $compile, template} = await getEngineFixture({token: EtaEngine}); | ||
await render(); | ||
|
||
expect(await render()).to.eq("<p>Tobi</p>\n"); | ||
expect($compile()).to.have.been.callCount(2); | ||
expect($compile()).to.have.been.calledWithExactly(template, {...locals, cache: false}); | ||
}); | ||
it("should render the given content (by string - with cache)", async () => { | ||
const {render, locals, $compile, template} = await getEngineFixture({token: EtaEngine, cache: true}); | ||
await render(); | ||
|
||
expect(await render()).to.eq("<p>Tobi</p>\n"); | ||
expect($compile()).to.have.been.callCount(2); | ||
expect($compile()).to.have.been.calledWithExactly(template, {...locals, cache: true}); | ||
}); | ||
it("should render the given content (by file - no cache)", async () => { | ||
const {renderFile, locals, $compileFile, path, template} = await getEngineFixture({token: EtaEngine, useTemplateName: true}); | ||
|
||
await renderFile({ | ||
views: dirname(path) | ||
}); | ||
|
||
const content = await renderFile({ | ||
views: dirname(path) | ||
}); | ||
|
||
expect(content).to.eq("<p>Tobi</p>\n"); | ||
expect($compileFile()).to.have.been.callCount(2); | ||
expect($compileFile()).to.have.been.calledWithExactly("user", { | ||
...locals, | ||
partials: undefined, | ||
filename: "user", | ||
cache: false, | ||
views: dirname(path) | ||
}); | ||
}); | ||
it("should render the given content (by file - with cache)", async () => { | ||
const {renderFile, locals, $compileFile, path, template} = await getEngineFixture({ | ||
token: EtaEngine, | ||
useTemplateName: true, | ||
cache: true | ||
}); | ||
|
||
await renderFile({ | ||
views: dirname(path) | ||
}); | ||
const content = await renderFile(); | ||
|
||
expect(content).to.eq("<p>Tobi</p>\n"); | ||
expect($compileFile()).to.have.been.callCount(1); | ||
expect($compileFile()).to.have.been.calledWithExactly("user", { | ||
...locals, | ||
partials: undefined, | ||
filename: "user", | ||
cache: true, | ||
views: dirname(path) | ||
}); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import {ViewEngine} from "../decorators/viewEngine"; | ||
import {Engine, EngineOptions} from "./Engine"; | ||
|
||
@ViewEngine("eta", { | ||
requires: "eta" | ||
}) | ||
export class EtaEngine extends Engine { | ||
protected cache = new Map(); | ||
|
||
protected getEngine(options: EngineOptions) { | ||
const root = options.root || options.views; | ||
const key = String(root || "default"); | ||
|
||
if (!this.cache.get(key)) { | ||
const {Eta} = this.engine; | ||
|
||
this.cache.set( | ||
key, | ||
new Eta({ | ||
...options, | ||
views: root | ||
}) | ||
); | ||
} | ||
|
||
return this.cache.get(key); | ||
} | ||
|
||
protected $compile(template: string, options: any) { | ||
const eta = this.getEngine(options); | ||
|
||
return () => eta.renderStringAsync(template, options); | ||
} | ||
|
||
protected $compileFile(file: string, options: EngineOptions): Promise<(options: EngineOptions) => Promise<string>> { | ||
const eta = this.getEngine(options); | ||
|
||
return Promise.resolve(() => eta.renderAsync(file, options)); | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
<p><%= it.user.name %></p> |
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