Skip to content

Commit

Permalink
Add additional tests (#134)
Browse files Browse the repository at this point in the history
closes #7
  • Loading branch information
vivek-p-a authored Nov 6, 2023
1 parent 3a1e50d commit 7d8b821
Show file tree
Hide file tree
Showing 23 changed files with 891 additions and 10 deletions.
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

0 comments on commit 7d8b821

Please sign in to comment.