Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[@mantine/hooks] use-media-query: Fix given initialValue not being us…
…ed (#3085) * Add undefined check to override boolean validation * Removed initialValue. Fetch directly from query * Updated tests and reset initialValue
- Loading branch information
Showing
3 changed files
with
100 additions
and
1 deletion.
There are no files selected for viewing
28 changes: 28 additions & 0 deletions
28
src/mantine-hooks/src/use-color-scheme/use-color-scheme.story.tsx
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,28 @@ | ||
import React, { useEffect, useState } from 'react'; | ||
import { storiesOf } from '@storybook/react'; | ||
import { useColorScheme } from './use-color-scheme'; | ||
|
||
const Demo = () => { | ||
const colorScheme = useColorScheme(undefined, { | ||
getInitialValueInEffect: false, | ||
}); | ||
const [history, setHistory] = useState([]); | ||
|
||
useEffect(() => { | ||
setHistory((current) => [...current, colorScheme]); | ||
}, [colorScheme, setHistory]); | ||
|
||
return ( | ||
<div> | ||
{history.map((mode, i) => ( | ||
<div key={i}>Color Scheme: {mode}</div> | ||
))} | ||
</div> | ||
); | ||
}; | ||
|
||
storiesOf('Hooks/use-color-scheme', module).add('General usage', () => ( | ||
<div style={{ padding: 40 }}> | ||
<Demo /> | ||
</div> | ||
)); |
71 changes: 71 additions & 0 deletions
71
src/mantine-hooks/src/use-color-scheme/use-color-scheme.test.tsx
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,71 @@ | ||
import React from 'react'; | ||
// import { renderHook } from '@testing-library/react-hooks'; | ||
import { render } from '@testing-library/react'; | ||
import { useColorScheme } from './use-color-scheme'; | ||
|
||
describe('@maintine/hooks/use-color-scheme', () => { | ||
let trace = jest.fn<(colorScheme: string) => void, string[]>(); | ||
const mockmatchMedia = jest.fn().mockImplementation(() => ({ | ||
matches: true, | ||
media: '', | ||
onchange: null, | ||
addListener: jest.fn(), | ||
removeListener: jest.fn(), | ||
})); | ||
const retainMatchMedia = window.matchMedia; | ||
beforeEach(() => { | ||
trace = jest.fn(); | ||
window.matchMedia = retainMatchMedia; | ||
}); | ||
function WrapperComponent({ | ||
initialValue, | ||
getInitialValueInEffect = true, | ||
}: { | ||
initialValue?: 'light' | 'dark'; | ||
getInitialValueInEffect?: boolean; | ||
}) { | ||
const colorScheme = useColorScheme(initialValue, { | ||
getInitialValueInEffect, | ||
}); | ||
trace(colorScheme); | ||
return <>{colorScheme}</>; | ||
} | ||
|
||
it('correctly returns initial dark state without useEffect', async () => { | ||
render(<WrapperComponent initialValue="dark" getInitialValueInEffect={false} />); | ||
expect(trace).toHaveBeenCalledTimes(2); | ||
expect(trace.mock.calls[0][0]).toBe('dark'); | ||
expect(trace.mock.calls[1][0]).toBe('light'); | ||
}); | ||
it('correctly returns initial dark state state without useEffect', async () => { | ||
window.matchMedia = mockmatchMedia; | ||
render(<WrapperComponent initialValue="dark" getInitialValueInEffect={false} />); | ||
expect(trace).toHaveBeenCalledTimes(1); | ||
expect(trace.mock.calls[0][0]).toBe('dark'); | ||
}); | ||
|
||
it('correctly returns initial light state with useEffect', async () => { | ||
render(<WrapperComponent initialValue="dark" getInitialValueInEffect />); | ||
expect(trace).toHaveBeenCalledTimes(2); | ||
expect(trace.mock.calls[0][0]).toBe('dark'); | ||
expect(trace.mock.calls[1][0]).toBe('light'); | ||
}); | ||
it('correctly returns initial dark state with useEffect', async () => { | ||
window.matchMedia = mockmatchMedia; | ||
render(<WrapperComponent initialValue="dark" getInitialValueInEffect />); | ||
expect(trace).toHaveBeenCalledTimes(1); | ||
expect(trace.mock.calls[0][0]).toBe('dark'); | ||
}); | ||
it('correctly returns initial light state with deafult props', async () => { | ||
render(<WrapperComponent />); | ||
expect(trace).toHaveBeenCalledTimes(1); | ||
expect(trace.mock.calls[0][0]).toBe('light'); | ||
}); | ||
it('correctly returns initial dark state with deafult props', async () => { | ||
window.matchMedia = mockmatchMedia; | ||
render(<WrapperComponent />); | ||
expect(trace).toHaveBeenCalledTimes(2); | ||
expect(trace.mock.calls[0][0]).toBe('light'); | ||
expect(trace.mock.calls[1][0]).toBe('dark'); | ||
}); | ||
}); |
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