Skip to content

Commit

Permalink
Add testing for filepath caching
Browse files Browse the repository at this point in the history
  • Loading branch information
nebrelbug committed Sep 16, 2020
1 parent aec8d91 commit 70bb7e5
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 1 deletion.
38 changes: 37 additions & 1 deletion test/file-utils.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* global it, expect, describe */

import { loadFile, templates } from '../src/index'
import { renderFile, loadFile, templates } from '../src/index'
import { config } from '../src/config'

var path = require('path'),
Expand All @@ -13,3 +13,39 @@ describe('File tests', () => {
expect(templates.get(filePath)({ name: 'Ben' }, config)).toBeTruthy()
})
})

describe('Filepath caching', () => {
it('Filepath caching works as expected', async () => {
// This test renders templates/has-include.eta with caching enabled, then checks to make sure
// `config.filepathCache` contains the expected result afterward

let viewsDir = path.join(__dirname, 'templates')

let templateResult = await renderFile('has-include', {}, { views: viewsDir, cache: true })

expect(templateResult).toEqual(
`This is the outermost template. Now we'll include a partial
===========================================================
This is a partial.
Hi Test Runner`
)

// The cache is indexed by {filename, path, root, views} (JSON.stringify ignores keys with undefined as their value)

// Filepath caching is based on the premise that given the same path, includer filename, root directory, and views directory (or directories)
// the getPath function will always return the same result (assuming that caching is enabled and we're not expecting the templates to change)

let pathToHasInclude = `{"filename":"${viewsDir}/has-include.eta","path":"./partial","views":"${viewsDir}"}`

let pathToPartial = `{"filename":"${viewsDir}/partial.eta","path":"./simple","views":"${viewsDir}"}`

let pathToSimple = `{"path":"has-include","views":"${viewsDir}"}`

expect(config.filepathCache).toEqual({
[pathToHasInclude]: `${viewsDir}/partial.eta`,
[pathToPartial]: `${viewsDir}/simple.eta`,
[pathToSimple]: `${viewsDir}/has-include.eta`
})
})
})
4 changes: 4 additions & 0 deletions test/templates/has-include.eta
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
This is the outermost template. Now we'll include a partial

===========================================================
<%~ includeFile('./partial') %>
2 changes: 2 additions & 0 deletions test/templates/partial.eta
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
This is a partial.
<%~ includeFile('./simple', {name: 'Test Runner'}) %>

0 comments on commit 70bb7e5

Please sign in to comment.