Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add additional tests #134

Merged
merged 2 commits into from
Nov 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"@storybook/react-webpack5": "^7.1.1",
"@testing-library/jest-dom": "^5.16.5",
"@testing-library/react": "12.1.5",
"@testing-library/react-hooks": "^8.0.1",
"@types/jest": "^28.1.7",
"@types/react": "^17.0.15",
"@types/styled-components": "^5.1.11",
Expand Down
2 changes: 1 addition & 1 deletion src/hooks/click-outside/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const useOnClickOutside = <T extends HTMLElement = HTMLElement>(
useEffect(() => {
const listener = (event: Event) => {
const el = ref?.current
if (!el || el.contains((event?.target as Node) || null)) {
if (!el || el.contains(event?.target as Node)) {
return
}

Expand Down
42 changes: 42 additions & 0 deletions src/hooks/click-outside/test.tsx
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()
})
})
29 changes: 29 additions & 0 deletions src/hooks/key-press/test.tsx
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()
})
})
38 changes: 38 additions & 0 deletions src/hooks/window-near-bottom/test.tsx
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()
})
})
Loading