Skip to content

Commit c5fcae1

Browse files
committed
Add and fix tests
1 parent f2143b3 commit c5fcae1

7 files changed

Lines changed: 159 additions & 12 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Changelog
44
# 1.3.0
55
- Add a configuration option to disallow a tooltip to close by normal mouse/keyboard events.
66

7-
This should only be used if you're going to close a tooltip programmatically. There are two ways to set this option.
7+
**This should only be used if you're going to close a tooltip programmatically**. There are two ways to set this option.
88

99
1. On the trigger element. Set the `data-tooltip-autoclose` attribute to false.
1010
2. Programmatically. When calling `isotip.open()`, set `autoClose` to false in the options object.

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ This sets the element that will have a scroll event bound to it. If your tooltip
8181

8282
### **`data-tooltip-autoclose`**
8383

84-
If set to false, the tooltip will *not* close unless you do so programmatically with `isotip.close()`.
84+
If set to false, the tooltip will *not* close unless you do so programmatically with `isotip.close()`. Normal tooltips will not open until the open one has been closed!
8585

8686
### **`init( config )`**
8787

@@ -120,7 +120,7 @@ var config = {
120120
title: 'Tooltip title', // the text to go in the title, if any
121121
container: document.querySelector('.container'), // the container to append the tooltip to
122122
scrollContainer: document.querySelector('.scroll-container'), // the container to bind the scroll event to
123-
autoClose: false // set to false if you only want to close the tooltip programmatically
123+
autoClose: false // set to false if you only want to close the tooltip programmatically. Normal tooltips won't open until the open one has been closed!
124124
};
125125

126126
isotip.open( '.tooltip', config );

dist/isotip.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,11 @@ module.exports = {
292292
return
293293
}
294294

295+
// If there's a tooltip open with autoClose set to false, don't open a new one
296+
if (this.currentTooltip && this.currentTooltip.getAttribute('data-autoclose') === 'false') {
297+
return
298+
}
299+
295300
tooltip.appendChild(this.createDOMElement('<div class="tooltip-accent"></div>'))
296301

297302
// If there should be an added class name, add it

dist/isotip.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

index.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ <h2 class="h2" id="api">API</h2>
274274
</dd>
275275
<dt><pre><code>data-tooltip-autoclose</code></pre></dt>
276276
<dd>
277-
<p>If set to false, the tooltip won't close until you programmatically close it with <code>isotip.close()</code>.</p>
277+
<p>If set to false, the tooltip won't close until you programmatically close it with <code>isotip.close()</code>. Note that normal tooltips will not open until the open tooltip has been closed!</p>
278278
</dd>
279279
</dl>
280280
<p>Alternatively, programattic creation and destruction of tooltips is available.</p>
@@ -311,7 +311,7 @@ <h2 class="h2" id="api">API</h2>
311311
title: 'Tooltip title', // the text to go in the title, if any
312312
container: document.querySelector('.container'), // the container to append the tooltip to
313313
scrollContainer: document.querySelector('.scroll-container'), // the container to bind the scroll event to
314-
autoClose: false // set to false if you only want to close the tooltip programmatically
314+
autoClose: false // set to false if you only want to close the tooltip programmatically. Normal tooltips will not open until this is closed!
315315
};
316316

317317
isotip.open( '.tooltip', config );</code></pre>

isotip.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,11 @@ module.exports = {
291291
return
292292
}
293293

294+
// If there's a tooltip open with autoClose set to false, don't open a new one
295+
if (this.currentTooltip && this.currentTooltip.getAttribute('data-autoclose') === 'false') {
296+
return
297+
}
298+
294299
tooltip.appendChild(this.createDOMElement('<div class="tooltip-accent"></div>'))
295300

296301
// If there should be an added class name, add it

test/index.js

Lines changed: 143 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -335,11 +335,10 @@ describe('tooltip position', function () {
335335

336336
describe('tooltip triggers', function () {
337337
beforeEach(function () {
338-
var tooltip = document.querySelector('.tooltip')
338+
isotip.currentTooltip && isotip.currentTooltip.parentNode.removeChild(isotip.currentTooltip)
339339

340-
if (tooltip) {
341-
tooltip.parentNode.removeChild(tooltip)
342-
}
340+
isotip.currentTooltip = null
341+
isotip.currentTrigger = null
343342
})
344343

345344
it('should open a tooltip on click', function () {
@@ -448,7 +447,7 @@ describe('tooltip triggers', function () {
448447
}
449448
})
450449

451-
var trigger = document.querySelector('.tooltip-click.tooltip-no-close')
450+
var trigger = document.querySelector('.tooltip-click')
452451
var tooltipTmp = isotip.open(trigger, { autoClose: false })
453452

454453
eventFire(trigger, 'click')
@@ -460,6 +459,52 @@ describe('tooltip triggers', function () {
460459
}, isotip.options.removalDelay + 1)
461460
})
462461

462+
it('should not open another tooltip if one with autoClose set to false is already open', function (done) {
463+
isotip.init({
464+
placement: 'top',
465+
windowPadding: {
466+
top: 10
467+
}
468+
})
469+
470+
var trigger = document.querySelector('.tooltip-click.tooltip-no-close')
471+
var tooltipTmp = isotip.open(trigger)
472+
473+
expect(document.documentElement.contains(tooltipTmp)).to.be.ok
474+
475+
var tooltipTmp2 = isotip.open(document.querySelector('.tooltip-click'))
476+
477+
window.setTimeout(function () {
478+
expect(document.documentElement.contains(tooltipTmp)).to.be.ok
479+
expect(document.documentElement.contains(tooltipTmp2)).to.not.be.ok
480+
481+
done()
482+
}, isotip.options.removalDelay + 1)
483+
})
484+
485+
it('should not open another tooltip programmatically if one with autoClose set to false is already open', function (done) {
486+
isotip.init({
487+
placement: 'top',
488+
windowPadding: {
489+
top: 10
490+
}
491+
})
492+
493+
var trigger = document.querySelector('.tooltip-click')
494+
var tooltipTmp = isotip.open(trigger, { autoClose: false })
495+
496+
expect(document.documentElement.contains(tooltipTmp)).to.be.ok
497+
498+
var tooltipTmp2 = isotip.open(document.querySelector('.tooltip-default'))
499+
500+
window.setTimeout(function () {
501+
expect(document.documentElement.contains(tooltipTmp)).to.be.ok
502+
expect(document.documentElement.contains(tooltipTmp2)).to.not.be.ok
503+
504+
done()
505+
}, isotip.options.removalDelay + 1)
506+
})
507+
463508
it('should not close a tooltip on click on a child element in toolip', function (done) {
464509
isotip.init({
465510
placement: 'top',
@@ -592,6 +637,29 @@ describe('tooltip triggers', function () {
592637
}, isotip.options.removalDelay + 1)
593638
})
594639

640+
it('should not open a new tooltip on mouseover if one with autoClose set to false exists', function (done) {
641+
isotip.init({
642+
placement: 'top',
643+
windowPadding: {
644+
top: -10
645+
}
646+
})
647+
648+
var trigger = document.querySelector('.tooltip-click.tooltip-no-close')
649+
var tooltipTmp = isotip.open(trigger)
650+
651+
expect(document.documentElement.contains(tooltipTmp)).to.be.ok
652+
653+
var tooltipTmp2 = isotip.open(document.querySelector('.tooltip-hover'))
654+
655+
setTimeout(function () {
656+
expect(document.documentElement.contains(tooltipTmp)).to.be.ok
657+
expect(document.documentElement.contains(tooltipTmp2)).to.not.be.ok
658+
659+
done()
660+
}, isotip.options.removalDelay + 1)
661+
})
662+
595663
it('should not close a tooltip on mouseout if autoClose is set to false programmatically', function (done) {
596664
isotip.init({
597665
placement: 'top',
@@ -612,6 +680,29 @@ describe('tooltip triggers', function () {
612680
}, isotip.options.removalDelay + 1)
613681
})
614682

683+
it('should not open a new tooltip on mouseover programmatically if one with autoClose set to false exists', function (done) {
684+
isotip.init({
685+
placement: 'top',
686+
windowPadding: {
687+
top: -10
688+
}
689+
})
690+
691+
var trigger = document.querySelector('.tooltip-click')
692+
var tooltipTmp = isotip.open(trigger, { autoClose: false })
693+
694+
expect(document.documentElement.contains(tooltipTmp)).to.be.ok
695+
696+
var tooltipTmp2 = isotip.open(document.querySelector('.tooltip-hover'))
697+
698+
setTimeout(function () {
699+
expect(document.documentElement.contains(tooltipTmp)).to.be.ok
700+
expect(document.documentElement.contains(tooltipTmp2)).to.not.be.ok
701+
702+
done()
703+
}, isotip.options.removalDelay + 1)
704+
})
705+
615706
it('should open a tooltip on focus', function () {
616707
isotip.init({
617708
placement: 'top',
@@ -673,7 +764,30 @@ describe('tooltip triggers', function () {
673764
}, isotip.options.removalDelay + 1)
674765
})
675766

676-
it('should not close a tooltip on blur if autoClose is set to false programmatically', function (done) {
767+
it('should open a new tooltip on blur if one with autoClose set to false exists', function (done) {
768+
isotip.init({
769+
placement: 'top',
770+
windowPadding: {
771+
top: 10
772+
}
773+
})
774+
775+
var trigger = document.querySelector('.tooltip-click.tooltip-no-close')
776+
var tooltipTmp = isotip.open(trigger)
777+
778+
expect(document.documentElement.contains(tooltipTmp)).to.be.ok
779+
780+
var tooltipTmp2 = isotip.open(document.querySelector('.tooltip-focus'))
781+
782+
setTimeout(function () {
783+
expect(document.documentElement.contains(tooltipTmp)).to.be.ok
784+
expect(document.documentElement.contains(tooltipTmp2)).to.not.be.ok
785+
786+
done()
787+
}, isotip.options.removalDelay + 1)
788+
})
789+
790+
it('should not close a tooltip on focus if autoClose is set to false programmatically', function (done) {
677791
isotip.init({
678792
placement: 'top',
679793
windowPadding: {
@@ -692,4 +806,27 @@ describe('tooltip triggers', function () {
692806
done()
693807
}, isotip.options.removalDelay + 1)
694808
})
809+
810+
it('should open a new tooltip programmatically on focus if one with autoClose set to false exists', function (done) {
811+
isotip.init({
812+
placement: 'top',
813+
windowPadding: {
814+
top: 10
815+
}
816+
})
817+
818+
var trigger = document.querySelector('.tooltip-click')
819+
var tooltipTmp = isotip.open(trigger, { autoClose: false })
820+
821+
expect(document.documentElement.contains(tooltipTmp)).to.be.ok
822+
823+
var tooltipTmp2 = isotip.open(document.querySelector('.tooltip-focus'))
824+
825+
setTimeout(function () {
826+
expect(document.documentElement.contains(tooltipTmp)).to.be.ok
827+
expect(document.documentElement.contains(tooltipTmp2)).to.not.be.ok
828+
829+
done()
830+
}, isotip.options.removalDelay + 1)
831+
})
695832
})

0 commit comments

Comments
 (0)