commit
7639e99b75
16 changed files with 269 additions and 27 deletions
@ -0,0 +1,15 @@ |
||||
import actions from 'content/actions'; |
||||
|
||||
const enable = () => { |
||||
return { type: actions.ADDON_ENABLE }; |
||||
}; |
||||
|
||||
const disable = () => { |
||||
return { type: actions.ADDON_DISABLE }; |
||||
}; |
||||
|
||||
const toggleEnabled = () => { |
||||
return { type: actions.ADDON_TOGGLE_ENABLED }; |
||||
}; |
||||
|
||||
export { enable, disable, toggleEnabled }; |
@ -0,0 +1,24 @@ |
||||
import actions from 'content/actions'; |
||||
|
||||
const defaultState = { |
||||
enabled: true, |
||||
}; |
||||
|
||||
export default function reducer(state = defaultState, action = {}) { |
||||
switch (action.type) { |
||||
case actions.ADDON_ENABLE: |
||||
return Object.assign({}, state, { |
||||
enabled: true, |
||||
}); |
||||
case actions.ADDON_DISABLE: |
||||
return Object.assign({}, state, { |
||||
enabled: false, |
||||
}); |
||||
case actions.ADDON_TOGGLE_ENABLED: |
||||
return Object.assign({}, state, { |
||||
enabled: !state.enabled, |
||||
}); |
||||
default: |
||||
return state; |
||||
} |
||||
} |
@ -0,0 +1,26 @@ |
||||
import { expect } from "chai"; |
||||
import actions from 'content/actions'; |
||||
import * as addonActions from 'content/actions/addon'; |
||||
|
||||
describe("addon actions", () => { |
||||
describe("enable", () => { |
||||
it('create ADDON_ENABLE action', () => { |
||||
let action = addonActions.enable(); |
||||
expect(action.type).to.equal(actions.ADDON_ENABLE); |
||||
}); |
||||
}); |
||||
|
||||
describe("disable", () => { |
||||
it('create ADDON_DISABLE action', () => { |
||||
let action = addonActions.disable(); |
||||
expect(action.type).to.equal(actions.ADDON_DISABLE); |
||||
}); |
||||
}); |
||||
|
||||
describe("toggle", () => { |
||||
it('create ADDON_TOGGLE_ENABLED action', () => { |
||||
let action = addonActions.toggleEnabled(); |
||||
expect(action.type).to.equal(actions.ADDON_TOGGLE_ENABLED); |
||||
}); |
||||
}); |
||||
}); |
@ -0,0 +1,102 @@ |
||||
import InputComponent from 'content/components/common/input'; |
||||
import { expect } from "chai"; |
||||
|
||||
describe('InputComponent', () => { |
||||
it('register callbacks', () => { |
||||
let component = new InputComponent(window.document); |
||||
component.onKey((key) => { |
||||
expect(key).is.equals('a'); |
||||
}); |
||||
component.onKeyDown({ key: 'a' }); |
||||
}); |
||||
|
||||
it('invoke callback once', () => { |
||||
let component = new InputComponent(window.document); |
||||
let a = 0, b = 0; |
||||
component.onKey((key) => { |
||||
if (key == 'a') { |
||||
++a; |
||||
} else { |
||||
key == 'b' |
||||
++b; |
||||
} |
||||
}); |
||||
component.onKeyDown({ key: 'a' }); |
||||
component.onKeyDown({ key: 'b' }); |
||||
component.onKeyPress({ key: 'a' }); |
||||
component.onKeyUp({ key: 'a' }); |
||||
component.onKeyPress({ key: 'b' }); |
||||
component.onKeyUp({ key: 'b' }); |
||||
|
||||
expect(a).is.equals(1); |
||||
expect(b).is.equals(1); |
||||
}) |
||||
|
||||
it('add prefix when ctrl pressed', () => { |
||||
let component = new InputComponent(window.document); |
||||
component.onKey((key) => { |
||||
expect(key).is.equals('<C-A>'); |
||||
}); |
||||
component.onKeyDown({ key: 'a', ctrlKey: true }); |
||||
}) |
||||
|
||||
it('press X', () => { |
||||
let component = new InputComponent(window.document); |
||||
component.onKey((key) => { |
||||
expect(key).is.equals('X'); |
||||
}); |
||||
component.onKeyDown({ key: 'X', shiftKey: true }); |
||||
}) |
||||
|
||||
it('press <Shift> + <Esc>', () => { |
||||
let component = new InputComponent(window.document); |
||||
component.onKey((key) => { |
||||
expect(key).is.equals('<S-Esc>'); |
||||
}); |
||||
component.onKeyDown({ key: 'Escape', shiftKey: true }); |
||||
}) |
||||
|
||||
it('press <Ctrl> + <Esc>', () => { |
||||
let component = new InputComponent(window.document); |
||||
component.onKey((key) => { |
||||
expect(key).is.equals('<C-Esc>'); |
||||
}); |
||||
component.onKeyDown({ key: 'Escape', ctrlKey: true }); |
||||
}) |
||||
|
||||
it('does not invoke only meta keys', () => { |
||||
let component = new InputComponent(window.document); |
||||
component.onKey((key) => { |
||||
expect.fail(); |
||||
}); |
||||
component.onKeyDown({ key: 'Shift' }); |
||||
component.onKeyDown({ key: 'Control' }); |
||||
component.onKeyDown({ key: 'Alt' }); |
||||
component.onKeyDown({ key: 'OS' }); |
||||
}) |
||||
|
||||
it('ignores events from input elements', () => { |
||||
['input', 'textarea', 'select'].forEach((name) => { |
||||
let target = window.document.createElement(name); |
||||
let component = new InputComponent(target); |
||||
component.onKey((key) => { |
||||
expect.fail(); |
||||
}); |
||||
component.onKeyDown({ key: 'x', target }); |
||||
}); |
||||
}); |
||||
|
||||
it('ignores events from contenteditable elements', () => { |
||||
let target = window.document.createElement('div'); |
||||
let component = new InputComponent(target); |
||||
component.onKey((key) => { |
||||
expect.fail(); |
||||
}); |
||||
|
||||
target.setAttribute('contenteditable', ''); |
||||
component.onKeyDown({ key: 'x', target }); |
||||
|
||||
target.setAttribute('contenteditable', 'true'); |
||||
component.onKeyDown({ key: 'x', target }); |
||||
}) |
||||
}); |
@ -0,0 +1,38 @@ |
||||
import { expect } from "chai"; |
||||
import actions from 'content/actions'; |
||||
import addonReducer from 'content/reducers/addon'; |
||||
|
||||
describe("addon reducer", () => { |
||||
it('return the initial state', () => { |
||||
let state = addonReducer(undefined, {}); |
||||
expect(state).to.have.property('enabled', true); |
||||
}); |
||||
|
||||
it('return next state for ADDON_ENABLE', () => { |
||||
let action = { type: actions.ADDON_ENABLE}; |
||||
let prev = { enabled: false }; |
||||
let state = addonReducer(prev, action); |
||||
|
||||
expect(state.enabled).is.equal(true); |
||||
}); |
||||
|
||||
it('return next state for ADDON_DISABLE', () => { |
||||
let action = { type: actions.ADDON_DISABLE}; |
||||
let prev = { enabled: true }; |
||||
let state = addonReducer(prev, action); |
||||
|
||||
expect(state.enabled).is.equal(false); |
||||
}); |
||||
|
||||
it('return next state for ADDON_TOGGLE_ENABLED', () => { |
||||
let action = { type: actions.ADDON_TOGGLE_ENABLED }; |
||||
let state = { enabled: false }; |
||||
|
||||
state = addonReducer(state, action); |
||||
expect(state.enabled).is.equal(true); |
||||
|
||||
state = addonReducer(state, action); |
||||
expect(state.enabled).is.equal(false); |
||||
}); |
||||
|
||||
}); |
Reference in new issue