From e1a2899ed2543a99c9460c157272a750ea13c9e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Abel=20Fern=C3=A1ndez?= <44572727+abefernan@users.noreply.github.com> Date: Thu, 10 Oct 2024 11:17:29 +0200 Subject: [PATCH] Add MockApiProvider --- providers/index.tsx | 9 ++++++--- providers/mock-api.tsx | 23 +++++++++++++++++++++++ 2 files changed, 29 insertions(+), 3 deletions(-) create mode 100644 providers/mock-api.tsx diff --git a/providers/index.tsx b/providers/index.tsx index 57c15bc..4a20f2b 100644 --- a/providers/index.tsx +++ b/providers/index.tsx @@ -1,11 +1,14 @@ import { TooltipProvider } from "@/components/ui/tooltip"; import { PropsWithChildren } from "react"; +import MockApiProvider from "./mock-api"; import ReactQueryProvider from "./react-query"; export default function Providers({ children }: PropsWithChildren) { return ( - - {children} - + + + {children} + + ); } diff --git a/providers/mock-api.tsx b/providers/mock-api.tsx new file mode 100644 index 0000000..1ed9080 --- /dev/null +++ b/providers/mock-api.tsx @@ -0,0 +1,23 @@ +"use client"; + +import { env } from "@/lib/env"; +import { mockWorkerService } from "@/tests/mocks/worker"; +import { PropsWithChildren, useEffect, useState } from "react"; + +export default function MockApiProvider({ children }: PropsWithChildren) { + const [shouldRenderChildren, setShouldRenderChildren] = useState(false); + + useEffect(() => { + if (env.NEXT_PUBLIC_MOCK_API === "TRUE") { + mockWorkerService.start().then(() => { + setShouldRenderChildren(true); + }); + } + + return () => { + mockWorkerService.stop(); + }; + }, []); + + return shouldRenderChildren ? children : null; +}