|
| 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