Skip to content

Commit 9150973

Browse files
authored
chore(unit testing): modal test updates (#2994)
1 parent df2e77a commit 9150973

File tree

5 files changed

+611
-163
lines changed

5 files changed

+611
-163
lines changed

src/components/modal/fixtures/modal.html

Lines changed: 0 additions & 37 deletions
This file was deleted.

src/components/modal/fixtures/modal.js

Lines changed: 0 additions & 21 deletions
This file was deleted.

src/components/modal/modal-defaults.spec.js

Lines changed: 0 additions & 42 deletions
This file was deleted.

src/components/modal/modal.js

Lines changed: 26 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -355,11 +355,9 @@ export default {
355355
},
356356
watch: {
357357
visible(newVal, oldVal) {
358-
if (newVal === oldVal) {
359-
/* istanbul ignore next */
360-
return
358+
if (newVal !== oldVal) {
359+
this[newVal ? 'show' : 'hide']()
361360
}
362-
this[newVal ? 'show' : 'hide']()
363361
}
364362
},
365363
created() {
@@ -381,7 +379,7 @@ export default {
381379
this.show()
382380
}
383381
},
384-
beforeDestroy() /* istanbul ignore next */ {
382+
beforeDestroy() {
385383
// Ensure everything is back to normal
386384
if (this._observer) {
387385
this._observer.disconnect()
@@ -409,11 +407,14 @@ export default {
409407
show() {
410408
if (this.is_visible || this.is_opening) {
411409
// if already open, on in the process of opening, do nothing
410+
/* istanbul ignore next */
412411
return
413412
}
414413
if (this.is_closing) {
415414
// if we are in the process of closing, wait until hidden before re-opening
415+
/* istanbul ignore next: very difficult to test */
416416
this.$once('hidden', this.show)
417+
/* istanbul ignore next */
417418
return
418419
}
419420
this.is_opening = true
@@ -433,11 +434,7 @@ export default {
433434
if (!this.noStacking) {
434435
// Find the z-index to use
435436
this.zIndex = getModalNextZIndex()
436-
// Show the modal
437-
this.doShow()
438-
return
439-
}
440-
if (hasClass(document.body, 'modal-open')) {
437+
} else if (hasClass(document.body, 'modal-open')) {
441438
// If another modal is already open, wait for it to close
442439
this.$root.$once('bv::modal::hidden', this.doShow)
443440
return
@@ -447,6 +444,7 @@ export default {
447444
},
448445
hide(trigger) {
449446
if (!this.is_visible || this.is_closing) {
447+
/* istanbul ignore next */
450448
return
451449
}
452450
this.is_closing = true
@@ -618,23 +616,15 @@ export default {
618616
},
619617
// Turn on/off focusin listener
620618
setEnforceFocus(on) {
621-
const options = { passive: true, capture: false }
622-
if (on) {
623-
eventOn(document, 'focusin', this.focusHandler, options)
624-
} else {
625-
eventOff(document, 'focusin', this.focusHandler, options)
626-
}
619+
const method = on ? eventOn : eventOff
620+
method(document, 'focusin', this.focusHandler, { passive: true, capture: false })
627621
},
628622
// Resize Listener
629-
setResizeEvent(on) /* istanbul ignore next: can't easily test in JSDOM */ {
630-
;['resize', 'orientationchange'].forEach(evtName => {
631-
const options = { passive: true, capture: false }
632-
if (on) {
633-
eventOn(window, evtName, this.adjustDialog, options)
634-
} else {
635-
eventOff(window, evtName, this.adjustDialog, options)
636-
}
637-
})
623+
setResizeEvent(on) {
624+
const options = { passive: true, capture: false }
625+
const method = on ? eventOn : eventOff
626+
method(window, 'resize', this.adjustDialog, options)
627+
method(window, 'orientationchange', this.adjustDialog, options)
638628
},
639629
// Root Listener handlers
640630
showHandler(id, triggerEl) {
@@ -673,6 +663,7 @@ export default {
673663
focusFirst() {
674664
// Don't try and focus if we are SSR
675665
if (typeof document === 'undefined') {
666+
/* istanbul ignore next */
676667
return
677668
}
678669
const modal = this.$refs.modal
@@ -713,14 +704,12 @@ export default {
713704
document.body.removeChild(scrollDiv)
714705
},
715706
setModalOpenClass(open) {
716-
if (open) {
717-
addClass(document.body, 'modal-open')
718-
} else {
719-
removeClass(document.body, 'modal-open')
720-
}
707+
const method = open ? addClass : removeClass
708+
method(document.body, 'modal-open')
721709
},
722710
adjustDialog() {
723711
if (!this.is_visible) {
712+
/* istanbul ignore next */
724713
return
725714
}
726715
const modal = this.$refs.modal
@@ -743,20 +732,23 @@ export default {
743732
modal.style.paddingRight = ''
744733
}
745734
},
746-
checkScrollbar() /* istanbul ignore next: getBCR can't be tested in JSDOM */ {
735+
checkScrollbar() {
747736
const { left, right, height } = getBCR(document.body)
748737
// Extra check for body.height needed for stacked modals
749738
this.isBodyOverflowing = left + right < window.innerWidth || height > window.innerHeight
750739
},
751740
setScrollbar() {
741+
const body = document.body
742+
// Storage place to cache changes to margins and padding
743+
// Note: THis assumes the following element types are not added to the
744+
// document after hte modal has opened.
745+
body._paddingChangedForModal = body._paddingChangedForModal || []
746+
body._marginChangedForModal = body._marginChangedForModal || []
752747
/* istanbul ignore if: get Computed Style can't be tested in JSDOM */
753748
if (this.isBodyOverflowing) {
754749
// Note: DOMNode.style.paddingRight returns the actual value or '' if not set
755750
// while $(DOMNode).css('padding-right') returns the calculated value or 0 if not set
756-
const body = document.body
757751
const scrollbarWidth = this.scrollbarWidth
758-
body._paddingChangedForModal = []
759-
body._marginChangedForModal = []
760752
// Adjust fixed content padding
761753
selectAll(Selector.FIXED_CONTENT).forEach(el => {
762754
const actualPadding = el.style.paddingRight

0 commit comments

Comments
 (0)