From 2006be41cf624c997d0438aa7dd194114362d0cb Mon Sep 17 00:00:00 2001 From: Robin Ward Date: Tue, 1 Dec 2015 15:31:15 -0500 Subject: [PATCH] Split up pretender a bit. It was getting unwieldly. --- .eslintignore | 1 - .../helpers/create-pretender.js.es6 | 108 ++---------------- .../helpers/fixture-pretender.js.es6 | 20 ++++ .../helpers/store-pretender.js.es6 | 79 +++++++++++++ 4 files changed, 109 insertions(+), 99 deletions(-) create mode 100644 test/javascripts/helpers/fixture-pretender.js.es6 create mode 100644 test/javascripts/helpers/store-pretender.js.es6 diff --git a/.eslintignore b/.eslintignore index 8e7c2d78f..069954e34 100644 --- a/.eslintignore +++ b/.eslintignore @@ -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 diff --git a/test/javascripts/helpers/create-pretender.js.es6 b/test/javascripts/helpers/create-pretender.js.es6 index 5c8d9168f..9754b3908 100644 --- a/test/javascripts/helpers/create-pretender.js.es6 +++ b/test/javascripts/helpers/create-pretender.js.es6 @@ -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] }); } }); diff --git a/test/javascripts/helpers/fixture-pretender.js.es6 b/test/javascripts/helpers/fixture-pretender.js.es6 new file mode 100644 index 000000000..dea96b35a --- /dev/null +++ b/test/javascripts/helpers/fixture-pretender.js.es6 @@ -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; +}; diff --git a/test/javascripts/helpers/store-pretender.js.es6 b/test/javascripts/helpers/store-pretender.js.es6 new file mode 100644 index 000000000..de7ff6b27 --- /dev/null +++ b/test/javascripts/helpers/store-pretender.js.es6 @@ -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); +};