-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Share messages between tabs * on tab focus fix * Unit tests
- Loading branch information
1 parent
88cef78
commit b586891
Showing
8 changed files
with
188 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { debounce } from './debounce'; | ||
|
||
describe('debounce', () => { | ||
|
||
beforeAll(() => { | ||
jest.useFakeTimers(); | ||
}); | ||
|
||
it('calls the function only once after the specified time', () => { | ||
const fn = jest.fn(); | ||
const debouncedFn = debounce(fn, 500); | ||
|
||
debouncedFn(); | ||
debouncedFn(); | ||
debouncedFn(); | ||
jest.runAllTimers(); | ||
|
||
expect(fn).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it('calls the function with the correct arguments', () => { | ||
const fn = jest.fn(); | ||
const debouncedFn = debounce(fn, 500); | ||
|
||
debouncedFn('test', 123); | ||
jest.runAllTimers(); | ||
|
||
expect(fn).toHaveBeenCalledWith('test', 123); | ||
}); | ||
|
||
it('uses the correct "this" context', () => { | ||
const fn = jest.fn(); | ||
const context = { value: 'context' }; | ||
const debouncedFn = debounce(fn, 500); | ||
|
||
debouncedFn.call(context); | ||
jest.runAllTimers(); | ||
|
||
expect(fn.mock.instances[0]).toBe(context); | ||
}); | ||
|
||
it('uses the default delay if none is provided', () => { | ||
const fn = jest.fn(); | ||
const debouncedFn = debounce(fn); | ||
|
||
debouncedFn(); | ||
|
||
jest.advanceTimersByTime(299); | ||
expect(fn).not.toHaveBeenCalled(); | ||
|
||
jest.advanceTimersByTime(1); | ||
expect(fn).toHaveBeenCalledTimes(1); | ||
}); | ||
}); |
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,7 @@ | ||
export const debounce = (fn: Function, ms = 300) => { | ||
let timeoutId: ReturnType<typeof setTimeout>; | ||
return function (this: any, ...args: any[]) { | ||
clearTimeout(timeoutId); | ||
timeoutId = setTimeout(() => fn.apply(this, args), ms); | ||
}; | ||
}; |
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,59 @@ | ||
import { broadcastChatHistoryEvent, receiveChatHistoryEvent } from './eventChannel'; | ||
|
||
describe('broadcastChatHistoryEvent', () => { | ||
beforeEach(() => { | ||
localStorage.clear(); | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('should not set or remove from localStorage if senderID is not set', () => { | ||
const setItemSpy = jest.spyOn(localStorage, 'setItem'); | ||
const removeItemSpy = jest.spyOn(localStorage, 'removeItem'); | ||
|
||
broadcastChatHistoryEvent('some chat history', ''); | ||
|
||
expect(setItemSpy).not.toHaveBeenCalled(); | ||
expect(removeItemSpy).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it('should set and then remove the chat history in localStorage for the given senderID', () => { | ||
const setItemSpy = jest.spyOn(localStorage, 'setItem'); | ||
const removeItemSpy = jest.spyOn(localStorage, 'removeItem'); | ||
|
||
const senderID = '123'; | ||
broadcastChatHistoryEvent('some chat history', senderID); | ||
|
||
expect(setItemSpy).toHaveBeenCalledWith(`rasaChatHistory-${senderID}`, 'some chat history'); | ||
expect(removeItemSpy).toHaveBeenCalledWith(`rasaChatHistory-${senderID}`); | ||
}); | ||
}); | ||
|
||
describe('receiveChatHistoryEvent', () => { | ||
it('should not call callback if the event key does not match the senderID', () => { | ||
const callback = jest.fn(); | ||
const ev = { key: '321', newValue: 'new chat history' }; | ||
|
||
receiveChatHistoryEvent(ev, callback, '123'); | ||
|
||
expect(callback).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it('should not call callback if newValue is falsy', () => { | ||
const callback = jest.fn(); | ||
const ev = { key: 'rasaChatHistory-123', newValue: null }; | ||
|
||
receiveChatHistoryEvent(ev, callback, '123'); | ||
|
||
expect(callback).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it('should call callback with new chat history if the event matches the senderID', () => { | ||
const callback = jest.fn(); | ||
const newChatHistory = 'new chat history'; | ||
const ev = { key: 'rasaChatHistory-123', newValue: newChatHistory }; | ||
|
||
receiveChatHistoryEvent(ev, callback, '123'); | ||
|
||
expect(callback).toHaveBeenCalledWith(newChatHistory); | ||
}); | ||
}); |
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,12 @@ | ||
export const broadcastChatHistoryEvent = (chatHistory: string, senderID) => { | ||
if (!senderID) return; | ||
localStorage.setItem(`rasaChatHistory-${senderID}`, chatHistory); | ||
localStorage.removeItem(`rasaChatHistory-${senderID}`); | ||
}; | ||
|
||
export const receiveChatHistoryEvent = (ev, callback, senderID) => { | ||
const newChatHistory = ev.newValue; | ||
|
||
if (ev.key != `rasaChatHistory-${senderID}` || !newChatHistory) return; | ||
callback(newChatHistory); | ||
}; |