-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactors and adding unit tests for various components (#279)
- Loading branch information
1 parent
f737581
commit c6a6ad1
Showing
38 changed files
with
1,125 additions
and
645 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
|
@@ -15,7 +15,7 @@ | |
"@babel/preset-env": "^7.22.9", | ||
"@babel/preset-react": "^7.22.5", | ||
"@babel/preset-typescript": "^7.22.5", | ||
"@relative-ci/agent": "^4.1.5", | ||
"@relative-ci/agent": "^4.1.6", | ||
"@sentry/react": "^7.60.1", | ||
"@sentry/tracing": "^7.60.1", | ||
"@types/bluebird": "^3.5.38", | ||
|
@@ -27,14 +27,14 @@ | |
"autoprefixer": "^10.4.14", | ||
"cssnano": "^6.0.1", | ||
"esbuild": "^0.18.17", | ||
"eslint": "^8.45.0", | ||
"eslint-config-prettier": "^8.8.0", | ||
"eslint-plugin-import": "2.27.5", | ||
"eslint": "^8.46.0", | ||
"eslint-config-prettier": "^8.9.0", | ||
"eslint-plugin-import": "2.28.0", | ||
"eslint-plugin-jest": "^27.2.3", | ||
"eslint-plugin-jsx-a11y": "^6.7.1", | ||
"eslint-plugin-node": "11.1.0", | ||
"eslint-plugin-promise": "6.1.1", | ||
"eslint-plugin-react": "^7.33.0", | ||
"eslint-plugin-react": "^7.33.1", | ||
"eslint-plugin-sort-keys-fix": "^1.1.2", | ||
"eslint-plugin-typescript-sort-keys": "^2.3.0", | ||
"fork-ts-checker-webpack-plugin": "^8.0.0", | ||
|
@@ -112,6 +112,7 @@ | |
}, | ||
"packageManager": "[email protected]", | ||
"devDependencies": { | ||
"@testing-library/react-hooks": "^8.0.1", | ||
"@vitest/coverage-v8": "^0.33.0", | ||
"postcss-scss": "^4.0.6" | ||
} | ||
|
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
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
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
52 changes: 52 additions & 0 deletions
52
packages/lib/components/common/effects/__tests__/useCloseOnEsc.test.ts
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,52 @@ | ||
import { renderHook, act } from '@testing-library/react-hooks'; | ||
import { useCloseOnEscape } from '../useCloseOnEsc'; | ||
import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest'; | ||
|
||
describe('useCloseOnEscape', () => { | ||
let container: HTMLDivElement; | ||
let handler; | ||
|
||
beforeEach(() => { | ||
container = document.createElement('div'); | ||
document.body.appendChild(container); | ||
handler = vi.fn(); | ||
}); | ||
|
||
afterEach(() => { | ||
document.body.removeChild(container); | ||
container.remove(); | ||
}); | ||
|
||
it('should call handler when Escape key is pressed', () => { | ||
renderHook(() => useCloseOnEscape(handler, { current: container })); | ||
|
||
act(() => { | ||
const event = new KeyboardEvent('keyup', { key: 'Escape' }); | ||
container.dispatchEvent(event); | ||
}); | ||
|
||
expect(handler).toHaveBeenCalled(); | ||
}); | ||
|
||
it('should not call handler when other key is pressed', () => { | ||
renderHook(() => useCloseOnEscape(handler, { current: container })); | ||
|
||
act(() => { | ||
const event = new KeyboardEvent('keyup', { key: 'Enter' }); | ||
container.dispatchEvent(event); | ||
}); | ||
|
||
expect(handler).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it('should not attach event listener when element does not exist', () => { | ||
renderHook(() => useCloseOnEscape(handler, { current: null })); | ||
|
||
act(() => { | ||
const event = new KeyboardEvent('keyup', { key: 'Escape' }); | ||
container.dispatchEvent(event); | ||
}); | ||
|
||
expect(handler).not.toHaveBeenCalled(); | ||
}); | ||
}); |
24 changes: 24 additions & 0 deletions
24
packages/lib/components/common/effects/__tests__/useFirstRender.test.ts
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,24 @@ | ||
import { renderHook, act } from '@testing-library/react-hooks'; | ||
import { useFirstRender } from '../useFirstRender'; | ||
import { describe, it, expect } from 'vitest'; | ||
|
||
describe('useFirstRender hook', () => { | ||
it('should return true on first render', () => { | ||
const { result, rerender } = renderHook(() => useFirstRender()); | ||
|
||
rerender(); | ||
|
||
expect(result.current.current).toBe(false); | ||
}); | ||
|
||
it('should return the same reference for every render', () => { | ||
const { result, rerender } = renderHook(() => useFirstRender()); | ||
const initialReference = result.current; | ||
|
||
act(() => { | ||
rerender(); | ||
}); | ||
|
||
expect(result.current).toBe(initialReference); | ||
}); | ||
}); |
66 changes: 66 additions & 0 deletions
66
packages/lib/components/common/effects/__tests__/useFocusNew.test.ts
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,66 @@ | ||
import { renderHook, act } from '@testing-library/react-hooks'; | ||
import { useRef } from 'react'; | ||
import { describe, it, assert } from 'vitest'; | ||
import useFocusNew from '../useFocusNew'; | ||
import styles from '../focus.module.scss'; | ||
|
||
describe('useFocusNew hook', () => { | ||
it('should add focus ring when keyup event is triggered', () => { | ||
const ref = { current: document.createElement('div') }; | ||
const callback = () => {}; | ||
const { rerender } = renderHook(() => useFocusNew(ref, callback)); | ||
|
||
act(() => { | ||
const event = new KeyboardEvent('keyup', { key: 'Enter' }); | ||
ref.current.dispatchEvent(event); | ||
}); | ||
|
||
rerender(); | ||
|
||
assert( | ||
ref.current.querySelector(`.${styles.focus_ring_active}`) !== null, | ||
'Focus ring should be active' | ||
); | ||
}); | ||
|
||
it('should remove focus ring when focusout event is triggered', () => { | ||
const ref = { current: document.createElement('div') }; | ||
const callback = () => {}; | ||
const { rerender } = renderHook(() => useFocusNew(ref, callback)); | ||
|
||
act(() => { | ||
const event = new Event('focusout'); | ||
ref.current.dispatchEvent(event); | ||
}); | ||
|
||
rerender(); | ||
|
||
assert( | ||
ref.current.querySelector(`.${styles.focus_ring_inactive}`) !== null, | ||
'Focus ring should be inactive' | ||
); | ||
}); | ||
|
||
it('should call the callback when Enter or Space is pressed', () => { | ||
let callCount = 0; | ||
const callback = () => { | ||
callCount++; | ||
}; | ||
const ref = { current: document.createElement('div') }; | ||
|
||
const { rerender } = renderHook(() => useFocusNew(ref, callback)); | ||
|
||
act(() => { | ||
const event = new KeyboardEvent('keyup', { key: 'Enter' }); | ||
ref.current.dispatchEvent(event); | ||
}); | ||
|
||
rerender(); | ||
|
||
assert.equal( | ||
callCount, | ||
1, | ||
'callback should be called once when Enter is pressed' | ||
); | ||
}); | ||
}); |
Oops, something went wrong.
c6a6ad1
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
react-creme – ./
react-creme-prabhuignoto.vercel.app
react-creme.vercel.app
react-creme.prabhumurthy.com
react-creme-git-master-prabhuignoto.vercel.app