-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add support for continuous patching (#334)
Co-authored-by: Matthias Lehner <143808484+matthiaslehnertum@users.noreply.github.com>
- Loading branch information
1 parent
62a3a8f
commit 0f00b6b
Showing
8 changed files
with
264 additions
and
23 deletions.
There are no files selected for viewing
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
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
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
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
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
File renamed without changes.
76 changes: 76 additions & 0 deletions
76
src/tests/unit/services/patcher/patcher-middleware-test.ts
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,76 @@ | ||
import sleep from 'sleep-promise'; | ||
|
||
import { createPatcherMiddleware } from '../../../../main/services/patcher/patcher-middleware'; | ||
import { Patcher } from '../../../../main/services/patcher/patcher'; | ||
|
||
describe('patcher middleware.', () => { | ||
test('should induce changes to the state to be reflected to the patcher.', async () => { | ||
const cb = jest.fn(); | ||
const patcher = new Patcher<{ x: number }>(); | ||
let state = { x: 42 }; | ||
|
||
const middleware = createPatcherMiddleware(patcher); | ||
patcher.subscribe(cb); | ||
|
||
middleware({ getState: () => state } as any)((() => { | ||
state = { x: 43 }; | ||
return state; | ||
}) as any)('ladida'); | ||
|
||
await sleep(1); | ||
|
||
expect(cb).toHaveBeenCalledTimes(1); | ||
expect(patcher.snapshot).toEqual({ x: 43 }); | ||
}); | ||
|
||
test('should induce continuous changes when the action is continuous.', async () => { | ||
const cb1 = jest.fn(); | ||
const cb2 = jest.fn(); | ||
const cb3 = jest.fn(); | ||
|
||
const patcher = new Patcher<{ x: number }>(); | ||
patcher.subscribe(cb1); | ||
patcher.subscribeToContinuousChanges(cb2); | ||
patcher.subscribeToDiscreteChanges(cb3); | ||
|
||
let state = { x: 42 }; | ||
|
||
const action1 = { type: 'a1' }; | ||
const action2 = { type: 'a2' }; | ||
const dispatch = (action: { type: string }) => { | ||
state = { x: state.x + 1 }; | ||
return state; | ||
}; | ||
|
||
const middleware = createPatcherMiddleware(patcher, { | ||
selectDiscrete: (action) => action.type === 'a1', | ||
selectContinuous: (action) => action.type === 'a2', | ||
}); | ||
|
||
const run = middleware({ getState: () => state } as any); | ||
|
||
run(dispatch as any)(action1); | ||
await sleep(1); | ||
|
||
expect(cb1).toHaveBeenCalledTimes(1); | ||
expect(cb2).toHaveBeenCalledTimes(0); | ||
expect(cb3).toHaveBeenCalledTimes(1); | ||
expect(patcher.snapshot).toEqual({ x: 43 }); | ||
|
||
run(dispatch as any)(action2); | ||
await sleep(1); | ||
|
||
expect(cb1).toHaveBeenCalledTimes(2); | ||
expect(cb2).toHaveBeenCalledTimes(1); | ||
expect(cb3).toHaveBeenCalledTimes(1); | ||
expect(patcher.snapshot).toEqual({ x: 43 }); | ||
|
||
run(dispatch as any)(action1); | ||
await sleep(1); | ||
|
||
expect(cb1).toHaveBeenCalledTimes(3); | ||
expect(cb2).toHaveBeenCalledTimes(1); | ||
expect(cb3).toHaveBeenCalledTimes(2); | ||
expect(patcher.snapshot).toEqual({ x: 45 }); | ||
}); | ||
}); |
Oops, something went wrong.