-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: storybook and msw for new shopper hooks
- Loading branch information
Showing
21 changed files
with
3,811 additions
and
102 deletions.
There are no files selected for viewing
26 changes: 26 additions & 0 deletions
26
packages/react-shopper-hooks/.storybook/elasticpath-context.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { QueryClient } from "@tanstack/react-query" | ||
import React from "react" | ||
import { ElasticPathProvider, ElasticPathProviderProps } from "../src" | ||
|
||
const queryClient = new QueryClient({ | ||
defaultOptions: { | ||
queries: { | ||
refetchOnWindowFocus: false, | ||
staleTime: Infinity, | ||
retry: 1, | ||
}, | ||
}, | ||
}) | ||
|
||
export default function DefaultElasticPathProvider( | ||
props: Omit<ElasticPathProviderProps, "queryClientProviderProps">, | ||
) { | ||
return ( | ||
<ElasticPathProvider | ||
{...props} | ||
clientId="123" | ||
host="shopper-mock.com" | ||
queryClientProviderProps={{ client: queryClient }} | ||
/> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import type { StorybookConfig } from "@storybook/react-vite" | ||
|
||
import { join, dirname } from "path" | ||
|
||
/** | ||
* This function is used to resolve the absolute path of a package. | ||
* It is needed in projects that use Yarn PnP or are set up within a monorepo. | ||
*/ | ||
function getAbsolutePath(value: string): any { | ||
return dirname(require.resolve(join(value, "package.json"))) | ||
} | ||
const config: StorybookConfig = { | ||
stories: [ | ||
"../stories/**/*.mdx", | ||
"../stories/**/*.stories.@(js|jsx|mjs|ts|tsx)", | ||
], | ||
addons: [ | ||
getAbsolutePath("@storybook/addon-links"), | ||
getAbsolutePath("@storybook/addon-essentials"), | ||
getAbsolutePath("@storybook/addon-interactions"), | ||
], | ||
framework: { | ||
name: getAbsolutePath("@storybook/react-vite"), | ||
options: {}, | ||
}, | ||
docs: { | ||
autodocs: "tag", | ||
}, | ||
typescript: { | ||
check: true, | ||
}, | ||
} | ||
export default config |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import type { Preview } from "@storybook/react" | ||
import DefaultElasticPathProvider from "./elasticpath-context" | ||
import React from "react" | ||
import { initialize, mswDecorator, mswLoader } from "msw-storybook-addon" | ||
import { shopperHandlers } from "../mocks/handlers" | ||
|
||
initialize() | ||
|
||
const preview: Preview = { | ||
parameters: { | ||
actions: { argTypesRegex: "^on[A-Z].*" }, | ||
msw: { | ||
handlers: [...shopperHandlers], | ||
}, | ||
controls: { | ||
matchers: { | ||
color: /(background|color)$/i, | ||
date: /Date$/i, | ||
}, | ||
}, | ||
}, | ||
loaders: [mswLoader], | ||
decorators: [ | ||
mswDecorator, | ||
(Story) => ( | ||
<DefaultElasticPathProvider> | ||
<Story /> | ||
</DefaultElasticPathProvider> | ||
), | ||
], | ||
} | ||
|
||
export default preview |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
{ | ||
"resources": { | ||
"cart": { | ||
"data": { | ||
"id": "655357c4-99b8-466b-96d4-28fc36b3e25c", | ||
"name": "Cart", | ||
"description": "", | ||
"type": "cart", | ||
"discount_settings": { | ||
"custom_discounts_enabled": false | ||
}, | ||
"links": { | ||
"self": "https://api.moltin.com/v2/carts/655357c4-99b8-466b-96d4-28fc36b3e25c" | ||
}, | ||
"meta": { | ||
"display_price": { | ||
"with_tax": { | ||
"amount": 1600, | ||
"currency": "USD", | ||
"formatted": "$16.00" | ||
}, | ||
"without_tax": { | ||
"amount": 1600, | ||
"currency": "USD", | ||
"formatted": "$16.00" | ||
}, | ||
"tax": { | ||
"amount": 0, | ||
"currency": "USD", | ||
"formatted": "$0.00" | ||
}, | ||
"discount": { | ||
"amount": 0, | ||
"currency": "USD", | ||
"formatted": "$0.00" | ||
}, | ||
"without_discount": { | ||
"amount": 1600, | ||
"currency": "USD", | ||
"formatted": "$16.00" | ||
}, | ||
"shipping": { | ||
"amount": 0, | ||
"currency": "USD", | ||
"formatted": "$0.00" | ||
} | ||
}, | ||
"timestamps": { | ||
"created_at": "2023-10-26T10:51:27Z", | ||
"updated_at": "2023-10-26T11:01:16Z", | ||
"expires_at": "2023-11-02T11:01:16Z" | ||
} | ||
}, | ||
"relationships": { | ||
"items": { | ||
"data": [ | ||
{ | ||
"type": "cart_item", | ||
"id": "2eda9f25-903e-4c22-8c42-0c04fbdf94ef" | ||
} | ||
] | ||
}, | ||
"customers": {} | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import data from "./fixtures.json" | ||
|
||
const resources = data["resources"] | ||
|
||
type Resources = typeof resources | ||
|
||
type ResourcesWithKey<Entity extends string, T> = { | ||
[K in keyof T]: { [_ in Entity]: K } & T[K] | ||
} | ||
|
||
type KeyedResources = ResourcesWithKey<"entity", Resources> | ||
|
||
export const fixtures = { | ||
get<Entity extends keyof Resources>( | ||
entity: Entity, | ||
): Omit<KeyedResources[Entity], "entity"> { | ||
return resources[entity as string] | ||
}, | ||
list<Entity extends keyof Resources>( | ||
entity: Entity, | ||
number = 2, | ||
): Omit<KeyedResources[Entity], "entity">[] { | ||
return Array(number) | ||
.fill(null) | ||
.map((_) => fixtures.get(entity)) | ||
}, | ||
} as const |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from "./shopper" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { rest } from "msw" | ||
import { fixtures } from "../data" | ||
// import { fixtures } from "../data" | ||
|
||
export const shopperHandlers = [ | ||
rest.post("https://shopper-mock.com/oauth/access_token", (req, res, ctx) => { | ||
return res( | ||
ctx.status(200), | ||
ctx.json({ | ||
identifier: "implicit", | ||
token_type: "Bearer", | ||
expires_in: 3600, | ||
expires: 9999999999, | ||
access_token: "mock-access-token-123", | ||
}), | ||
) | ||
}), | ||
rest.get("https://shopper-mock.com/v2/carts/:cartId", (req, res, ctx) => { | ||
return res(ctx.status(200), ctx.json(fixtures.get("cart"))) | ||
}), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import { setupServer } from "msw/node" | ||
import { shopperHandlers } from "./handlers" | ||
|
||
export const server = setupServer(...shopperHandlers) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.