1 | 'use strict';
|
2 |
|
3 | const expect = require('chai').expect;
|
4 | const Context = require('../lib/Context');
|
5 | const Method = require('../lib/Method');
|
6 |
|
7 | describe('class Context', function() {
|
8 | let context, method;
|
9 | let noop = function() {};
|
10 |
|
11 | beforeEach(function() {
|
12 | method = new Method('test', {
|
13 | params: [
|
14 | { name: 'username', type: 'string' },
|
15 | { name: 'age', type: 'number' },
|
16 | { name: 'randomDate', type: 'date' }
|
17 | ]
|
18 | }, noop);
|
19 | context = new Context({}, {}, method, {});
|
20 | });
|
21 |
|
22 | describe('constructor(req, res, method, options)', function() {
|
23 | it('should raise an error if req is invalid', function() {
|
24 | expect(function() {
|
25 | new Context();
|
26 | }).to.throw(Error).have.property('message', 'req is invalid');
|
27 | });
|
28 |
|
29 | it('should raise an error if res is invalid', function() {
|
30 | expect(function() {
|
31 | new Context({});
|
32 | }).to.throw(Error).have.property('message', 'res is invalid');
|
33 | });
|
34 |
|
35 | it('should raise an error if method is invalid', function() {
|
36 | expect(function() {
|
37 | new Context({}, {});
|
38 | }).to.throw(Error).have.property('message', 'method must be an instance of Method');
|
39 | });
|
40 |
|
41 | [
|
42 | ['request', function() { return { context }; }],
|
43 | ['req', function() { return { context }; }],
|
44 | ['response', function() { return { context }; }],
|
45 | ['res', function() { return { context }; }],
|
46 | ['options', {}],
|
47 | ['_method', function() { return method; }],
|
48 | ['methodName', 'test'],
|
49 | ['fullPath', '/'],
|
50 | ['argsBuilt', false],
|
51 | ['args', {}],
|
52 | ['_done', false],
|
53 | ['state', {}]
|
54 | ].forEach(function(prop) {
|
55 | it(`should have property '${prop[0]}' and corresponding value`, function() {
|
56 | let value = typeof prop[1] === 'function' ? prop[1]() : prop[1];
|
57 | expect(context).to.have.property(prop[0]).deep.eq(value);
|
58 | });
|
59 | });
|
60 |
|
61 | it('should parse arrayItemDelimiters options as regexp', function() {
|
62 | let context = new Context({}, {}, method, { arrayItemDelimiters: [',', ' '] });
|
63 | expect(context).to.have.deep.property('options.arrayItemDelimiters').eq(/,| /g);
|
64 | });
|
65 | });
|
66 |
|
67 | describe('buildArgs()', function() {
|
68 | it('should build args by method params config', function() {
|
69 | let date = new Date('1998-01-01');
|
70 | context.args = { name: 'Felix', username: 'lyfeyaj', age: 27, randomDate: date };
|
71 | expect(context.buildArgs()).to.deep.eq({ username: 'lyfeyaj', age: 27, randomDate: date });
|
72 | });
|
73 |
|
74 | it('should convert args to specific type', function() {
|
75 | context.args = { name: 'Felix', username: 'lyfeyaj', age: '27', randomDate: '1998-01-01' };
|
76 | expect(context.buildArgs()).to.deep.eq({ username: 'lyfeyaj', age: 27, randomDate: new Date('1998-01-01') });
|
77 | });
|
78 |
|
79 | it('should build and convert inner params', function() {
|
80 | let method = new Method('test', {
|
81 | params: [
|
82 | { name: 'username', type: 'string' },
|
83 | { name: 'age', type: 'number' },
|
84 | { name: 'randomDate', type: 'date' },
|
85 | {
|
86 | name: 'profile',
|
87 | type: 'object',
|
88 | params: [
|
89 | { name: 'gender', type: 'number' },
|
90 | { name: 'hobbies', type: ['string'] },
|
91 | { name: 'tags', type: ['string'] }
|
92 | ]
|
93 | }
|
94 | ]
|
95 | }, noop);
|
96 | let context = new Context({}, {}, method, { arrayItemDelimiters: ',' });
|
97 | context.args = {
|
98 | name: 'Felix',
|
99 | username: 'lyfeyaj',
|
100 | age: '27',
|
101 | randomDate: '1998-01-01',
|
102 | profile: {
|
103 | gender: '0',
|
104 | hobbies: 'pingpong',
|
105 | tags: 'programmer,writer'
|
106 | }
|
107 | };
|
108 | expect(context.buildArgs()).to.deep.eq(
|
109 | {
|
110 | username: 'lyfeyaj',
|
111 | age: 27,
|
112 | randomDate: new Date('1998-01-01'),
|
113 | profile: {
|
114 | gender: 0,
|
115 | hobbies: ['pingpong'],
|
116 | tags: ['programmer', 'writer']
|
117 | }
|
118 | }
|
119 | );
|
120 | });
|
121 |
|
122 | it('should split string into array by options.arrayItemDelimiters', function() {
|
123 | let method = new Method('test', {
|
124 | params: [
|
125 | { name: 'hobbies', type: ['string'] }
|
126 | ]
|
127 | }, noop);
|
128 | let context = new Context({}, {}, method, { arrayItemDelimiters: ',' });
|
129 | context.args = { hobbies: 'pingpong,table tennis,swimming,badminton' };
|
130 | expect(context.buildArgs()).to.deep.eq({ hobbies: ['pingpong', 'table tennis', 'swimming', 'badminton'] });
|
131 | });
|
132 | });
|
133 |
|
134 | describe('param(name)', function() {
|
135 | it('should return param by name', function() {
|
136 | context.args = { name: 'Felix' };
|
137 | expect(context.param('name')).eq('Felix');
|
138 | });
|
139 |
|
140 | it('should support deep key query', function() {
|
141 | context.args = { profile: { name: 'Felix' } };
|
142 | expect(context.param('profile.name')).eq('Felix');
|
143 | });
|
144 | });
|
145 |
|
146 | describe('throw()', function() {
|
147 | it('should throw error on http status code', function() {
|
148 | expect(function() {
|
149 | context.throw(404);
|
150 | }).to.throw(Error).have.property('message', 'Not Found');
|
151 | });
|
152 | });
|
153 |
|
154 | describe('isFinished()', function() {
|
155 | it('should throw error for not implement', function() {
|
156 | expect(function() {
|
157 | context.isFinished();
|
158 | }).to.throw(Error).have.property('message', 'Not Implement');
|
159 | });
|
160 | });
|
161 |
|
162 | describe('done()', function() {
|
163 | it('should throw error for not implement', function() {
|
164 | expect(function() {
|
165 | context.done();
|
166 | }).to.throw(Error).have.property('message', 'Not Implement');
|
167 | });
|
168 | });
|
169 | });
|