Skip to content

Commit

Permalink
Merge pull request #79 from rwjblue/fix-tests-for-beta
Browse files Browse the repository at this point in the history
Prevent errors when Ember.View / Ember._MetamorphView are undefined.
  • Loading branch information
rwjblue committed Jul 20, 2015
2 parents d8f990c + 0db2127 commit f5fba33
Show file tree
Hide file tree
Showing 10 changed files with 96 additions and 33 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
9 changes: 5 additions & 4 deletions bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,15 @@
],
"dependencies": {
"klassy": "cerebris/klassy.js#0.1.0",
"ember": "^1.13.0"
"ember": "^1.13.0",
"ember-data": "canary",
"pretender": "^0.7.0"
},
"devDependencies": {
"qunit": "^1.15.0",
"loader.js": "ember-cli/loader.js#3.0.0",
"ember-cli-shims": "ember-cli/ember-cli-shims#0.0.3",
"ember-cli-test-loader": "rwjblue/ember-cli-test-loader#0.0.4",
"jquery": "~2.1.1",
"ember-data": "~1.0.0-beta.19.2"
"jquery": "~2.1.1"
}
}
}
12 changes: 10 additions & 2 deletions config/ember-try.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,21 @@ module.exports = {
{
name: 'ember-1.11.3',
dependencies: {
"ember": "1.11.3"
"ember": "1.11.3",
"ember-data": "~1.0.0-beta.19.2"
}
},
{
name: 'ember-1.12.1',
dependencies: {
"ember": "1.12.1"
"ember": "1.12.1",
"ember-data": "~1.0.0-beta.19.2"
}
},
{
name: 'ember-1.13.5',
dependencies: {
"ember": "1.13.5"
}
},
{
Expand Down
14 changes: 12 additions & 2 deletions lib/ember-test-helpers/build-registry.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,18 @@ export default function(resolver) {

// Ember 1.10.0 did not properly add `view:toplevel` or `view:default`
// to the registry in Ember.Application.buildRegistry :(
register('view:toplevel', Ember.View.extend());
register('view:default', Ember._MetamorphView);
//
// Ember 2.0.0 removed Ember.View as public API, so only do this when
// Ember.View is present
if (Ember.View) {
register('view:toplevel', Ember.View.extend());
}

// Ember 2.0.0 removed Ember._MetamorphView from the Ember global, so only
// do this when present
if (Ember._MetamorphView) {
register('view:default', Ember._MetamorphView);
}

var globalContext = typeof global === 'object' && global || self;
if (globalContext.DS) {
Expand Down
10 changes: 5 additions & 5 deletions lib/ember-test-helpers/test-module-for-component.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,16 @@ export default TestModule.extend({
context.dispatcher.setup({}, '#ember-testing');

this.callbacks.render = function() {
var containerView = Ember.ContainerView.create({container: container});
var subject;

Ember.run(function(){
var subject = context.subject();
containerView.pushObject(subject);
containerView.appendTo('#ember-testing');
subject = context.subject();
subject.appendTo('#ember-testing');
});

_this.teardownSteps.unshift(function() {
Ember.run(function() {
Ember.tryInvoke(containerView, 'destroy');
Ember.tryInvoke(subject, 'destroy');
});
});
};
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
7 changes: 6 additions & 1 deletion lib/ember-test-helpers/test-module.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,12 @@ export default Klass.extend({

teardownTestElements: function() {
Ember.$('#ember-testing').empty();
Ember.View.views = {};

// Ember 2.0.0 removed Ember.View as public API, so only do this when
// Ember.View is present
if (Ember.View) {
Ember.View.views = {};
}
},

defaultSubject: function(options, factory) {
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"
}
}
32 changes: 22 additions & 10 deletions tests/test-module-for-component-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ var PrettyColor = Ember.Component.extend({
}.property('name')
});

var ColorController = Ember.ObjectController.extend({
var ColorController = Ember.Controller.extend({
hexa: function() {
switch( this.get('model') ) {
case 'red':
Expand Down Expand Up @@ -239,16 +239,28 @@ test('it allows missing callbacks', function() {
ok(true, 'no errors are thrown');
});

moduleForComponent('x-bad', {
needs: ['mis:sing'],
beforeEach: function(assert) {
// won't be called because of setup error
var done = assert.async();
assert.ok(true);
done();
}
module('moduleForComponent: handles errors thrown during setup', {
beforeEach: function(assert) {
var done = assert.async();
testModule = new TestModuleForComponent('x-bad', {
needs: ['mis:sing'],

beforeEach: function(assert) {
// won't be called because of setup error
var done = assert.async();
assert.ok(true);
done();
}
});

testModule.setup()
.catch(function(error) {
ok(error.message.indexOf('mis:sing') > -1, 'correct error was thrown from module setup');
})
.finally(done);
}
});

test('it happens', function() {
expect(0);
ok(true, 'errors are properly thrown/handled');
});
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 = adapter.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 f5fba33

Please sign in to comment.