2014-03-18 19:16:27 +08:00
|
|
|
!function(){
|
2014-12-17 15:33:48 +08:00
|
|
|
'use strict';
|
2014-03-18 19:16:27 +08:00
|
|
|
|
2014-08-31 03:08:56 +08:00
|
|
|
describe('Testing Backbone Model Plugin', function(){
|
2014-03-18 19:16:27 +08:00
|
|
|
|
2014-12-17 15:33:48 +08:00
|
|
|
Backbone.$.ajaxSetup({
|
|
|
|
headers: {
|
|
|
|
"Access-Control-Allow-Origin": "*"
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2014-08-31 03:08:56 +08:00
|
|
|
var File = Backbone.Model.extend({
|
2014-12-17 15:33:48 +08:00
|
|
|
url: 'http://localhost:8989/',
|
2014-08-31 03:08:56 +08:00
|
|
|
fileAttribute: 'fileAttachment'
|
|
|
|
});
|
|
|
|
|
2014-12-17 19:56:31 +08:00
|
|
|
|
|
|
|
|
2014-08-31 03:08:56 +08:00
|
|
|
var fileModel;
|
|
|
|
var simulatedFileObj;
|
|
|
|
|
|
|
|
beforeEach(function(){
|
|
|
|
|
|
|
|
simulatedFileObj = new Blob(['<strong>Hello World</strong>'], {type : 'text/html'});
|
|
|
|
|
|
|
|
fileModel = new File({
|
|
|
|
from: 'sample@email.com',
|
|
|
|
subject: 'Hello, friend!',
|
|
|
|
body: 'Dear friend, Just saying hello! Love, Yours truly.',
|
|
|
|
nestedObject: {
|
2015-04-07 15:17:47 +08:00
|
|
|
nest: 'eggs',
|
|
|
|
nestier: {
|
|
|
|
nestiest: {
|
|
|
|
0: 'one',
|
|
|
|
1: 'two',
|
|
|
|
2: 'three'
|
|
|
|
}
|
|
|
|
}
|
2014-08-31 03:08:56 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should detect the file(blob) save successfully', function(done){
|
|
|
|
|
|
|
|
// Arrange
|
|
|
|
fileModel.set({fileAttachment: simulatedFileObj});
|
|
|
|
|
|
|
|
// Listen
|
|
|
|
fileModel.on('sync', function(model){
|
|
|
|
|
|
|
|
// Assert
|
|
|
|
expect(model.get('from')).toBe('sample@email.com');
|
2014-12-17 15:33:48 +08:00
|
|
|
|
|
|
|
// Assert Blob (phantomJS can't do Blobs so just test minimal attributes)
|
|
|
|
expect(model.get('fileAttachment').size).toBe(28);
|
|
|
|
expect(model.get('fileAttachment').type).toBe('text/html');
|
|
|
|
//expect(model.get('fileAttachment').data).toBe('data:text/html;base64,PHN0cm9uZz5IZWxsbyBXb3JsZDwvc3Ryb25nPg==');
|
2014-08-31 03:08:56 +08:00
|
|
|
|
|
|
|
done();
|
|
|
|
|
2014-12-17 15:33:48 +08:00
|
|
|
});
|
2014-08-31 03:08:56 +08:00
|
|
|
|
|
|
|
// Act
|
|
|
|
fileModel.save(null);
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
it ('should be able to be saved in as an argument of "save" as object', function(done){
|
|
|
|
|
|
|
|
// Listen
|
|
|
|
fileModel.on('sync', function(model){
|
|
|
|
|
|
|
|
// Assert
|
|
|
|
expect(model.get('from')).toBe('sample@email.com');
|
2014-12-17 15:33:48 +08:00
|
|
|
|
|
|
|
// Assert Blob (phantomJS can't do Blobs so just test minimal attributes)
|
|
|
|
expect(model.get('fileAttachment').size).toBe(28);
|
|
|
|
expect(model.get('fileAttachment').type).toBe('text/html');
|
|
|
|
//expect(model.get('fileAttachment').data).toBe('data:text/html;base64,PHN0cm9uZz5IZWxsbyBXb3JsZDwvc3Ryb25nPg==');
|
2014-08-31 03:08:56 +08:00
|
|
|
|
|
|
|
done();
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
// Act
|
|
|
|
fileModel.save({fileAttachment: simulatedFileObj});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
it ('should be able to be saved in as an argument of "save" as key/value argument', function(done){
|
|
|
|
|
|
|
|
// Listen
|
|
|
|
fileModel.on('sync', function(model){
|
|
|
|
|
|
|
|
// Assert
|
|
|
|
expect(model.get('from')).toBe('sample@email.com');
|
2014-12-17 15:33:48 +08:00
|
|
|
// Assert Blob (phantomJS can't do Blobs so just test minimal attributes)
|
|
|
|
expect(model.get('fileAttachment').size).toBe(28);
|
|
|
|
expect(model.get('fileAttachment').type).toBe('text/html');
|
|
|
|
//expect(model.get('fileAttachment').data).toBe('data:text/html;base64,PHN0cm9uZz5IZWxsbyBXb3JsZDwvc3Ryb25nPg==');
|
2014-08-31 03:08:56 +08:00
|
|
|
|
|
|
|
done();
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
// Act
|
|
|
|
fileModel.save('fileAttachment', simulatedFileObj);
|
2014-03-18 19:16:27 +08:00
|
|
|
|
2014-08-31 03:08:56 +08:00
|
|
|
});
|
2014-03-18 19:16:27 +08:00
|
|
|
|
2014-08-31 03:08:56 +08:00
|
|
|
it ('should be able to have "wait" and "validate" option', function(done){
|
2014-03-18 19:16:27 +08:00
|
|
|
|
2014-08-31 03:08:56 +08:00
|
|
|
// Listen
|
|
|
|
fileModel.on('sync', function(model){
|
|
|
|
|
|
|
|
// Assert
|
|
|
|
expect(model.get('from')).toBe('sample@email.com');
|
2014-12-17 15:33:48 +08:00
|
|
|
// Assert Blob (phantomJS can't do Blobs so just test minimal attributes)
|
|
|
|
expect(model.get('fileAttachment').size).toBe(28);
|
|
|
|
expect(model.get('fileAttachment').type).toBe('text/html');
|
|
|
|
//expect(model.get('fileAttachment').data).toBe('data:text/html;base64,PHN0cm9uZz5IZWxsbyBXb3JsZDwvc3Ryb25nPg==');
|
2014-08-31 03:08:56 +08:00
|
|
|
|
|
|
|
done();
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
// Act
|
|
|
|
fileModel.save({fileAttachment: simulatedFileObj},{wait: true, validate: false});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should still have the option to save normally by setting it and save(null)', function(done){
|
|
|
|
|
|
|
|
// Listen
|
|
|
|
fileModel.on('sync', function(model){
|
|
|
|
|
|
|
|
// Assert
|
|
|
|
expect(model.get('from')).toBe('somethingelse@email.com');
|
|
|
|
//expect(model.get('fileAttachment').data).toBe('data:text/html;base64,PHN0cm9uZz5IZWxsbyBXb3JsZDwvc3Ryb25nPg==');
|
|
|
|
|
|
|
|
done();
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
// Act
|
|
|
|
fileModel.set({from: 'somethingelse@email.com'});
|
2014-12-17 19:56:31 +08:00
|
|
|
fileModel.save();
|
2014-08-31 03:08:56 +08:00
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should still have the option to save normally by save("from","yes")', function(done){
|
|
|
|
|
|
|
|
// Listen
|
|
|
|
fileModel.on('sync', function(model){
|
|
|
|
|
|
|
|
// Assert
|
|
|
|
expect(model.get('from')).toBe('yes');
|
|
|
|
//expect(model.get('fileAttachment').data).toBe('data:text/html;base64,PHN0cm9uZz5IZWxsbyBXb3JsZDwvc3Ryb25nPg==');
|
|
|
|
|
|
|
|
done();
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
// Act
|
|
|
|
fileModel.save('from','yes');
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should still have the option to save normally by save({from: "yes"})', function(done){
|
|
|
|
|
|
|
|
// Listen
|
|
|
|
fileModel.on('sync', function(model){
|
|
|
|
|
|
|
|
// Assert
|
|
|
|
expect(model.get('from')).toBe('yes');
|
|
|
|
//expect(model.get('fileAttachment').data).toBe('data:text/html;base64,PHN0cm9uZz5IZWxsbyBXb3JsZDwvc3Ryb25nPg==');
|
|
|
|
|
|
|
|
done();
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
// Act
|
|
|
|
fileModel.save({from: "yes"});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
2014-08-31 03:17:39 +08:00
|
|
|
it('should still silent true with file', function(done){
|
|
|
|
|
|
|
|
var changed = false;
|
|
|
|
|
|
|
|
// Listen
|
|
|
|
fileModel.on('sync', function(model){
|
|
|
|
|
|
|
|
// Assert
|
2014-12-17 15:33:48 +08:00
|
|
|
// Assert Blob (phantomJS can't do Blobs so just test minimal attributes)
|
|
|
|
expect(model.get('fileAttachment').size).toBe(28);
|
|
|
|
expect(model.get('fileAttachment').type).toBe('text/html');
|
|
|
|
//expect(model.get('fileAttachment').data).toBe('data:text/html;base64,PHN0cm9uZz5IZWxsbyBXb3JsZDwvc3Ryb25nPg==');
|
2014-08-31 03:17:39 +08:00
|
|
|
|
|
|
|
setTimeout(function(){
|
|
|
|
expect(changed).not.toBeTruthy();
|
|
|
|
done();
|
|
|
|
},500);
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
fileModel.on('change', function(){
|
|
|
|
changed = true;
|
2014-12-17 15:33:48 +08:00
|
|
|
});
|
2014-08-31 03:17:39 +08:00
|
|
|
|
|
|
|
// Act
|
|
|
|
fileModel.save({fileAttachment: simulatedFileObj}, {silent: true});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should still silent true without file', function(done){
|
|
|
|
|
|
|
|
var changed = false;
|
|
|
|
|
|
|
|
// Listen
|
|
|
|
fileModel.on('sync', function(model){
|
|
|
|
|
|
|
|
// Assert
|
|
|
|
expect(model.get('from')).toBe('yes');
|
|
|
|
|
|
|
|
setTimeout(function(){
|
|
|
|
expect(changed).not.toBeTruthy();
|
|
|
|
done();
|
|
|
|
},500);
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
fileModel.on('change', function(){
|
|
|
|
changed = true;
|
2014-12-17 15:33:48 +08:00
|
|
|
});
|
2014-08-31 03:17:39 +08:00
|
|
|
|
|
|
|
// Act
|
|
|
|
fileModel.save({from: "yes"}, {silent: true});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
2015-04-07 15:17:47 +08:00
|
|
|
it('should flatten correctly using a bracket notation', function(){
|
|
|
|
|
|
|
|
// Arrange
|
|
|
|
var expected = {
|
|
|
|
"from": "sample@email.com",
|
|
|
|
"subject": "Hello, friend!",
|
|
|
|
"body": "Dear friend, Just saying hello! Love, Yours truly.",
|
|
|
|
"nestedObject[nest]": "eggs",
|
|
|
|
"nestedObject[nestier[nestiest[0]]]": "one",
|
|
|
|
"nestedObject[nestier[nestiest[1]]]": "two",
|
|
|
|
"nestedObject[nestier[nestiest[2]]]": "three"
|
|
|
|
};
|
|
|
|
|
|
|
|
var flattened = fileModel._flatten(fileModel.toJSON());
|
|
|
|
|
|
|
|
expect(flattened["nestedObject[nest]"]).toBe("eggs")
|
|
|
|
expect(flattened["nestedObject[nestier[nestiest[0]]]"]).toBe("one");
|
|
|
|
expect(flattened["nestedObject[nestier[nestiest[1]]]"]).toBe("two");
|
|
|
|
expect(flattened["nestedObject[nestier[nestiest[2]]]"]).toBe("three");
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should unflatten correctly using internal "unflatten" function', function(){
|
|
|
|
|
|
|
|
var flattened = fileModel._flatten(fileModel.toJSON()),
|
|
|
|
unflattened = fileModel._unflatten(flattened);
|
|
|
|
|
|
|
|
expect(_.isEqual(unflattened, fileModel.toJSON())).toBeTruthy();
|
|
|
|
|
|
|
|
console.log(fileModel._flatten({
|
|
|
|
'family': 'The Smiths',
|
|
|
|
'grandpa': {
|
|
|
|
'name': 'Ole Joe Smith',
|
|
|
|
'children': [
|
|
|
|
{
|
|
|
|
'name': 'Mary Lee',
|
|
|
|
'spouse': 'John Lee',
|
|
|
|
'children': [
|
|
|
|
{
|
|
|
|
'name': 'Tiny Lee'
|
|
|
|
}
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
'name': 'Susan Smith'
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
|
|
|
|
});
|
|
|
|
|
2014-08-31 03:08:56 +08:00
|
|
|
});
|
2014-03-18 19:16:27 +08:00
|
|
|
|
2015-04-07 15:17:47 +08:00
|
|
|
|
2014-03-18 19:16:27 +08:00
|
|
|
}();
|