Skip to content

Commit

Permalink
fix: invoke Tag.run method when registering the tag
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Sep 23, 2019
1 parent 40c03a6 commit 386a8a9
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/Edge/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import * as Tags from '../Tags'
import { Loader } from '../Loader'
import { Compiler } from '../Compiler'
import { EdgeRenderer } from '../Renderer'
import { Context } from '../Context'

import {
TagContract,
Expand All @@ -34,7 +35,7 @@ export class Edge implements EdgeContract {
* List of registered tags. Adding new tags will only impact
* this list
*/
private _tags = Object.assign({}, Tags)
private _tags = {}

/**
* The loader to load templates. A loader can read and return
Expand All @@ -49,6 +50,7 @@ export class Edge implements EdgeContract {
public compiler = new Compiler(this.loader, this._tags, !!this._options.cache)

constructor (private _options: EdgeOptions = {}) {
Object.keys(Tags).forEach((name) => this.registerTag(Tags[name]))
}

/**
Expand Down Expand Up @@ -125,6 +127,10 @@ export class Edge implements EdgeContract {
* ```
*/
public registerTag (tag: TagContract): this {
if (typeof (tag.run) === 'function') {
tag.run(Context)
}

this._tags[tag.tagName] = tag
return this
}
Expand Down
21 changes: 21 additions & 0 deletions test/edge.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,27 @@ test.group('Edge', (group) => {
assert.deepEqual(edge.compiler['_tags'].mytag, MyTag)
})

test('invoke tag run method when registering the tag', (assert) => {
assert.plan(2)

const edge = new Edge()

class MyTag {
public static tagName = 'mytag'
public static block = true
public static seekable = true
public static compile (): void {
}

public static run (): void {
assert.isTrue(true)
}
}

edge.registerTag(MyTag)
assert.deepEqual(edge.compiler['_tags'].mytag, MyTag)
})

test('render a view using the render method', async (assert) => {
const edge = new Edge()
await fs.add('foo.edge', 'Hello {{ username }}')
Expand Down

0 comments on commit 386a8a9

Please sign in to comment.