Skip to content

Commit

Permalink
passthrough tests moved from mocha to ava
Browse files Browse the repository at this point in the history
  • Loading branch information
izelnakri committed Oct 22, 2018
1 parent c39f0d3 commit 674e2e0
Show file tree
Hide file tree
Showing 2 changed files with 237 additions and 0 deletions.
30 changes: 30 additions & 0 deletions TODO
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
- investigate silent Errors within MemServer.Server handler context
- make passthrough PASS
- serializerExtraFields params

- add initializer.js
Expand All @@ -11,3 +13,31 @@
- non-json request/responses? - just kidding... or do I?!?
- rollup -i ./lib/response.js -o response.js -f cjs
- memserver glue [jsonEndpoint]



export default Model({
schema: {
username: {
type: String,
required: true,
default: 'xxx',
notNull: true,
unique: true // other min, max,
check(model, next) {
// custom checks if that returns
}
}
},
relationships: {
emails: { // belongsTo, hasMany, hasMany through, hasOne, onReplace, maybe do FK contraint here
model: //

}
}
});

// relationships declaration

import { cast, validateLength, validateRequired, foreignKeyConstraint } from 'memserver/changeset';
import { from } from 'memserver/query';
207 changes: 207 additions & 0 deletions test/server/passthrough.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,207 @@
import test from 'ava';
import cors from 'cors';
import express from 'express';
import fs from 'fs-extra';
import sinon from 'sinon';

let actualServer;

const AUTHENTICATION_TOKEN = 'ec25fc7b-6ee2-4bda-b57c-6c9867b30ff4';
const AJAX_AUTHORIZATION_HEADERS = {
'Content-Type': 'application/json', 'Authorization': `Token ${AUTHENTICATION_TOKEN}`
};
const CWD = process.cwd();

test.before(async () => {
await fs.mkdir(`${CWD}/memserver`);
await fs.mkdir(`${CWD}/memserver/models`);
await Promise.all([
fs.writeFile(`${CWD}/memserver/models/photo.js`, `
import Model from '${CWD}/lib/model';
export default Model({
});`),
fs.mkdir(`${CWD}/memserver/fixtures`)
]);
await fs.writeFile(`${CWD}/memserver/fixtures/photos.js`, `export default [
{
id: 1,
name: 'Ski trip',
href: 'ski-trip.jpeg',
is_public: false,
user_id: 1
},
{
id: 2,
name: 'Family photo',
href: 'family-photo.jpeg',
is_public: true,
user_id: 1
},
{
id: 3,
name: 'Selfie',
href: 'selfie.jpeg',
is_public: false,
user_id: 1
}
];`);

const app = express();

app.use(cors());

app.get('/films', (req, res) => {
res.json({ film: 'responsed correctly' });
});

app.get('/movies/too-big-to-fail', (req, res) => {
res.json({ movie: 'is too-big-to-fail' });
});

actualServer = app.listen(4000, () => console.log('Web server started on port 4000'));
});

test.beforeEach(async () => {
await fs.writeFile(`${CWD}/memserver/server.js`, `
import Response from '../lib/response';
export default function({ Photo }) {
this.get('/photos', () => {
const photos = Photo.findAll();
if (!photos || photos.length === 0) {
return Response(404, { error: 'Not found' });
}
return { photos: Photo.serializer(photos) };
});
this.passthrough('/films');
this.passthrough('http://localhost:4000/films');
this.passthrough('http://localhost:4000/movies/*');
}
`);

Object.keys(require.cache).forEach((key) => delete require.cache[key]);
});

test.after.always(async () => {
if (await fs.exists(`${CWD}/memserver`)) {
await fs.remove(`${CWD}/memserver`);
}

actualServer.close();
});

test.serial('[MemServer.Server] throws an error when MemServer tried to intercept an undeclared route', (t) => {
t.plan(1);

const MemServer = require('../../lib/index.js');

MemServer.start();
MemServer.Server.unhandledRequest = sinon.spy();
window.$ = require('jquery');

window.$.ajax({
type: 'GET', url: '/izelnakri', headers: { 'Content-Type': 'application/json' }
});

t.true(MemServer.Server.unhandledRequest.calledOnce, 'MemServer.Server.unhandledRequest called once');
});

test.serial('[MemServer.Server] this.passthrough(url) shortcut works', async (t) => {
t.plan(2);

const MemServer = require('../../lib/index.js');

MemServer.start();
window.$ = require('jquery');

await window.$.ajax({
type: 'GET', url: 'http://localhost:4000/films', headers: { 'Content-Type': 'application/json' }
}).then((data, textStatus, jqXHR) => {
t.is(jqXHR.status, 200);
t.deepEqual(jqXHR.responseJSON, { film: 'responsed correctly' });
});
});


test.serial('[MemServer.Server] this.passthrough(url) shortcut works with wild cards', async (t) => {
t.plan(2);

const MemServer = require('../../lib/index.js');

MemServer.start();
window.$ = require('jquery');

await window.$.ajax({
type: 'GET', url: 'http://localhost:4000/movies/too-big-to-fail',
headers: { 'Content-Type': 'application/json' }
}).then((data, textStatus, jqXHR) => {
t.is(jqXHR.status, 200);
t.deepEqual(jqXHR.responseJSON, { movie: 'is too-big-to-fail' });
});
});

// TODO: TEST BELOW ISNT WORKING: has beforeEach!! afterwards
// test.serial('[MemServer.Server] can create global passthrough via this.passthrough()', async (t) => {
// t.plan(6);
//
// Object.keys(require.cache).forEach((key) => delete require.cache[key]);
//
// await writeGlobalPassthroughServerFile();
//
// const MemServer = require('../../lib/index.js');
// const { Photo } = MemServer.Models;
//
// MemServer.start();
// MemServer.Server.unhandledRequest = sinon.spy();
// window.$ = require('jquery');
//
// await window.$.ajax({
// type: 'GET', url: '/photos', headers: { 'Content-Type': 'application/json' }
// }).then((data, textStatus, jqXHR) => {
// t.is(jqXHR.status, 200);
// t.deepEqual(jqXHR.responseJSON, { photos: Photo.serializer(Photo.findAll()) });
// });
// await window.$.ajax({
// type: 'GET', url: 'http://localhost:4000/films', headers: { 'Content-Type': 'application/json' }
// }).then((data, textStatus, jqXHR) => {
// t.equal(jqXHR.status, 200);
// t.deepEqual(jqXHR.responseJSON, { film: 'responsed correctly' });
// });
// await window.$.ajax({
// type: 'GET', url: 'http://localhost:4000/movies/too-big-to-fail',
// headers: { 'Content-Type': 'application/json' }
// }).then((data, textStatus, jqXHR) => {
// t.is(jqXHR.status, 200);
// t.deepEqual(jqXHR.responseJSON, { movie: 'is too-big-to-fail' });
// });
// });

// TODO: test this.passthrough('/something') when there is this.namespace;

// NOTE: passthrough order? investigate

async function writeGlobalPassthroughServerFile() {
await fs.writeFile(`${CWD}/memserver/server.js`, `
import Response from '../lib/response';
export default function({ Photo }) {
this.get('/photos', () => {
const photos = Photo.findAll();
if (!photos || photos.length === 0) {
return Response(404, { error: 'Not found' });
}
return { photos: Photo.serializer(photos) };
});
this.passthrough();
}
`);

Object.keys(require.cache).forEach((key) => delete require.cache[key]);
}

0 comments on commit 674e2e0

Please sign in to comment.