-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): new operate
addDebugListener
for debugging
You can use this operater to debug all the stream of the application closes #52
- Loading branch information
Showing
2 changed files
with
90 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import { | ||
addDebugListener, | ||
createEventType, | ||
register, | ||
useAction, | ||
useEvent, | ||
useStream, | ||
useTrigger, | ||
} from '../src' | ||
|
||
describe('use debugger', () => { | ||
it('should attach console log to main app stream', done => { | ||
const handler = jest.fn() | ||
const handlerReal = jest.fn() | ||
const handlerNotFoundStream = jest.fn() | ||
const handlerSpecificStream = jest.fn() | ||
|
||
register(() => { | ||
const SAMPLE_EVENT = createEventType('SAMPLE_EVENT') | ||
const STREAM_ID = 'dev.barajs.debugger' | ||
useStream<number>(({ setName, addEventType, emit }) => { | ||
setName(STREAM_ID) | ||
addEventType(SAMPLE_EVENT) | ||
|
||
let counter = 0 | ||
const timer = setInterval(() => { | ||
emit(SAMPLE_EVENT, counter) | ||
counter += 1 | ||
if (counter > 9) { | ||
clearInterval(timer) | ||
} | ||
}, 500) | ||
|
||
return () => { | ||
clearInterval(timer) | ||
} | ||
}) | ||
|
||
// tslint:disable-next-line | ||
addDebugListener('app', (data: any) => { | ||
handler(data) | ||
}) | ||
|
||
// Add specific stream debugger by attaching to its name | ||
addDebugListener(STREAM_ID, handlerSpecificStream) | ||
|
||
// Add not found stream and expect a handling error | ||
expect(() => { | ||
addDebugListener(STREAM_ID + '-diff', handlerNotFoundStream) | ||
}).toThrow( | ||
`[Bara Debugger] Not found any stream registered with name ${STREAM_ID}-diff. You can specify stream 'app' for all stream event.`, | ||
) | ||
|
||
useTrigger(() => { | ||
const event = useEvent(SAMPLE_EVENT) | ||
const action = useAction(handlerReal) | ||
return { event, action } | ||
}) | ||
}) | ||
|
||
setTimeout(() => { | ||
expect(handler).toHaveBeenCalledTimes(9) | ||
expect(handlerSpecificStream).toHaveBeenCalledTimes(9) | ||
expect(handlerNotFoundStream).not.toHaveBeenCalled() | ||
expect(handler).toHaveBeenCalled() | ||
done() | ||
}, 5000) | ||
}) | ||
}) |