forked from chatwoot/chatwoot
-
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: Reconnect logic (chatwoot#9453)
Co-authored-by: Muhsin Keloth <[email protected]> Co-authored-by: Shivam Mishra <[email protected]>
- Loading branch information
1 parent
00da2ac
commit af90f21
Showing
19 changed files
with
774 additions
and
93 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { emitter } from 'shared/helpers/mitt'; | ||
import { onMounted, onBeforeUnmount } from 'vue'; | ||
|
||
// this will automatically add event listeners to the emitter | ||
// and remove them when the component is destroyed | ||
const useEmitter = (eventName, callback) => { | ||
const cleanup = () => { | ||
emitter.off(eventName, callback); | ||
}; | ||
|
||
onMounted(() => { | ||
emitter.on(eventName, callback); | ||
}); | ||
|
||
onBeforeUnmount(cleanup); | ||
|
||
return cleanup; | ||
}; | ||
|
||
export { useEmitter }; |
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,51 @@ | ||
import { shallowMount } from '@vue/test-utils'; | ||
import { emitter } from 'shared/helpers/mitt'; | ||
import { useEmitter } from '../emitter'; | ||
|
||
jest.mock('shared/helpers/mitt', () => ({ | ||
emitter: { | ||
on: jest.fn(), | ||
off: jest.fn(), | ||
}, | ||
})); | ||
|
||
describe('useEmitter', () => { | ||
let wrapper; | ||
const eventName = 'my-event'; | ||
const callback = jest.fn(); | ||
|
||
beforeEach(() => { | ||
wrapper = shallowMount({ | ||
template: ` | ||
<div> | ||
Hello world | ||
</div> | ||
`, | ||
setup() { | ||
return { | ||
cleanup: useEmitter(eventName, callback), | ||
}; | ||
}, | ||
}); | ||
}); | ||
|
||
afterEach(() => { | ||
jest.resetAllMocks(); | ||
}); | ||
|
||
it('should add an event listener on mount', () => { | ||
expect(emitter.on).toHaveBeenCalledWith(eventName, callback); | ||
}); | ||
|
||
it('should remove the event listener when the component is unmounted', () => { | ||
wrapper.destroy(); | ||
expect(emitter.off).toHaveBeenCalledWith(eventName, callback); | ||
}); | ||
|
||
it('should return the cleanup function', () => { | ||
const cleanup = wrapper.vm.cleanup; | ||
expect(typeof cleanup).toBe('function'); | ||
cleanup(); | ||
expect(emitter.off).toHaveBeenCalledWith(eventName, callback); | ||
}); | ||
}); |
Oops, something went wrong.