-
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce
usePage
and useSuperglue
hooks.
These are lightweight wrappers around Redux's `useSelector` and a nice, but must have convenience as we move away from `mapStateToProps`.
- Loading branch information
Showing
5 changed files
with
95 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { useSelector } from 'react-redux' | ||
import { Page, RootState, SuperglueState } from '../types' | ||
|
||
/** | ||
* A lightweight hook that grabs the superglue state from the store. | ||
*/ | ||
export function useSuperglue() { | ||
return useSelector<RootState, SuperglueState>((state) => state.superglue) | ||
} | ||
|
||
/** | ||
* A lightweight hook that grabs the current page from the store. | ||
*/ | ||
export function usePage() { | ||
const superglueState = useSuperglue() | ||
const currentPageKey = superglueState.currentPageKey | ||
|
||
return useSelector<RootState, Page>((state) => state.pages[currentPageKey]) | ||
} |
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,73 @@ | ||
import { renderHook } from '@testing-library/react' | ||
import { usePage, useSuperglue } from '../../lib' | ||
import { describe, it } from 'vitest' | ||
import { Provider } from 'react-redux' | ||
import { configureStore } from '@reduxjs/toolkit' | ||
|
||
describe('hooks', () => { | ||
describe('useSuperglue', () => { | ||
it('returns the superglue state', () => { | ||
const preloadedState = { | ||
superglue: { | ||
currentPageKey: '/current?abc=123', | ||
pathname: '/current', | ||
search: '?abc=123', | ||
csrfToken: 'csrf123', | ||
assets: ['js-asset-123'], | ||
}, | ||
} | ||
|
||
let store = configureStore({ | ||
preloadedState, | ||
reducer: (state) => state, | ||
}) | ||
const wrapper = ({ children }) => ( | ||
<Provider store={store}>{children}</Provider> | ||
) | ||
const { result } = renderHook(() => useSuperglue(), { wrapper }) | ||
|
||
expect(result.current).toEqual(preloadedState.superglue) | ||
}) | ||
}) | ||
|
||
describe('usePage', () => { | ||
it('returns the page state', () => { | ||
const preloadedState = { | ||
superglue: { | ||
currentPageKey: '/current?abc=123', | ||
pathname: '/current', | ||
search: '?abc=123', | ||
csrfToken: 'csrf123', | ||
assets: ['js-asset-123'], | ||
}, | ||
pages: { | ||
'/current?abc=123': { | ||
data: { | ||
heading: 'selected', | ||
}, | ||
}, | ||
'/other': { | ||
data: { | ||
heading: 'not selected', | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
let store = configureStore({ | ||
preloadedState, | ||
reducer: (state) => state, | ||
}) | ||
const wrapper = ({ children }) => ( | ||
<Provider store={store}>{children}</Provider> | ||
) | ||
const { result } = renderHook(() => usePage(), { wrapper }) | ||
|
||
expect(result.current).toEqual({ | ||
data: { | ||
heading: 'selected', | ||
}, | ||
}) | ||
}) | ||
}) | ||
}) |
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