UNPKG

8.42 kBJavaScriptView Raw
1const assert = require('assert');
2const fs = require('fs');
3const rimraf = require('rimraf');
4
5describe('MemServer.Server shortcut functionality', function() {
6 before(function() {
7 fs.mkdirSync(`./memserver`);
8 fs.mkdirSync(`./memserver/models`);
9 fs.writeFileSync(`${process.cwd()}/memserver/models/photo.js`, `
10 import Model from '${process.cwd()}/lib/model';
11
12 export default Model({
13 defaultAttributes: {
14 is_public: true,
15 name() {
16 return 'Some default name';
17 }
18 }
19 });
20 `);
21 fs.writeFileSync(`${process.cwd()}/memserver/models/photo-comment.js`, `
22 import Model from '${process.cwd()}/lib/model';
23
24 export default Model({
25 defaultAttributes: {
26 inserted_at() {
27 return '2017-10-25T20:54:04.447Z';
28 },
29 is_important: true
30 }
31 });
32 `);
33 fs.mkdirSync(`./memserver/fixtures`);
34 fs.writeFileSync(`${process.cwd()}/memserver/fixtures/photos.js`, `export default [
35 {
36 id: 1,
37 name: 'Ski trip',
38 href: 'ski-trip.jpeg',
39 is_public: false,
40 user_id: 1
41 },
42 {
43 id: 2,
44 name: 'Family photo',
45 href: 'family-photo.jpeg',
46 is_public: true,
47 user_id: 1
48 },
49 {
50 id: 3,
51 name: 'Selfie',
52 href: 'selfie.jpeg',
53 is_public: false,
54 user_id: 1
55 }
56 ];`);
57 fs.writeFileSync(`${process.cwd()}/memserver/fixtures/photo-comments.js`, `export default [
58 {
59 uuid: '499ec646-493f-4eea-b92e-e383d94182f4',
60 content: 'What a nice photo!',
61 photo_id: 1,
62 user_id: 1
63 },
64 {
65 uuid: '77653ad3-47e4-4ec2-b49f-57ea36a627e7',
66 content: 'I agree',
67 photo_id: 1,
68 user_id: 2
69 },
70 {
71 uuid: 'd351963d-e725-4092-a37c-1ca1823b57d3',
72 content: 'I was kidding',
73 photo_id: 1,
74 user_id: 1
75 },
76 {
77 uuid: '374c7f4a-85d6-429a-bf2a-0719525f5f29',
78 content: 'Interesting indeed',
79 photo_id: 2,
80 user_id: 1
81 }
82 ];`);
83 });
84
85 beforeEach(function() {
86 Object.keys(require.cache).forEach((key) => delete require.cache[key]);
87 });
88
89 after(function(done) {
90 if (fs.existsSync(`${process.cwd()}/memserver`)) {
91 rimraf.sync(`${process.cwd()}/memserver`);
92 }
93
94 done();
95 });
96
97 it('namespace configuration option could be passed in during MemServer.start()', async function() {
98 fs.writeFileSync(`${process.cwd()}/memserver/server.js`, `
99 import Response from '../lib/response';
100
101 export default function({ Photo }) {
102 this.get('/photos', () => {
103 const photos = Photo.findAll();
104
105 if (!photos || photos.length === 0) {
106 return Response(404, { error: 'Not found' });
107 }
108
109 return { photos: Photo.serializer(photos) };
110 });
111 }
112 `);
113
114 Object.keys(require.cache).forEach((key) => delete require.cache[key]);
115
116 const MemServer = require('../../lib/index.js');
117 const { Photo } = MemServer.Models;
118
119 MemServer.start({ namespace: 'api/v1' });
120 window.$ = require('jquery');
121
122 await window.$.ajax({
123 type: 'GET', url: '/api/v1/photos', headers: { 'Content-Type': 'application/json' }
124 }).then((data, textStatus, jqXHR) => {
125 assert.equal(jqXHR.status, 200);
126 assert.deepEqual(data, { photos: Photo.serializer(Photo.findAll()) });
127 });
128 });
129
130 it('server this.namespace() configuration can overwrite existing namespace config', async function() {
131 fs.writeFileSync(`${process.cwd()}/memserver/server.js`, `
132 import Response from '../lib/response';
133
134 export default function({ Photo }) {
135 this.namespace = 'api/';
136
137 this.get('/photos', () => {
138 const photos = Photo.findAll();
139
140 if (!photos || photos.length === 0) {
141 return Response(404, { error: 'Not found' });
142 }
143
144 return { photos: Photo.serializer(photos) };
145 });
146 }
147 `);
148
149 Object.keys(require.cache).forEach((key) => delete require.cache[key]);
150
151 const MemServer = require('../../lib/index.js');
152 const { Photo } = MemServer.Models;
153
154 MemServer.start({ namespace: 'api/v1' });
155 window.$ = require('jquery');
156
157 await window.$.ajax({
158 type: 'GET', url: '/api/photos', headers: { 'Content-Type': 'application/json' }
159 }).then((data, textStatus, jqXHR) => {
160 assert.equal(jqXHR.status, 200);
161 assert.deepEqual(data, { photos: Photo.serializer(Photo.findAll()) });
162 });
163 });
164
165 it('urlPrefix configuration option could be passed in during MemServer.start()', async function() {
166 fs.writeFileSync(`${process.cwd()}/memserver/server.js`, `
167 import Response from '../lib/response';
168
169 export default function({ Photo }) {
170 this.namespace = 'api/';
171 this.get('/photos', () => {
172 const photos = Photo.findAll();
173
174 if (!photos || photos.length === 0) {
175 return Response(404, { error: 'Not found' });
176 }
177
178 return { photos: Photo.serializer(photos) };
179 });
180 }
181 `);
182
183 Object.keys(require.cache).forEach((key) => delete require.cache[key]);
184
185 const MemServer = require('../../lib/index.js');
186 const { Photo } = MemServer.Models;
187
188 MemServer.start({ urlPrefix: 'http://twitter.com' });
189 window.$ = require('jquery');
190
191 await window.$.ajax({
192 type: 'GET', url: 'http://twitter.com/api/photos', headers: { 'Content-Type': 'application/json' }
193 }).then((data, textStatus, jqXHR) => {
194 assert.equal(jqXHR.status, 200);
195 assert.deepEqual(data, { photos: Photo.serializer(Photo.findAll()) });
196 });
197 });
198
199 it('server this.urlPrefix() configuration can overwrite existing urlPrefix config', async function() {
200 this.timeout(5000);
201
202 fs.writeFileSync(`${process.cwd()}/memserver/server.js`, `
203 import Response from '../lib/response';
204
205 export default function({ Photo }) {
206 this.urlPrefix = 'http://facebook.com/';
207 this.namespace = 'api';
208
209 this.get('/photos', () => {
210 const photos = Photo.findAll();
211
212 if (!photos || photos.length === 0) {
213 return Response(404, { error: 'Not found' });
214 }
215
216 return { photos: Photo.serializer(photos) };
217 });
218 }
219 `);
220
221 Object.keys(require.cache).forEach((key) => delete require.cache[key]);
222
223 const MemServer = require('../../lib/index.js');
224 const { Photo } = MemServer.Models;
225
226 MemServer.start({ urlPrefix: 'http://twitter.com' });
227 window.$ = require('jquery');
228
229 await window.$.ajax({
230 type: 'GET', url: 'http://facebook.com/api/photos',
231 headers: { 'Content-Type': 'application/json' }
232 }).then((data, textStatus, jqXHR) => {
233 assert.equal(jqXHR.status, 200);
234 assert.deepEqual(data, { photos: Photo.serializer(Photo.findAll()) });
235 });
236 });
237
238 // it('timing configuration option could be passed in during MemServer.start()', async function() {
239 // this.timeout(5000);
240 //
241 // fs.writeFileSync(`${process.cwd()}/memserver/server.js`, `
242 // import Response from '../lib/response';
243 //
244 // export default function({ Photo }) {
245 // this.get('/photos', () => {
246 // const photos = Photo.findAll();
247 //
248 // if (!photos || photos.length === 0) {
249 // return Response(404, { error: 'Not found' });
250 // }
251 //
252 // return { photos: Photo.serializer(photos) };
253 // });
254 // }
255 // `);
256 //
257 // const MemServer = require('../../lib/index.js');
258 // const { Photo } = MemServer.Models;
259 //
260 // MemServer.start({ timing: 3000 });
261 //
262 // let timer;
263 // var ThreeSecondsPassed = false;
264 // setTimeout(() => { ThreeSecondsPassed = true; }, 1900);
265 // await window.$.ajax({
266 // type: 'GET', url: '/photos', headers: { 'Content-Type': 'application/json' }
267 // }).then((data, textStatus, jqXHR) => {
268 // assert.equal(ThreeSecondsPassed, true);
269 // assert.equal(jqXHR.status, 200);
270 // assert.deepEqual(data, { photos: Photo.serializer(Photo.findAll()) });
271 // });
272 // });
273
274 // it('server this.get(url, timing) configuration can overwrite existing timing config', function() {
275 // const MemServer = require('../../lib/index.js');
276 //
277 // MemServer.start({ timing: 2000 });
278 // });
279});