Skip to content

Commit

Permalink
add test for scheme model
Browse files Browse the repository at this point in the history
  • Loading branch information
Bricks666 committed Aug 25, 2024
1 parent 31867d7 commit bd82f2b
Show file tree
Hide file tree
Showing 8 changed files with 191 additions and 10 deletions.
15 changes: 10 additions & 5 deletions configs/tests/setup.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { expect, afterEach, beforeAll, afterAll, vi } from 'vitest';
import { cleanup } from '@testing-library/react';
import * as matchers from '@testing-library/jest-dom/matchers';
import { server } from '~/test-utils';
import { matchMedia, MediaQueryListEvent } from 'mock-match-media';
import { server, cleanup as cleanupMatchMedia } from '~/test-utils';

expect.extend(matchers);

Expand All @@ -16,18 +17,22 @@ vi.mock('@farfetched/core', async (importOriginal) => {
};
});

afterEach(() => {
cleanup();
});

beforeAll(() => {
server.listen();
});

afterEach(() => {
cleanup();
cleanupMatchMedia();
server.resetHandlers();
});

afterAll(() => {
server.close();
});

window.MediaQueryListEvent = MediaQueryListEvent;
window.matchMedia = (...args) => {
console.log(args);
return matchMedia(...args);
};
31 changes: 31 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 @@ -78,6 +78,7 @@
"husky": "^8.0.3",
"jsdom": "^24.1.0",
"lint-staged": "^15.0.2",
"mock-match-media": "^0.4.3",
"msw": "^2.3.1",
"typed-css-modules": "^0.8.0",
"typescript": "^5.2.2",
Expand Down
139 changes: 139 additions & 0 deletions src/shared/models/scheme.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
import { useColorScheme } from '@mui/material';
import { ColorSchemeContextValue } from '@mui/system';
import { beforeEach, describe, expect, test } from 'vitest';

import { started } from './app';
import {
$biScheme,
$scheme,
colorSchemeChanged,
useSyncScheme
} from './scheme';

import {
RenderHookResult,
Scope,
act,
allSettled,
fork,
renderHook,
setMedia
} from '~/test-utils';

describe('shared/models/scheme', () => {
const schemes = ['light', 'system', 'dark'] as const;
let scope: Scope;

beforeEach(async () => {
setMedia({
'prefers-color-scheme': 'light',
});

scope = fork();

await allSettled(started, { scope, });
});

test('should has system value by default', async () => {
expect(scope.getState($scheme)).toBe('system');
});

describe('change', () => {
test.each(schemes)(
'should change color scheme on colorSchemeChanged call with %s',
async (scheme) => {
await allSettled(colorSchemeChanged, { scope, params: scheme, });

expect(scope.getState($scheme)).toBe(scheme);
}
);

test('should keep old value if colorSchemeChanged was called with empty one', async () => {
const currentScheme = scope.getState($scheme);

await allSettled(colorSchemeChanged, { scope, params: null, });

expect(scope.getState($scheme)).toBe(currentScheme);
});
});

describe('bivalue scheme', () => {
test('should convert light scheme to light in bivalue one', async () => {
await allSettled(colorSchemeChanged, { scope, params: 'light', });

expect(scope.getState($biScheme)).toBe('light');
});

test('should convert light scheme to dark in bivalue one', async () => {
await allSettled(colorSchemeChanged, { scope, params: 'dark', });

expect(scope.getState($biScheme)).toBe('dark');
});

test.skip.each(['dark', 'light'])(
'should convert auto scheme to %s in bivalue one if prefers-ciolor-scheme=%s',
async (scheme) => {
setMedia({
'prefers-color-scheme': scheme,
});

await allSettled(colorSchemeChanged, { scope, params: 'system', });

expect(scope.getState($biScheme)).toBe(scheme);
}
);
});

describe('persist', () => {
const key = 'abc-color-scheme';

test.each(schemes)(
'should save current scheme into local storage',
async (scheme) => {
// To initiate reading from local storage
window.dispatchEvent(new StorageEvent('storage', { key, }));

await allSettled(colorSchemeChanged, { scope, params: scheme, });

expect(localStorage.getItem(key)).toBe(JSON.stringify(scheme));
}
);

test.each(schemes)(
'should load saved %s scheme from local storage',
(scheme) => {
localStorage.setItem(key, JSON.stringify(scheme));

window.dispatchEvent(new StorageEvent('storage', { key, }));

expect(scope.getState($scheme)).toBe(scheme);
}
);
});

describe('useSyncScheme', () => {
let wrapper: RenderHookResult<ColorSchemeContextValue<string>, void>;

const createComponent = () => {
wrapper = renderHook(
() => {
useSyncScheme();
return useColorScheme();
},
{ scope, }
);
};

beforeEach(async () => {
await act(() => createComponent());
});

test('should sync biScheme with mui scheme', async () => {
expect(wrapper.result.current.mode).toBe(scope.getState($biScheme));

await allSettled(colorSchemeChanged, { scope, params: 'dark', });

expect(wrapper.result.current.mode).toBe(scope.getState($biScheme));
});
});
});
2 changes: 1 addition & 1 deletion src/shared/models/scheme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export const useSyncScheme = () => {

persist({
store: $scheme,
key: 'bt-color-scheme',
key: 'abc-color-scheme',
});

sample({
Expand Down
2 changes: 2 additions & 0 deletions test-utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/* eslint-disable import/no-extraneous-dependencies */
export * from './fixtures';
export * from './mock-server';
export * from './utils';
export { setMedia, MediaQueryListEvent } from 'mock-match-media';
9 changes: 6 additions & 3 deletions test-utils/utils/render.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/* eslint-disable import/no-extraneous-dependencies */
import { Experimental_CssVarsProvider as CssVarsProvider } from '@mui/material';
import { LocalizationProvider } from '@mui/x-date-pickers';
import { AdapterDayjs } from '@mui/x-date-pickers/AdapterDayjs';
import {
Expand Down Expand Up @@ -42,9 +43,11 @@ const createAllProviders = (
return (
<StoreProvider value={scope}>
<RouterProvider router={router}>
<LocalizationProvider dateAdapter={AdapterDayjs}>
<Wrapper>{children}</Wrapper>
</LocalizationProvider>
<CssVarsProvider>
<LocalizationProvider dateAdapter={AdapterDayjs}>
<Wrapper>{children}</Wrapper>
</LocalizationProvider>
</CssVarsProvider>
</RouterProvider>
</StoreProvider>
);
Expand Down
2 changes: 1 addition & 1 deletion vitest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { defineConfig } from 'vitest/config';
export default defineConfig({
test: {
environment: 'jsdom',
setupFiles: ['./configs/tests/setup.ts'],
setupFiles: [path.resolve(__dirname, './configs/tests/setup.ts')],
include: ['./src/**/*.spec.{ts,tsx}'],
clearMocks: true,
globals: true,
Expand Down

0 comments on commit bd82f2b

Please sign in to comment.