UNPKG

3.04 kBPlain TextView Raw
1import * as sysUtil from 'systemjs-builder/lib/utils.js';
2import * as fs from 'fs';
3import * as chai from 'chai';
4import {BundleConfig} from '../lib/models';
5import * as sinon from 'sinon';
6import * as sinonChi from 'sinon-chai';
7import * as bundler from '../lib/bundler';
8import * as Builder from 'systemjs-builder';
9import * as serializer from '../lib/config-serializer';
10
11let expect = chai.expect;
12chai.use(sinonChi);
13
14describe('inject bundle', () => {
15 let sandbox: sinon.SinonSandbox;
16 beforeEach(() => {
17 sandbox = sinon.sandbox.create();
18 });
19
20 afterEach(() => {
21 sandbox.restore();
22 });
23
24 it('saves bundle config to disk', () => {
25 let appCfg = { bundles: {} };
26 let configPath = '';
27
28 sandbox.stub(serializer, 'saveAppConfig');
29 sandbox.stub(serializer, 'getAppConfig').returns(appCfg);
30 sandbox.stub(sysUtil, 'toFileURL');
31
32 let cfg = {
33 baseURL: '',
34 options: {
35 injectionConfigPath: configPath
36 }
37 };
38
39 let builder = new Builder('.');
40 sandbox.stub(builder, 'getCanonicalName');
41
42 let output = { modules: [], source: '', sourceMap: '' };
43 let outfile = '';
44
45 bundler.injectBundle(builder, output, outfile, cfg as any as BundleConfig);
46 expect(serializer.saveAppConfig).to.have.been.calledOnce;
47 });
48});
49
50describe('write bundle output', () => {
51 let sandbox: sinon.SinonSandbox;
52 beforeEach(() => {
53 sandbox = sinon.sandbox.create();
54 });
55
56 afterEach(() => {
57 sandbox.restore();
58 });
59
60 it('writes the bundle file to disk', () => {
61 sandbox.stub(fs, 'writeFileSync');
62 sandbox.stub(fs, 'mkdirSync');
63 bundler.writeOutput({ source: 'sdfsdf', sourceMap: '', modules: [] }, 'outfile.js', true, false);
64 expect(fs.writeFileSync).to.have.been.calledOnce;
65 });
66
67 it('creates output directory when not exists', () => {
68 sandbox.stub(fs, 'mkdirSync');
69 sandbox.stub(fs, 'existsSync').returns(false);
70 sandbox.stub(fs, 'writeFileSync');
71
72 bundler.writeOutput(
73 { source: 'bundler source', sourceMap: '', modules: [] },
74 'outfile', true, false);
75 expect(fs.mkdirSync).to.have.been.calledOnce;
76 });
77
78 describe('given out file exits', () => {
79 it('does not overwrite the out file', () => {
80 sandbox.stub(fs, 'existsSync').returns(true);
81 sandbox.stub(fs, 'writeFileSync');
82 expect(() => {
83 bundler.writeOutput(
84 { source: 'bundler source', sourceMap: '', modules: [] },
85 'outfile.js', false, false);
86 }).to.throw(/A bundle named/);
87
88 expect(fs.writeFileSync).to.not.have.been.calledOnce;
89 });
90
91 it('removes the existing file when `force` option is supplied', () => {
92 sandbox.stub(fs, 'existsSync').returns(true);
93 sandbox.spy(fs, 'unlinkSync');
94 sandbox.stub(fs, 'writeFileSync');
95
96 expect(() => {
97 bundler.writeOutput({ source: 'bundler source', sourceMap: '', modules: [] },
98 'outfile.js', true, false);
99 }).to.not.throw(/A bundle named/);
100
101 expect(fs.unlinkSync).to.have.been.calledOnce;
102 });
103 });
104});