-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
83 additions
and
14 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
File renamed without changes.
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,35 @@ | ||
import { renderHook } from '@testing-library/react' | ||
import { FC } from 'react' | ||
import { State } from '@/context/global' | ||
import { GlobalStateProvider, useGlobal, useSelector } from '../global' | ||
|
||
const GlobalWrapper: FC<{ children: React.ReactNode }> = ({ children }) => ( | ||
<GlobalStateProvider>{children}</GlobalStateProvider> | ||
) | ||
|
||
describe('useGlobal', () => { | ||
it('fails if called outside provider', async () => { | ||
console.error = jest.fn() | ||
expect(() => renderHook(() => useGlobal())).toThrow( | ||
'useGlobal must be used within a GlobalStateProvider' | ||
) | ||
expect(console.error).toHaveBeenCalled() | ||
}) | ||
|
||
it('provides context when called inside provider', async () => { | ||
const { result } = renderHook(() => useGlobal(), { | ||
wrapper: GlobalWrapper, | ||
}) | ||
expect(result.current.state.debug).toBeFalsy() | ||
}) | ||
}) | ||
|
||
describe('useSelector', () => { | ||
it('should bind selector to state', () => { | ||
const testSelector = (state: State) => state.debug | ||
const { result } = renderHook(() => useSelector(testSelector), { | ||
wrapper: GlobalWrapper, | ||
}) | ||
expect(result.current).toBeFalsy | ||
}) | ||
}) |
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,21 @@ | ||
import { initialState, State } from '@/context/global' | ||
import { selectDebug, selectAdvancedConfiguration } from './global-selectors' | ||
|
||
describe('Experiment selectors', () => { | ||
let state: State | ||
beforeEach(() => { | ||
state = JSON.parse(JSON.stringify(initialState)) | ||
}) | ||
|
||
it('should select debug', () => { | ||
state.debug = true | ||
expect(selectDebug(state)).toBeTruthy() | ||
}) | ||
|
||
describe('Flags', () => { | ||
it('should selectAdvancedConfiguration', () => { | ||
state.flags.advancedConfiguration = true | ||
expect(selectAdvancedConfiguration(state)).toBeTruthy() | ||
}) | ||
}) | ||
}) |
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,8 @@ | ||
import { State } from '@/context/global' | ||
|
||
export const selectDebug = (state: State) => state.debug | ||
|
||
export const selectFlags = (state: State) => state.flags | ||
|
||
export const selectAdvancedConfiguration = (state: State) => | ||
selectFlags(state).advancedConfiguration |