Skip to content

Commit

Permalink
Add MockApiProvider
Browse files Browse the repository at this point in the history
  • Loading branch information
abefernan committed Oct 15, 2024
1 parent ade0761 commit e1a2899
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
9 changes: 6 additions & 3 deletions providers/index.tsx
Original file line number Diff line number Diff line change
@@ -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 (
<ReactQueryProvider>
<TooltipProvider>{children}</TooltipProvider>
</ReactQueryProvider>
<MockApiProvider>
<ReactQueryProvider>
<TooltipProvider>{children}</TooltipProvider>
</ReactQueryProvider>
</MockApiProvider>
);
}
23 changes: 23 additions & 0 deletions providers/mock-api.tsx
Original file line number Diff line number Diff line change
@@ -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;
}

0 comments on commit e1a2899

Please sign in to comment.