From f1c920e0003238a8f319fd29cd7aea068fd4f231 Mon Sep 17 00:00:00 2001 From: Shin'ya Ueoka Date: Thu, 17 Aug 2017 22:33:46 +0900 Subject: add HintKeyProducer --- test/content/hint-key-producer.test.js | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 test/content/hint-key-producer.test.js (limited to 'test/content') diff --git a/test/content/hint-key-producer.test.js b/test/content/hint-key-producer.test.js new file mode 100644 index 0000000..74fb462 --- /dev/null +++ b/test/content/hint-key-producer.test.js @@ -0,0 +1,25 @@ +import { expect } from "chai"; +import HintKeyProducer from '../../src/content/hint-key-producer'; + +describe('HintKeyProducer class', () => { + describe('#constructor', () => { + it('throws an exception on empty charset', () => { + expect(() => new HintKeyProducer([])).to.throw(TypeError); + }); + }); + + describe('#produce', () => { + it('produce incremented keys', () => { + let charset = 'abc'; + let sequences = [ + 'a', 'b', 'c', + 'aa', 'ab', 'ac', 'ba', 'bb', 'bc', 'ca', 'cb', 'cc', + 'aaa', 'aab', 'aac', 'aba'] + + let producer = new HintKeyProducer(charset); + for (let i = 0; i < sequences.length; ++i) { + expect(producer.produce()).to.equal(sequences[i]); + } + }); + }); +}); -- cgit v1.2.3 From 55147feb8e64a81ac665575aa6801d315da85e88 Mon Sep 17 00:00:00 2001 From: Shin'ya Ueoka Date: Sun, 20 Aug 2017 21:34:06 +0900 Subject: add Follow test --- test/content/follow.test.js | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 test/content/follow.test.js (limited to 'test/content') diff --git a/test/content/follow.test.js b/test/content/follow.test.js new file mode 100644 index 0000000..eb3d679 --- /dev/null +++ b/test/content/follow.test.js @@ -0,0 +1,14 @@ +import { expect } from "chai"; +import Follow from '../../src/content/follow'; + +describe('Follow class', () => { + describe('#codeChars', () => { + it('returns a string for key codes', () => { + let chars = [ + KeyboardEvent.DOM_VK_0, KeyboardEvent.DOM_VK_1, + KeyboardEvent.DOM_VK_A, KeyboardEvent.DOM_VK_B]; + expect(Follow.codeChars(chars)).to.equal('01ab'); + expect(Follow.codeChars([])).to.be.equal(''); + }); + }); +}); -- cgit v1.2.3 From c50f463bc12da7e3a5de490b714b4ff1ea8d3e56 Mon Sep 17 00:00:00 2001 From: Shin'ya Ueoka Date: Mon, 21 Aug 2017 21:01:29 +0900 Subject: add follow tests --- src/content/follow.js | 16 +++++++++++++++- test/content/follow.html | 9 +++++++++ test/content/follow.test.js | 11 +++++++++++ 3 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 test/content/follow.html (limited to 'test/content') diff --git a/src/content/follow.js b/src/content/follow.js index c0b7a44..d678351 100644 --- a/src/content/follow.js +++ b/src/content/follow.js @@ -105,6 +105,20 @@ export default class Follow { } static getTargetElements(doc) { - return doc.querySelectorAll('a') + let all = doc.querySelectorAll('a'); + let filtered = Array.prototype.filter.call(all, (e) => { + return Follow.isVisibleElement(e); + }); + return filtered; + } + + static isVisibleElement(element) { + var style = window.getComputedStyle(element); + if (style.display === 'none') { + return false; + } else if (style.visibility === 'hidden') { + return false; + } + return true; } } diff --git a/test/content/follow.html b/test/content/follow.html new file mode 100644 index 0000000..6bd8f87 --- /dev/null +++ b/test/content/follow.html @@ -0,0 +1,9 @@ + + + + link + invisible 1 + invisible 2 + not link + + diff --git a/test/content/follow.test.js b/test/content/follow.test.js index eb3d679..fd4f0bc 100644 --- a/test/content/follow.test.js +++ b/test/content/follow.test.js @@ -11,4 +11,15 @@ describe('Follow class', () => { expect(Follow.codeChars([])).to.be.equal(''); }); }); + + describe('#getTargetElements', () => { + beforeEach(() => { + document.body.innerHTML = __html__['test/content/follow.html']; + }); + + it('returns visible links', () => { + let links = Follow.getTargetElements(window.document); + expect(links).to.have.lengthOf(1); + }); + }); }); -- cgit v1.2.3 From a052ec92b7c7f27447211222231f43f07c2990c8 Mon Sep 17 00:00:00 2001 From: Shin'ya Ueoka Date: Mon, 21 Aug 2017 22:19:01 +0900 Subject: add Hint tests --- src/content/hint.js | 7 ++++-- test/content/hint.html | 1 + test/content/hint.test.js | 58 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 64 insertions(+), 2 deletions(-) create mode 100644 test/content/hint.html create mode 100644 test/content/hint.test.js (limited to 'test/content') 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 @@ +link 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 + }); +}); + + -- cgit v1.2.3