UNPKG

7.76 kBJavaScriptView Raw
1'use strict';
2const path = require('path');
3const chai = require('chai');
4const expect = chai.expect;
5const AdapterInterface = require(path.join(__dirname, '../../index'));
6
7chai.use(require('chai-spies'));
8
9var reqFactory = function (jsonp) {
10 let req = {
11 query: (jsonp) ? { callback: 'fakefn' } : {}
12 };
13 let res = {
14 status: num => res,
15 jsonp: data => data,
16 send: data => data
17 };
18 Object.keys(res).forEach(key => {
19 res[key] = chai.spy(res[key]);
20 });
21 return { req, res };
22};
23
24describe('JSON adapter spec', function () {
25 describe('Basic .render functionality', function () {
26 let jsonAdapter = AdapterInterface.create({ adapter: 'json' });
27 it('Should return a Promise if cb is not passed and .sync is false', done => {
28 jsonAdapter.render({ foo: 'bar' })
29 .try(result => {
30 expect(result).to.have.property('result');
31 expect(result.result).to.equal('success');
32 expect(result).to.have.property('status');
33 expect(result).to.have.property('data');
34 expect(result.data).to.have.property('foo');
35 done();
36 })
37 .catch(done);
38 });
39 it('Should invoke the callback if argument is passed', done => {
40 jsonAdapter.render({ foo: 'bar' }, (err, result) => {
41 if (err) done(err);
42 else {
43 expect(result).to.have.property('result');
44 expect(result.result).to.equal('success');
45 expect(result).to.have.property('status');
46 expect(result).to.have.property('data');
47 expect(result.data).to.have.property('foo');
48 done();
49 }
50 });
51 });
52 it('Should be handled synchronously if .sync is true', () => {
53 let result = jsonAdapter.render({ foo: 'bar' }, { sync: true });
54 expect(result).to.have.property('result');
55 expect(result.result).to.equal('success');
56 expect(result).to.have.property('status');
57 expect(result).to.have.property('data');
58 expect(result.data).to.have.property('foo');
59 });
60 it('Should send a response if options.req and options.res are defined', done => {
61 let { req, res } = reqFactory();
62 jsonAdapter.render({ foo: 'bar' }, { req, res })
63 .try(result => {
64 expect(res.status).to.have.been.called.with(200);
65 expect(result.data).to.deep.equal({ foo: 'bar' });
66 done();
67 })
68 .catch(done);
69 });
70 it('Should use jsonp if req.query.callback is defined', done => {
71 let { req, res } = reqFactory(true);
72 jsonAdapter.render({ foo: 'bar' }, { req, res })
73 .try(result => {
74 expect(result.data).to.deep.equal({ foo: 'bar' });
75 expect(res.status).to.have.been.called.with(200);
76 expect(res.jsonp).to.have.been.called.with(result);
77 done();
78 })
79 .catch(done);
80 });
81 it('Should not send a response if options.skip_response is defined', done => {
82 let { req, res } = reqFactory();
83 jsonAdapter.render({ foo: 'bar' }, { req, res, skip_response: true })
84 .try(result => {
85 expect(result.data).to.deep.equal({ foo: 'bar' });
86 expect(res.status).to.not.have.been.called();
87 done();
88 })
89 .catch(done);
90 });
91 it('Should call the error handler if there is an error', done => {
92 let { req, res } = reqFactory();
93 jsonAdapter.formatRender = function () {
94 throw Error('Some Random Error');
95 };
96 let error = jsonAdapter.error.bind(jsonAdapter);
97 jsonAdapter.error = chai.spy(error);
98 jsonAdapter.render({ foo: 'bar' }, { req, res })
99 .try(result => {
100 jsonAdapter.formatRender = undefined;
101 expect(result.result).to.equal('error');
102 expect(result.data.error.message).to.equal('Some Random Error');
103 expect(jsonAdapter.error).to.have.been.called();
104 jsonAdapter.error = error;
105 done();
106 })
107 .catch(done);
108 });
109 });
110 describe('Basic .error functionality', function () {
111 let jsonAdapter = AdapterInterface.create({ adapter: 'json' });
112 it('Should return a Promise if cb is not passed and .sync is false', done => {
113 jsonAdapter.error(new Error('Some Random Error'))
114 .try(result => {
115 expect(result).to.have.property('result');
116 expect(result.result).to.equal('error');
117 expect(result).to.have.property('status');
118 expect(result).to.have.property('data');
119 expect(result.data).to.have.property('error');
120 done();
121 })
122 .catch(done);
123 });
124 it('Should invoke the callback if argument is passed', done => {
125 jsonAdapter.error(new Error('Some Random Error'), (err, result) => {
126 expect(result).to.have.property('result');
127 expect(result.result).to.equal('error');
128 expect(result).to.have.property('status');
129 expect(result).to.have.property('data');
130 expect(result.data).to.have.property('error');
131 done();
132 });
133 });
134 it('Should be handled synchronously if .sync is true', () => {
135 let result = jsonAdapter.error(new Error('Some Random Error'), { sync: true });
136 expect(result).to.have.property('result');
137 expect(result.result).to.equal('error');
138 expect(result).to.have.property('status');
139 expect(result).to.have.property('data');
140 expect(result.data).to.have.property('error');
141 });
142 it('Should send an error response if options.req and options.res are defined', done => {
143 let { req, res } = reqFactory();
144 jsonAdapter.error(new Error('Some Random Error'), { req, res })
145 .try(result => {
146 expect(res.status).to.have.been.called.with(500);
147 expect(result.data.error.message).to.equal('Some Random Error');
148 done();
149 })
150 .catch(done);
151 });
152 it('Should use jsonp if req.query.callback is defined', done => {
153 let { req, res } = reqFactory(true);
154 jsonAdapter.error(new Error('Some Random Error'), { req, res })
155 .try(result => {
156 expect(res.jsonp).to.have.been.called();
157 expect(res.send).to.not.have.been.called();
158 done();
159 })
160 .catch(done);
161 });
162 it('Should not send a response if options.skip_response is defined', done => {
163 let { req, res } = reqFactory();
164 jsonAdapter.error(new Error('Some Random Error'), { req, res, skip_response: true })
165 .try(result => {
166 expect(res.status).to.not.have.been.called();
167 expect(result.data.error.message).to.equal('Some Random Error');
168 done();
169 })
170 .catch(done);
171 });
172 it('Should handle an error', done => {
173 let { req, res } = reqFactory();
174 res.status = false;
175 jsonAdapter.error(undefined, { req, res })
176 .then(() => {
177 done(new Error('Should not execute'));
178 }, e => {
179 expect(e instanceof Error).to.be.true;
180 done();
181 });
182 });
183 });
184 describe('Custom rendering function', function () {
185 let jsonAdapter = AdapterInterface.create({
186 adapter: 'json',
187 formatRender: function (data) {
188 return JSON.stringify(data);
189 }
190 });
191 it('Should handle a custom rendering function', done => {
192 jsonAdapter.render({ foo: 'bar' })
193 .try(result => {
194 expect(result).to.be.a('string');
195 done();
196 })
197 .catch(done);
198 });
199 it('Should handle an error in a custom rendering function', done => {
200 jsonAdapter.render({ foo: 'bar' }, {
201 formatRender: function (data) {
202 throw new Error('Some Random Error');
203 }
204 }, err => {
205 expect(err instanceof Error).to.be.true;
206 done();
207 });
208 });
209 });
210 describe('Custom error function', function () {
211 let jsonAdapter = AdapterInterface.create({
212 adapter: 'json',
213 formatError: function (err) {
214 return err.message;
215 }
216 });
217 it('Should handle a custom error function', done => {
218 jsonAdapter.error(new Error('Some Random Error'))
219 .try(result => {
220 expect(result).to.equal('Some Random Error');
221 done();
222 })
223 .catch(done);
224 });
225 it('Should handle an error in a custom error function', done => {
226 jsonAdapter.error({ foo: 'bar' }, {
227 formatError: function (data) {
228 throw new Error('Some Random Error');
229 }
230 }, err => {
231 expect(err instanceof Error).to.be.true;
232 done();
233 });
234 });
235 });
236});