Skip to content

Commit

Permalink
Future proof test-helpers for Ember Data >= 2
Browse files Browse the repository at this point in the history
(cherry picked from commit 5dfe3fe)
  • Loading branch information
martndemus authored and rwjblue committed Jul 20, 2015
1 parent f98a853 commit d8c0b4e
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 10 deletions.
9 changes: 8 additions & 1 deletion Brocfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
Expand Down
3 changes: 2 additions & 1 deletion bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
3 changes: 2 additions & 1 deletion lib/ember-test-helpers/test-module-for-model.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(){
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
31 changes: 25 additions & 6 deletions tests/test-module-for-model-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down Expand Up @@ -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));
});

Expand Down Expand Up @@ -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();
}
}
});

Expand All @@ -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');
});
});
Expand Down

0 comments on commit d8c0b4e

Please sign in to comment.