Split up pretender a bit. It was getting unwieldly.

This commit is contained in:
Robin Ward 2015-12-01 15:31:15 -05:00
parent 868079f2a8
commit 2006be41cf
4 changed files with 109 additions and 99 deletions

View File

@ -17,7 +17,6 @@ lib/es6_module_transpiler/support/es6-module-transpiler.js
public/javascripts/
spec/phantom_js/smoke_test.js
vendor/
test/javascripts/helpers/
test/javascripts/test_helper.js
test/javascripts/test_helper.js
test/javascripts/fixtures

View File

@ -1,3 +1,6 @@
import storePretender from 'helpers/store-pretender';
import fixturePretender from 'helpers/fixture-pretender';
function parsePostData(query) {
const result = {};
query.split("&").forEach(function(part) {
@ -26,52 +29,16 @@ function response(code, obj) {
}
const success = () => response({ success: true });
const _widgets = [
{id: 123, name: 'Trout Lure'},
{id: 124, name: 'Evil Repellant'}
];
const _moreWidgets = [
{id: 223, name: 'Bass Lure'},
{id: 224, name: 'Good Repellant'}
];
const fruits = [{id: 1, name: 'apple', farmer_id: 1, color_ids: [1,2], category_id: 4},
{id: 2, name: 'banana', farmer_id: 1, color_ids: [3], category_id: 3},
{id: 3, name: 'grape', farmer_id: 2, color_ids: [2], category_id: 5}];
const farmers = [{id: 1, name: 'Old MacDonald'},
{id: 2, name: 'Luke Skywalker'}];
const colors = [{id: 1, name: 'Red'},
{id: 2, name: 'Green'},
{id: 3, name: 'Yellow'}];
const loggedIn = () => !!Discourse.User.current();
const helpers = { response, success, parsePostData };
export default function() {
const server = new Pretender(function() {
const fixturesByUrl = {};
// Load any fixtures automatically
const self = this;
Ember.keys(require._eak_seen).forEach(function(entry) {
if (/^fixtures/.test(entry)) {
const fixture = require(entry, null, null, true);
if (fixture && fixture.default) {
const obj = fixture.default;
Ember.keys(obj).forEach(function(url) {
fixturesByUrl[url] = obj[url];
self.get(url, function() {
return response(obj[url]);
});
});
}
}
});
storePretender.call(this, helpers);
const fixturesByUrl = fixturePretender.call(this, helpers);
this.get('/admin/plugins', () => response({ plugins: [] }));
@ -204,61 +171,6 @@ export default function() {
});
});
this.get('/fruits/:id', function() {
const fruit = fruits[0];
return response({ __rest_serializer: "1", fruit, farmers, colors });
});
this.get('/fruits', function() {
return response({ __rest_serializer: "1", fruits, farmers, colors, extras: {hello: 'world'} });
});
this.get('/widgets/:widget_id', function(request) {
const w = _widgets.findBy('id', parseInt(request.params.widget_id));
if (w) {
return response({widget: w});
} else {
return response(404);
}
});
this.post('/widgets', function(request) {
const widget = parsePostData(request.requestBody).widget;
widget.id = 100;
return response(200, {widget});
});
this.put('/widgets/:widget_id', function(request) {
const widget = parsePostData(request.requestBody).widget;
return response({ widget });
});
this.put('/cool_things/:cool_thing_id', function(request) {
const cool_thing = parsePostData(request.requestBody).cool_thing;
return response({ cool_thing });
});
this.get('/widgets', function(request) {
let result = _widgets;
const qp = request.queryParams;
if (qp) {
if (qp.name) { result = result.filterBy('name', qp.name); }
if (qp.id) { result = result.filterBy('id', parseInt(qp.id)); }
}
return response({ widgets: result,
total_rows_widgets: 4,
load_more_widgets: '/load-more-widgets',
refresh_widgets: '/widgets?refresh=true' });
});
this.get('/load-more-widgets', function() {
return response({ widgets: _moreWidgets, total_rows_widgets: 4, load_more_widgets: '/load-more-widgets' });
});
this.delete('/widgets/:widget_id', success);
this.post('/topics/timings', () => response(200, {}));
const siteText = {id: 'site.test', value: 'Test McTest'};
@ -266,9 +178,9 @@ export default function() {
this.get('/admin/customize/site_texts', request => {
if (request.queryParams.overridden) {
return response(200, {site_texts: [overridden] })
return response(200, {site_texts: [overridden] });
} else {
return response(200, {site_texts: [siteText, overridden] })
return response(200, {site_texts: [siteText, overridden] });
}
});

View File

@ -0,0 +1,20 @@
export default function(helpers) {
const { response } = helpers;
const fixturesByUrl = {};
// Load any fixtures automatically
Ember.keys(require._eak_seen).forEach(entry => {
if (/^fixtures/.test(entry)) {
const fixture = require(entry, null, null, true);
if (fixture && fixture.default) {
const obj = fixture.default;
Ember.keys(obj).forEach(url => {
fixturesByUrl[url] = obj[url];
this.get(url, () => response(obj[url]));
});
}
}
});
return fixturesByUrl;
};

View File

@ -0,0 +1,79 @@
const _widgets = [
{id: 123, name: 'Trout Lure'},
{id: 124, name: 'Evil Repellant'}
];
const _moreWidgets = [
{id: 223, name: 'Bass Lure'},
{id: 224, name: 'Good Repellant'}
];
const fruits = [{id: 1, name: 'apple', farmer_id: 1, color_ids: [1,2], category_id: 4},
{id: 2, name: 'banana', farmer_id: 1, color_ids: [3], category_id: 3},
{id: 3, name: 'grape', farmer_id: 2, color_ids: [2], category_id: 5}];
const farmers = [{id: 1, name: 'Old MacDonald'},
{id: 2, name: 'Luke Skywalker'}];
const colors = [{id: 1, name: 'Red'},
{id: 2, name: 'Green'},
{id: 3, name: 'Yellow'}];
export default function(helpers) {
const { response, success, parsePostData } = helpers;
this.get('/fruits/:id', function() {
const fruit = fruits[0];
return response({ __rest_serializer: "1", fruit, farmers, colors });
});
this.get('/fruits', function() {
return response({ __rest_serializer: "1", fruits, farmers, colors, extras: {hello: 'world'} });
});
this.get('/widgets/:widget_id', function(request) {
const w = _widgets.findBy('id', parseInt(request.params.widget_id));
if (w) {
return response({widget: w});
} else {
return response(404);
}
});
this.post('/widgets', function(request) {
const widget = parsePostData(request.requestBody).widget;
widget.id = 100;
return response(200, {widget});
});
this.put('/widgets/:widget_id', function(request) {
const widget = parsePostData(request.requestBody).widget;
return response({ widget });
});
this.put('/cool_things/:cool_thing_id', function(request) {
const cool_thing = parsePostData(request.requestBody).cool_thing;
return response({ cool_thing });
});
this.get('/widgets', function(request) {
let result = _widgets;
const qp = request.queryParams;
if (qp) {
if (qp.name) { result = result.filterBy('name', qp.name); }
if (qp.id) { result = result.filterBy('id', parseInt(qp.id)); }
}
return response({ widgets: result,
total_rows_widgets: 4,
load_more_widgets: '/load-more-widgets',
refresh_widgets: '/widgets?refresh=true' });
});
this.get('/load-more-widgets', function() {
return response({ widgets: _moreWidgets, total_rows_widgets: 4, load_more_widgets: '/load-more-widgets' });
});
this.delete('/widgets/:widget_id', success);
};