-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from rstgroup/errorHandling
error handling on emit
- Loading branch information
Showing
6 changed files
with
148 additions
and
14 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
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,60 @@ | ||
import React from 'react'; | ||
import { render } from '@testing-library/react'; | ||
import EventrixProvider from '../context/EventrixProvider'; | ||
import useEvent from '../hooks/useEvent'; | ||
import EventrixScope from '../context/EventrixScope'; | ||
import { defaultEventrixInstance } from './context'; | ||
|
||
describe('context with default eventrix instance', () => { | ||
const ItemComponent = ({ callback }: any) => { | ||
useEvent('testEvent', callback); | ||
return <div>Test Item Component</div>; | ||
}; | ||
// @ts-ignore | ||
const TestContainer = ({ children }: any) => <EventrixProvider>{children}</EventrixProvider>; | ||
|
||
it('should invoke callback when event emitted', () => { | ||
const eventrixInstance = defaultEventrixInstance; | ||
const callbackMock = jest.fn(); | ||
|
||
render( | ||
<TestContainer> | ||
<ItemComponent callback={callbackMock} /> | ||
</TestContainer>, | ||
); | ||
eventrixInstance.emit('testEvent', 'test'); | ||
expect(callbackMock).toHaveBeenCalledWith('test', []); | ||
}); | ||
|
||
it('should invoke callback when event emitted with scope', () => { | ||
const eventrixInstance = defaultEventrixInstance; | ||
const callbackMock = jest.fn(); | ||
|
||
render( | ||
<TestContainer> | ||
<EventrixScope event="Test"> | ||
<ItemComponent callback={callbackMock} /> | ||
</EventrixScope> | ||
</TestContainer>, | ||
); | ||
eventrixInstance.emit('Test:testEvent', 'test'); | ||
expect(callbackMock).toHaveBeenCalledWith('test', []); | ||
}); | ||
|
||
it('should invoke callback when event emitted with deep scope', () => { | ||
const eventrixInstance = defaultEventrixInstance; | ||
const callbackMock = jest.fn(); | ||
|
||
render( | ||
<TestContainer> | ||
<EventrixScope event="Test"> | ||
<EventrixScope event="List"> | ||
<ItemComponent callback={callbackMock} /> | ||
</EventrixScope> | ||
</EventrixScope> | ||
</TestContainer>, | ||
); | ||
eventrixInstance.emit('Test:List:testEvent', 'test'); | ||
expect(callbackMock).toHaveBeenCalledWith('test', []); | ||
}); | ||
}); |