|
| 1 | +import { DummyManager } from './dummy-manager'; |
| 2 | + |
| 3 | +import { expect } from 'chai'; |
| 4 | + |
| 5 | +import * as widgets from '../../lib'; |
| 6 | + |
| 7 | +function getFileInput(view: widgets.FileUploadView): HTMLInputElement { |
| 8 | + const elem = view.fileInput; |
| 9 | + return elem as HTMLInputElement; |
| 10 | +} |
| 11 | + |
| 12 | +function getProxyButton(view: widgets.FileUploadView): HTMLButtonElement { |
| 13 | + const elem = view.el; |
| 14 | + return elem as HTMLButtonElement; |
| 15 | +} |
| 16 | + |
| 17 | +function fileInputForModel(model: widgets.FileUploadModel): HTMLInputElement { |
| 18 | + // For a given model, create and render a view and return the |
| 19 | + // view's input. |
| 20 | + const options = { model }; |
| 21 | + const view = new widgets.FileUploadView(options); |
| 22 | + view.render(); |
| 23 | + return getFileInput(view); |
| 24 | +} |
| 25 | + |
| 26 | +function proxyButtonForModel( |
| 27 | + model: widgets.FileUploadModel |
| 28 | +): HTMLButtonElement { |
| 29 | + const options = { model }; |
| 30 | + const view = new widgets.FileUploadView(options); |
| 31 | + view.render(); |
| 32 | + return getProxyButton(view); |
| 33 | +} |
| 34 | + |
| 35 | +function simulateUpload(fileInput: HTMLInputElement, files: Array<File>): void { |
| 36 | + // The 'files' property on an input element is normally not writeable |
| 37 | + // programmatically, so we explicitly overwrite it. |
| 38 | + |
| 39 | + // The type of fileInput.files is FileList, an Array with an |
| 40 | + // extra `.item` method. |
| 41 | + const fileList: any = files; |
| 42 | + fileList.item = (index: number): File => files[index]; |
| 43 | + Object.defineProperty(fileInput, 'files', { |
| 44 | + value: fileList, |
| 45 | + writable: false |
| 46 | + }); |
| 47 | + fileInput.dispatchEvent(new Event('change', { bubbles: true })); |
| 48 | +} |
| 49 | + |
| 50 | +describe('FileUploadView', function() { |
| 51 | + beforeEach(async function() { |
| 52 | + this.manager = new DummyManager(); |
| 53 | + const modelId = 'u-u-i-d'; |
| 54 | + this.model = await this.manager.new_model( |
| 55 | + { |
| 56 | + model_name: 'FileUploadModel', |
| 57 | + model_module: '@jupyter-widgets/controls', |
| 58 | + model_module_version: '1.0.0', |
| 59 | + model_id: modelId |
| 60 | + }, |
| 61 | + {} |
| 62 | + ); |
| 63 | + }); |
| 64 | + |
| 65 | + it('construction', function() { |
| 66 | + const options = { model: this.model }; |
| 67 | + const view = new widgets.FileUploadView(options); |
| 68 | + expect(view).to.not.be.undefined; |
| 69 | + }); |
| 70 | + |
| 71 | + it('default options', function() { |
| 72 | + const options = { model: this.model }; |
| 73 | + const view = new widgets.FileUploadView(options); |
| 74 | + view.render(); |
| 75 | + const fileInput = getFileInput(view); |
| 76 | + const proxyButton = getProxyButton(view); |
| 77 | + expect(fileInput.disabled).to.be.false; |
| 78 | + expect(fileInput.multiple).to.be.false; |
| 79 | + expect(proxyButton.innerText).to.equal('Upload (0)'); |
| 80 | + expect(proxyButton.querySelector('i')).to.not.be.null; |
| 81 | + expect(proxyButton.querySelector('i')!.className).to.equal('fa fa-upload'); |
| 82 | + }); |
| 83 | + |
| 84 | + it('multiple', function() { |
| 85 | + this.model.set('multiple', true); |
| 86 | + const fileInput = fileInputForModel(this.model); |
| 87 | + expect(fileInput.multiple).to.be.true; |
| 88 | + }); |
| 89 | + |
| 90 | + it('accept', function() { |
| 91 | + this.model.set('accept', 'text/csv'); |
| 92 | + const fileInput = fileInputForModel(this.model); |
| 93 | + expect(fileInput.accept).to.equal('text/csv'); |
| 94 | + }); |
| 95 | + |
| 96 | + it('disabled', function() { |
| 97 | + this.model.set('disabled', true); |
| 98 | + const proxyButton = proxyButtonForModel(this.model); |
| 99 | + expect(proxyButton.disabled).to.be.true; |
| 100 | + }); |
| 101 | + |
| 102 | + it('no icon', function() { |
| 103 | + this.model.set('icon', ''); |
| 104 | + const proxyButton = proxyButtonForModel(this.model); |
| 105 | + expect(proxyButton.querySelector('i')).to.be.null; |
| 106 | + }); |
| 107 | + |
| 108 | + it('other icon', function() { |
| 109 | + this.model.set('icon', 'check'); |
| 110 | + const proxyButton = proxyButtonForModel(this.model); |
| 111 | + expect(proxyButton.querySelector('i')).to.not.be.null; |
| 112 | + expect(proxyButton.querySelector('i')!.className).to.equal('fa fa-check'); |
| 113 | + }); |
| 114 | + |
| 115 | + it('description', function() { |
| 116 | + this.model.set('description', 'some text'); |
| 117 | + const proxyButton = proxyButtonForModel(this.model); |
| 118 | + expect(proxyButton.innerText).to.equal('some text (0)'); |
| 119 | + }); |
| 120 | + |
| 121 | + it('set model value on upload', function(done) { |
| 122 | + const fileInput = fileInputForModel(this.model); |
| 123 | + const lastModified = Date.UTC(2019, 0, 0); |
| 124 | + |
| 125 | + const uploadedFile = new File(['some file content'], 'some-name', { |
| 126 | + type: 'text/plain', |
| 127 | + lastModified |
| 128 | + }); |
| 129 | + |
| 130 | + simulateUpload(fileInput, [uploadedFile]); |
| 131 | + setTimeout(() => { |
| 132 | + expect(this.model.get('value')).to.have.length(1); |
| 133 | + const [fileInModel] = this.model.get('value'); |
| 134 | + expect(fileInModel.name).to.equal('some-name'); |
| 135 | + expect(fileInModel.type).to.equal('text/plain'); |
| 136 | + expect(fileInModel.lastModified).to.equal(lastModified); |
| 137 | + |
| 138 | + const contentInModel = new TextDecoder('utf-8').decode( |
| 139 | + fileInModel.content |
| 140 | + ); |
| 141 | + expect(contentInModel).to.equal('some file content'); |
| 142 | + done(); |
| 143 | + }, 100); |
| 144 | + }); |
| 145 | +}); |
0 commit comments