Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
pmelab committed Nov 27, 2023
1 parent 80c3a73 commit 91d7081
Show file tree
Hide file tree
Showing 9 changed files with 195 additions and 575 deletions.
7 changes: 6 additions & 1 deletion packages/schema/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ import {
} from './generated/index.js';
import { createOperator } from './operators.js';

export { registerOperator, drupalOperator } from './operators.js';
export {
registerOperator,
drupalOperator,
clearRegistry,
} from './operators.js';

export * from './generated/index.js';
export * from '@amazeelabs/scalars';
Expand All @@ -23,5 +27,6 @@ export function useOperation<TOperation extends AnyOperationId>(
suspense: false,
},
);
console.log(result.data);
return result.data;
}
24 changes: 10 additions & 14 deletions packages/ui/.storybook/preview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,9 @@ import '../tailwind.css';

import { Decorator } from '@storybook/react';
import { IntlProvider } from '../src/utils/intl';
import {
LocationProvider,
drupalOperator,
registerOperator,
} from '@custom/schema';
import { LocationProvider } from '@custom/schema';
import React from 'react';
import { initialize, mswLoader } from 'msw-storybook-addon';
import { clearRegistry, registerOperator } from '@custom/schema';

// Every story is wrapped in an IntlProvider by default.
const IntlDecorator: Decorator = (Story) => (
Expand All @@ -17,7 +13,13 @@ const IntlDecorator: Decorator = (Story) => (
</IntlProvider>
);

registerOperator(drupalOperator('/graphql'));
const OperatorDecorator: Decorator = (Story, ctx) => {
clearRegistry();
ctx.parameters.operators?.forEach(([id, impl]) =>
registerOperator((_, vars) => impl(vars), id),
);
return <Story />;
};

const LocationDecorator: Decorator = (Story, ctx) => {
return (
Expand All @@ -31,10 +33,4 @@ export const parameters = {
chromatic: { viewports: [320, 840, 1440] },
};

initialize({
onUnhandledRequest: 'bypass',
});

export const loaders = [mswLoader];

export const decorators = [LocationDecorator, IntlDecorator];
export const decorators = [LocationDecorator, IntlDecorator, OperatorDecorator];
9 changes: 2 additions & 7 deletions packages/ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,6 @@
"eslint-plugin-storybook": "^0.6.12",
"eslint-plugin-tailwindcss": "^3.13.0",
"happy-dom": "^9.20.3",
"msw": "^1.2.2",
"msw-storybook-addon": "^1.8.0",
"nyc": "^15.1.0",
"postcss": "^8.4.24",
"postcss-cli": "^10.1.0",
Expand All @@ -107,11 +105,8 @@
"tailwindcss": "^3.3.2",
"typescript": "^5.1.6",
"vite": "^4.4.9",
"vite-imagetools": "^5.0.8",
"vite-plugin-turbosnap": "^1.0.2",
"vitest": "^0.34.2",
"vite-imagetools": "^5.0.8"
},
"msw": {
"workerDirectory": "static/stories"
"vitest": "^0.34.2"
}
}
96 changes: 39 additions & 57 deletions packages/ui/src/components/Organisms/ContentHub.stories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
import Landscape from '@stories/landscape.jpg?as=metadata';
import Portrait from '@stories/portrait.jpg?as=metadata';
import { Meta, StoryObj } from '@storybook/react';
import { rest } from 'msw';

import { image } from '../../helpers/image';
import { ContentHub } from './ContentHub';
Expand All @@ -17,21 +16,9 @@ export default {

export const Empty = {
parameters: {
msw: {
handlers: {
contentHub: [
rest.get('/graphql', (req, res, ctx) => {
const result: ContentHubQuery = {
contentHub: {
total: 0,
items: [],
},
};
return res(ctx.json({ data: result }));
}),
],
},
},
operators: [
[ContentHubQuery, () => ({ contentHub: { total: 0, items: [] } })],
],
},
} satisfies StoryObj<typeof ContentHub>;

Expand All @@ -40,46 +27,41 @@ export const WithResults = {
pageSize: 6,
},
parameters: {
msw: {
handlers: {
contentHub: [
rest.get('/graphql', (req, res, ctx) => {
const items = [...Array(82).keys()].map(
(i) =>
({
path: `/item/${i + 1}` as Url,
title: `${i % 3 === 2 ? 'Article' : 'Story'} #${i + 1}`,
teaserImage:
i % 3 === 1
? undefined
: {
alt: `Image for item #${i + 1}`,
source: image(i % 2 === 0 ? Landscape : Portrait, {
width: 400,
height: 300,
}),
},
} satisfies ContentHubResultItemFragment),
);
const vars = JSON.parse(
req.url.searchParams.get('variables') || '{}',
);
const filtered = items.filter(
(item) => !vars.query || item.title.includes(vars.query),
);
const result: ContentHubQuery = {
contentHub: {
total: filtered.length,
items: filtered.slice(
vars.pagination.offset,
vars.pagination.offset + vars.pagination.limit,
),
},
};
return res(ctx.json({ data: result }));
}),
],
},
},
operators: [
[
ContentHubQuery,
(vars) => {
const items = [...Array(82).keys()].map(
(i) =>
({
path: `/item/${i + 1}` as Url,
title: `${i % 3 === 2 ? 'Article' : 'Story'} #${i + 1}`,
teaserImage:
i % 3 === 1
? undefined
: {
alt: `Image for item #${i + 1}`,
source: image(i % 2 === 0 ? Landscape : Portrait, {
width: 400,
height: 300,
}),
},
} satisfies ContentHubResultItemFragment),
);
const filtered = items.filter(
(item) => !vars.query || item.title.includes(vars.query),
);
return {
contentHub: {
total: filtered.length,
items: filtered.slice(
vars.pagination.offset,
vars.pagination.offset + vars.pagination.limit,
),
},
};
},
],
],
},
} satisfies StoryObj<typeof ContentHub>;
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,3 @@ declare module '*as=metadata' {
const content: Parameters<typeof buildResponsiveImage>[1];
export default content;
}

interface ImportMetaEnv {
readonly VITE_CLOUDINARY_CLOUDNAME?: string;
}

interface ImportMeta {
readonly env: ImportMetaEnv;
}
23 changes: 23 additions & 0 deletions packages/ui/src/helpers/parameters.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import type {
AnyOperationId,
LocationProvider,
OperationResult,
OperationVariables,
} from '@custom/schema';
import { ComponentProps } from 'react';

type OperationMock<TOperation extends AnyOperationId> = (
vars: OperationVariables<TOperation>,
) => OperationResult<TOperation> | Promise<OperationResult<TOperation>>;

type OperationMockTuple<TOperation extends AnyOperationId> = [
TOperation,
OperationMock<TOperation>,
];

declare module '@storybook/types' {
interface Parameters {
operators?: Array<OperationMockTuple>;
location?: ComponentProps<typeof LocationProvider>['currentLocation'];
}
}
2 changes: 0 additions & 2 deletions packages/ui/static/stories/.gitignore

This file was deleted.

Loading

0 comments on commit 91d7081

Please sign in to comment.