1 | import * as path from 'path';
|
2 | import * as fs from 'fs';
|
3 | import * as chai from 'chai';
|
4 | import * as sinonChai from 'sinon-chai';
|
5 | import * as revHash from 'rev-hash';
|
6 | import * as sinon from 'sinon';
|
7 | import { bundle, getOutputFileName } from '../lib/html-import-template-bundler';
|
8 | import { BundleConfig } from '../lib/models';
|
9 |
|
10 | let expect = chai.expect;
|
11 | chai.use(sinonChai);
|
12 |
|
13 | describe('A template bundler', () => {
|
14 | it('writes the bundle to disk', async () => {
|
15 | let sandbox = sinon.sandbox.create();
|
16 | sandbox.stub(fs, 'writeFileSync').returns(0);
|
17 | sandbox.stub(fs, 'existsSync').returns(false);
|
18 | let cfg = { baseURL: '.', bundleName: 'tmp-bundle', includes: ['test/fixture/tmp1.html', 'test/fixture/tmp2.html'] } as BundleConfig;
|
19 | await bundle(cfg);
|
20 | let outPath = `${path.resolve(cfg.bundleName)}.html`;
|
21 |
|
22 | expect(fs.writeFileSync).to.be.calledWith(outPath, '<template id="test/fixture/tmp1.html">test</template>\n<template id="test/fixture/tmp2.html">test</template>');
|
23 | sandbox.restore();
|
24 | });
|
25 |
|
26 | it('appends a hash to file name', () => {
|
27 | let baseURL = 'url';
|
28 | let bundleName = 'bundle';
|
29 | let content = 'test\ntest';
|
30 | let expectedHash = revHash(new Buffer(content, 'utf-8'));
|
31 | let revUrl = getOutputFileName(baseURL, bundleName, content, true);
|
32 | let revLessUrl = getOutputFileName(baseURL, bundleName, content, false);
|
33 |
|
34 | expect(revUrl).to.contain(expectedHash);
|
35 | expect(revLessUrl).to.not.contain(expectedHash);
|
36 | });
|
37 | });
|