-
-
Notifications
You must be signed in to change notification settings - Fork 97
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(overmind): completely rework the typing
BREAKING CHANGE: new typing approach, unified actions with operators
- Loading branch information
1 parent
4a694e7
commit cd3ff4e
Showing
17 changed files
with
2,085 additions
and
2,363 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import * as proxyStateTree from 'proxy-state-tree' | ||
import { IConfiguration } from './types' | ||
import * as internalTypes from './internalTypes' | ||
import { Overmind } from './Overmind' | ||
import * as utils from './utils' | ||
|
||
export interface OvermindMock<Config extends IConfiguration> | ||
extends Overmind<Config> { | ||
onInitialize: () => Promise<proxyStateTree.IMutation[]> | ||
mutations: proxyStateTree.IMutation[] | ||
} | ||
|
||
export function createOvermindMock<Config extends IConfiguration>( | ||
config: Config | ||
): OvermindMock<Config> | ||
export function createOvermindMock<Config extends IConfiguration>( | ||
config: Config, | ||
setInitialState: (state: Config['state']) => void | ||
): OvermindMock<Config> | ||
export function createOvermindMock<Config extends IConfiguration>( | ||
config: Config, | ||
mockedEffects: internalTypes.NestedPartial<Config['effects']> | ||
): OvermindMock<Config> | ||
export function createOvermindMock<Config extends IConfiguration>( | ||
config: Config, | ||
mockedEffects: internalTypes.NestedPartial<Config['effects']>, | ||
setInitialState: (state: Config['state']) => void | ||
): OvermindMock<Config> | ||
export function createOvermindMock<Config extends IConfiguration>( | ||
...args: | ||
| [Config] | ||
| [Config, (state: Config['state']) => void] | ||
| [Config, internalTypes.NestedPartial<Config['effects']>] | ||
| [ | ||
Config, | ||
internalTypes.NestedPartial<Config['effects']>, | ||
(state: Config['state']) => void | ||
] | ||
): OvermindMock<Config> { | ||
const setState = typeof args[1] === 'function' ? args[1] : args[2] | ||
const mockedEffects = typeof args[1] === 'function' ? undefined : args[1] | ||
|
||
const state = utils.deepCopy(args[0].state) | ||
|
||
if (setState) { | ||
;(setState as any)(state) | ||
} | ||
const mock = new Overmind( | ||
Object.assign({}, args[0], { | ||
state, | ||
}), | ||
{ | ||
devtools: false, | ||
}, | ||
{ | ||
mode: utils.MODE_TEST, | ||
options: { | ||
effectsCallback: (effect) => { | ||
const mockedEffect = (effect.name | ||
? effect.name.split('.') | ||
: [] | ||
).reduce((aggr, key) => (aggr ? aggr[key] : aggr), mockedEffects) | ||
|
||
if (!mockedEffect || (mockedEffect && !mockedEffect[effect.method])) { | ||
throw new Error( | ||
`The effect "${effect.name}" with method ${ | ||
effect.method | ||
} has not been mocked` | ||
) | ||
} | ||
return mockedEffect[effect.method](...effect.args) | ||
}, | ||
}, | ||
} as internalTypes.TestMode | ||
) as OvermindMock<Config> | ||
|
||
const action = (mock as any).createAction( | ||
'onInitialize', | ||
args[0].onInitialize | ||
) | ||
|
||
mock.onInitialize = () => action(mock) | ||
mock.mutations = [] | ||
|
||
return mock as any | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { IConfiguration } from './types' | ||
import { Overmind } from './Overmind' | ||
import * as proxyStateTree from 'proxy-state-tree' | ||
import * as utils from './utils' | ||
import * as internalTypes from './internalTypes' | ||
|
||
export interface OvermindSSR<Config extends IConfiguration> | ||
extends Overmind<Config> { | ||
hydrate(): proxyStateTree.IMutation[] | ||
} | ||
|
||
export function createOvermindSSR<Config extends IConfiguration>( | ||
config: Config | ||
): OvermindSSR<Config> { | ||
const ssr = new Overmind( | ||
config, | ||
{ | ||
devtools: false, | ||
}, | ||
{ | ||
mode: utils.MODE_SSR, | ||
} as internalTypes.SSRMode | ||
) as any | ||
|
||
const mutationTree = ssr.proxyStateTreeInstance.getMutationTree() | ||
|
||
ssr.state = mutationTree.state | ||
ssr.hydrate = () => { | ||
return mutationTree.flush().mutations | ||
} | ||
return ssr | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.