UNPKG

2.85 kBJavaScriptView Raw
1var chronicle = require('../lib');
2var co = require('co');
3var expect = require('chai')
4 .use(require('chai-as-promised'))
5 .use(require('sinon-chai'))
6 .expect;
7var nock = require('nock');
8var path = require('path');
9var sinon = require('sinon');
10var url = require('url');
11
12// TODO describe('bundle')
13
14describe('Press', function () {
15 var press;
16
17 describe('create', function () {
18 it('return instance of Press', function () {
19 press = chronicle.Press.create();
20 expect(press).to.have.all.keys(['run']);
21 });
22 });
23
24 describe('run', function () {
25 it('throw if no report', function () {
26 return expect(press.run()).to.eventually.be.rejectedWith(TypeError);
27 });
28 it('accept report function', function () {
29 var template = 'Test';
30 var report = () => template;
31 return expect(press.run(report)).to.eventually.equal(template);
32 });
33 it('optionally accept parameters', function () {
34 var report = (parameters) => parameters;
35 var parameters = 'Test';
36 return expect(press.run(report, parameters)).to.eventually.equal(parameters);
37 });
38 it('accept file path to report module', function () {
39 var modulePath = path.resolve(__dirname, 'fixtures/module.js');
40 var parameters = 'Test';
41 return expect(press.run(modulePath, parameters)).to.eventually.equal(parameters);
42 });
43
44 // Test network requests
45 var filename = 'bundle.js';
46 var domain = 'http://test.com';
47 var bundlePath = path.resolve(__dirname, 'fixtures', filename);
48 var bundleUrl = url.resolve(domain, filename);
49 var parameters = 'Test';
50 it('accept fully qualified url to bundled report module', function () {
51 // nock request
52 nock(domain)
53 .get(`/${filename}`)
54 .replyWithFile(200, bundlePath);
55 return expect(press.run(bundleUrl, parameters)).to.eventually.equal(parameters);
56 });
57 it('should cache report module bundles', function () {
58 // nock same request (304, return nothing)
59 nock(domain)
60 .get(`/${filename}`)
61 .reply(304);
62 return expect(press.run(bundleUrl, parameters)).to.eventually.equal(parameters);
63 });
64 });
65});
66
67
68describe('Report', function () {
69 var press = chronicle.Press.create();
70
71 it('can be function that returns a string', function () {
72 var html = 'Test';
73 var report = () => html;
74 return expect(press.run(report)).to.eventually.be.fulfilled
75 .and.to.equal(html);
76 });
77 it('can be yieldable function that is fulfilled with a string', function () {
78 var html = 'Test';
79 var report = co.wrap(function * () { return html; });
80 return expect(press.run(report)).to.eventually.be.fulfilled
81 .and.to.equal(html);
82 });
83 it('called with parameters', function () {
84 return co(function * () {
85 var parameters = {};
86 var report = sinon.stub().returns('Test');
87 yield press.run(report, parameters);
88 expect(report).to.be.calledOnce
89 .and.to.be.calledWithExactly(parameters);
90 });
91 });
92});