Skip to content

Commit

Permalink
Setting up the failing tests for abuiles#14
Browse files Browse the repository at this point in the history
  • Loading branch information
esbanarango committed Apr 28, 2015
1 parent 2b52aaa commit 88a56c3
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 0 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"body-parser": "^1.2.0",
"broccoli-asset-rev": "0.3.1",
"broccoli-ember-hbs-template-compiler": "^1.6.1",
"rimraf": "2.3.2",
"ember-cli": "0.1.2",
"ember-cli-content-security-policy": "0.3.0",
"ember-cli-ic-ajax": "0.1.1",
Expand Down
3 changes: 3 additions & 0 deletions tests/dummy/app/adapters/magazine.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import DS from 'ember-data';

export default DS.ActiveModelAdapter.extend();
16 changes: 16 additions & 0 deletions tests/dummy/app/models/magazine.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import DS from 'ember-data';
import Attachable from '../mixins/attachable';

export default DS.Model.extend(Attachable,{

attachmentAs: 'cover',

title: DS.attr('string'),
coverUrl: DS.attr('string'),
publishedAt: DS.attr('date'),

embeddedDataA1: DS.attr('string'),
embeddedDataB1: DS.attr('string'),
embeddedDataB21: DS.attr('string')

});
23 changes: 23 additions & 0 deletions tests/dummy/app/serializers/magazine.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import DS from 'ember-data';

export default DS.ActiveModelSerializer.extend({
serialize: function(magazine) {
var json = {
title: magazine.attr('title'),
cover_url: magazine.attr('coverUrl'),
published_at: magazine.attr('publishedAt'),
embbeded_data: {
embedded_data_a: {
embedded_data_a_1: magazine.attr('embeddedDataA1'),
},
embedded_data_b: {
embedded_data_b_1: magazine.attr('embeddedDataB1'),
embedded_data_b_2:{
embedded_data_b_2_1: magazine.attr('embeddedDataB21')
}
}
}
};
return json;
}
});
70 changes: 70 additions & 0 deletions tests/unit/mixins/attachable-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -266,3 +266,73 @@ test('save model with attachment but fails', function(){
deepEqual(post.get('errors.messages'), [{'photo': ['Photo is invalid']}], 'errors property should be set from response payload');
});
});

moduleForModel('magazine', 'Magazine model with attachement', {

needs: ['adapter:magazine','serializer:magazine'],

setup: function(){
// PhantomJS doesn't support Blob
// var content = '<a id="a"><b id="b">hey!</b></a>';
// fileBlob = new Blob([content], { type: "text/xml"});
fileBlob = 'BLOB';

// stub upload progress
FakeXMLHttpRequest.prototype.upload = {};
},

teardown: function(){
if (server){
server.shutdown();
}
}

});

test('saves model with attachment', function(){
expect(4);

server = new Pretender(function(){
this.post('/magazines', function(request){

var constructorName = request.requestBody.constructor.name;
if (constructorName){
equal(constructorName, 'FormData', 'Should be instance of FormData');
}else{
ok(true, "Can't assert constructor of FormData instance in PhantomJs");
}

return [ 200,
{
"Content-Type": "application/json"
},
JSON.stringify({
magazine: {
id: 1,
coverUrl: 'path/to/upload_file'
}
})
];
});
});

var magazineModel = this.subject();
// magazineModel.set('cover', fileBlob);

var result;
Ember.run(function(){

magazineModel.set('embeddedDataA1', 'embedded data a1');
magazineModel.set('embeddedDataB1', 'embedded data b1');
magazineModel.set('embeddedDataB21', 'embedded data b21');


result = magazineModel.saveWithAttachment();
});

result.then(function(createdUser){
ok(createdUser.get('isLoaded'), 'record is in isLoaded state');
equal(createdUser.get('id'), 1, 'id is set');
equal(createdUser.get('coverUrl'), 'path/to/upload_file', 'coverUrl attr should be set from response payload');
});
});

0 comments on commit 88a56c3

Please sign in to comment.