From 24e72aa6e079009d4c24c7f6be6bfb82c21e6643 Mon Sep 17 00:00:00 2001 From: Shin'ya Ueoka Date: Wed, 7 Mar 2018 20:54:28 +0900 Subject: add global find keyword --- test/background/actions/find.test.js | 13 +++++++++++++ test/background/reducers/find.test.js | 19 +++++++++++++++++++ 2 files changed, 32 insertions(+) create mode 100644 test/background/actions/find.test.js create mode 100644 test/background/reducers/find.test.js (limited to 'test') diff --git a/test/background/actions/find.test.js b/test/background/actions/find.test.js new file mode 100644 index 0000000..467604f --- /dev/null +++ b/test/background/actions/find.test.js @@ -0,0 +1,13 @@ +import { expect } from "chai"; +import actions from 'background/actions'; +import * as findActions from 'background/actions/find'; + +describe("find actions", () => { + describe("setKeyword", () => { + it('create FIND_SET_KEYWORD action', () => { + let action = findActions.setKeyword('banana'); + expect(action.type).to.equal(actions.FIND_SET_KEYWORD); + expect(action.keyword).to.equal('banana'); + }); + }); +}); diff --git a/test/background/reducers/find.test.js b/test/background/reducers/find.test.js new file mode 100644 index 0000000..707d73a --- /dev/null +++ b/test/background/reducers/find.test.js @@ -0,0 +1,19 @@ +import { expect } from "chai"; +import actions from 'background/actions'; +import findReducer from 'background/reducers/find'; + +describe("find reducer", () => { + it('return the initial state', () => { + let state = findReducer(undefined, {}); + expect(state).to.have.deep.property('keyword', null); + }); + + it('return next state for FIND_SET_KEYWORD', () => { + let action = { + type: actions.FIND_SET_KEYWORD, + keyword: 'cherry', + }; + let state = findReducer(undefined, action); + expect(state).to.have.deep.property('keyword', 'cherry') + }); +}); -- cgit v1.2.3 From 92f8365be7127c3fa0276b1a6e890571f634622e Mon Sep 17 00:00:00 2001 From: Shin'ya Ueoka Date: Wed, 7 Mar 2018 21:05:52 +0900 Subject: set find keyword from background --- src/content/actions/find.js | 51 ++++++++++++++++++++++++-------------- src/content/reducers/find.js | 2 +- test/content/reducers/find.test.js | 2 +- 3 files changed, 35 insertions(+), 20 deletions(-) (limited to 'test') diff --git a/src/content/actions/find.js b/src/content/actions/find.js index b266216..c7345cc 100644 --- a/src/content/actions/find.js +++ b/src/content/actions/find.js @@ -5,6 +5,7 @@ // NOTE: window.find is not standard API // https://developer.mozilla.org/en-US/docs/Web/API/Window/find +import messages from 'shared/messages'; import actions from 'content/actions'; import * as consoleFrames from '../console-frames'; @@ -31,35 +32,49 @@ const find = (string, backwards) => { return window.find(string, caseSensitive, backwards, wrapScan); }; -const findNext = (keyword, reset, backwards) => { +const findNext = (currentKeyword, reset, backwards) => { if (reset) { window.getSelection().removeAllRanges(); } - let found = find(keyword, backwards); - if (!found) { - window.getSelection().removeAllRanges(); - found = find(keyword, backwards); - } - if (found) { - postPatternFound(keyword); + let promise = Promise.resolve(currentKeyword); + if (currentKeyword) { + browser.runtime.sendMessage({ + type: messages.FIND_SET_KEYWORD, + keyword: currentKeyword, + }); } else { - postPatternNotFound(keyword); + promise = browser.runtime.sendMessage({ + type: messages.FIND_GET_KEYWORD, + }); } - return { - type: actions.FIND_SET_KEYWORD, - keyword, - found, - }; + return promise.then((keyword) => { + let found = find(keyword, backwards); + if (!found) { + window.getSelection().removeAllRanges(); + found = find(keyword, backwards); + } + if (found) { + postPatternFound(keyword); + } else { + postPatternNotFound(keyword); + } + + return { + type: actions.FIND_SET_KEYWORD, + keyword, + found, + }; + }); }; -const next = (keyword, reset) => { - return findNext(keyword, reset, false); +const next = (currentKeyword, reset) => { + return findNext(currentKeyword, reset, false); }; -const prev = (keyword, reset) => { - return findNext(keyword, reset, true); +const prev = (currentKeyword, reset) => { + return findNext(currentKeyword, reset, true); }; export { next, prev }; diff --git a/src/content/reducers/find.js b/src/content/reducers/find.js index eb43c37..8d63ee5 100644 --- a/src/content/reducers/find.js +++ b/src/content/reducers/find.js @@ -1,7 +1,7 @@ import actions from 'content/actions'; const defaultState = { - keyword: '', + keyword: null, found: false, }; diff --git a/test/content/reducers/find.test.js b/test/content/reducers/find.test.js index 93625da..908b01b 100644 --- a/test/content/reducers/find.test.js +++ b/test/content/reducers/find.test.js @@ -5,7 +5,7 @@ import findReducer from 'content/reducers/find'; describe("find reducer", () => { it('return the initial state', () => { let state = findReducer(undefined, {}); - expect(state).to.have.property('keyword', ''); + expect(state).to.have.property('keyword', null); expect(state).to.have.property('found', false); }); -- cgit v1.2.3