-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
closes #7
- Loading branch information
Showing
23 changed files
with
891 additions
and
10 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { renderHook } from '@testing-library/react-hooks' | ||
import { fireEvent } from '../../utils/testUtils' | ||
import useOnClickOutside from '.' | ||
import { RefObject } from 'react' | ||
|
||
describe('click-outside', () => { | ||
it('should test that the callback passed is called when the user clicks outside', () => { | ||
const callbackToBeCalled = jest.fn() | ||
const refObject = { current: document.head } | ||
renderHook(() => useOnClickOutside(refObject, callbackToBeCalled)) | ||
|
||
expect(callbackToBeCalled).not.toHaveBeenCalled() | ||
|
||
fireEvent.mouseDown(document.body) | ||
|
||
expect(callbackToBeCalled).toHaveBeenCalled() | ||
}) | ||
|
||
it('should test that the callback passed is not called when the user clicks inside', () => { | ||
const callbackToBeCalled = jest.fn() | ||
const refObject = { current: document.head } | ||
renderHook(() => useOnClickOutside(refObject, callbackToBeCalled)) | ||
|
||
expect(callbackToBeCalled).not.toHaveBeenCalled() | ||
|
||
fireEvent.mouseDown(document.head) | ||
|
||
expect(callbackToBeCalled).not.toHaveBeenCalled() | ||
}) | ||
|
||
it('should test that the callback passed is not called when ref object is undefined', () => { | ||
const callbackToBeCalled = jest.fn() | ||
const refObject = undefined as unknown as RefObject<HTMLElement> | ||
renderHook(() => useOnClickOutside(refObject, callbackToBeCalled)) | ||
|
||
expect(callbackToBeCalled).not.toHaveBeenCalled() | ||
|
||
fireEvent.mouseDown(document.head) | ||
|
||
expect(callbackToBeCalled).not.toHaveBeenCalled() | ||
}) | ||
}) |
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,29 @@ | ||
import { renderHook } from '@testing-library/react-hooks' | ||
import { fireEvent } from '../../utils/testUtils' | ||
import useOnKeyPress from '.' | ||
|
||
describe('key-press', () => { | ||
it('should test that when the key specified by the user is pressed, the callback function is invoked', () => { | ||
const callbackToBeCalled = jest.fn() | ||
renderHook(() => useOnKeyPress('Enter', callbackToBeCalled)) | ||
|
||
expect(callbackToBeCalled).not.toHaveBeenCalled() | ||
|
||
fireEvent.keyUp(document.body, { | ||
key: 'Enter' | ||
}) | ||
|
||
expect(callbackToBeCalled).toHaveBeenCalled() | ||
}) | ||
|
||
it('should test that when the callback is not called when a different key is pressed by the user', () => { | ||
const callbackToBeCalled = jest.fn() | ||
renderHook(() => useOnKeyPress('Enter', callbackToBeCalled)) | ||
|
||
fireEvent.keyUp(document.body, { | ||
key: 'Escape' | ||
}) | ||
|
||
expect(callbackToBeCalled).not.toHaveBeenCalled() | ||
}) | ||
}) |
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,38 @@ | ||
import { renderHook } from '@testing-library/react-hooks' | ||
import { fireEvent } from '../../utils/testUtils' | ||
import useWindowOnNearBottom from '.' | ||
|
||
const setScrollHeight = (height: number) => { | ||
Object.defineProperty(document.body, 'scrollHeight', { | ||
configurable: true, | ||
value: height | ||
}) | ||
} | ||
|
||
describe('window-near-bottom', () => { | ||
it('should test that the callback function is invoked when user scrolls to the bottom', () => { | ||
setScrollHeight(768) | ||
|
||
const callbackToBeCalled = jest.fn() | ||
renderHook(() => useWindowOnNearBottom(callbackToBeCalled)) | ||
|
||
expect(callbackToBeCalled).not.toHaveBeenCalled() | ||
|
||
fireEvent.scroll(window) | ||
|
||
expect(callbackToBeCalled).toHaveBeenCalled() | ||
}) | ||
|
||
it('should test that the callback function is not invoked if user if not near the bottom', () => { | ||
setScrollHeight(1000) | ||
|
||
const callbackToBeCalled = jest.fn() | ||
renderHook(() => useWindowOnNearBottom(callbackToBeCalled)) | ||
|
||
expect(callbackToBeCalled).not.toHaveBeenCalled() | ||
|
||
fireEvent.scroll(window) | ||
|
||
expect(callbackToBeCalled).not.toHaveBeenCalled() | ||
}) | ||
}) |
Oops, something went wrong.