-
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: hooks for account queries with storybook
- Loading branch information
Showing
22 changed files
with
1,105 additions
and
7 deletions.
There are no files selected for viewing
29 changes: 29 additions & 0 deletions
29
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,29 @@ | ||
import { QueryClient } from "@tanstack/react-query" | ||
import React from "react" | ||
import { | ||
ElasticPathProvider, | ||
ElasticPathProviderProps, | ||
} from "../src/elasticpath/elasticpath" | ||
|
||
const queryClient = new QueryClient({ | ||
defaultOptions: { | ||
queries: { | ||
refetchOnWindowFocus: false, | ||
staleTime: Infinity, | ||
retry: 1, | ||
}, | ||
}, | ||
}) | ||
|
||
export default function DefaultElasticPathProvider( | ||
props: Omit<ElasticPathProviderProps, "queryClientProviderProps">, | ||
) { | ||
return ( | ||
<ElasticPathProvider | ||
{...props} | ||
clientId="w4QC7WVmxpwZfLaujDNMNJkgTcMmL17Lh3o9CdX3Ou" | ||
// 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,32 @@ | ||
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: [ | ||
"../src/stories/**/*.mdx", | ||
"../src/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,24 @@ | ||
import type { Preview } from "@storybook/react" | ||
import DefaultElasticPathProvider from "./elasticpath-context" | ||
import React from "react" | ||
|
||
const preview: Preview = { | ||
parameters: { | ||
actions: { argTypesRegex: "^on[A-Z].*" }, | ||
controls: { | ||
matchers: { | ||
color: /(background|color)$/i, | ||
date: /Date$/i, | ||
}, | ||
}, | ||
}, | ||
decorators: [ | ||
(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,147 @@ | ||
{ | ||
"resources": { | ||
"cart": { | ||
"data": [ | ||
{ | ||
"id": "2eda9f25-903e-4c22-8c42-0c04fbdf94ef", | ||
"type": "cart_item", | ||
"product_id": "ec02203d-34a3-43d6-8530-c98ebce55e67", | ||
"name": "Simple T-Shirt - Blue", | ||
"description": "The simple t-shirt is a light, breathable, and easy to wear t-shirt. It is made of a soft cotton blend and has a ribbed crew neck. The simple t-shirt is perfect for layering or wearing alone. It can be worn with any bottoms and can be easily dressed up or down...", | ||
"sku": "TSHIRT-01BlueSMLong", | ||
"slug": "simple-t-shirtBlueSMLong", | ||
"image": { | ||
"mime_type": "image/jpeg", | ||
"file_name": "womens-tshirts.jpeg", | ||
"href": "https://files-eu.epusercontent.com/856eeae6-45ea-453f-ab75-e53e84bf3c61/1fa7be8b-bdcf-43a0-8748-33e549d2c03e.jpeg" | ||
}, | ||
"quantity": 1, | ||
"manage_stock": false, | ||
"unit_price": { | ||
"amount": 1600, | ||
"currency": "USD", | ||
"includes_tax": false | ||
}, | ||
"value": { | ||
"amount": 1600, | ||
"currency": "USD", | ||
"includes_tax": false | ||
}, | ||
"links": { | ||
"product": "https://api.moltin.com/v2/products/ec02203d-34a3-43d6-8530-c98ebce55e67" | ||
}, | ||
"meta": { | ||
"display_price": { | ||
"with_tax": { | ||
"unit": { | ||
"amount": 1600, | ||
"currency": "USD", | ||
"formatted": "$16.00" | ||
}, | ||
"value": { | ||
"amount": 1600, | ||
"currency": "USD", | ||
"formatted": "$16.00" | ||
} | ||
}, | ||
"without_tax": { | ||
"unit": { | ||
"amount": 1600, | ||
"currency": "USD", | ||
"formatted": "$16.00" | ||
}, | ||
"value": { | ||
"amount": 1600, | ||
"currency": "USD", | ||
"formatted": "$16.00" | ||
} | ||
}, | ||
"tax": { | ||
"unit": { | ||
"amount": 0, | ||
"currency": "USD", | ||
"formatted": "$0.00" | ||
}, | ||
"value": { | ||
"amount": 0, | ||
"currency": "USD", | ||
"formatted": "$0.00" | ||
} | ||
}, | ||
"discount": { | ||
"unit": { | ||
"amount": 0, | ||
"currency": "USD", | ||
"formatted": "$0.00" | ||
}, | ||
"value": { | ||
"amount": 0, | ||
"currency": "USD", | ||
"formatted": "$0.00" | ||
} | ||
}, | ||
"without_discount": { | ||
"unit": { | ||
"amount": 1600, | ||
"currency": "USD", | ||
"formatted": "$16.00" | ||
}, | ||
"value": { | ||
"amount": 1600, | ||
"currency": "USD", | ||
"formatted": "$16.00" | ||
} | ||
} | ||
}, | ||
"timestamps": { | ||
"created_at": "2023-10-26T11:01:16Z", | ||
"updated_at": "2023-10-26T11:01:16Z" | ||
} | ||
}, | ||
"catalog_id": "e4c2d061-3712-408d-bc2c-cfebd0bd104f", | ||
"catalog_source": "pim" | ||
} | ||
], | ||
"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" | ||
} | ||
}, | ||
"included": {} | ||
} | ||
} | ||
} |
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,23 @@ | ||
import { rest } from "msw" | ||
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/items", | ||
(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/shopper" | ||
|
||
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.