it('beforeSaveFile should contain metadata and tags saved from client', async () => { await reconfigureServer({ filesAdapter: mockAdapter }); const createFileSpy = spyOn(mockAdapter, 'createFile').and.callThrough(); Parse.Cloud.beforeSaveFile(async (req) => { expect(req.triggerName).toEqual('beforeSaveFile'); expect(req.fileSize).toBe(3); expect(req.file).toBeInstanceOf(Parse.File); expect(req.file.name()).toBe('popeye.txt'); expect(req.file.metadata()).toEqual({ foo: 'bar' }); expect(req.file.tags()).toEqual({ bar: 'foo' }); }); const file = new Parse.File('popeye.txt', [1, 2, 3], 'text/plain'); file.setMetadata({ foo: 'bar' }); file.setTags({ bar: 'foo' }); const result = await file.save({ useMasterKey: true }); expect(result).toBeInstanceOf(Parse.File); const options = { metadata: { foo: 'bar' }, tags: { bar: 'foo' }, }; expect(createFileSpy).toHaveBeenCalledWith(jasmine.any(String), jasmine.any(Buffer), 'text/plain', options); });
it('beforeSaveFile should change values by returning new fileObject', async () => { await reconfigureServer({ filesAdapter: mockAdapter }); const createFileSpy = spyOn(mockAdapter, 'createFile').and.callThrough(); Parse.Cloud.beforeSaveFile(async (req) => { expect(req.triggerName).toEqual('beforeSaveFile'); expect(req.fileSize).toBe(3); const newFile = new Parse.File('donald_duck.pdf', [4, 5, 6], 'application/pdf'); newFile.setMetadata({ foo: 'bar' }); newFile.setTags({ tagA: 'some-tag' }); return newFile; }); const file = new Parse.File('popeye.txt', [1, 2, 3], 'text/plain'); const result = await file.save({ useMasterKey: true }); expect(result).toBeInstanceOf(Parse.File); const newData = new Buffer([4, 5, 6]); const newContentType = 'application/pdf'; const newOptions = { tags: { tagA: 'some-tag', }, metadata: { foo: 'bar', }, }; expect(createFileSpy).toHaveBeenCalledWith(jasmine.any(String), newData, newContentType, newOptions); const expectedFileName = 'donald_duck.pdf'; expect(file._name.indexOf(expectedFileName)).toBe(file._name.length - expectedFileName.length); });