Refactor Modal.dispose and add test#27455
Refactor Modal.dispose and add test#27455Johann-S merged 2 commits intotwbs:v4-devfrom unrevised6419:patch-1
Modal.dispose and add test#27455Conversation
_isTransitioning default value_isTransitioning default value for Modal
|
It seems your change decrease our coverage 🤔 can you investigate why ? Your changes looks good to me 👍 |
|
@Johann-S I don't have so much experience with coveralls, what does this means? ps: good talk https://www.youtube.com/watch?v=O2tsvUJT09U |
|
Yep we have a lot of unit test we should add 😄 We don't want a 100% of coverage but at least 90% would be great 👍 |
_isTransitioning default value for Modal_isTransitioning default value for Modal
|
@iamandrewluca this line remove all the event listeners we attached during the use of our modal |
|
@Johann-S yes, I understand this. jQuery(element1, element2)This will try to find element1 inside element2. |
|
Yep for me it should remove the event listeners on each elements and that's what we should check |
There was a problem hiding this comment.
I don't understand the need to use show/hide methods here 🤔
var $modal = $('<div id="modal-test"/>').bootstrapModal()
assert.ok(typeof $modal.data('bs.modal') !== 'undefined', 'modal created')
$modal.bootstrapModal('dispose')
assert.ok(typeof $modal.data('bs.modal') === 'undefined', 'modal data object was disposed')If you want to go further you can use a SinonJS spy to be sure dispose is called
There was a problem hiding this comment.
From start bs.modal is undefined, then is set on show, then is removed again on hide
There was a problem hiding this comment.
Disposing modal in show state is ambiguous. All listeners are removed, and you're stuck with an open modal on page.
There was a problem hiding this comment.
The modal instance is created here: https://github.com/twbs/bootstrap/blob/94a0144b2cfa5d50b2976295da9d07465acb22ad/js/src/modal.js#L509
And me remove the data on dispose here: https://github.com/twbs/bootstrap/blob/94a0144b2cfa5d50b2976295da9d07465acb22ad/js/src/modal.js#L199
No need to show or hide
There was a problem hiding this comment.
You mean I have just to call $('#myModal').modal() ?
This 2 are the same
$('#myModal').modal() // will call `show` method
$('#myModal').modal('show') // will call `show` methodOn tests page I get an error if I don't hide modal before dispose, when trying to click on something
QUnit.test('should dispose modal', function (assert) {
assert.expect(1)
var done = assert.async()
var $modal = $('<div id="modal-test"/>')
.bootstrapModal()
.bootstrapModal('dispose')
assert.ok(typeof $modal.data('bs.modal') === 'undefined', 'modal data object was disposed')
done()
})Also if I don't hide, body will have the padding added when there is overflow.
There was a problem hiding this comment.
Tested that :
QUnit.test('should dispose modal', function (assert) {
assert.expect(2)
var $el = $('<div id="modal-test"/>')
$el.bootstrapModal()
assert.ok($el.data('bs.modal') !== undefined)
$el.bootstrapModal('dispose')
assert.ok($el.data('bs.modal') === undefined)
})and it works fine on our unit tests
There was a problem hiding this comment.
Tried to click any input on the tests page? you will get an error, and also body padding is added.
Tests are passing, but tests page looks weird.
There was a problem hiding this comment.
That's not the purpose here, we test if our dispose methods do the job that's all, so we just have to check if the data is removed and if the listeners too.
I did the first check (check if the data is removed).
We did that on other tests for our Modal and it's fine.
There was a problem hiding this comment.
If you want we can discuss about that on the Bootstrap Slack 😉
_isTransitioning default value for Modaldispose method
dispose methodModal.dispose method
Modal.dispose methodModal.dispose method
Having variables initialised from start `_isTransitioning` is better. Would be better to add an eslint rule to check for undeclared variables use. Reordered enter checks for `show` and `hide` by priority.
|
@Johann-S Done.
ps: my next PR also will be on modal improvement 😏 (about fixed, sticky elements) |
Modal.dispose methodModal.dispose and add test
There was a problem hiding this comment.
var modal = $(this).data('bs.modal')There was a problem hiding this comment.
Can you remove this comment please ? We don't need to keep the history in comment, we have git which handle that for us 😉
There was a problem hiding this comment.
Instead of doing that, you should use SinonJS spy to be sure we call .off on those elements



Having variables initialised from start
_isTransitioningis better.Would be better to add an
no-undef eslint ruleto check for undefined variables use.Reordered variables by priority at
showandhidefunction entrance.Modal.disposemethodModal.dispose_isTransitioningdefault valueno-undef- this rule is enabled by default, but does not what we wantno-use-before-define- kind of does what we want, but does not work for class membersResolves #27456