Skip to content

Commit f722dc2

Browse files
authored
fix(File): add missing fields to File#toJSON (#439)
1 parent 44f3559 commit f722dc2

File tree

2 files changed

+29
-8
lines changed

2 files changed

+29
-8
lines changed

src/utils.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -379,12 +379,18 @@ const init = (AV) => {
379379
if (!value.url() && !value.id) {
380380
throw "Tried to save an object containing an unsaved file.";
381381
}
382-
return {
382+
const json = {
383383
__type: "File",
384384
id: value.id,
385+
objectId: value.id,
385386
name: value.name(),
386387
url: value.url()
387388
};
389+
const createdAt = value.get('createdAt');
390+
if (createdAt) json.createdAt = createdAt.toJSON();
391+
const updatedAt = value.get('updatedAt');
392+
if (updatedAt) json.updatedAt = updatedAt.toJSON();
393+
return json;
388394
}
389395
if (_.isObject(value)) {
390396
var output = {};

test/file.js

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ describe('files', function() {
124124
});
125125
});
126126

127-
describe('Fetch', function() {
127+
describe('#fetch', function() {
128128
var fileId = '52f9dd5ae4b019816c865985';
129129
it('createWithoutData() should return a File', function() {
130130
var file = AV.File.createWithoutData(fileId);
@@ -137,20 +137,35 @@ describe('files', function() {
137137
file.save();
138138
}).to.throwError(/File already saved\./);
139139
});
140-
it('fetch() should retrieve all data', function(done) {
141-
var file = AV.File.createWithoutData(fileId);
142-
file.fetch().then(function(file) {
140+
describe('fetch', () => {
141+
before(function() {
142+
return AV.File.createWithoutData(fileId)
143+
.fetch()
144+
.then(file => this.file = file);
145+
})
146+
it('should retrieve all data', function() {
147+
var file = this.file;
148+
return file.fetch().then(function(file) {
149+
expect(file).to.be.a(AV.File);
150+
expect(file.id).to.be(fileId);
151+
expect(file.name()).to.be('myfile.txt');
152+
expect(typeof file.url()).to.be('string');
153+
});
154+
});
155+
it('decode and encode', function () {
156+
const json = this.file.toJSON();
157+
// backward compatible check
158+
expect(json).to.have.keys(['__type', 'id', 'name', 'url']);
159+
const file = AV._decode('', json);
143160
expect(file).to.be.a(AV.File);
144161
expect(file.id).to.be(fileId);
145162
expect(file.name()).to.be('myfile.txt');
146163
expect(typeof file.url()).to.be('string');
147-
done();
148-
}).catch(function(err) {
149-
console.log(err);
150164
});
151165
});
152166
});
153167

168+
154169
describe('File get and set', function() {
155170
it('should be equal', function(done) {
156171
var base64 = 'd29ya2luZyBhdCBhdm9zY2xvdWQgaXMgZ3JlYXQh';

0 commit comments

Comments
 (0)