Skip to content

Commit 82ee78d

Browse files
committed
feat: add storybook
add stories for several components replace some tests with stories or use portable stories with them
1 parent e18ad40 commit 82ee78d

34 files changed

Lines changed: 2652 additions & 840 deletions

File tree

.eslintrc.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,16 @@
2525
"import/no-cycle": "warn",
2626
"import/order": "off",
2727
"import/prefer-default-export": "warn",
28+
"import/no-extraneous-dependencies": ["error", {
29+
"devDependencies": [
30+
// these globs cannot be combined
31+
".storybook/**",
32+
"**/*.stories.*",
33+
"**/*.test.*",
34+
"**/test.*",
35+
"vite.config.*"
36+
]
37+
}],
2838
"max-len": [
2939
"off",
3040
{

.github/workflows/npm-test.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ jobs:
1313
- run: npm ci
1414
- run: npm run lint
1515
- run: npm run check-types
16+
- run: npx playwright install chromium
1617
- run: npm run test -- --reporter=default --reporter=junit --outputFile=coverage/junit.xml --coverage
1718
- name: Publish Unit Test Results
1819
uses: EnricoMi/publish-unit-test-result-action@v1.6

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,6 @@ Thumbs.db
4242
/stats.json
4343
/dist
4444
/dist\ copy
45-
.env.*.local
45+
.env.*.local
46+
*storybook.log
47+
storybook-static

.storybook/main.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { defineMain } from '@storybook/react-vite/node';
2+
3+
export default defineMain({
4+
"stories": [
5+
"../src/**/*.stories.tsx"
6+
],
7+
"addons": [
8+
"@storybook/addon-vitest",
9+
],
10+
"framework": "@storybook/react-vite",
11+
});

.storybook/preview.tsx

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import React from 'react';
2+
import ThemeProvider from '../src/theme';
3+
import { initialize, mswLoader, getWorker } from 'msw-storybook-addon'
4+
import { QueryClient, QueryClientProvider } from 'react-query';
5+
import '../src/static/graphkb-env-config';
6+
import { HttpResponse, http } from 'msw';
7+
import { QueryBody } from '../src/components/types';
8+
import { definePreview } from '@storybook/react-vite';
9+
import addonTest from '@storybook/addon-vitest';
10+
import { MswParameters } from 'msw-storybook-addon'
11+
import { PreviewAddon } from 'storybook/internal/csf';
12+
13+
export function mockQueryHandler(resolver: (body: QueryBody) => HttpResponse<any>) {
14+
return http.post(`${window._env_.API_BASE_URL}/api/query`, async ({ request }) => {
15+
const body = await request.clone().json();
16+
return resolver(body as any);
17+
})
18+
}
19+
20+
// see https://github.com/mswjs/msw-storybook-addon/issues/180
21+
try {
22+
getWorker();
23+
} catch (_) {
24+
initialize({
25+
onUnhandledRequest: ({ url }, print) => {
26+
if (url.startsWith(window._env_.API_BASE_URL)) {
27+
print.error();
28+
}
29+
},
30+
quiet: import.meta.env.CI === 'true',
31+
})
32+
}
33+
34+
type MswTypes = {
35+
parameters: MswParameters
36+
}
37+
38+
const addonMsw = () => ({}) as PreviewAddon<MswTypes>;
39+
40+
export default definePreview({
41+
addons: [addonTest(), addonMsw()],
42+
parameters: {
43+
controls: {
44+
matchers: {
45+
color: /(background|color)$/i,
46+
date: /Date$/i,
47+
},
48+
},
49+
},
50+
decorators: [
51+
(Story) => <ThemeProvider><Story /></ThemeProvider>,
52+
(Story, context) => {
53+
return <QueryClientProvider client={context.loaded.queryClient}><Story /></QueryClientProvider>
54+
}
55+
],
56+
loaders: [mswLoader, () => ({ queryClient: new QueryClient({ defaultOptions: { queries: { retry: false }}})})],
57+
});

config/jest/windowEnvMock.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
1-
import { expect, afterEach } from 'vitest';
1+
import { expect, afterEach, beforeAll } from 'vitest';
22
import { cleanup } from '@testing-library/react';
3+
import { setProjectAnnotations } from '@storybook/react-vite';
4+
import * as previewAnnotations from '../../.storybook/preview';
5+
6+
const annotations = setProjectAnnotations([previewAnnotations]);
7+
8+
// Run Storybook's beforeAll hook
9+
beforeAll(annotations.beforeAll);
10+
311
// necessary for extend-expect to work until upgrade to v6
4-
global.expect = expect;
512
window.expect = expect;
613

714
// Automatically clean up the DOM after each test run

0 commit comments

Comments
 (0)