Resolve issue #599

This commit is contained in:
Eric MORAND 2023-12-06 12:58:39 +00:00
parent 2aaf14c6da
commit 048b8c78ee
2 changed files with 27 additions and 1 deletions

View File

@ -127,7 +127,7 @@ export interface TwingEnvironment {
*
* When a template is encountered, Twing environment emits a `template` event with the name of the encountered template and the source of the template that initiated the loading.
*/
on(eventName: "load", listener: (name: string, from: TwingSource | null) => void): void;
on(eventName: "load", listener: (name: string, from: string | null) => void): void;
/**
* Converts a token list to a template.

View File

@ -2,6 +2,7 @@ import * as tape from "tape";
import {createEnvironment} from "../../../../../src/lib/environment";
import {createFilesystemLoader, TwingFilesystemLoaderFilesystem} from "../../../../../src/lib/loader/filesystem";
import {spy} from "sinon";
import {createArrayLoader} from "../../../../../src/lib/loader/array";
tape('createEnvironment::loadTemplate', ({test}) => {
test('cache the loaded template under it fully qualified name', ({same, end}) => {
@ -40,4 +41,29 @@ tape('createEnvironment::loadTemplate', ({test}) => {
})
.finally(end);
});
test('emits a "load" event', ({same, end}) => {
const environment = createEnvironment(createArrayLoader({
index: `{{ include("partial") }}`,
partial: ``
}));
const loadedTemplates: Array<[string, string | null]> = [];
environment.on("load", (name, from) => {
loadedTemplates.push([name, from]);
});
return environment.loadTemplate(('index'))
.then((template) => {
return template.render({});
})
.then(() => {
same(loadedTemplates, [
['index', null],
['partial', 'index']
]);
})
.finally(end);
});
});