i have a component, it has a form control → input[type=file]. Now, i need write unit test for the action, the action method in component is:
addFile(e) {
set(this, 'file', e.target.files[0]);
this._checkFileSize(e.target.files[0]);
const fileSizeAvailable = get(this, 'fileSizeAvailable');
if (!fileSizeAvailable) {
set(this, 'ifEditing', false);
set(this, 'errorMessage', get(this, 'i18n').t('individual.new.attachFile_file_size_error'));
return;
}
this._checkFileType(e.target.files[0]);
const fileTypeAvailable = get(this, 'fileTypeAvailable');
if (!fileTypeAvailable) {
set(this, 'ifEditing', false);
set(this, 'errorMessage', get(this, 'i18n').t('individual.new.attachFile_file_type_error'));
return;
}
set(this, 'errorMessage', null);
set(this, 'attachFileData.fileName', e.target.files[0].name);
}
the action ‘addFile’ is trigger by ‘change’ event.
testing framework which i used is ‘ember-mocha’.
Does anybody has the experience for it?
the testing code that i have written down is:
describe(‘Unit | Component | attachfile organization’, () => { setupComponentTest(‘attachfile-organization’, { needs: , unit: true }); it(‘should add the file meta data if user upload a file’, function () { const component = this.subject({ file: {}, fileSizeAvailable: true, fileTypeAvailable: true });
component.send('addFile', {
target: {
files: [
{
lastModified: 1500514945000,
lastModifiedDate: 'Thu Jul 20 2017 09:42:25 GMT+0800 (CST)',
name: 'hahaa.docx',
size: 223678
}
]
}
});
expect(component.get('file')).to.be.ok;
});});