From 34dbcf37a62da218e63145c7eec40834b60a655b Mon Sep 17 00:00:00 2001 From: Josiah Baldwin Date: Tue, 19 Jan 2016 11:43:02 -0800 Subject: [PATCH] Added test for array of files. Also added JSON.stringify around the flatten call in the log call at the end of the unflatten test --- test/backbone-model-file-upload.spec.js | 49 ++++++++++++++++++------- 1 file changed, 36 insertions(+), 13 deletions(-) diff --git a/test/backbone-model-file-upload.spec.js b/test/backbone-model-file-upload.spec.js index 562051b..a330779 100644 --- a/test/backbone-model-file-upload.spec.js +++ b/test/backbone-model-file-upload.spec.js @@ -18,10 +18,12 @@ var fileModel; var simulatedFileObj; + var simulatedFileObj2; beforeEach(function(){ simulatedFileObj = new Blob(['Hello World'], {type : 'text/html'}); + simulatedFileObj2 = new Blob(['Hello Again, World'], {type : 'text/html'}); fileModel = new File({ from: 'sample@email.com', @@ -42,13 +44,13 @@ }); 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'); @@ -70,7 +72,7 @@ // Listen fileModel.on('sync', function(model){ - + // Assert expect(model.get('from')).toBe('sample@email.com'); @@ -92,7 +94,6 @@ // Listen fileModel.on('sync', function(model){ - // Assert expect(model.get('from')).toBe('sample@email.com'); // Assert Blob (phantomJS can't do Blobs so just test minimal attributes) @@ -109,11 +110,33 @@ }); + it ('should be able to save an array of multiple file objects', function(done){ + // Listen + fileModel.on('sync', function(model){ + // Assert + expect(model.get('from')).toBe('sample@email.com'); + + // Assert Blob (phantomJS can't do Blobs so just test minimal attributes) + expect(model.get('fileAttachment')[0].size).toBe(28); + expect(model.get('fileAttachment')[0].type).toBe('text/html'); + //expect(model.get('fileAttachment')[0].data).toBe('data:text/html;base64,PHN0cm9uZz5IZWxsbyBXb3JsZDwvc3Ryb25nPg==') + expect(model.get('fileAttachment')[1].size).toBe(35); + expect(model.get('fileAttachment')[1].type).toBe('text/html'); + //expect(model.get('fileAttachment')[1].data).toBe('data:text/html;base64,PHN0cm9uZz5IZWxsbyBBZ2FpbiwgV29ybGQ8L3N0cm9uZz4K=') + + done(); + + }); + + // Act + fileModel.save('fileAttachment', [simulatedFileObj, simulatedFileObj2], {formData: true}); + }); + it ('should be able to have "wait" and "validate" option', function(done){ // Listen fileModel.on('sync', function(model){ - + // Assert expect(model.get('from')).toBe('sample@email.com'); // Assert Blob (phantomJS can't do Blobs so just test minimal attributes) @@ -134,7 +157,7 @@ // 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=='); @@ -153,7 +176,7 @@ // Listen fileModel.on('sync', function(model){ - + // Assert expect(model.get('from')).toBe('yes'); //expect(model.get('fileAttachment').data).toBe('data:text/html;base64,PHN0cm9uZz5IZWxsbyBXb3JsZDwvc3Ryb25nPg=='); @@ -171,7 +194,7 @@ // Listen fileModel.on('sync', function(model){ - + // Assert expect(model.get('from')).toBe('yes'); //expect(model.get('fileAttachment').data).toBe('data:text/html;base64,PHN0cm9uZz5IZWxsbyBXb3JsZDwvc3Ryb25nPg=='); @@ -191,7 +214,7 @@ // Listen fileModel.on('sync', function(model){ - + // Assert // Assert Blob (phantomJS can't do Blobs so just test minimal attributes) expect(model.get('fileAttachment').size).toBe(28); @@ -220,7 +243,7 @@ // Listen fileModel.on('sync', function(model){ - + // Assert expect(model.get('from')).toBe('yes'); @@ -268,7 +291,7 @@ expect(_.isEqual(unflattened, fileModel.toJSON())).toBeTruthy(); - console.log(fileModel._flatten({ + console.log(JSON.stringify(fileModel._flatten({ 'family': 'The Smiths', 'grandpa': { 'name': 'Ole Joe Smith', @@ -287,11 +310,11 @@ } ] } - })); + }))); }); }); -}(); \ No newline at end of file +}();