Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pr/media query initialvalue #3085

Merged
merged 3 commits into from Dec 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 28 additions & 0 deletions src/mantine-hooks/src/use-color-scheme/use-color-scheme.story.tsx
@@ -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 src/mantine-hooks/src/use-color-scheme/use-color-scheme.test.tsx
@@ -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');
});
});
2 changes: 1 addition & 1 deletion src/mantine-hooks/src/use-media-query/use-media-query.ts
Expand Up @@ -40,7 +40,7 @@ export function useMediaQuery(
}
) {
const [matches, setMatches] = useState(
getInitialValueInEffect ? false : getInitialValue(query, initialValue)
getInitialValueInEffect ? initialValue : getInitialValue(query, initialValue)
);
const queryRef = useRef<MediaQueryList>();

Expand Down