Skip to content

Commit

Permalink
feat: hooks for account queries with storybook
Browse files Browse the repository at this point in the history
  • Loading branch information
field123 committed Nov 27, 2023
1 parent 075262a commit b54ccbf
Show file tree
Hide file tree
Showing 22 changed files with 1,105 additions and 7 deletions.
29 changes: 29 additions & 0 deletions packages/react-shopper-hooks/.storybook/elasticpath-context.tsx
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 }}
/>
)
}
32 changes: 32 additions & 0 deletions packages/react-shopper-hooks/.storybook/main.ts
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
24 changes: 24 additions & 0 deletions packages/react-shopper-hooks/.storybook/preview.tsx
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
147 changes: 147 additions & 0 deletions packages/react-shopper-hooks/mocks/data/fixtures.json
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": {}
}
}
}
27 changes: 27 additions & 0 deletions packages/react-shopper-hooks/mocks/data/index.ts
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
23 changes: 23 additions & 0 deletions packages/react-shopper-hooks/mocks/handlers/shopper.ts
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")))
},
),
]
4 changes: 4 additions & 0 deletions packages/react-shopper-hooks/mocks/server.ts
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)
23 changes: 20 additions & 3 deletions packages/react-shopper-hooks/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
"dev": "vite",
"build": "tsup",
"clean": "rimraf ./dist",
"test": "vitest --run"
"test": "vitest --run",
"storybook": "storybook dev -p 6006",
"build-storybook": "storybook build"
},
"type": "module",
"types": "./dist/index.d.ts",
Expand All @@ -30,17 +32,28 @@
],
"devDependencies": {
"@moltin/sdk": "^27.0.0",
"@storybook/addon-essentials": "^7.5.3",
"@storybook/addon-interactions": "^7.5.3",
"@storybook/addon-links": "^7.5.3",
"@storybook/blocks": "^7.5.3",
"@storybook/react": "^7.5.3",
"@storybook/react-vite": "^7.5.3",
"@storybook/testing-library": "^0.2.2",
"@tanstack/react-query": "^5.8.4",
"@types/js-cookie": "^3.0.6",
"@types/react": "^18.2.33",
"@types/react-dom": "^18.2.14",
"@vitejs/plugin-react": "^2.2.0",
"esbuild-plugin-file-path-extensions": "^1.0.0",
"msw": "^1.2.1",
"msw-storybook-addon": "^1.10.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-json-view": "^1.21.3",
"rimraf": "^5.0.5",
"storybook": "^7.5.3",
"tsup": "^8.0.1",
"typescript": "^4.8.4",
"typescript": "^5.3.2",
"vite": "^3.2.3",
"vite-plugin-dts": "^1.7.0",
"vitest": "^0.31.1"
Expand All @@ -57,6 +70,10 @@
"dependencies": {
"@elasticpath/shopper-common": "workspace:^0.2.1",
"js-cookie": "^3.0.5",
"jwt-decode": "^3.1.2",
"rxjs": "7.5.7"
},
"msw": {
"workerDirectory": "public"
}
}
}
Loading

0 comments on commit b54ccbf

Please sign in to comment.