UNPKG

5.97 kBJavaScriptView Raw
1const assert = require('assert');
2const fs = require('fs');
3const rimraf = require('rimraf');
4const sinon = require('sinon');
5const express = require('express');
6const cors = require('cors');
7
8let actualServer;
9
10describe('MemServer.Server general functionality', function() {
11 before(function() {
12 fs.mkdirSync(`./memserver`);
13 fs.mkdirSync(`./memserver/models`);
14 fs.writeFileSync(`${process.cwd()}/memserver/models/photo.js`, `
15 import Model from '${process.cwd()}/lib/model';
16
17 export default Model({
18 });
19 `);
20 fs.mkdirSync(`./memserver/fixtures`);
21 fs.writeFileSync(`${process.cwd()}/memserver/fixtures/photos.js`, `export default [
22 {
23 id: 1,
24 name: 'Ski trip',
25 href: 'ski-trip.jpeg',
26 is_public: false,
27 user_id: 1
28 },
29 {
30 id: 2,
31 name: 'Family photo',
32 href: 'family-photo.jpeg',
33 is_public: true,
34 user_id: 1
35 },
36 {
37 id: 3,
38 name: 'Selfie',
39 href: 'selfie.jpeg',
40 is_public: false,
41 user_id: 1
42 }
43 ];`);
44
45 let app = express();
46
47 app.use(cors());
48
49 app.get('/films', (req, res) => {
50 res.json({ film: 'responsed correctly' });
51 });
52
53 app.get('/movies/too-big-to-fail', (req, res) => {
54 res.json({ movie: 'is too-big-to-fail' });
55 });
56
57 actualServer = app.listen(4000, () => console.log('Web server started on port 4000'));
58 });
59
60 beforeEach(function() {
61 fs.writeFileSync(`${process.cwd()}/memserver/server.js`, `
62 import Response from '../lib/response';
63
64 export default function({ Photo }) {
65 this.get('/photos', () => {
66 const photos = Photo.findAll();
67
68 if (!photos || photos.length === 0) {
69 return Response(404, { error: 'Not found' });
70 }
71
72 return { photos: Photo.serializer(photos) };
73 });
74
75 this.passthrough('/films');
76 this.passthrough('http://localhost:4000/films');
77 this.passthrough('http://localhost:4000/movies/*');
78 }
79 `);
80
81 Object.keys(require.cache).forEach((key) => delete require.cache[key]);
82 });
83
84 after(function(done) {
85 if (fs.existsSync(`${process.cwd()}/memserver`)) {
86 rimraf.sync(`${process.cwd()}/memserver`);
87 }
88
89 actualServer.close();
90 done();
91 });
92
93 it('throws an error when MemServer tried to intercept an undeclared route', function() {
94 const MemServer = require('../../lib/index.js');
95
96 MemServer.start();
97 MemServer.Server.unhandledRequest = sinon.spy();
98 window.$ = require('jquery');
99
100 window.$.ajax({
101 type: 'GET', url: '/izelnakri', headers: { 'Content-Type': 'application/json' }
102 });
103
104 assert.ok(MemServer.Server.unhandledRequest.calledOnce, 'MemServer.Server.unhandledRequest called once');
105 });
106
107 it('this.passthrough(url) shortcut works', async function() {
108 const MemServer = require('../../lib/index.js');
109
110 MemServer.start();
111 window.$ = require('jquery');
112
113 await window.$.ajax({
114 type: 'GET', url: 'http://localhost:4000/films', headers: { 'Content-Type': 'application/json' }
115 }).then((data, textStatus, jqXHR) => {
116 assert.equal(jqXHR.status, 200);
117 assert.deepEqual(jqXHR.responseJSON, { film: 'responsed correctly' });
118 });
119 });
120
121 it('this.passthrough(url) shortcut works with wild cards', async function() {
122 const MemServer = require('../../lib/index.js');
123
124 MemServer.start();
125 window.$ = require('jquery');
126
127 await window.$.ajax({
128 type: 'GET', url: 'http://localhost:4000/movies/too-big-to-fail',
129 headers: { 'Content-Type': 'application/json' }
130 }).then((data, textStatus, jqXHR) => {
131 assert.equal(jqXHR.status, 200);
132 assert.deepEqual(jqXHR.responseJSON, { movie: 'is too-big-to-fail' });
133 });
134 });
135
136 describe('global passthrough feature', function() {
137 beforeEach(function(done) {
138 fs.writeFileSync(`${process.cwd()}/memserver/server.js`, `
139 import Response from '../lib/response';
140
141 export default function({ Photo }) {
142 this.get('/photos', () => {
143 const photos = Photo.findAll();
144
145 if (!photos || photos.length === 0) {
146 return Response(404, { error: 'Not found' });
147 }
148
149 return { photos: Photo.serializer(photos) };
150 });
151
152 this.passthrough();
153 }
154 `);
155
156 Object.keys(require.cache).forEach((key) => delete require.cache[key]);
157
158 done();
159 });
160
161 it('can create global passthrough via this.passthrough()', async function() {
162 this.timeout(10000);
163
164 Object.keys(require.cache).forEach((key) => delete require.cache[key]);
165
166 const MemServer = require('../../lib/index.js');
167 const { Photo } = MemServer.Models;
168
169 MemServer.start();
170 MemServer.Server.unhandledRequest = sinon.spy();
171 window.$ = require('jquery');
172
173 await window.$.ajax({
174 type: 'GET', url: '/photos', headers: { 'Content-Type': 'application/json' }
175 }).then((data, textStatus, jqXHR) => {
176 assert.equal(jqXHR.status, 200);
177 assert.deepEqual(jqXHR.responseJSON, { photos: Photo.serializer(Photo.findAll()) });
178 });
179 await window.$.ajax({
180 type: 'GET', url: 'http://localhost:4000/films', headers: { 'Content-Type': 'application/json' }
181 }).then((data, textStatus, jqXHR) => {
182 assert.equal(jqXHR.status, 200);
183 assert.deepEqual(jqXHR.responseJSON, { film: 'responsed correctly' });
184 });
185 await window.$.ajax({
186 type: 'GET', url: 'http://localhost:4000/movies/too-big-to-fail',
187 headers: { 'Content-Type': 'application/json' }
188 }).then((data, textStatus, jqXHR) => {
189 assert.equal(jqXHR.status, 200);
190 assert.deepEqual(jqXHR.responseJSON, { movie: 'is too-big-to-fail' });
191 });
192 });
193 });
194
195 // TODO: test this.passthrough('/something') when there is this.namespace;
196
197 // NOTE: passthrough order? investigate
198});