diff options
-rw-r--r-- | src/content/hint.js | 7 | ||||
-rw-r--r-- | test/content/hint.html | 1 | ||||
-rw-r--r-- | test/content/hint.test.js | 58 |
3 files changed, 64 insertions, 2 deletions
diff --git a/src/content/hint.js b/src/content/hint.js index 7979cf1..f59899d 100644 --- a/src/content/hint.js +++ b/src/content/hint.js @@ -2,6 +2,10 @@ import './hint.css'; export default class Hint { constructor(target, tag) { + if (!(document.body instanceof HTMLElement)) { + throw new TypeError('target is not an HTMLElement'); + } + this.target = target; let doc = target.ownerDocument @@ -31,8 +35,7 @@ export default class Hint { activate() { if (this.target.tagName.toLowerCase() === 'a') { - let href = this.target.href; - window.location.href = href; + this.target.click(); } } } diff --git a/test/content/hint.html b/test/content/hint.html new file mode 100644 index 0000000..b50c5fe --- /dev/null +++ b/test/content/hint.html @@ -0,0 +1 @@ +<a id='test-link' href='javascript:window.vimvixenTest="hello"' >link</a> diff --git a/test/content/hint.test.js b/test/content/hint.test.js new file mode 100644 index 0000000..9b2ab6e --- /dev/null +++ b/test/content/hint.test.js @@ -0,0 +1,58 @@ +import { expect } from "chai"; +import Hint from '../../src/content/hint'; + +describe('Hint class', () => { + beforeEach(() => { + document.body.innerHTML = __html__['test/content/hint.html']; + }); + + describe('#constructor', () => { + it('creates a hint element with tag name', () => { + let link = document.getElementById('test-link'); + let hint = new Hint(link, 'abc'); + expect(hint.element.textContent.trim()).to.be.equal('abc'); + }); + + it('throws an exception when non-element given', () => { + expect(() => new Hint(window, 'abc')).to.throw(TypeError); + }); + }); + + describe('#show', () => { + it('shows an element', () => { + let link = document.getElementById('test-link'); + let hint = new Hint(link, 'abc'); + hint.hide(); + hint.show(); + + expect(hint.element.style.display).to.not.equal('none'); + }); + }); + + describe('#hide', () => { + it('hides an element', () => { + let link = document.getElementById('test-link'); + let hint = new Hint(link, 'abc'); + hint.hide(); + + expect(hint.element.style.display).to.equal('none'); + }); + }); + + describe('#remove', () => { + it('removes an element', () => { + let link = document.getElementById('test-link'); + let hint = new Hint(link, 'abc'); + + expect(hint.element.parentElement).to.not.be.null; + hint.remove(); + expect(hint.element.parentElement).to.be.null; + }); + }); + + describe('#activate', () => { + // TODO test activations + }); +}); + + |