-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🪟 Per-Stream state new flow (#14634)
* [WIP] new per stream flow * Start work on ModalSerice * Continue work * Fix typo * Change wording * Add todos and remove dead code * Remove dead message * Adjust to new API * Adjust to new modal changes * Remove debug output * Fix e2e test * Add data-testids * Adjust for PR review * Switch to new ModalFooter/Body components * Add ModalService tests * Downgrade user-events again * Only compare selected streams * Update airbyte-webapp/src/locales/en.json Co-authored-by: Andy Jih <andyjih@users.noreply.github.com> * Update airbyte-webapp/src/locales/en.json Co-authored-by: Andy Jih <andyjih@users.noreply.github.com> * Remove redundant space Co-authored-by: Andy Jih <andyjih@users.noreply.github.com>
- Loading branch information
1 parent
823a826
commit 54810d0
Showing
17 changed files
with
325 additions
and
173 deletions.
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
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
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
87 changes: 87 additions & 0 deletions
87
airbyte-webapp/src/hooks/services/Modal/ModalService.test.tsx
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,87 @@ | ||
import { render, waitFor } from "@testing-library/react"; | ||
import userEvent from "@testing-library/user-event"; | ||
import { useEffectOnce } from "react-use"; | ||
|
||
import { ModalServiceProvider, useModalService } from "./ModalService"; | ||
import { ModalResult } from "./types"; | ||
|
||
const TestComponent: React.FC<{ onModalResult?: (result: ModalResult<unknown>) => void }> = ({ onModalResult }) => { | ||
const { openModal } = useModalService(); | ||
useEffectOnce(() => { | ||
openModal({ | ||
title: "Test Modal Title", | ||
content: ({ onCancel, onClose }) => ( | ||
<div data-testid="testModalContent"> | ||
<button onClick={onCancel} data-testid="cancel"> | ||
Cancel | ||
</button> | ||
<button onClick={() => onClose("reason1")} data-testid="close-reason1"> | ||
Close Reason 1 | ||
</button> | ||
<button onClick={() => onClose("reason2")} data-testid="close-reason2"> | ||
Close Reason 2 | ||
</button> | ||
</div> | ||
), | ||
}).then(onModalResult); | ||
}); | ||
return null; | ||
}; | ||
|
||
const renderModal = (resultCallback?: (reason: unknown) => void) => { | ||
return render( | ||
<ModalServiceProvider> | ||
<TestComponent onModalResult={resultCallback} /> | ||
</ModalServiceProvider> | ||
); | ||
}; | ||
|
||
describe("ModalService", () => { | ||
it("should open a modal on openModal", () => { | ||
const rendered = renderModal(); | ||
|
||
expect(rendered.getByText("Test Modal Title")).toBeTruthy(); | ||
expect(rendered.getByTestId("testModalContent")).toBeTruthy(); | ||
}); | ||
|
||
it("should close the modal with escape and emit a cancel result", async () => { | ||
const resultCallback = jest.fn(); | ||
|
||
const rendered = renderModal(resultCallback); | ||
|
||
await waitFor(() => userEvent.keyboard("{Escape}")); | ||
|
||
expect(rendered.queryByTestId("testModalContent")).toBeFalsy(); | ||
expect(resultCallback).toHaveBeenCalledWith({ type: "canceled" }); | ||
}); | ||
|
||
it("should allow cancelling the modal from inside", async () => { | ||
const resultCallback = jest.fn(); | ||
|
||
const rendered = renderModal(resultCallback); | ||
|
||
await waitFor(() => userEvent.click(rendered.getByTestId("cancel"))); | ||
|
||
expect(rendered.queryByTestId("testModalContent")).toBeFalsy(); | ||
expect(resultCallback).toHaveBeenCalledWith({ type: "canceled" }); | ||
}); | ||
|
||
it("should allow closing the button with a reason and return that reason", async () => { | ||
const resultCallback = jest.fn(); | ||
|
||
let rendered = renderModal(resultCallback); | ||
|
||
await waitFor(() => userEvent.click(rendered.getByTestId("close-reason1"))); | ||
|
||
expect(rendered.queryByTestId("testModalContent")).toBeFalsy(); | ||
expect(resultCallback).toHaveBeenCalledWith({ type: "closed", reason: "reason1" }); | ||
|
||
resultCallback.mockReset(); | ||
rendered = renderModal(resultCallback); | ||
|
||
await waitFor(() => userEvent.click(rendered.getByTestId("close-reason2"))); | ||
|
||
expect(rendered.queryByTestId("testModalContent")).toBeFalsy(); | ||
expect(resultCallback).toHaveBeenCalledWith({ type: "closed", reason: "reason2" }); | ||
}); | ||
}); |
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,64 @@ | ||
import React, { useContext, useMemo, useRef, useState } from "react"; | ||
import { firstValueFrom, Subject } from "rxjs"; | ||
|
||
import { Modal } from "components"; | ||
|
||
import { ModalOptions, ModalResult, ModalServiceContext } from "./types"; | ||
|
||
const modalServiceContext = React.createContext<ModalServiceContext | undefined>(undefined); | ||
|
||
export const ModalServiceProvider: React.FC = ({ children }) => { | ||
// The any here is due to the fact, that every call to open a modal might come in with | ||
// a different type, thus we can't type this with unknown or a generic. | ||
// The consuming code of this service though is properly typed, so that this `any` stays | ||
// encapsulated within this component. | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
const [modalOptions, setModalOptions] = useState<ModalOptions<any>>(); | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
const resultSubjectRef = useRef<Subject<ModalResult<any>>>(); | ||
|
||
const service: ModalServiceContext = useMemo( | ||
() => ({ | ||
openModal: (options) => { | ||
resultSubjectRef.current = new Subject(); | ||
setModalOptions(options); | ||
|
||
return firstValueFrom(resultSubjectRef.current).then((reason) => { | ||
setModalOptions(undefined); | ||
resultSubjectRef.current = undefined; | ||
return reason; | ||
}); | ||
}, | ||
closeModal: () => { | ||
resultSubjectRef.current?.next({ type: "canceled" }); | ||
}, | ||
}), | ||
[] | ||
); | ||
|
||
return ( | ||
<modalServiceContext.Provider value={service}> | ||
{children} | ||
{modalOptions && ( | ||
<Modal | ||
title={modalOptions.title} | ||
size={modalOptions.size} | ||
onClose={() => resultSubjectRef.current?.next({ type: "canceled" })} | ||
> | ||
<modalOptions.content | ||
onCancel={() => resultSubjectRef.current?.next({ type: "canceled" })} | ||
onClose={(reason) => resultSubjectRef.current?.next({ type: "closed", reason })} | ||
/> | ||
</Modal> | ||
)} | ||
</modalServiceContext.Provider> | ||
); | ||
}; | ||
|
||
export const useModalService = () => { | ||
const context = useContext(modalServiceContext); | ||
if (!context) { | ||
throw new Error("Can't use ModalService outside ModalServiceProvider"); | ||
} | ||
return context; | ||
}; |
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 @@ | ||
export * from "./ModalService"; |
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,21 @@ | ||
import React from "react"; | ||
|
||
import { ModalProps } from "components/Modal/Modal"; | ||
|
||
export interface ModalOptions<T> { | ||
title: ModalProps["title"]; | ||
size?: ModalProps["size"]; | ||
content: React.ComponentType<ModalContentProps<T>>; | ||
} | ||
|
||
export type ModalResult<T> = { type: "canceled" } | { type: "closed"; reason: T }; | ||
|
||
interface ModalContentProps<T> { | ||
onClose: (reason: T) => void; | ||
onCancel: () => void; | ||
} | ||
|
||
export interface ModalServiceContext { | ||
openModal: <ResultType>(options: ModalOptions<ResultType>) => Promise<ModalResult<ResultType>>; | ||
closeModal: () => void; | ||
} |
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
Oops, something went wrong.