1 | import chai from 'chai';
|
2 | import sinon from 'sinon';
|
3 | import sinonChai from 'sinon-chai';
|
4 |
|
5 | var expect = chai.expect;
|
6 |
|
7 | chai.use(sinonChai)
|
8 |
|
9 | import {App} from '../index';
|
10 |
|
11 | function buildCtx (path, data) {
|
12 | var request = {
|
13 | path: path,
|
14 | method: 'GET',
|
15 | };
|
16 |
|
17 | return {
|
18 | request,
|
19 | path: request.path,
|
20 | method: request.method,
|
21 | redirect: sinon.spy(),
|
22 | };
|
23 | }
|
24 |
|
25 | describe('App', function() {
|
26 | it('is a thing', function() {
|
27 | expect(App).to.not.be.null;
|
28 | });
|
29 |
|
30 | it('has config', function() {
|
31 | var config = {
|
32 | test: 1,
|
33 | };
|
34 |
|
35 | var app = new App(config);
|
36 | expect(app.getConfig('test')).to.equal(config.test);
|
37 | });
|
38 |
|
39 | describe('router', function() {
|
40 | it('is created on construction', function() {
|
41 | var app = new App();
|
42 | expect(app.router).to.not.be.null;
|
43 | });
|
44 |
|
45 | it('calls routes', function(done) {
|
46 | var path = '/';
|
47 |
|
48 | var app = new App();
|
49 | var spy = sinon.spy();
|
50 | var ctx = buildCtx(path);
|
51 |
|
52 | var route = function * (next) {
|
53 | spy();
|
54 | }
|
55 |
|
56 | app.router.get(path, route);
|
57 |
|
58 | app.route(ctx).then(function() {
|
59 | expect(spy).to.have.been.calledOnce;
|
60 | done();
|
61 | });
|
62 | });
|
63 |
|
64 | it('plays nice with async', function(done) {
|
65 | var path = '/';
|
66 |
|
67 | var app = new App();
|
68 | var ctx = buildCtx(path);
|
69 |
|
70 | function get (data, cb) {
|
71 | setTimeout(function() {
|
72 | cb(data);
|
73 | }, 500);
|
74 | }
|
75 |
|
76 | function wrappedGet(data, ctx) {
|
77 | return new Promise(function(resolve) {
|
78 | get(data, resolve);
|
79 | });
|
80 | }
|
81 |
|
82 | var route = function * (next) {
|
83 | var data = yield wrappedGet('b');
|
84 | this.a = data;
|
85 | };
|
86 |
|
87 | app.router.get(path, route);
|
88 |
|
89 | app.route(ctx).then(function() {
|
90 | expect(ctx.a).to.equal('b');
|
91 | done();
|
92 | });
|
93 | });
|
94 |
|
95 | it('can error gracefully', function() {
|
96 | var app = new App();
|
97 | var ctx = buildCtx('/');
|
98 |
|
99 | sinon.stub(app, 'error');
|
100 |
|
101 | app.router.get('/', function(req, res, next) {
|
102 | throw 'EVERTHING IS WRONG';
|
103 | });
|
104 |
|
105 | app.route(ctx).then(function() {
|
106 | expect(app.error).to.have.been.calledOnce;
|
107 | });
|
108 | });
|
109 |
|
110 | it('can redirect to /404 on 404s', function(done) {
|
111 | var app = new App();
|
112 | var ctx = buildCtx('/');
|
113 |
|
114 | app.route(ctx).then(function() {
|
115 | expect(ctx.redirect).to.have.been.calledOnce;
|
116 | expect(ctx.redirect).to.have.been.calledWith('/404', '/404');
|
117 | done();
|
118 | });
|
119 | });
|
120 | });
|
121 |
|
122 | describe('event emitter', function() {
|
123 | it('is created on construction', function() {
|
124 | var app = new App();
|
125 | expect(app.emitter).to.not.be.null;
|
126 | });
|
127 | });
|
128 |
|
129 | describe('plugins', function() {
|
130 | it('registers plugins once', function() {
|
131 | var app = new App();
|
132 | var plugin = function() { };
|
133 |
|
134 | app.registerPlugin(plugin);
|
135 | app.registerPlugin(plugin);
|
136 |
|
137 | expect(app.plugins.length).to.equal(1);
|
138 | expect(app.plugins[0]).to.equal(plugin);
|
139 | });
|
140 | });
|
141 | });
|