Skip to content

Commit 11d082d

Browse files
committed
adding unit tests
1 parent 9a78f27 commit 11d082d

File tree

11 files changed

+261
-3
lines changed

11 files changed

+261
-3
lines changed

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,15 @@
1818
"dustjs-linkedin": "^2.6.0"
1919
},
2020
"devDependencies": {
21+
"chai": "^3.5.0",
2122
"dustjs-linkedin": "^2.6.0",
23+
"mocha": "^3.2.0",
2224
"rollup": "^0.41.6",
2325
"rollup-watch": "^3.2.2"
2426
},
2527
"scripts": {
2628
"build": "rollup -c",
27-
"build-w": "rollup -c -w"
29+
"build-w": "rollup -c -w",
30+
"test": "rollup -c && mocha test/index.js"
2831
}
2932
}

test/cases/partials/child.dust

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<h1>{text}</h1>

test/cases/partials/index.dust

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<div>
2+
{>"./child.dust" text="Heading" /}
3+
<p>Body</p>
4+
</div>

test/cases/partials/index.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import template from './index.dust'
2+
import { render } from '../utils.js'
3+
4+
export default {
5+
expected: '<div><h1>Heading</h1><p>Body</p></div>',
6+
actual: render(template)
7+
}

test/cases/simple/index.dust

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<span>{name}</span>

test/cases/simple/index.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import template from './index.dust'
2+
import { render } from '../utils.js'
3+
4+
export default {
5+
expected: '<span>Chris</span>',
6+
actual: render(template, {
7+
name: 'Chris'
8+
})
9+
}

test/cases/utils.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { render as dustRender } from 'dustjs-linkedin'
2+
3+
/**
4+
* Synchronous dust render
5+
*/
6+
export function render (template, data = {}) {
7+
let result
8+
9+
dustRender(template, data, (error, output) => {
10+
if (error) {
11+
throw error
12+
} else {
13+
result = output
14+
}
15+
})
16+
17+
return result
18+
}

test/cases/whitespace/index.dust

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<span>one</span>
2+
3+
<span>two</span>

test/cases/whitespace/index.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import template from './index.dust'
2+
import { render } from '../utils.js'
3+
4+
export default {
5+
expected: '<span>one</span>\n\n<span>two</span>\n',
6+
actual: render(template)
7+
}

test/index.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
const dustjs = require('../build/index.js')
2+
const { expect } = require('chai')
3+
const { rollup } = require('rollup')
4+
const { join } = require('path')
5+
6+
/**
7+
* Runs provided source code as a node module
8+
*/
9+
function runAsModule (source) {
10+
const run = new Function('module', 'exports', 'require', source)
11+
const module = {
12+
exports: {}
13+
}
14+
15+
run(module, module.exports, require)
16+
return module.exports
17+
}
18+
19+
function runTestCase (name, options = {}) {
20+
const config = {
21+
compile: {
22+
entry: join(__dirname, `cases/${name}/index.js`),
23+
external: ['dustjs-linkedin'],
24+
plugins: [dustjs(options)]
25+
},
26+
generate: {
27+
format: 'cjs',
28+
}
29+
}
30+
31+
return rollup(config.compile)
32+
.then(function (bundle) {
33+
const { code } = bundle.generate(config.generate)
34+
const { actual, expected } = runAsModule(code)
35+
36+
expect(actual).to.equal(expected)
37+
})
38+
}
39+
40+
describe('dustjs plugin', () => {
41+
it(
42+
'should compile templates when imported',
43+
() => runTestCase('simple')
44+
)
45+
46+
it(
47+
'should dynamically import partials',
48+
() => runTestCase('partials')
49+
)
50+
51+
it(
52+
'should preserve whitespace when the option is passed',
53+
() => runTestCase('whitespace', { whitespace: true })
54+
)
55+
})

0 commit comments

Comments
 (0)