Skip to content

Commit

Permalink
refactor: accept an object of options when instantiating edge
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Sep 14, 2019
1 parent c3c69e4 commit 4041b38
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 48 deletions.
3 changes: 1 addition & 2 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
* file that was distributed with this source code.
*/

import { Loader } from './src/Loader'
import { Edge } from './src/Edge'

export {
Expand All @@ -29,5 +28,5 @@ export {

export { Edge }

const edge = new Edge(new Loader())
const edge = new Edge()
export default edge
10 changes: 10 additions & 0 deletions src/Contracts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,21 @@ export interface EdgeRendererContract {
render (templatePath: string, state?: any): string,
}

/**
* Shape of options that can be passed to the
* edge constructor
*/
export type EdgeOptions = {
loader?: LoaderContract,
cache?: boolean,
}

/**
* Shape of the main module
*/
export interface EdgeContract {
loader: LoaderContract,
compiler: CompilerContract,

registerTag (tag: TagContract): this,
registerTemplate (templatePath: string, contents: LoaderTemplate): this,
Expand Down
15 changes: 11 additions & 4 deletions src/Edge/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ import { Compiler } from '../Compiler'
import { EdgeRenderer } from '../Renderer'

import {
LoaderContract,
TagContract,
LoaderTemplate,
EdgeOptions,
EdgeContract,
LoaderTemplate,
EdgeRendererContract,
} from '../Contracts'

Expand All @@ -36,12 +36,19 @@ export class Edge implements EdgeContract {
*/
private _tags = Object.assign({}, Tags)

/**
* The loader to load templates. A loader can read and return
* templates from anywhere. The default loader reads files
* from the disk
*/
public loader = this._options.loader || new Loader()

/**
* The underlying compiler in use
*/
public compiler = new Compiler(this.loader, this._tags, false)
public compiler = new Compiler(this.loader, this._tags, !!this._options.cache)

constructor (public loader: LoaderContract = new Loader()) {
constructor (private _options: EdgeOptions = {}) {
}

/**
Expand Down
59 changes: 17 additions & 42 deletions test/edge.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import { join } from 'path'
import { Filesystem } from '@poppinss/dev-utils'

import { Edge } from '../src/Edge'
import { Loader } from '../src/Loader'

const fs = new Filesystem(join(__dirname, 'views'))

Expand All @@ -22,32 +21,32 @@ test.group('Edge', (group) => {
})

test('mount default disk', (assert) => {
const edge = new Edge(new Loader())
const edge = new Edge()
edge.mount(fs.basePath)
assert.deepEqual(edge.loader.mounted, { default: fs.basePath })
})

test('mount named disk', (assert) => {
const edge = new Edge(new Loader())
const edge = new Edge()
edge.mount('foo', fs.basePath)
assert.deepEqual(edge.loader.mounted, { foo: fs.basePath })
})

test('unmount named disk', (assert) => {
const edge = new Edge(new Loader())
const edge = new Edge()
edge.mount('foo', fs.basePath)
edge.unmount('foo')
assert.deepEqual(edge.loader.mounted, {})
})

test('register globals', (assert) => {
const edge = new Edge(new Loader())
const edge = new Edge()
edge.global('foo', 'bar')
assert.deepEqual(edge['_globals'].foo, 'bar')
})

test('add a custom tag to the tags list', (assert) => {
const edge = new Edge(new Loader())
const edge = new Edge()

class MyTag {
public static tagName = 'mytag'
Expand All @@ -62,15 +61,15 @@ test.group('Edge', (group) => {
})

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

edge.mount(fs.basePath)
assert.equal(edge.render('foo', { username: 'virk' }).trim(), 'Hello virk')
})

test('pass locals to the view context', async (assert) => {
const edge = new Edge(new Loader())
const edge = new Edge()
await fs.add('foo.edge', `Hello {{ username || 'guest' }}`)

edge.mount(fs.basePath)
Expand All @@ -83,7 +82,7 @@ test.group('Edge', (group) => {
})

test('register a template as a string', async (assert) => {
const edge = new Edge(new Loader())
const edge = new Edge()

edge.registerTemplate('foo', {
template: `Hello {{ username }}`,
Expand All @@ -93,7 +92,7 @@ test.group('Edge', (group) => {
})

test('register a template on a named disk', async (assert) => {
const edge = new Edge(new Loader())
const edge = new Edge()
edge.mount('hello', fs.basePath)

edge.registerTemplate('hello::foo', {
Expand All @@ -107,10 +106,7 @@ test.group('Edge', (group) => {
assert.plan(1)
await fs.add('foo.edge', '@if(1 + 1)')

const loader = new Loader()
loader.mount('default', fs.basePath)

const edge = new Edge(new Loader())
const edge = new Edge()
edge.mount(fs.basePath)

try {
Expand All @@ -124,10 +120,7 @@ test.group('Edge', (group) => {
assert.plan(1)
await fs.add('foo.edge', 'Hello {{ a,:b }}')

const loader = new Loader()
loader.mount('default', fs.basePath)

const edge = new Edge(new Loader())
const edge = new Edge()
edge.mount(fs.basePath)

try {
Expand All @@ -142,10 +135,7 @@ test.group('Edge', (group) => {
await fs.add('foo.edge', `@layout('bar')`)
await fs.add('bar.edge', `@if(username)`)

const loader = new Loader()
loader.mount('default', fs.basePath)

const edge = new Edge(new Loader())
const edge = new Edge()
edge.mount(fs.basePath)

try {
Expand All @@ -160,10 +150,7 @@ test.group('Edge', (group) => {
await fs.add('foo.edge', `@layout('bar')`)
await fs.add('bar.edge', `{{ a:b }}`)

const loader = new Loader()
loader.mount('default', fs.basePath)

const edge = new Edge(new Loader())
const edge = new Edge()
edge.mount(fs.basePath)

try {
Expand All @@ -178,10 +165,7 @@ test.group('Edge', (group) => {
await fs.add('foo.edge', `@include('bar')`)
await fs.add('bar.edge', `@if(username)`)

const loader = new Loader()
loader.mount('default', fs.basePath)

const edge = new Edge(new Loader())
const edge = new Edge()
edge.mount(fs.basePath)

try {
Expand All @@ -196,10 +180,7 @@ test.group('Edge', (group) => {
await fs.add('foo.edge', `@include('bar')`)
await fs.add('bar.edge', `{{ a:b }}`)

const loader = new Loader()
loader.mount('default', fs.basePath)

const edge = new Edge(new Loader())
const edge = new Edge()
edge.mount(fs.basePath)

try {
Expand All @@ -214,10 +195,7 @@ test.group('Edge', (group) => {
await fs.add('foo.edge', `@!component('bar')`)
await fs.add('bar.edge', `@if(username)`)

const loader = new Loader()
loader.mount('default', fs.basePath)

const edge = new Edge(new Loader())
const edge = new Edge()
edge.mount(fs.basePath)

try {
Expand All @@ -232,10 +210,7 @@ test.group('Edge', (group) => {
await fs.add('foo.edge', `@!component('bar')`)
await fs.add('bar.edge', `{{ a:b }}`)

const loader = new Loader()
loader.mount('default', fs.basePath)

const edge = new Edge(new Loader())
const edge = new Edge()
edge.mount(fs.basePath)

try {
Expand Down

0 comments on commit 4041b38

Please sign in to comment.