diff options
Diffstat (limited to 'test')
-rw-r--r-- | test/content/usecases/KeymapUseCase.test.ts | 133 | ||||
-rw-r--r-- | test/settings/components/form/BlacklistForm.test.tsx | 25 | ||||
-rw-r--r-- | test/shared/settings/Blacklist.test.ts | 183 |
3 files changed, 279 insertions, 62 deletions
diff --git a/test/content/usecases/KeymapUseCase.test.ts b/test/content/usecases/KeymapUseCase.test.ts new file mode 100644 index 0000000..5f2feba --- /dev/null +++ b/test/content/usecases/KeymapUseCase.test.ts @@ -0,0 +1,133 @@ +import KeymapUseCase from '../../../src/content/usecases/KeymapUseCase'; +import {expect} from 'chai'; +import SettingRepository from "../../../src/content/repositories/SettingRepository"; +import Settings from "../../../src/shared/settings/Settings"; +import AddonEnabledRepository from "../../../src/content/repositories/AddonEnabledRepository"; +import {KeymapRepositoryImpl} from "../../../src/content/repositories/KeymapRepository"; +import Key from "../../../src/shared/settings/Key"; +import AddressRepository from "../../../src/content/repositories/AddressRepository"; + +class MockSettingRepository implements SettingRepository { + constructor( + private readonly settings: Settings, + ) { + } + + get(): Settings { + return this.settings; + } + + set(_setting: Settings): void { + throw new Error('TODO'); + } +} + +class MockAddonEnabledRepository implements AddonEnabledRepository { + constructor( + private readonly enabled: boolean, + ) { + } + + get(): boolean { + return this.enabled; + } + + set(_on: boolean): void { + throw new Error('TODO'); + } +} + +class MockAddressRepository implements AddressRepository { + constructor( + private url: URL, + ) { + } + + getCurrentURL(): URL { + return this.url; + } +} + + +describe('KeymapUseCase', () => { + it('returns matched operation', () => { + let settings = Settings.fromJSON({ + keymaps: { + k: {type: 'scroll.vertically', count: -1}, + j: {type: 'scroll.vertically', count: 1}, + gg: {type: 'scroll.top'}, + }, + }); + let sut = new KeymapUseCase( + new KeymapRepositoryImpl(), + new MockSettingRepository(settings), + new MockAddonEnabledRepository(true), + new MockAddressRepository(new URL('https://example.com')), + ); + + expect(sut.nextOp(Key.fromMapKey('k'))).to.deep.equal({type: 'scroll.vertically', count: -1}); + expect(sut.nextOp(Key.fromMapKey('j'))).to.deep.equal({type: 'scroll.vertically', count: 1}); + expect(sut.nextOp(Key.fromMapKey('g'))).to.be.null; + expect(sut.nextOp(Key.fromMapKey('g'))).to.deep.equal({type: 'scroll.top'}); + expect(sut.nextOp(Key.fromMapKey('z'))).to.be.null; + }); + + it('returns only ADDON_ENABLE and ADDON_TOGGLE_ENABLED operation', () => { + let settings = Settings.fromJSON({ + keymaps: { + k: {type: 'scroll.vertically', count: -1}, + a: {type: 'addon.enable'}, + b: {type: 'addon.toggle.enabled'}, + }, + }); + let sut = new KeymapUseCase( + new KeymapRepositoryImpl(), + new MockSettingRepository(settings), + new MockAddonEnabledRepository(false), + new MockAddressRepository(new URL('https://example.com')), + ); + + expect(sut.nextOp(Key.fromMapKey('k'))).to.be.null; + expect(sut.nextOp(Key.fromMapKey('a'))).to.deep.equal({type: 'addon.enable'}); + expect(sut.nextOp(Key.fromMapKey('b'))).to.deep.equal({type: 'addon.toggle.enabled'}); + }); + + it('blocks keys in the partial blacklist', () => { + let settings = Settings.fromJSON({ + keymaps: { + k: {type: 'scroll.vertically', count: -1}, + j: {type: 'scroll.vertically', count: 1}, + gg: {"type": "scroll.top"}, + G: {"type": "scroll.bottom"}, + }, + blacklist: [ + { url: "example.com", keys: ['g'] }, + { url: "example.org", keys: ['<S-G>'] } + ], + }); + + let sut = new KeymapUseCase( + new KeymapRepositoryImpl(), + new MockSettingRepository(settings), + new MockAddonEnabledRepository(true), + new MockAddressRepository(new URL('https://example.com')), + ); + + expect(sut.nextOp(Key.fromMapKey('k'))).to.deep.equal({type: 'scroll.vertically', count: -1}); + expect(sut.nextOp(Key.fromMapKey('j'))).to.deep.equal({type: 'scroll.vertically', count: 1}); + expect(sut.nextOp(Key.fromMapKey('g'))).to.be.null; + expect(sut.nextOp(Key.fromMapKey('g'))).to.be.null; + expect(sut.nextOp(Key.fromMapKey('G'))).to.deep.equal({type: 'scroll.bottom'}); + + sut = new KeymapUseCase( + new KeymapRepositoryImpl(), + new MockSettingRepository(settings), + new MockAddonEnabledRepository(true), + new MockAddressRepository(new URL('https://example.org')), + ); + + expect(sut.nextOp(Key.fromMapKey('g'))).to.be.null; + expect(sut.nextOp(Key.fromMapKey('g'))).to.deep.equal({type: 'scroll.top'}); + expect(sut.nextOp(Key.fromMapKey('G'))).to.be.null; + }); +}); diff --git a/test/settings/components/form/BlacklistForm.test.tsx b/test/settings/components/form/BlacklistForm.test.tsx index 2be5d96..7daf513 100644 --- a/test/settings/components/form/BlacklistForm.test.tsx +++ b/test/settings/components/form/BlacklistForm.test.tsx @@ -2,13 +2,16 @@ import React from 'react'; import ReactDOM from 'react-dom'; import ReactTestRenderer from 'react-test-renderer'; import ReactTestUtils from 'react-dom/test-utils'; -import BlacklistForm from 'settings/components/form/BlacklistForm' +import { expect } from 'chai' + +import BlacklistForm from '../../../../src/settings/components/form/BlacklistForm' +import Blacklist from '../../../../src/shared/settings/Blacklist'; describe("settings/form/BlacklistForm", () => { describe('render', () => { it('renders BlacklistForm', () => { let root = ReactTestRenderer.create( - <BlacklistForm value={['*.slack.com', 'www.google.com/maps']} />, + <BlacklistForm value={Blacklist.fromJSON(['*.slack.com', 'www.google.com/maps'])} />, ).root; let children = root.children[0].children; @@ -43,10 +46,10 @@ describe("settings/form/BlacklistForm", () => { it('invokes onChange event on edit', (done) => { ReactTestUtils.act(() => { ReactDOM.render(<BlacklistForm - value={['*.slack.com', 'www.google.com/maps*']} + value={Blacklist.fromJSON(['*.slack.com', 'www.google.com/maps*'])} onChange={value => { - expect(value).to.have.lengthOf(2); - expect(value).to.have.members(['gitter.im', 'www.google.com/maps*']); + let urls = value.items.map(item => item.pattern); + expect(urls).to.have.members(['gitter.im', 'www.google.com/maps*']); done(); }} />, container) @@ -60,10 +63,10 @@ describe("settings/form/BlacklistForm", () => { it('invokes onChange event on delete', (done) => { ReactTestUtils.act(() => { ReactDOM.render(<BlacklistForm - value={['*.slack.com', 'www.google.com/maps*']} + value={Blacklist.fromJSON(['*.slack.com', 'www.google.com/maps*'])} onChange={value => { - expect(value).to.have.lengthOf(1); - expect(value).to.have.members(['www.google.com/maps*']); + let urls = value.items.map(item => item.pattern); + expect(urls).to.have.members(['www.google.com/maps*']); done(); }} />, container) @@ -76,10 +79,10 @@ describe("settings/form/BlacklistForm", () => { it('invokes onChange event on add', (done) => { ReactTestUtils.act(() => { ReactDOM.render(<BlacklistForm - value={['*.slack.com']} + value={Blacklist.fromJSON(['*.slack.com'])} onChange={value => { - expect(value).to.have.lengthOf(2); - expect(value).to.have.members(['*.slack.com', '']); + let urls = value.items.map(item => item.pattern); + expect(urls).to.have.members(['*.slack.com', '']); done(); }} />, container); diff --git a/test/shared/settings/Blacklist.test.ts b/test/shared/settings/Blacklist.test.ts index fbacf5d..133112c 100644 --- a/test/shared/settings/Blacklist.test.ts +++ b/test/shared/settings/Blacklist.test.ts @@ -1,77 +1,158 @@ -import Blacklist from '../../../src/shared/settings/Blacklist'; +import Blacklist, { BlacklistItem } from '../../../src/shared/settings/Blacklist'; import { expect } from 'chai'; - -describe('Blacklist', () => { - describe('fromJSON', () => { - it('returns empty array by empty settings', () => { - let blacklist = Blacklist.fromJSON([]); - expect(blacklist.toJSON()).to.be.empty; +import Key from '../../../src/shared/settings/Key'; + +describe('BlacklistItem', () => { + describe('#fromJSON', () => { + it('parses string pattern', () => { + let item = BlacklistItem.fromJSON('example.com'); + expect(item.pattern).to.equal('example.com'); + expect(item.partial).to.be.false; }); - it('returns blacklist by valid settings', () => { - let blacklist = Blacklist.fromJSON([ - 'github.com', - 'circleci.com', - ]); - - expect(blacklist.toJSON()).to.deep.equal([ - 'github.com', - 'circleci.com', - ]); + it('parses partial blacklist item', () => { + let item = BlacklistItem.fromJSON({ url: 'example.com', keys: ['j', 'k']}); + expect(item.pattern).to.equal('example.com'); + expect(item.partial).to.be.true; + expect(item.keys).to.deep.equal(['j', 'k']); }); - it('throws a TypeError by invalid settings', () => { - expect(() => Blacklist.fromJSON(null)).to.throw(TypeError); - expect(() => Blacklist.fromJSON({})).to.throw(TypeError); - expect(() => Blacklist.fromJSON([1,2,3])).to.throw(TypeError); + it('throws a TypeError', () => { + expect(() => BlacklistItem.fromJSON(null)).to.throw(TypeError); + expect(() => BlacklistItem.fromJSON(100)).to.throw(TypeError); + expect(() => BlacklistItem.fromJSON({})).to.throw(TypeError); + expect(() => BlacklistItem.fromJSON({url: 'google.com'})).to.throw(TypeError); + expect(() => BlacklistItem.fromJSON({keys: ['a']})).to.throw(TypeError); + expect(() => BlacklistItem.fromJSON({url: 'google.com', keys: 10})).to.throw(TypeError); + expect(() => BlacklistItem.fromJSON({url: 'google.com', keys: ['a', 'b', 3]})).to.throw(TypeError); }); }); - describe('#includes', () => { - it('matches by *', () => { - let blacklist = new Blacklist(['*']); - - expect(blacklist.includes('https://github.com/abc')).to.be.true; + describe('#matches', () => { + it('matches by "*"', () => { + let item = BlacklistItem.fromJSON('*'); + expect(item.matches(new URL('https://github.com/abc'))).to.be.true; }); it('matches by hostname', () => { - let blacklist = new Blacklist(['github.com']); - - expect(blacklist.includes('https://github.com')).to.be.true; - expect(blacklist.includes('https://gist.github.com')).to.be.false; - expect(blacklist.includes('https://github.com/ueokande')).to.be.true; - expect(blacklist.includes('https://github.org')).to.be.false; - expect(blacklist.includes('https://google.com/search?q=github.org')).to.be.false; + let item = BlacklistItem.fromJSON('github.com'); + expect(item.matches(new URL('https://github.com'))).to.be.true; + expect(item.matches(new URL('https://gist.github.com'))).to.be.false; + expect(item.matches(new URL('https://github.com/ueokande'))).to.be.true; + expect(item.matches(new URL('https://github.org'))).to.be.false; + expect(item.matches(new URL('https://google.com/search?q=github.org'))).to.be.false; }); it('matches by hostname with wildcard', () => { - let blacklist = new Blacklist(['*.github.com']); + let item = BlacklistItem.fromJSON('*.github.com'); - expect(blacklist.includes('https://github.com')).to.be.false; - expect(blacklist.includes('https://gist.github.com')).to.be.true; - }) + expect(item.matches(new URL('https://github.com'))).to.be.false; + expect(item.matches(new URL('https://gist.github.com'))).to.be.true; + }); it('matches by path', () => { - let blacklist = new Blacklist(['github.com/abc']); + let item = BlacklistItem.fromJSON('github.com/abc'); - expect(blacklist.includes('https://github.com/abc')).to.be.true; - expect(blacklist.includes('https://github.com/abcdef')).to.be.false; - expect(blacklist.includes('https://gist.github.com/abc')).to.be.false; - }) + expect(item.matches(new URL('https://github.com/abc'))).to.be.true; + expect(item.matches(new URL('https://github.com/abcdef'))).to.be.false; + expect(item.matches(new URL('https://gist.github.com/abc'))).to.be.false; + }); it('matches by path with wildcard', () => { - let blacklist = new Blacklist(['github.com/abc*']); + let item = BlacklistItem.fromJSON('github.com/abc*'); - expect(blacklist.includes('https://github.com/abc')).to.be.true; - expect(blacklist.includes('https://github.com/abcdef')).to.be.true; - expect(blacklist.includes('https://gist.github.com/abc')).to.be.false; - }) + expect(item.matches(new URL('https://github.com/abc'))).to.be.true; + expect(item.matches(new URL('https://github.com/abcdef'))).to.be.true; + expect(item.matches(new URL('https://gist.github.com/abc'))).to.be.false; + }); it('matches address and port', () => { - let blacklist = new Blacklist(['127.0.0.1:8888']); + let item = BlacklistItem.fromJSON('127.0.0.1:8888'); + + expect(item.matches(new URL('http://127.0.0.1:8888/'))).to.be.true; + expect(item.matches(new URL('http://127.0.0.1:8888/hello'))).to.be.true; + }); + + it('matches with partial blacklist', () => { + let item = BlacklistItem.fromJSON({ url: 'google.com', keys: ['j', 'k'] }); + + expect(item.matches(new URL('https://google.com'))).to.be.true; + expect(item.matches(new URL('https://yahoo.com'))).to.be.false; + }) + }); + + describe('#includesPartialKeys', () => { + it('matches with partial keys', () => { + let item = BlacklistItem.fromJSON({url: 'google.com', keys: ['j', 'k', '<C-U>']}); + + expect(item.includeKey(new URL('http://google.com/maps'), Key.fromMapKey('j'))).to.be.true; + expect(item.includeKey(new URL('http://google.com/maps'), Key.fromMapKey('<C-U>'))).to.be.true; + expect(item.includeKey(new URL('http://google.com/maps'), Key.fromMapKey('z'))).to.be.false; + expect(item.includeKey(new URL('http://google.com/maps'), Key.fromMapKey('u'))).to.be.false; + expect(item.includeKey(new URL('http://maps.google.com/'), Key.fromMapKey('j'))).to.be.false; + }) + }); +}); + +describe('Blacklist', () => { + describe('#fromJSON', () => { + it('parses string list', () => { + let blacklist = Blacklist.fromJSON(['example.com', 'example.org']); + expect(blacklist.toJSON()).to.deep.equals([ + 'example.com', 'example.org', + ]); + }); + + it('parses mixed blacklist', () => { + let blacklist = Blacklist.fromJSON([ + { url: 'example.com', keys: ['j', 'k']}, + 'example.org', + ]); + expect(blacklist.toJSON()).to.deep.equals([ + { url: 'example.com', keys: ['j', 'k']}, + 'example.org', + ]); + }); - expect(blacklist.includes('http://127.0.0.1:8888/')).to.be.true; - expect(blacklist.includes('http://127.0.0.1:8888/hello')).to.be.true; + it('parses empty blacklist', () => { + let blacklist = Blacklist.fromJSON([]); + expect(blacklist.toJSON()).to.deep.equals([]); + }); + + it('throws a TypeError', () => { + expect(() => Blacklist.fromJSON(null)).to.throw(TypeError); + expect(() => Blacklist.fromJSON(100)).to.throw(TypeError); + expect(() => Blacklist.fromJSON({})).to.throw(TypeError); + expect(() => Blacklist.fromJSON([100])).to.throw(TypeError); + expect(() => Blacklist.fromJSON([{}])).to.throw(TypeError); }) - }) + }); + + describe('#includesEntireBlacklist', () => { + it('matches a url with entire blacklist', () => { + let blacklist = Blacklist.fromJSON(['google.com', '*.github.com']); + expect(blacklist.includesEntireBlacklist(new URL('https://google.com'))).to.be.true; + expect(blacklist.includesEntireBlacklist(new URL('https://github.com'))).to.be.false; + expect(blacklist.includesEntireBlacklist(new URL('https://gist.github.com'))).to.be.true; + }); + + it('does not matches with partial blacklist', () => { + let blacklist = Blacklist.fromJSON(['google.com', { url: 'yahoo.com', keys: ['j', 'k'] }]); + expect(blacklist.includesEntireBlacklist(new URL('https://google.com'))).to.be.true; + expect(blacklist.includesEntireBlacklist(new URL('https://yahoo.com'))).to.be.false; + }); + }); + + describe('#includesKeys', () => { + it('matches with entire blacklist or keys in the partial blacklist', () => { + let blacklist = Blacklist.fromJSON([ + 'google.com', + { url: 'github.com', keys: ['j', 'k'] }, + ]); + + expect(blacklist.includeKey(new URL('https://google.com'), Key.fromMapKey('j'))).to.be.true; + expect(blacklist.includeKey(new URL('https://github.com'), Key.fromMapKey('j'))).to.be.true; + expect(blacklist.includeKey(new URL('https://github.com'), Key.fromMapKey('a'))).to.be.false; + }); + }); }); |