Skip to content

Commit

Permalink
feat: presets selected feature add (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
minsgy authored Jun 9, 2024
2 parents 8da66fd + 6b12fbd commit 618d63f
Show file tree
Hide file tree
Showing 16 changed files with 249 additions and 106 deletions.
12 changes: 8 additions & 4 deletions example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,27 @@
"preview": "vite preview"
},
"devDependencies": {
"msw-devtools": "link:../",
"@types/node": "20.12.7",
"@types/react": "18.2.14",
"@types/react-dom": "18.2.6",
"@types/node": "20.12.7",
"@typescript-eslint/eslint-plugin": "^7.2.0",
"@typescript-eslint/parser": "^7.2.0",
"@vitejs/plugin-react": "^4.2.1",
"react": "18.2.0",
"react-dom": "18.2.0",
"eslint": "^8.57.0",
"eslint-plugin-react-hooks": "^4.6.0",
"eslint-plugin-react-refresh": "^0.4.6",
"msw": "2.2.13",
"msw-devtools": "link:../",
"react": "18.2.0",
"react-dom": "18.2.0",
"typescript": "^5.2.2",
"vite": "^5.2.0"
},
"msw": {
"workerDirectory": "public"
},
"dependencies": {
"@tanstack/react-query": "^5.40.1",
"react-json-view-lite": "^1.4.0"
}
}
32 changes: 32 additions & 0 deletions example/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

57 changes: 37 additions & 20 deletions example/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,31 +1,48 @@
import { useQuery } from "@tanstack/react-query"
import { JsonView, darkStyles } from "react-json-view-lite"
import "./App.css"
import { useState } from "react"

function App() {
const testFetchVite = async () => {
try {
const response = await fetch("https://api.example.com/user", {
method: "GET",
headers: {
"Content-Type": "application/json",
},
})
const data = await response.json()
console.log(data)
} catch (error) {
console.error(error)
}
}
const [error, setError] = useState<string | null>(null)
const { data } = useQuery({
queryKey: ["test"],
queryFn: async () => {
try {
const response = await fetch("https://json-test.com/v1/user")
setError(null)
return response.json()
} catch (e) {
const error = e as Error
setError(error.message)
}
},
gcTime: 0,
staleTime: 0,
})

return (
<>
<div></div>
<h1>Vite + React</h1>
<div className="card">
<button onClick={() => testFetchVite()}>Test Fetch Vite</button>
<h1>MSW Devtools</h1>
<div
style={{
textAlign: "left",
}}
>
<JsonView style={darkStyles} data={data} />
{error && (
<p
style={{
color: "red",
fontSize: "1.2rem",
fontWeight: "bold",
}}
>
{error}
</p>
)}
</div>
<p className="read-the-docs">
Click on the Vite and React logos to learn more
</p>
</>
)
}
Expand Down
18 changes: 15 additions & 3 deletions example/src/main.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { MSWDevtools } from "msw-devtools"
import "msw-devtools/styles"
import "react-json-view-lite/dist/index.css"

import React from "react"
import ReactDOM from "react-dom/client"
import App from "./App.tsx"
import { QueryClient, QueryClientProvider } from "@tanstack/react-query"
import "./index.css"

const isDev = process.env.NODE_ENV === "development"
Expand All @@ -17,12 +19,22 @@ const prepareWorker = async () => {
return undefined
}

const queryClient = new QueryClient()
prepareWorker().then((worker) =>
ReactDOM.createRoot(document.getElementById("root")!).render(
<React.StrictMode>
<MSWDevtools worker={worker} isEnabled={isDev}>
<App />
</MSWDevtools>
<QueryClientProvider client={queryClient}>
<MSWDevtools
worker={worker}
isEnabled={isDev}
onRouteUpdate={() => {
console.log("[MSW Devtools] Route updated")
queryClient.resetQueries()
}}
>
<App />
</MSWDevtools>
</QueryClientProvider>
</React.StrictMode>
)
)
6 changes: 3 additions & 3 deletions example/src/mocks/browser.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// src/mocks/browser.js
import { setupWorker } from "msw/browser";
import { handlers } from "./handlers";
import { setupWorker } from "msw/browser"
import { handlers } from "./handlers"

export const worker = setupWorker(...handlers);
export const worker = setupWorker(...handlers)
39 changes: 17 additions & 22 deletions example/src/mocks/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import { http } from "msw-devtools"

export const handlers = [
http
.get("https://api.example.com/user", () => {
.get("https://json-test.com/v1/user", () => {
return HttpResponse.json({
firstName: "John",
lastName: "Maverick",
firstName: "zzz",
lastName: "zzz",
})
})
.presets([
Expand All @@ -18,24 +18,19 @@ export const handlers = [
lastName: "Maverick",
},
},
{
status: 401,
description: "Unauthorized",
response: {
error: "Unauthorized",
},
},
{
status: 404,
description: "Not Found",
response: {
error: "Not Found",
},
},
]),
http.post("https://api.example.com/user", () => {
return HttpResponse.json({ success: true })
}),
http.put("https://api.example.com/user/:id", () => {
return HttpResponse.json({ success: true })
}),

http.delete("https://api.example.com/user/:id", () => {
return HttpResponse.json({ success: true })
}),
http.patch("https://api.example.com/user/:id", () => {
return HttpResponse.json({ success: true })
}),
http.options("https://api.example.com/user", () => {
return HttpResponse.json({ success: true })
}),
http.head("https://api.example.com/user", () => {
return HttpResponse.json({ success: true })
}),
]
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@
"react-dom": ">=18"
},
"engines": {
"node": ">=20.11.1",
"pnpm": ">=9.0.6"
"node": ">=20.11.1"
}
}
7 changes: 6 additions & 1 deletion src/app/MSWDevtools.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export const MSWDevtools = ({
isEnabled = false,
children,
worker,
onRouteUpdate,
}: MSWDevtoolsProps) => {
if ((isEnabled && !worker) || (isEnabled && worker && !worker)) {
console.warn(
Expand All @@ -20,7 +21,11 @@ export const MSWDevtools = ({

return (
<>
<MswDevToolsProvider isEnabled={isEnabled} worker={worker}>
<MswDevToolsProvider
isEnabled={isEnabled}
worker={worker}
onRouteUpdate={onRouteUpdate}
>
<MSWDevtoolsPanel />
</MswDevToolsProvider>
{children}
Expand Down
11 changes: 6 additions & 5 deletions src/features/DevtoolsHandlerList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { Label } from "@/shared/ui/label"
import { Switch } from "@/shared/ui/switch"

export const DevtoolsHandlerList = () => {
const { routes, onToggleHandler } = useRoute()
const { routes, onToggleHandler, onSelectHandler } = useRoute()
const { onOpenEditPanel } = useEditorRouteState()

return (
Expand All @@ -29,7 +29,7 @@ export const DevtoolsHandlerList = () => {
{routes.map((route) => (
<li key={route.id} className="p-[12px] flex items-center">
<Switch
checked={route.isSkip}
checked={route.enabled}
onCheckedChange={(checked) => {
onToggleHandler(route.id, checked)
}}
Expand All @@ -43,10 +43,11 @@ export const DevtoolsHandlerList = () => {
<div className="ml-auto flex items-center">
<InputContainer label="delay" />
<HandlerSelect
options={route.handlers ?? []}
defaultValue={
route.handlers?.[route.selectedHandlerIndex]?.id ?? undefined
onValueChange={(handlerId) =>
onSelectHandler(route.id, handlerId)
}
options={route.handlers ?? []}
defaultValue={route.handlers?.[0]?.id ?? undefined}
/>
<Button
className="ml-[12px]"
Expand Down
Loading

0 comments on commit 618d63f

Please sign in to comment.