8000 chore(b-form-file): additional unit tests (#5116) · bootstrap-vue/bootstrap-vue@d7cf463 · GitHub
[go: up one dir, main page]

Skip to content

Commit d7cf463

Browse files
authored
chore(b-form-file): additional unit tests (#5116)
1 parent b1f74a8 commit d7cf463

File tree

2 files changed

+96
-4
lines changed

2 files changed

+96
-4
lines changed

src/components/form-file/form-file.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -225,21 +225,23 @@ export const BFormFile = /*#__PURE__*/ Vue.extend({
225225
// Triggered when the parent form (if any) is reset
226226
this.selectedFile = this.multiple ? [] : null
227227
},
228-
onDragover(evt) /* istanbul ignore next: difficult to test in JSDOM */ {
228+
onDragover(evt) {
229229
evt.preventDefault()
230230
evt.stopPropagation()
231231
if (this.noDrop || !this.custom) {
232232
return
233233
}
234234
this.dragging = true
235-
evt.dataTransfer.dropEffect = 'copy'
235+
try {
236+
evt.dataTransfer.dropEffect = 'copy'
237+
} catch {}
236238
},
237-
onDragleave(evt) /* istanbul ignore next: difficult to test in JSDOM */ {
239+
onDragleave(evt) {
238240
evt.preventDefault()
239241
evt.stopPropagation()
240242
this.dragging = false
241243
},
242-
onDrop(evt) /* istanbul ignore next: difficult to test in JSDOM */ {
244+
onDrop(evt) {
243245
evt.preventDefault()
244246
evt.stopPropagation()
245247
if (this.noDrop) {

src/components/form-file/form-file.spec.js

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,22 +232,35 @@ describe('form-file', () => {
232232

233233
// Emulate the files array
234234
wrapper.vm.setFiles(files)
235+
await waitNT(wrapper.vm)
235236
expect(wrapper.emitted('input')).toBeDefined()
236237
expect(wrapper.emitted('input').length).toEqual(1)
237238
expect(wrapper.emitted('input')[0][0]).toEqual(files)
238239

239240
// Setting to same array of files should not emit event
240241
wrapper.vm.setFiles(files)
242+
await waitNT(wrapper.vm)
241243
expect(wrapper.emitted('input')).toBeDefined()
242244
expect(wrapper.emitted('input').length).toEqual(1)
243245

244246
// Setting to new array of same files should not emit event
245247
wrapper.vm.setFiles([file1, file2])
248+
await waitNT(wrapper.vm)
246249
expect(wrapper.emitted('input').length).toEqual(1)
247250

248251
// Setting to array of new files should emit event
249252
wrapper.vm.setFiles(files.slice().reverse())
253+
await waitNT(wrapper.vm)
250254
expect(wrapper.emitted('input').length).toEqual(2)
255+
expect(wrapper.emitted('input')[1][0]).toEqual(files.slice().reverse())
256+
257+
// Internally setting `selectedFile` to `null` should emit empty array
258+
wrapper.setData({
259+
selectedFile: null
260+
})
261+
await waitNT(wrapper.vm)
262+
expect(wrapper.emitted('input').length).toEqual(3)
263+
expect(wrapper.emitted('input')[2][0]).toEqual([])
251264

252265
wrapper.destroy()
253266
})
@@ -498,6 +511,83 @@ describe('form-file', () => {
498511
wrapper.destroy()
499512
})
500513

514+
it('drag placeholder and drop works', async () => {
515+
const wrapper = mount(BFormFile, {
516+
propsData: {
517+
id: 'foo',
518+
placeholder: 'PLACEHOLDER',
519+
dropPlaceholder: 'DROPHERE',
520+
noDrop: true
521+
}
522+
})
523+
const file = new File(['foo'], 'foo.txt', {
524+
type: 'text/plain',
525+
lastModified: Date.now()
526+
})
527+
528+
expect(wrapper.isVueInstance()).toBe(true)
529+
const $label = wrapper.find('label')
530+
expect($label.exists()).toBe(true)
531+
expect($label.text()).toContain('PLACEHOLDER')
532+
expect($label.text()).not.toContain('DROPHERE')
533+
534+
wrapper.trigger('dragover')
535+
await waitNT(wrapper.vm)
536+
537+
expect($label.text()).toContain('PLACEHOLDER')
538+
expect($label.text()).not.toContain('DROPHERE')
539+
540+
wrapper.trigger('drop', {
541+
dataTransfer: {
542+
files: [file]
543+
}
544+
})
545+
await waitNT(wrapper.vm)
546+
547+
expect($label.text()).toContain('PLACEHOLDER')
548+
expect($label.text()).not.toContain('DROPHERE')
549+
expect($label.text()).not.toContain(file.name)
550+
551+
wrapper.setProps({
552+
noDrop: false
553+
})
554+
await waitNT(wrapper.vm)
555+
556+
expect($label.text()).toContain('PLACEHOLDER')
557+
expect($label.text()).not.toContain('DROPHERE')
558+
559+
wrapper.trigger('dragover')
560+
await waitNT(wrapper.vm)
561+
562+
expect($label.text()).not.toContain('PLACEHOLDER')
563+
expect($label.text()).toContain('DROPHERE')
564+
565+
wrapper.trigger('dragleave')
566+
await waitNT(wrapper.vm)
567+
568+
expect($label.text()).toContain('PLACEHOLDER')
569+
expect($label.text()).not.toContain('DROPHERE')
570+
571+
wrapper.trigger('dragover')
572+
await waitNT(wrapper.vm)
573+
574+
expect($label.text()).not.toContain('PLACEHOLDER')
575+
expect($label.text()).toContain('DROPHERE')
576+
577+
wrapper.trigger('drop', {
578+
dataTransfer: {
579+
files: [file]
580+
}
581+
})
582+
await waitNT(wrapper.vm)
583+
584+
expect($label.text()).not.toContain('PLACEHOLDER')
585+
expect($label.text()).not.toContain('DROPHERE')
586+
expect($label.text()).toContain(file.name)
587+
588+
wrapper.destroy()
589+
})
590+
501591
// These tests are wrapped in a new describe to limit the scope of the getBCR Mock
502592
describe('prop `autofocus`', () => {
503593
const origGetBCR = Element.prototype.getBoundingClientRect

0 commit comments

Comments
 (0)
0