Skip to content

Commit

Permalink
Merge pull request #102 from acelaya-forks/feature/use-parsed-query-hook
Browse files Browse the repository at this point in the history
Add useParsedQuery hook
  • Loading branch information
acelaya committed Dec 15, 2023
2 parents 65a8f7b + b4998e2 commit 5b0dcc7
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 5 deletions.
5 changes: 3 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org).

## [Unreleased]
## [0.4.1] - 2023-12-15
### Added
* *Nothing*
* Add `useParsedQuery` hook
* Add `getSystemPreferredTheme` helper function

### Changed
* Replace `classnames` package with `clsx`
Expand Down
10 changes: 10 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
"axe-core": "^4.8.2",
"bootstrap": "5.2.3",
"eslint": "^8.55.0",
"history": "^5.3.0",
"jsdom": "^23.0.1",
"resize-observer-polyfill": "^1.5.1",
"sass": "^1.69.5",
Expand Down
9 changes: 8 additions & 1 deletion src/hooks/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { useCallback, useRef, useState } from 'react';
import { useCallback, useMemo, useRef, useState } from 'react';
import { useLocation } from 'react-router-dom';
import { v4 as uuid } from 'uuid';
import { parseQuery } from '../utils';

type ToggleResult = [boolean, () => void, () => void, () => void];

Expand Down Expand Up @@ -44,3 +46,8 @@ export const useDomId = (): string => {
};

export const useElementRef = <T>() => useRef<T | null>(null);

export const useParsedQuery = <T>(): T => {
const { search } = useLocation();
return useMemo(() => parseQuery<T>(search), [search]);
};
4 changes: 4 additions & 0 deletions src/theme/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,7 @@ export type Theme = 'dark' | 'light';
export const changeThemeInMarkup = (theme: Theme) => document.querySelector('html')?.setAttribute('data-theme', theme);

export const isDarkThemeEnabled = (): boolean => document.querySelector('html')?.getAttribute('data-theme') === 'dark';

export const getSystemPreferredTheme = (_matchMedia = window.matchMedia.bind(window)): Theme => (
_matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light'
);
36 changes: 34 additions & 2 deletions test/hooks/index.test.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { screen } from '@testing-library/react';
import { useTimeoutToggle } from '../../src';
import { render, screen } from '@testing-library/react';
import { createMemoryHistory } from 'history';
import { Router } from 'react-router-dom';
import { useParsedQuery, useTimeoutToggle } from '../../src';
import { renderWithEvents } from '../__helpers__/setUpTest';

describe('useTimeoutToggle', () => {
Expand Down Expand Up @@ -40,3 +42,33 @@ describe('useTimeoutToggle', () => {
expect(clearTimeout).toHaveBeenCalledOnce();
});
});

describe('useParsedQuery', () => {
const FakeComponent = () => {
const { foo, bar } = useParsedQuery<{ foo?: string; bar?: number }>();

return (
<div>
<span data-testid="foo">{foo}</span>
<span data-testid="bar">{bar}</span>
</div>
);
};
const setUp = (search: string) => {
const history = createMemoryHistory();
history.push({ search });

render(
<Router location={history.location} navigator={history}>
<FakeComponent />
</Router>,
);
};

it('parses query as expected', () => {
setUp('foo=hello&bar=123');

expect(screen.getByTestId('foo')).toHaveTextContent('hello');
expect(screen.getByTestId('bar')).toHaveTextContent('123');
});
});
13 changes: 13 additions & 0 deletions test/theme/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { getSystemPreferredTheme } from '../../src';

describe('getSystemPreferredTheme', () => {
it.each([
{ matches: true, expectedTheme: 'dark' },
{ matches: false, expectedTheme: 'light' },
])('returns expected theme', ({ matches, expectedTheme }) => {
const matchMedia = vi.fn().mockReturnValue({ matches });

expect(getSystemPreferredTheme(matchMedia)).toEqual(expectedTheme);
expect(matchMedia).toHaveBeenCalledWith('(prefers-color-scheme: dark)');
});
});

0 comments on commit 5b0dcc7

Please sign in to comment.