-
-
Notifications
You must be signed in to change notification settings - Fork 78.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Refactor Modal.dispose
and add test
#27455
Conversation
_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 |
js/tests/unit/modal.js
Outdated
|
||
var $modal = $('<div id="modal-test"/>') | ||
.bootstrapModal('show') | ||
.bootstrapModal('hide') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
From start bs.modal
is undefined, then is set on show
, then is removed again on hide
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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` method
On 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you want we can discuss about that on the Bootstrap Slack 😉
_isTransitioning
default value for Modal
dispose
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
js/tests/unit/modal.js
Outdated
].join('')).appendTo('#qunit-fixture') | ||
|
||
$modal.on('shown.bs.modal', function () { | ||
var data = $(this).data() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
var modal = $(this).data('bs.modal')
js/src/modal.js
Outdated
@@ -197,7 +198,21 @@ class Modal { | |||
dispose() { | |||
$.removeData(this._element, DATA_KEY) | |||
|
|||
$(window, document, this._element, this._backdrop).off(EVENT_KEY) | |||
/** |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you remove this comment please ? We don't need to keep the history in comment, we have git which handle that for us 😉
js/tests/unit/modal.js
Outdated
|
||
assert.ok(typeof data['bs.modal'] === 'undefined', 'modal data object was disposed') | ||
|
||
const modalEvents = [window, document, element, dialog] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of doing that, you should use SinonJS spy to be sure we call .off
on those elements
Having variables initialised from start
_isTransitioning
is better.Would be better to add an
no-undef eslint ruleto check for undefined variables use.Reordered variables by priority at
show
andhide
function entrance.Modal.dispose
methodModal.dispose
_isTransitioning
default 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