|
| 1 | +/* |
| 2 | + * The MIT License (MIT) |
| 3 | + * |
| 4 | + * Copyright (c) 2015 - present Instructure, Inc. |
| 5 | + * |
| 6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | + * of this software and associated documentation files (the "Software"), to deal |
| 8 | + * in the Software without restriction, including without limitation the rights |
| 9 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | + * copies of the Software, and to permit persons to whom the Software is |
| 11 | + * furnished to do so, subject to the following conditions: |
| 12 | + * |
| 13 | + * The above copyright notice and this permission notice shall be included in all |
| 14 | + * copies or substantial portions of the Software. |
| 15 | + * |
| 16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | + * SOFTWARE. |
| 23 | + */ |
| 24 | +import React from 'react' |
| 25 | +import { expect } from 'chai' |
| 26 | +import 'cypress-real-events' |
| 27 | + |
| 28 | +import '../support/component' |
| 29 | +import { FileDrop } from '@instructure/ui' |
| 30 | + |
| 31 | +describe('<FileDrop/>', () => { |
| 32 | + describe('file-type checking when onDrop', () => { |
| 33 | + it('responds to drop event', async () => { |
| 34 | + const onDrop = cy.spy() |
| 35 | + cy.mount(<FileDrop renderLabel="fake label" onDrop={onDrop} />) |
| 36 | + |
| 37 | + cy.get('label').trigger('drop') |
| 38 | + |
| 39 | + cy.wrap(onDrop).should('have.been.calledWithMatch') |
| 40 | + }) |
| 41 | + |
| 42 | + it('responds to change event', async () => { |
| 43 | + const onDrop = cy.spy() |
| 44 | + cy.mount(<FileDrop renderLabel="fake label" onDrop={onDrop} />) |
| 45 | + |
| 46 | + cy.get('input[type="file"]').trigger('change', { force: true }) |
| 47 | + |
| 48 | + cy.wrap(onDrop).should('have.been.called') |
| 49 | + }) |
| 50 | + |
| 51 | + it('accepts correct files using mimetypes', async () => { |
| 52 | + const onDrop = cy.spy() |
| 53 | + const onDropAccepted = cy.spy() |
| 54 | + const onDropRejected = cy.spy() |
| 55 | + |
| 56 | + cy.mount( |
| 57 | + <FileDrop |
| 58 | + renderLabel="fake label" |
| 59 | + accept="image/*" |
| 60 | + onDrop={onDrop} |
| 61 | + onDropAccepted={onDropAccepted} |
| 62 | + onDropRejected={onDropRejected} |
| 63 | + /> |
| 64 | + ) |
| 65 | + const file = new File([''], 'napoleon.png', { type: 'image/png' }) |
| 66 | + const dataTransfer = new DataTransfer() |
| 67 | + dataTransfer.items.add(file) |
| 68 | + |
| 69 | + cy.get('label').trigger('drop', { dataTransfer }) |
| 70 | + |
| 71 | + cy.wrap(onDrop) |
| 72 | + .should('have.been.called') |
| 73 | + .then(() => { |
| 74 | + const { args } = onDrop.lastCall |
| 75 | + const accepted = args[0] |
| 76 | + const rejected = args[1] |
| 77 | + |
| 78 | + expect(accepted).to.deep.equal([file]) |
| 79 | + expect(rejected).to.deep.equal([]) |
| 80 | + }) |
| 81 | + |
| 82 | + cy.wrap(onDropAccepted).should('have.been.called') |
| 83 | + cy.wrap(onDropRejected).should('not.have.been.called') |
| 84 | + }) |
| 85 | + |
| 86 | + it('rejects incorrect files using mimetypes and shouldEnablePreview', async () => { |
| 87 | + const onDrop = cy.spy() |
| 88 | + const onDropAccepted = cy.spy() |
| 89 | + const onDropRejected = cy.spy() |
| 90 | + |
| 91 | + cy.mount( |
| 92 | + <FileDrop |
| 93 | + renderLabel="fake label" |
| 94 | + shouldEnablePreview |
| 95 | + accept="image/*" |
| 96 | + onDrop={onDrop} |
| 97 | + onDropAccepted={onDropAccepted} |
| 98 | + onDropRejected={onDropRejected} |
| 99 | + /> |
| 100 | + ) |
| 101 | + const file = new File([''], 'napoleon.pdf', { type: 'application/pdf' }) |
| 102 | + const dataTransfer = new DataTransfer() |
| 103 | + dataTransfer.items.add(file) |
| 104 | + |
| 105 | + cy.get('label').trigger('drop', { dataTransfer }) |
| 106 | + |
| 107 | + cy.wrap(onDrop) |
| 108 | + .should('have.been.called') |
| 109 | + .then(() => { |
| 110 | + const { args } = onDrop.lastCall |
| 111 | + const accepted = args[0] |
| 112 | + const rejected = args[1] |
| 113 | + |
| 114 | + expect(accepted).to.deep.equal([]) |
| 115 | + expect(rejected).to.deep.equal([file]) |
| 116 | + }) |
| 117 | + |
| 118 | + cy.wrap(onDropAccepted).should('not.have.been.called') |
| 119 | + cy.wrap(onDropRejected).should('have.been.called') |
| 120 | + }) |
| 121 | + |
| 122 | + it('accepts correct files using mimetypes and enablePreview', async () => { |
| 123 | + const onDrop = cy.spy() |
| 124 | + const onDropAccepted = cy.spy() |
| 125 | + const onDropRejected = cy.spy() |
| 126 | + |
| 127 | + cy.mount( |
| 128 | + <FileDrop |
| 129 | + renderLabel="fake label" |
| 130 | + accept="image/*" |
| 131 | + shouldEnablePreview |
| 132 | + onDrop={onDrop} |
| 133 | + onDropAccepted={onDropAccepted} |
| 134 | + onDropRejected={onDropRejected} |
| 135 | + /> |
| 136 | + ) |
| 137 | + const file = new File([''], 'napoleon.png', { type: 'image/png' }) |
| 138 | + const dataTransfer = new DataTransfer() |
| 139 | + dataTransfer.items.add(file) |
| 140 | + |
| 141 | + cy.get('label').trigger('drop', { dataTransfer }) |
| 142 | + |
| 143 | + cy.wrap(onDrop) |
| 144 | + .should('have.been.called') |
| 145 | + .then(() => { |
| 146 | + const { args } = onDrop.lastCall |
| 147 | + const accepted = args[0] |
| 148 | + const rejected = args[1] |
| 149 | + |
| 150 | + expect(accepted).to.deep.equal([file]) |
| 151 | + expect(rejected).to.deep.equal([]) |
| 152 | + }) |
| 153 | + |
| 154 | + cy.wrap(onDropAccepted).should('have.been.called') |
| 155 | + cy.wrap(onDropRejected).should('not.have.been.called') |
| 156 | + }) |
| 157 | + |
| 158 | + it('accepts correct files using extensions', async () => { |
| 159 | + const onDrop = cy.spy() |
| 160 | + const onDropAccepted = cy.spy() |
| 161 | + const onDropRejected = cy.spy() |
| 162 | + |
| 163 | + cy.mount( |
| 164 | + <FileDrop |
| 165 | + renderLabel="fake label" |
| 166 | + accept="jpeg" |
| 167 | + onDrop={onDrop} |
| 168 | + onDropAccepted={onDropAccepted} |
| 169 | + onDropRejected={onDropRejected} |
| 170 | + /> |
| 171 | + ) |
| 172 | + const file = new File([''], 'napoleon.jpeg', { type: 'image/jpeg' }) |
| 173 | + const dataTransfer = new DataTransfer() |
| 174 | + dataTransfer.items.add(file) |
| 175 | + |
| 176 | + cy.get('label').trigger('drop', { dataTransfer }) |
| 177 | + |
| 178 | + cy.wrap(onDrop) |
| 179 | + .should('have.been.called') |
| 180 | + .then(() => { |
| 181 | + const { args } = onDrop.lastCall |
| 182 | + const accepted = args[0] |
| 183 | + const rejected = args[1] |
| 184 | + |
| 185 | + expect(accepted).to.deep.equal([file]) |
| 186 | + expect(rejected).to.deep.equal([]) |
| 187 | + }) |
| 188 | + |
| 189 | + cy.wrap(onDropAccepted).should('have.been.called') |
| 190 | + cy.wrap(onDropRejected).should('not.have.been.called') |
| 191 | + }) |
| 192 | + |
| 193 | + it('rejects incorrect files using mimetypes', async () => { |
| 194 | + const onDrop = cy.spy() |
| 195 | + const onDropAccepted = cy.spy() |
| 196 | + const onDropRejected = cy.spy() |
| 197 | + |
| 198 | + cy.mount( |
| 199 | + <FileDrop |
| 200 | + renderLabel="fake label" |
| 201 | + accept="image/*" |
| 202 | + onDrop={onDrop} |
| 203 | + onDropAccepted={onDropAccepted} |
| 204 | + onDropRejected={onDropRejected} |
| 205 | + /> |
| 206 | + ) |
| 207 | + const file = new File([''], 'napoleon.pdf', { type: 'application/pdf' }) |
| 208 | + const dataTransfer = new DataTransfer() |
| 209 | + dataTransfer.items.add(file) |
| 210 | + |
| 211 | + cy.get('label').trigger('drop', { dataTransfer }) |
| 212 | + |
| 213 | + cy.wrap(onDrop) |
| 214 | + .should('have.been.called') |
| 215 | + .then(() => { |
| 216 | + const { args } = onDrop.lastCall |
| 217 | + const accepted = args[0] |
| 218 | + const rejected = args[1] |
| 219 | + |
| 220 | + expect(accepted).to.deep.equal([]) |
| 221 | + expect(rejected).to.deep.equal([file]) |
| 222 | + }) |
| 223 | + |
| 224 | + cy.wrap(onDropAccepted).should('not.have.been.called') |
| 225 | + cy.wrap(onDropRejected).should('have.been.called') |
| 226 | + }) |
| 227 | + |
| 228 | + it('rejects incorrect files using extensions', async () => { |
| 229 | + const onDrop = cy.spy() |
| 230 | + const onDropAccepted = cy.spy() |
| 231 | + const onDropRejected = cy.spy() |
| 232 | + |
| 233 | + cy.mount( |
| 234 | + <FileDrop |
| 235 | + renderLabel="fake label" |
| 236 | + accept="jpeg" |
| 237 | + onDrop={onDrop} |
| 238 | + onDropAccepted={onDropAccepted} |
| 239 | + onDropRejected={onDropRejected} |
| 240 | + /> |
| 241 | + ) |
| 242 | + const file = new File([''], 'napoleon.pdf', { type: 'application/pdf' }) |
| 243 | + const dataTransfer = new DataTransfer() |
| 244 | + dataTransfer.items.add(file) |
| 245 | + |
| 246 | + cy.get('label').trigger('drop', { dataTransfer }) |
| 247 | + |
| 248 | + cy.wrap(onDrop) |
| 249 | + .should('have.been.called') |
| 250 | + .then(() => { |
| 251 | + const { args } = onDrop.lastCall |
| 252 | + const accepted = args[0] |
| 253 | + const rejected = args[1] |
| 254 | + |
| 255 | + expect(accepted).to.deep.equal([]) |
| 256 | + expect(rejected).to.deep.equal([file]) |
| 257 | + }) |
| 258 | + |
| 259 | + cy.wrap(onDropAccepted).should('not.have.been.called') |
| 260 | + cy.wrap(onDropRejected).should('have.been.called') |
| 261 | + }) |
| 262 | + }) |
| 263 | + |
| 264 | + describe('onDrag events', () => { |
| 265 | + it('responds to onDragEnter event', async () => { |
| 266 | + const onDragEnter = cy.spy() |
| 267 | + cy.mount(<FileDrop renderLabel="fake label" onDragEnter={onDragEnter} />) |
| 268 | + |
| 269 | + cy.get('label').trigger('dragenter') |
| 270 | + |
| 271 | + cy.wrap(onDragEnter).should('have.been.called') |
| 272 | + }) |
| 273 | + |
| 274 | + it('responds to onDragOver event', async () => { |
| 275 | + const onDragOver = cy.spy() |
| 276 | + cy.mount(<FileDrop renderLabel="fake label" onDragOver={onDragOver} />) |
| 277 | + |
| 278 | + cy.get('label').trigger('dragover') |
| 279 | + |
| 280 | + cy.wrap(onDragOver).should('have.been.called') |
| 281 | + }) |
| 282 | + |
| 283 | + it('responds to onDragLeave event', async () => { |
| 284 | + const onDragLeave = cy.spy() |
| 285 | + cy.mount(<FileDrop renderLabel="fake label" onDragLeave={onDragLeave} />) |
| 286 | + |
| 287 | + cy.get('label').trigger('dragleave') |
| 288 | + |
| 289 | + cy.wrap(onDragLeave).should('have.been.called') |
| 290 | + }) |
| 291 | + }) |
| 292 | + |
| 293 | + it('stops propagation when the ESC key is released and file browser is open', async () => { |
| 294 | + cy.mount(<FileDrop renderLabel="fake label" />) |
| 295 | + |
| 296 | + cy.get('label').click() |
| 297 | + cy.get('input').should('be.focused') |
| 298 | + |
| 299 | + cy.get('input').then(($input) => { |
| 300 | + const stopPropagationSpy = cy.spy() |
| 301 | + |
| 302 | + $input[0].addEventListener('keyup', (event) => { |
| 303 | + // eslint-disable-next-line no-param-reassign |
| 304 | + event.stopPropagation = stopPropagationSpy |
| 305 | + }) |
| 306 | + |
| 307 | + cy.get('input').realPress('Escape') |
| 308 | + |
| 309 | + cy.wrap(stopPropagationSpy).should('have.been.called') |
| 310 | + }) |
| 311 | + }) |
| 312 | +}) |
0 commit comments