Hopefully a fix for #12364

* register the callback before invoking the toggling
* listen for `hidden` instead of `hide`
* account for the possibility of the associated `show` event being preventDefault-ed

Adds relevant unit tests.
Merge pull request #13627 from twbs/maybe-fix-12364

Hopefully fixes trigger focus restoration on modal close
add failing test for #13268
put the tooltips into the DOM in the tooltip unit tests
don't show tooltips/popovers whose element isn't in the DOM; fixes #13268