-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
29918cf
commit 011af90
Showing
5 changed files
with
77 additions
and
1 deletion.
There are no files selected for viewing
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
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
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,39 @@ | ||
import React from "react"; | ||
import { render, screen } from "@testing-library/react"; | ||
import "@testing-library/jest-dom"; | ||
|
||
import { DialogConsumer, DialogProvider } from "../use-dialog.context"; | ||
|
||
describe("Dialog context", () => { | ||
it("throw error without provider", () => { | ||
expect(() => render(<DialogConsumer>{() => <div></div>}</DialogConsumer>)).toThrowError( | ||
"DialogConsumer must be used within a DialogProvider." | ||
); | ||
}); | ||
|
||
it("assign default value", () => { | ||
render( | ||
<DialogProvider> | ||
<DialogConsumer<null, null>> | ||
{({ isOpen, params, results }) => ( | ||
<div> | ||
<span> | ||
Dialog state: <pre>{isOpen.toString()}</pre> | ||
</span> | ||
<span> | ||
Dialog params: <pre>{JSON.stringify(params)}</pre> | ||
</span> | ||
<span> | ||
Dialog results: <pre>{JSON.stringify(results)}</pre> | ||
</span> | ||
</div> | ||
)} | ||
</DialogConsumer> | ||
</DialogProvider> | ||
); | ||
|
||
expect(screen.getByText(/^Dialog state:/)).toHaveTextContent("Dialog state: false"); | ||
expect(screen.getByText(/^Dialog params:/)).toHaveTextContent("Dialog params: null"); | ||
expect(screen.getByText(/^Dialog results:/)).toHaveTextContent("Dialog results: null"); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export { useDialog } from "./use-dialog"; | ||
export { DialogConsumer, DialogContext, DialogProvider } from "./use-dialog.context"; | ||
export type { UseDialogInterface } from "./use-dialog"; |
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,30 @@ | ||
import React, { createContext, ReactElement } from "react"; | ||
|
||
import { useDialog, UseDialogInterface } from "./use-dialog"; | ||
|
||
export const DialogContext = createContext<UseDialogInterface | undefined>(undefined); | ||
|
||
export const DialogProvider: React.FC = ({ children }) => { | ||
const value = useDialog(); | ||
|
||
return <DialogContext.Provider value={value}>{children}</DialogContext.Provider>; | ||
}; | ||
|
||
interface DialogConsumerProps<Params = unknown, Results = unknown> { | ||
children: (context: UseDialogInterface<Params, Results>) => ReactElement; | ||
} | ||
|
||
export function DialogConsumer<Params = unknown, Results = unknown>({ | ||
children, | ||
}: DialogConsumerProps<Params, Results>) { | ||
return ( | ||
<DialogContext.Consumer> | ||
{(context) => { | ||
if (context === undefined) { | ||
throw new Error("DialogConsumer must be used within a DialogProvider."); | ||
} | ||
return children(context as UseDialogInterface<Params, Results>); | ||
}} | ||
</DialogContext.Consumer> | ||
); | ||
} |