commit
ccc81312a1
14 changed files with 126 additions and 27 deletions
@ -0,0 +1,10 @@ |
||||
import actions from 'content/actions'; |
||||
|
||||
const set = (value) => { |
||||
return { |
||||
type: actions.SETTING_SET, |
||||
value, |
||||
}; |
||||
}; |
||||
|
||||
export { set }; |
@ -0,0 +1,13 @@ |
||||
import actions from 'content/actions'; |
||||
|
||||
const defaultState = {}; |
||||
|
||||
export default function reducer(state = defaultState, action = {}) { |
||||
switch (action.type) { |
||||
case actions.SETTING_SET: |
||||
return Object.assign({}, action.value); |
||||
default: |
||||
return state; |
||||
} |
||||
} |
||||
|
@ -0,0 +1,6 @@ |
||||
const fromWildcard = (pattern) => { |
||||
let regexStr = '^' + pattern.replace(/\*/g, '.*') + '$'; |
||||
return new RegExp(regexStr); |
||||
}; |
||||
|
||||
export { fromWildcard }; |
@ -0,0 +1,13 @@ |
||||
import { expect } from "chai"; |
||||
import actions from 'content/actions'; |
||||
import * as settingActions from 'content/actions/setting'; |
||||
|
||||
describe("setting actions", () => { |
||||
describe("set", () => { |
||||
it('create SETTING_SET action', () => { |
||||
let action = settingActions.set({ red: 'apple', yellow: 'banana' }); |
||||
expect(action.type).to.equal(actions.SETTING_SET); |
||||
expect(action.value).to.deep.equal({ red: 'apple', yellow: 'banana' }); |
||||
}); |
||||
}); |
||||
}); |
@ -0,0 +1,18 @@ |
||||
import { expect } from "chai"; |
||||
import actions from 'content/actions'; |
||||
import settingReducer from 'content/reducers/setting'; |
||||
|
||||
describe("content setting reducer", () => { |
||||
it('return the initial state', () => { |
||||
let state = settingReducer(undefined, {}); |
||||
expect(state).to.deep.equal({}); |
||||
}); |
||||
|
||||
it('return next state for SETTING_SET', () => { |
||||
let newSettings = { red: 'apple', yellow: 'banana' }; |
||||
let action = { type: actions.SETTING_SET, value: newSettings }; |
||||
let state = settingReducer(undefined, action); |
||||
expect(state).to.deep.equal(newSettings); |
||||
expect(state).not.to.equal(newSettings); // assert deep copy
|
||||
}); |
||||
}); |
@ -0,0 +1,20 @@ |
||||
import { expect } from 'chai'; |
||||
import * as re from 'shared/utils/re'; |
||||
|
||||
describe("re util", () => { |
||||
it('matches by pattern', () => { |
||||
let regex = re.fromWildcard('*.example.com/*'); |
||||
expect('foo.example.com/bar').to.match(regex); |
||||
expect('foo.example.com').not.to.match(regex); |
||||
expect('example.com/bar').not.to.match(regex); |
||||
|
||||
regex = re.fromWildcard('example.com/*') |
||||
expect('example.com/foo').to.match(regex); |
||||
expect('example.com/').to.match(regex); |
||||
|
||||
regex = re.fromWildcard('example.com/*bar') |
||||
expect('example.com/foobar').to.match(regex); |
||||
expect('example.com/bar').to.match(regex); |
||||
expect('example.com/foobarfoo').not.to.match(regex); |
||||
}) |
||||
}); |
Reference in new issue