Extract createStore into a separate module
This commit is contained in:
parent
ae670c8ea3
commit
3257a6ffb4
@ -5,3 +5,4 @@ export * from './login'
|
|||||||
export * from './middleware'
|
export * from './middleware'
|
||||||
export * from './redux'
|
export * from './redux'
|
||||||
export * from './renderer'
|
export * from './renderer'
|
||||||
|
export * from './store'
|
||||||
|
|||||||
@ -1,4 +1,3 @@
|
|||||||
export * from './ClientRenderer'
|
export * from './ClientRenderer'
|
||||||
export * from './IRenderer'
|
export * from './IRenderer'
|
||||||
export * from './IStoreFactory'
|
export * from './IStoreFactory'
|
||||||
export * from './ServerRenderer'
|
|
||||||
|
|||||||
32
packages/client/src/store/createStore.ts
Normal file
32
packages/client/src/store/createStore.ts
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
import {PromiseMiddleware} from '../middleware'
|
||||||
|
import {
|
||||||
|
applyMiddleware,
|
||||||
|
createStore as create,
|
||||||
|
combineReducers,
|
||||||
|
Middleware,
|
||||||
|
Action,
|
||||||
|
// AnyAction,
|
||||||
|
DeepPartial,
|
||||||
|
ReducersMapObject,
|
||||||
|
// Reducer,
|
||||||
|
} from 'redux'
|
||||||
|
|
||||||
|
export interface ICreateStoreParams<State, A extends Action> {
|
||||||
|
reducers: ReducersMapObject<State, A | any>
|
||||||
|
state?: DeepPartial<State>
|
||||||
|
middleware?: Middleware[]
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a Redux store.
|
||||||
|
*/
|
||||||
|
export function createStore<State, A extends Action>(
|
||||||
|
params: ICreateStoreParams<State, A>,
|
||||||
|
) {
|
||||||
|
const middleware = params.middleware || [new PromiseMiddleware().handle]
|
||||||
|
return create(
|
||||||
|
combineReducers(params.reducers),
|
||||||
|
params.state,
|
||||||
|
applyMiddleware(...middleware),
|
||||||
|
)
|
||||||
|
}
|
||||||
1
packages/client/src/store/index.ts
Normal file
1
packages/client/src/store/index.ts
Normal file
@ -0,0 +1 @@
|
|||||||
|
export * from './createStore'
|
||||||
@ -3,26 +3,16 @@ import ReactDOM from 'react-dom'
|
|||||||
import T from 'react-dom/test-utils'
|
import T from 'react-dom/test-utils'
|
||||||
import {Connector, IStateSelector} from '../redux'
|
import {Connector, IStateSelector} from '../redux'
|
||||||
import {Provider} from 'react-redux'
|
import {Provider} from 'react-redux'
|
||||||
import {PromiseMiddleware} from '../middleware'
|
import {createStore} from '../store'
|
||||||
import {
|
import {
|
||||||
Action,
|
Action,
|
||||||
AnyAction,
|
AnyAction,
|
||||||
DeepPartial,
|
DeepPartial,
|
||||||
Middleware,
|
|
||||||
Reducer,
|
Reducer,
|
||||||
ReducersMapObject,
|
ReducersMapObject,
|
||||||
Store,
|
|
||||||
applyMiddleware,
|
|
||||||
combineReducers,
|
combineReducers,
|
||||||
createStore,
|
|
||||||
} from 'redux'
|
} from 'redux'
|
||||||
|
|
||||||
interface IStoreParams<State, A extends Action<any>> {
|
|
||||||
reducer: Reducer<State, A>
|
|
||||||
state?: DeepPartial<State>
|
|
||||||
middleware?: Middleware[]
|
|
||||||
}
|
|
||||||
|
|
||||||
interface IRenderParams<State> {
|
interface IRenderParams<State> {
|
||||||
reducers: ReducersMapObject<State, any>
|
reducers: ReducersMapObject<State, any>
|
||||||
state?: DeepPartial<State>
|
state?: DeepPartial<State>
|
||||||
@ -31,6 +21,11 @@ interface IRenderParams<State> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export class TestUtils {
|
export class TestUtils {
|
||||||
|
/**
|
||||||
|
* Create a redux store
|
||||||
|
*/
|
||||||
|
readonly createStore = createStore
|
||||||
|
|
||||||
render(jsx: JSX.Element) {
|
render(jsx: JSX.Element) {
|
||||||
const component = T.renderIntoDocument(jsx) as React.Component<any>
|
const component = T.renderIntoDocument(jsx) as React.Component<any>
|
||||||
const node = ReactDOM.findDOMNode(component) as Element
|
const node = ReactDOM.findDOMNode(component) as Element
|
||||||
@ -44,20 +39,6 @@ export class TestUtils {
|
|||||||
return combineReducers(reducers)
|
return combineReducers(reducers)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Create a redux store
|
|
||||||
*/
|
|
||||||
createStore<State, A extends Action<any> = AnyAction>(
|
|
||||||
params: IStoreParams<State, A>,
|
|
||||||
): Store<State, A> {
|
|
||||||
const middleware = params.middleware || [new PromiseMiddleware().handle]
|
|
||||||
return createStore(
|
|
||||||
params.reducer,
|
|
||||||
params.state,
|
|
||||||
applyMiddleware(...middleware),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a redux store, connects a component, and provides the `render`
|
* Creates a redux store, connects a component, and provides the `render`
|
||||||
* method to render the connected component with a `Provider`.
|
* method to render the connected component with a `Provider`.
|
||||||
@ -66,7 +47,7 @@ export class TestUtils {
|
|||||||
params: IRenderParams<State>,
|
params: IRenderParams<State>,
|
||||||
) {
|
) {
|
||||||
const store = this.createStore({
|
const store = this.createStore({
|
||||||
reducer: this.combineReducers(params.reducers),
|
reducers: params.reducers,
|
||||||
state: params.state,
|
state: params.state,
|
||||||
})
|
})
|
||||||
const Component = params.connector.connect(params.select)
|
const Component = params.connector.connect(params.select)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user