-
Notifications
You must be signed in to change notification settings - Fork 6.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
--- For more details, open the [Copilot Workspace session](https://copilot-workspace.githubnext.com/nodejs/nodejs.org?shareId=XXXX-XXXX-XXXX-XXXX).
- Loading branch information
Showing
19 changed files
with
716 additions
and
12 deletions.
There are no files selected for viewing
88 changes: 88 additions & 0 deletions
88
apps/site/hooks/react-client/__tests__/useNavigationState.test.mjs
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,88 @@ | ||
import { renderHook, act } from '@testing-library/react'; | ||
import { useRef } from 'react'; | ||
|
||
import useNavigationState from '@/hooks/react-client/useNavigationState'; | ||
import { NavigationStateContext } from '@/providers/navigationStateProvider'; | ||
|
||
describe('useNavigationState', () => { | ||
it('should save and restore scroll position', () => { | ||
const mockElement = { | ||
scrollLeft: 0, | ||
scrollTop: 0, | ||
addEventListener: jest.fn(), | ||
removeEventListener: jest.fn(), | ||
scroll: jest.fn(), | ||
}; | ||
|
||
const mockRef = { current: mockElement }; | ||
const mockContextValue = {}; | ||
|
||
const wrapper = ({ children }) => ( | ||
<NavigationStateContext.Provider value={mockContextValue}> | ||
{children} | ||
</NavigationStateContext.Provider> | ||
); | ||
|
||
renderHook(() => useNavigationState('test-id', mockRef), { wrapper }); | ||
|
||
expect(mockElement.addEventListener).toHaveBeenCalledWith( | ||
'scroll', | ||
expect.any(Function), | ||
{ passive: true } | ||
); | ||
|
||
act(() => { | ||
mockElement.scrollTop = 100; | ||
mockElement.scrollLeft = 50; | ||
mockElement.addEventListener.mock.calls[0][1](); | ||
}); | ||
|
||
expect(mockContextValue['test-id']).toEqual({ x: 50, y: 100 }); | ||
|
||
act(() => { | ||
mockElement.scrollTop = 0; | ||
mockElement.scrollLeft = 0; | ||
mockElement.scroll.mock.calls[0][0](); | ||
}); | ||
|
||
expect(mockElement.scroll).toHaveBeenCalledWith({ | ||
top: 100, | ||
behavior: 'instant', | ||
}); | ||
}); | ||
|
||
it('should add and remove scroll event listener', () => { | ||
const mockElement = { | ||
scrollLeft: 0, | ||
scrollTop: 0, | ||
addEventListener: jest.fn(), | ||
removeEventListener: jest.fn(), | ||
}; | ||
|
||
const mockRef = { current: mockElement }; | ||
const mockContextValue = {}; | ||
|
||
const wrapper = ({ children }) => ( | ||
<NavigationStateContext.Provider value={mockContextValue}> | ||
{children} | ||
</NavigationStateContext.Provider> | ||
); | ||
|
||
const { unmount } = renderHook(() => useNavigationState('test-id', mockRef), { | ||
wrapper, | ||
}); | ||
|
||
expect(mockElement.addEventListener).toHaveBeenCalledWith( | ||
'scroll', | ||
expect.any(Function), | ||
{ passive: true } | ||
); | ||
|
||
unmount(); | ||
|
||
expect(mockElement.removeEventListener).toHaveBeenCalledWith( | ||
'scroll', | ||
expect.any(Function) | ||
); | ||
}); | ||
}); |
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,74 @@ | ||
import releaseReducer, { releaseState, getActions } from '@/reducers/releaseReducer'; | ||
|
||
describe('releaseReducer', () => { | ||
it('should return the initial state', () => { | ||
expect(releaseReducer(undefined, {})).toEqual(releaseState); | ||
}); | ||
|
||
it('should handle SET_VERSION', () => { | ||
const action = { type: 'SET_VERSION', payload: 'v14.17.0' }; | ||
const expectedState = { ...releaseState, version: 'v14.17.0' }; | ||
expect(releaseReducer(releaseState, action)).toEqual(expectedState); | ||
}); | ||
|
||
it('should handle SET_OS', () => { | ||
const action = { type: 'SET_OS', payload: 'WIN' }; | ||
const expectedState = { ...releaseState, os: 'WIN' }; | ||
expect(releaseReducer(releaseState, action)).toEqual(expectedState); | ||
}); | ||
|
||
it('should handle SET_PLATFORM', () => { | ||
const action = { type: 'SET_PLATFORM', payload: 'x64' }; | ||
const expectedState = { ...releaseState, platform: 'x64' }; | ||
expect(releaseReducer(releaseState, action)).toEqual(expectedState); | ||
}); | ||
|
||
it('should handle SET_INSTALL_METHOD', () => { | ||
const action = { type: 'SET_INSTALL_METHOD', payload: 'brew' }; | ||
const expectedState = { ...releaseState, installMethod: 'brew' }; | ||
expect(releaseReducer(releaseState, action)).toEqual(expectedState); | ||
}); | ||
|
||
it('should handle SET_MANAGER', () => { | ||
const action = { type: 'SET_MANAGER', payload: 'yarn' }; | ||
const expectedState = { ...releaseState, packageManager: 'yarn' }; | ||
expect(releaseReducer(releaseState, action)).toEqual(expectedState); | ||
}); | ||
}); | ||
|
||
describe('getActions', () => { | ||
it('should create setVersion action', () => { | ||
const dispatch = jest.fn(); | ||
const actions = getActions(dispatch); | ||
actions.setVersion('v14.17.0'); | ||
expect(dispatch).toHaveBeenCalledWith({ type: 'SET_VERSION', payload: 'v14.17.0' }); | ||
}); | ||
|
||
it('should create setOS action', () => { | ||
const dispatch = jest.fn(); | ||
const actions = getActions(dispatch); | ||
actions.setOS('WIN'); | ||
expect(dispatch).toHaveBeenCalledWith({ type: 'SET_OS', payload: 'WIN' }); | ||
}); | ||
|
||
it('should create setPlatform action', () => { | ||
const dispatch = jest.fn(); | ||
const actions = getActions(dispatch); | ||
actions.setPlatform('x64'); | ||
expect(dispatch).toHaveBeenCalledWith({ type: 'SET_PLATFORM', payload: 'x64' }); | ||
}); | ||
|
||
it('should create setInstallMethod action', () => { | ||
const dispatch = jest.fn(); | ||
const actions = getActions(dispatch); | ||
actions.setInstallMethod('brew'); | ||
expect(dispatch).toHaveBeenCalledWith({ type: 'SET_INSTALL_METHOD', payload: 'brew' }); | ||
}); | ||
|
||
it('should create setPackageManager action', () => { | ||
const dispatch = jest.fn(); | ||
const actions = getActions(dispatch); | ||
actions.setPackageManager('yarn'); | ||
expect(dispatch).toHaveBeenCalledWith({ type: 'SET_MANAGER', payload: 'yarn' }); | ||
}); | ||
}); |
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,27 @@ | ||
import { getBlogPosts, getBlogCategories, getBlogTags } from '@/util/blogUtils'; | ||
|
||
describe('blogUtils', () => { | ||
describe('getBlogPosts', () => { | ||
it('should retrieve blog posts', () => { | ||
const blogPosts = getBlogPosts(); | ||
expect(blogPosts).toBeDefined(); | ||
expect(Array.isArray(blogPosts)).toBe(true); | ||
}); | ||
}); | ||
|
||
describe('getBlogCategories', () => { | ||
it('should retrieve blog categories', () => { | ||
const blogCategories = getBlogCategories(); | ||
expect(blogCategories).toBeDefined(); | ||
expect(Array.isArray(blogCategories)).toBe(true); | ||
}); | ||
}); | ||
|
||
describe('getBlogTags', () => { | ||
it('should retrieve blog tags', () => { | ||
const blogTags = getBlogTags(); | ||
expect(blogTags).toBeDefined(); | ||
expect(Array.isArray(blogTags)).toBe(true); | ||
}); | ||
}); | ||
}); |
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
Oops, something went wrong.