1 | const assert = require('assert');
|
2 | const fs = require('fs');
|
3 | const rimraf = require('rimraf');
|
4 |
|
5 | process.setMaxListeners(0);
|
6 |
|
7 | describe('MemServer.Server shortcut functionality', function() {
|
8 | before(function() {
|
9 | fs.mkdirSync(`./memserver`);
|
10 | fs.mkdirSync(`./memserver/models`);
|
11 | fs.writeFileSync(`${process.cwd()}/memserver/models/photo.js`, `
|
12 | import Model from '${process.cwd()}/lib/model';
|
13 |
|
14 | export default Model({
|
15 | defaultAttributes: {
|
16 | is_public: true,
|
17 | name() {
|
18 | return 'Some default name';
|
19 | }
|
20 | }
|
21 | });
|
22 | `);
|
23 | fs.mkdirSync(`./memserver/fixtures`);
|
24 | fs.writeFileSync(`${process.cwd()}/memserver/fixtures/photos.js`, `export default [
|
25 | {
|
26 | id: 1,
|
27 | name: 'Ski trip',
|
28 | href: 'ski-trip.jpeg',
|
29 | is_public: false,
|
30 | user_id: 1
|
31 | },
|
32 | {
|
33 | id: 2,
|
34 | name: 'Family photo',
|
35 | href: 'family-photo.jpeg',
|
36 | is_public: true,
|
37 | user_id: 1
|
38 | },
|
39 | {
|
40 | id: 3,
|
41 | name: 'Selfie',
|
42 | href: 'selfie.jpeg',
|
43 | is_public: false,
|
44 | user_id: 1
|
45 | }
|
46 | ];`);
|
47 | });
|
48 |
|
49 | beforeEach(function() {
|
50 | Object.keys(require.cache).forEach((key) => delete require.cache[key]);
|
51 | });
|
52 |
|
53 | after(function(done) {
|
54 | if (fs.existsSync(`${process.cwd()}/memserver`)) {
|
55 | rimraf.sync(`${process.cwd()}/memserver`);
|
56 | }
|
57 |
|
58 | done();
|
59 | });
|
60 |
|
61 | describe('route shortcuts work', function() {
|
62 | before(function() {
|
63 | fs.writeFileSync(`${process.cwd()}/memserver/server.js`, `
|
64 | export default function(Models) {
|
65 | this.post('/photos');
|
66 | this.get('/photos');
|
67 | this.get('/photos/:id');
|
68 | this.put('/photos/:id');
|
69 | this.delete('/photos/:id');
|
70 | }
|
71 | `);
|
72 | });
|
73 |
|
74 | it('POST /resources work with shortcut', async function() {
|
75 | this.timeout(5000);
|
76 |
|
77 | const MemServer = require('../../lib/index.js');
|
78 | const { Photo } = MemServer.Models;
|
79 |
|
80 | MemServer.start();
|
81 | window.$ = require('jquery');
|
82 |
|
83 | assert.equal(Photo.count(), 3);
|
84 |
|
85 | await window.$.ajax({
|
86 | type: 'POST', url: '/photos', headers: { 'Content-Type': 'application/json' },
|
87 | data: JSON.stringify({ photo: { name: 'Izel Nakri' }})
|
88 | }).then((data, textStatus, jqXHR) => {
|
89 | assert.equal(jqXHR.status, 201);
|
90 | assert.deepEqual(data, { photo: Photo.serializer(Photo.find(4)) });
|
91 | assert.equal(Photo.count(), 4);
|
92 | assert.deepEqual(Photo.find(4), {
|
93 | id: 4, name: 'Izel Nakri', is_public: true, href: null, user_id: null
|
94 | })
|
95 | });
|
96 | });
|
97 |
|
98 | it('GET /resources works with shortcut', async function() {
|
99 | const MemServer = require('../../lib/index.js');
|
100 | const { Photo } = MemServer.Models;
|
101 |
|
102 | MemServer.start();
|
103 | window.$ = require('jquery');
|
104 |
|
105 | assert.equal(Photo.count(), 3);
|
106 |
|
107 | await window.$.ajax({
|
108 | type: 'GET', url: '/photos', headers: { 'Content-Type': 'application/json' }
|
109 | }).then((data, textStatus, jqXHR) => {
|
110 | assert.equal(jqXHR.status, 200);
|
111 | assert.deepEqual(data, { photos: Photo.serializer(Photo.findAll()) });
|
112 | assert.equal(Photo.count(), 3);
|
113 | });
|
114 | });
|
115 |
|
116 | it('GET /resources/:id works with shortcut', async function() {
|
117 | const MemServer = require('../../lib/index.js');
|
118 | const { Photo } = MemServer.Models;
|
119 |
|
120 | MemServer.start();
|
121 | window.$ = require('jquery');
|
122 |
|
123 | await window.$.ajax({
|
124 | type: 'GET', url: '/photos/1', headers: { 'Content-Type': 'application/json' }
|
125 | }).then((data, textStatus, jqXHR) => {
|
126 | assert.equal(jqXHR.status, 200);
|
127 | assert.deepEqual(data, { photo: Photo.serializer(Photo.find(1)) });
|
128 | });
|
129 | });
|
130 |
|
131 | it('PUT /resources/:id works with shortcut', async function() {
|
132 | const MemServer = require('../../lib/index.js');
|
133 | const { Photo } = MemServer.Models;
|
134 |
|
135 | MemServer.start();
|
136 | window.$ = require('jquery');
|
137 |
|
138 | assert.equal(Photo.find(1).name, 'Ski trip')
|
139 |
|
140 | await window.$.ajax({
|
141 | type: 'PUT', url: '/photos/1', headers: { 'Content-Type': 'application/json' },
|
142 | data: JSON.stringify({ photo: { id: 1, name: 'New custom title'} })
|
143 | }, (data, textStatus, jqXHR) => {
|
144 | const photo = Photo.find(1);
|
145 |
|
146 | assert.equal(jqXHR.status, 200);
|
147 | assert.deepEqual(data, { photo: Photo.serializer(photo) });
|
148 | assert.equal(photo.name, 'New custom title');
|
149 | });
|
150 | });
|
151 |
|
152 | it('DELETE /resources/:id works with shortcut', async function() {
|
153 | const MemServer = require('../../lib/index.js');
|
154 | const { Photo } = MemServer.Models;
|
155 |
|
156 | MemServer.start();
|
157 | window.$ = require('jquery');
|
158 |
|
159 | assert.equal(Photo.count(), 3);
|
160 |
|
161 | await window.$.ajax({
|
162 | type: 'DELETE', url: '/photos/1', headers: { 'Content-Type': 'application/json' }
|
163 | }, (data, textStatus, jqXHR) => {
|
164 | assert.equal(jqXHR.status, 204);
|
165 | assert.deepEqual(data, {});
|
166 | assert.equal(Photo.count(), 2);
|
167 | assert.equal(PHoto.find(1), undefined);
|
168 | });
|
169 | });
|
170 | });
|
171 |
|
172 | it('throws an helpful error message when shortcuts model is not found', async function() {
|
173 | this.timeout(5000);
|
174 |
|
175 | fs.writeFileSync(`${process.cwd()}/memserver/server.js`, `
|
176 | export default function(Models) {
|
177 | this.post('/photos');
|
178 | this.get('/photos');
|
179 | this.get('/photos/:id');
|
180 | this.put('/photos/:id');
|
181 | this.delete('/photos/:id');
|
182 |
|
183 | this.get('/houses');
|
184 | }
|
185 | `);
|
186 |
|
187 | Object.keys(require.cache).forEach((key) => delete require.cache[key]);
|
188 |
|
189 | const MemServer = require('../../lib/index.js');
|
190 |
|
191 | assert.throws(() => MemServer.start(), (err) => {
|
192 | return (err instanceof Error) &&
|
193 | /\[MemServer\] GET \/houses route handler cannot be generated automatically\: House is not a valid MemServer\.Model, please check that your route name matches the model reference or create a custom handler function/.test(err);
|
194 | });
|
195 | });
|
196 |
|
197 | describe('Server route handlers default responses', function() {
|
198 | before(function() {
|
199 | fs.writeFileSync(`${process.cwd()}/memserver/server.js`, `
|
200 | export default function(Models) {
|
201 | this.post('/photos', () => {});
|
202 | this.get('/photos', () => {});
|
203 | this.get('/photos/:id', () => {});
|
204 | this.put('/photos/:id', () => {});
|
205 | this.delete('/photos/:id', () => {});
|
206 | }
|
207 | `);
|
208 | });
|
209 |
|
210 | it('POST /resources works correctly with undefined handler response', async function() {
|
211 | const MemServer = require('../../lib/index.js');
|
212 | const { Photo } = MemServer.Models;
|
213 |
|
214 | MemServer.start();
|
215 | window.$ = require('jquery');
|
216 |
|
217 | assert.equal(Photo.count(), 3);
|
218 |
|
219 | await window.$.ajax({
|
220 | type: 'POST', url: '/photos', headers: { 'Content-Type': 'application/json' }
|
221 | }).catch((jqXHR) => {
|
222 | assert.equal(jqXHR.status, 500);
|
223 | assert.deepEqual(jqXHR.responseJSON, { error: '[MemServer] POST /photos route handler did not return anything to respond to the request!' });
|
224 | assert.equal(Photo.count(), 3);
|
225 | });
|
226 | });
|
227 |
|
228 | it('GET /resources works correctly with undefined handler response', async function() {
|
229 | const MemServer = require('../../lib/index.js');
|
230 | const { Photo } = MemServer.Models;
|
231 |
|
232 | MemServer.start();
|
233 | window.$ = require('jquery');
|
234 |
|
235 | await window.$.ajax({
|
236 | type: 'GET', url: '/photos', headers: { 'Content-Type': 'application/json' }
|
237 | }).catch((jqXHR) => {
|
238 | assert.equal(jqXHR.status, 500);
|
239 | assert.deepEqual(jqXHR.responseJSON, { error: '[MemServer] GET /photos route handler did not return anything to respond to the request!' });
|
240 | });
|
241 | });
|
242 |
|
243 | it('GET /resources/:id works correctly with undefined handler response', async function() {
|
244 | const MemServer = require('../../lib/index.js');
|
245 | const { Photo } = MemServer.Models;
|
246 |
|
247 | MemServer.start();
|
248 | window.$ = require('jquery');
|
249 |
|
250 | await window.$.ajax({
|
251 | type: 'GET', url: '/photos/1', headers: { 'Content-Type': 'application/json' }
|
252 | }).catch((jqXHR) => {
|
253 | assert.equal(jqXHR.status, 500);
|
254 | assert.deepEqual(jqXHR.responseJSON, { error: '[MemServer] GET /photos/1 route handler did not return anything to respond to the request!' });
|
255 | });
|
256 | });
|
257 |
|
258 | it('PUT /resources/:id works correctly with undefined handler response', async function() {
|
259 | const MemServer = require('../../lib/index.js');
|
260 | const { Photo } = MemServer.Models;
|
261 |
|
262 | MemServer.start();
|
263 | window.$ = require('jquery');
|
264 |
|
265 | await window.$.ajax({
|
266 | type: 'PUT', url: '/photos/1', headers: { 'Content-Type': 'application/json' },
|
267 | data: JSON.stringify({ photo: { id: 1, name: 'New Name' }})
|
268 | }).catch((jqXHR) => {
|
269 | assert.equal(jqXHR.status, 500);
|
270 | assert.deepEqual(jqXHR.responseJSON, { error: '[MemServer] PUT /photos/1 route handler did not return anything to respond to the request!' });
|
271 | });
|
272 | });
|
273 |
|
274 | it('DELETE /resources/:id works correctly with undefined handler response', async function() {
|
275 | const MemServer = require('../../lib/index.js');
|
276 | const { Photo } = MemServer.Models;
|
277 |
|
278 | MemServer.start();
|
279 | window.$ = require('jquery');
|
280 |
|
281 | await window.$.ajax({
|
282 | type: 'DELETE', url: '/photos/1', headers: { 'Content-Type': 'application/json' }
|
283 | }).then((data, textStatus, jqXHR) => {
|
284 | assert.equal(jqXHR.status, 204);
|
285 | assert.deepEqual(jqXHR.responseJSON, {});
|
286 | });
|
287 | });
|
288 | });
|
289 | });
|