From 410ffbb0376b9399928ef8d4dd13079bfb120e14 Mon Sep 17 00:00:00 2001 From: Shin'ya UEOKA Date: Fri, 4 Oct 2019 04:01:35 +0000 Subject: Make Keymap class --- test/shared/SettingData.test.ts | 48 +++++++++++++++++--------- test/shared/Settings.test.ts | 45 +++++------------------- test/shared/settings/Keymaps.test.ts | 66 ++++++++++++++++++++++++++++++++++++ 3 files changed, 106 insertions(+), 53 deletions(-) create mode 100644 test/shared/settings/Keymaps.test.ts (limited to 'test/shared') diff --git a/test/shared/SettingData.test.ts b/test/shared/SettingData.test.ts index 8736ecb..9567f76 100644 --- a/test/shared/SettingData.test.ts +++ b/test/shared/SettingData.test.ts @@ -1,8 +1,9 @@ import SettingData, { - FormKeymaps, JSONSettings, FormSettings, + FormKeymaps, JSONTextSettings, FormSettings, } from '../../src/shared/SettingData'; -import Settings, { Keymaps } from '../../src/shared/Settings'; +import Settings from '../../src/shared/Settings'; import { expect } from 'chai'; +import Keymaps from '../../src/shared/settings/Keymaps'; describe('shared/SettingData', () => { describe('FormKeymaps', () => { @@ -11,9 +12,9 @@ describe('shared/SettingData', () => { let data = { 'scroll.vertically?{"count":1}': 'j', 'scroll.home': '0', - } + }; - let keymaps = FormKeymaps.valueOf(data).toKeymaps(); + let keymaps = FormKeymaps.valueOf(data).toKeymaps().toJSON(); expect(keymaps).to.deep.equal({ 'j': { type: 'scroll.vertically', count: 1 }, '0': { type: 'scroll.home' }, @@ -23,13 +24,13 @@ describe('shared/SettingData', () => { describe('#fromKeymaps to #toJSON', () => { it('create from a Keymaps and create a JSON object', () => { - let data: Keymaps = { + let keymaps: Keymaps = Keymaps.fromJSON({ 'j': { type: 'scroll.vertically', count: 1 }, '0': { type: 'scroll.home' }, - } + }); - let keymaps = FormKeymaps.fromKeymaps(data).toJSON(); - expect(keymaps).to.deep.equal({ + let form = FormKeymaps.fromKeymaps(keymaps).toJSON(); + expect(form).to.deep.equal({ 'scroll.vertically?{"count":1}': 'j', 'scroll.home': '0', }); @@ -56,15 +57,20 @@ describe('shared/SettingData', () => { "blacklist": [] }`; - let settings = JSONSettings.valueOf(o).toSettings(); - expect(settings).to.deep.equal(JSON.parse(o)); + let settings = JSONTextSettings.fromText(o).toSettings(); + expect({ + keymaps: settings.keymaps.toJSON(), + search: settings.search, + properties: settings.properties, + blacklist: settings.blacklist, + }).to.deep.equal(JSON.parse(o)); }); }); describe('#fromSettings to #toJSON', () => { it('create from a Settings and create a JSON string', () => { let o = { - keymaps: {}, + keymaps: Keymaps.fromJSON({}), search: { default: "google", engines: { @@ -79,8 +85,13 @@ describe('shared/SettingData', () => { blacklist: [], }; - let json = JSONSettings.fromSettings(o).toJSON(); - expect(JSON.parse(json)).to.deep.equal(o); + let json = JSONTextSettings.fromSettings(o).toJSONText(); + expect(JSON.parse(json)).to.deep.equal({ + keymaps: o.keymaps.toJSON(), + search: o.search, + properties: o.properties, + blacklist: o.blacklist, + }); }); }); }); @@ -108,7 +119,12 @@ describe('shared/SettingData', () => { }; let settings = FormSettings.valueOf(data).toSettings(); - expect(settings).to.deep.equal({ + expect({ + keymaps: settings.keymaps.toJSON(), + search: settings.search, + properties: settings.properties, + blacklist: settings.blacklist, + }).to.deep.equal({ keymaps: { 'j': { type: 'scroll.vertically', count: 1 }, '0': { type: 'scroll.home' }, @@ -132,10 +148,10 @@ describe('shared/SettingData', () => { describe('#fromSettings to #toJSON', () => { it('create from a Settings and create a JSON string', () => { let data: Settings = { - keymaps: { + keymaps: Keymaps.fromJSON({ 'j': { type: 'scroll.vertically', count: 1 }, '0': { type: 'scroll.home' }, - }, + }), search: { default: "google", engines: { diff --git a/test/shared/Settings.test.ts b/test/shared/Settings.test.ts index 04b28c4..9faf9d1 100644 --- a/test/shared/Settings.test.ts +++ b/test/shared/Settings.test.ts @@ -1,31 +1,7 @@ import * as settings from '../../src/shared/Settings'; -import { expect } from 'chai'; +import {expect} from 'chai'; describe('Settings', () => { - describe('#keymapsValueOf', () => { - it('returns empty object by empty settings', () => { - let keymaps = settings.keymapsValueOf({}); - expect(keymaps).to.be.empty; - }); - - it('returns keymaps by valid settings', () => { - let keymaps = settings.keymapsValueOf({ - k: { type: "scroll.vertically", count: -1 }, - j: { type: "scroll.vertically", count: 1 }, - }); - - expect(keymaps['k']).to.deep.equal({ type: "scroll.vertically", count: -1 }); - expect(keymaps['j']).to.deep.equal({ type: "scroll.vertically", count: 1 }); - }); - - it('throws a TypeError by invalid settings', () => { - expect(() => settings.keymapsValueOf(null)).to.throw(TypeError); - expect(() => settings.keymapsValueOf({ - k: { type: "invalid.operation" }, - })).to.throw(TypeError); - }); - }); - describe('#searchValueOf', () => { it('returns search settings by valid settings', () => { let search = settings.searchValueOf({ @@ -110,16 +86,6 @@ describe('Settings', () => { complete: "sbh" }); }); - - it('throws a TypeError by invalid settings', () => { - expect(() => settings.keymapsValueOf(null)).to.throw(TypeError); - expect(() => settings.keymapsValueOf({ - smoothscroll: 'false', - })).to.throw(TypeError); - expect(() => settings.keymapsValueOf({ - unknown: 'xyz' - })).to.throw(TypeError); - }); }); describe('#blacklistValueOf', () => { @@ -161,7 +127,12 @@ describe('Settings', () => { "blacklist": [] }); - expect(x).to.deep.equal({ + expect({ + keymaps: x.keymaps.toJSON(), + search: x.search, + properties: x.properties, + blacklist: x.blacklist, + }).to.deep.equal({ keymaps: {}, search: { default: "google", @@ -180,7 +151,7 @@ describe('Settings', () => { it('sets default settings', () => { let value = settings.valueOf({}); - expect(value.keymaps).to.not.be.empty; + expect(value.keymaps.toJSON()).to.not.be.empty; expect(value.properties).to.not.be.empty; expect(value.search.default).to.be.a('string'); expect(value.search.engines).to.be.an('object'); diff --git a/test/shared/settings/Keymaps.test.ts b/test/shared/settings/Keymaps.test.ts new file mode 100644 index 0000000..7896a63 --- /dev/null +++ b/test/shared/settings/Keymaps.test.ts @@ -0,0 +1,66 @@ +import Keymaps from '../../../src/shared/settings/Keymaps'; +import { expect } from 'chai'; + +describe('Keymaps', () => { + describe('#valueOf', () => { + it('returns empty object by empty settings', () => { + let keymaps = Keymaps.fromJSON({}).toJSON(); + expect(keymaps).to.be.empty; + }); + + it('returns keymaps by valid settings', () => { + let keymaps = Keymaps.fromJSON({ + k: { type: "scroll.vertically", count: -1 }, + j: { type: "scroll.vertically", count: 1 }, + }).toJSON(); + + expect(keymaps['k']).to.deep.equal({ type: "scroll.vertically", count: -1 }); + expect(keymaps['j']).to.deep.equal({ type: "scroll.vertically", count: 1 }); + }); + + it('throws a TypeError by invalid settings', () => { + expect(() => Keymaps.fromJSON(null)).to.throw(TypeError); + expect(() => Keymaps.fromJSON({ + k: { type: "invalid.operation" }, + })).to.throw(TypeError); + }); + }); + + describe('#combine', () => { + it('returns combined keymaps', () => { + let keymaps = Keymaps.fromJSON({ + k: { type: "scroll.vertically", count: -1 }, + j: { type: "scroll.vertically", count: 1 }, + }).combine(Keymaps.fromJSON({ + n: { type: "find.next" }, + N: { type: "find.prev" }, + })); + + let entries = keymaps.entries().sort(([name1], [name2]) => name1.localeCompare(name2)); + expect(entries).deep.equals([ + ['j', { type: "scroll.vertically", count: 1 }], + ['k', { type: "scroll.vertically", count: -1 }], + ['n', { type: "find.next" }], + ['N', { type: "find.prev" }], + ]); + }); + + it('overrides current keymaps', () => { + let keymaps = Keymaps.fromJSON({ + k: { type: "scroll.vertically", count: -1 }, + j: { type: "scroll.vertically", count: 1 }, + }).combine(Keymaps.fromJSON({ + n: { type: "find.next" }, + j: { type: "find.prev" }, + })); + + let entries = keymaps.entries().sort(([name1], [name2]) => name1.localeCompare(name2)); + expect(entries).deep.equals([ + ['j', { type: "find.prev" }], + ['k', { type: "scroll.vertically", count: -1 }], + ['n', { type: "find.next" }], + ]); + }); + }); +}); + -- cgit v1.2.3