From d8c0b4e41cc178fd6e9d273e6eb1c566053696b3 Mon Sep 17 00:00:00 2001 From: Marten Schilstra Date: Mon, 20 Jul 2015 12:33:40 +0200 Subject: [PATCH] Future proof test-helpers for Ember Data >= 2 (cherry picked from commit 5dfe3fe484d9557523d6c4a088fe747b61e9eb68) --- Brocfile.js | 9 +++++- bower.json | 3 +- .../test-module-for-model.js | 3 +- package.json | 2 +- tests/test-module-for-model-test.js | 31 +++++++++++++++---- 5 files changed, 38 insertions(+), 10 deletions(-) diff --git a/Brocfile.js b/Brocfile.js index ab03da8cd..40c178ce8 100644 --- a/Brocfile.js +++ b/Brocfile.js @@ -43,10 +43,17 @@ var vendor = concat('bower_components', { inputFiles: ['jquery/dist/jquery.js', 'ember/ember-template-compiler.js', 'ember/ember.debug.js', - 'ember-data/ember-data.js'], + 'ember-data/ember-data.js', + 'FakeXMLHttpRequest/fake_xml_http_request.js', + 'route-recognizer/dist/route-recognizer.js', + 'pretender/pretender.js'], outputFile: '/assets/vendor.js' }); +var pretender = pickFiles('bower_components', { + +}); + var qunit = pickFiles('bower_components', { srcDir: '/qunit/qunit', files: ['qunit.js', 'qunit.css'], diff --git a/bower.json b/bower.json index fd635f728..60e1939ee 100644 --- a/bower.json +++ b/bower.json @@ -25,7 +25,8 @@ ], "dependencies": { "klassy": "cerebris/klassy.js#0.1.0", - "ember": "^1.13.0" + "ember": "^1.13.0", + "pretender": "^0.7.0" }, "devDependencies": { "qunit": "^1.15.0", diff --git a/lib/ember-test-helpers/test-module-for-model.js b/lib/ember-test-helpers/test-module-for-model.js index 5942eb7af..178067bf5 100644 --- a/lib/ember-test-helpers/test-module-for-model.js +++ b/lib/ember-test-helpers/test-module-for-model.js @@ -18,7 +18,8 @@ export default TestModule.extend({ var adapterFactory = container.lookupFactory('adapter:application'); if (!adapterFactory) { - container.register('adapter:application', DS.FixtureAdapter); + adapterFactory = DS.JSONAPIAdapter || DS.FixtureAdapter; + container.register('adapter:application', adapterFactory); } callbacks.store = function(){ diff --git a/package.json b/package.json index be08b317d..93479c8e8 100644 --- a/package.json +++ b/package.json @@ -28,7 +28,7 @@ "broccoli-static-compiler": "^0.1.4", "ember-cli": "^0.2.7", "ember-cli-release": "^0.2.4", - "ember-try": "0.0.5", + "ember-try": "0.0.7", "testem": "^0.6.19" } } diff --git a/tests/test-module-for-model-test.js b/tests/test-module-for-model-test.js index a0915a2f5..3b3ff6e72 100644 --- a/tests/test-module-for-model-test.js +++ b/tests/test-module-for-model-test.js @@ -8,16 +8,20 @@ function moduleForModel(name, description, callbacks) { qunitModuleFor(module); } +var server; + +var adapter = DS.JSONAPIAdapter || DS.FixtureAdapter; + var Whazzit = DS.Model.extend({ gear: DS.attr('string') }); var whazzitAdapterFindAllCalled = false; -var WhazzitAdapter = DS.FixtureAdapter.extend({ +var WhazzitAdapter = adapter.extend({ findAll: function(store, type) { whazzitAdapterFindAllCalled = true; return this._super.apply(this, arguments); } }); -var ApplicationAdapter = DS.FixtureAdapter.extend(); +var ApplicationAdapter = DS.JSONAPIAdapter.extend(); function setupRegistry() { setResolverRegistry({ @@ -66,11 +70,11 @@ test('model exists as subject', function() { ok(model instanceof Whazzit); }); -test('FixtureAdapter is registered for model', function() { +test('JSONAPIAdapter (ED >= 2) or FixtureAdapter (ED < 2) is registered for model', function() { var model = this.subject(), store = this.store(); - ok(store.adapterFor(model.constructor) instanceof DS.FixtureAdapter); + ok(store.adapterFor(model.constructor) instanceof adapter); ok(!(store.adapterFor(model.constructor) instanceof WhazzitAdapter)); }); @@ -101,7 +105,22 @@ moduleForModel('whazzit', 'model:whazzit with custom adapter', { setup: function() { Whazzit.FIXTURES = []; + + if (DS.JSONAPIAdapter && adapter === DS.JSONAPIAdapter) { + server = new Pretender(function() { + this.get('/whazzits', function(request) { + return [200, {"Content-Type": "application/json"}, JSON.stringify({ data: Whazzit.FIXTURES })]; + }); + }); + } + whazzitAdapterFindAllCalled = false; + }, + + teardown: function() { + if (DS.JSONAPIAdapter && adapter === DS.JSONAPIAdapter) { + server.shutdown(); + } } }); @@ -112,14 +131,14 @@ test('WhazzitAdapter is registered for model', function() { ok(store.adapterFor(model.constructor) instanceof WhazzitAdapter); }); -test('WhazzitAdapter is used for `find`', function() { +test('WhazzitAdapter is used for `findAll`', function() { expect(2); ok(!whazzitAdapterFindAllCalled, 'precond - custom adapter has not yet been called'); var store = this.store(); return Ember.run(function() { - return store.find('whazzit').then(function() { + return store.findAll('whazzit', { reload: true }).then(function() { ok(whazzitAdapterFindAllCalled, 'uses the custom adapter'); }); });