This repository has been archived by the owner on Mar 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 190
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
021b1c4
commit d092c43
Showing
13 changed files
with
2,057 additions
and
176 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
import { translations } from "app/i18n/common/i18n"; | ||
import { toasts } from "app/services"; | ||
import { ipcRenderer } from "electron"; | ||
import { createMemoryHistory } from "history"; | ||
import React from "react"; | ||
import { Route } from "react-router-dom"; | ||
import { getDefaultProfileId, getDefaultWalletId, renderWithRouter } from "testing-library"; | ||
|
||
import { useDeeplink } from "./use-deeplink"; | ||
|
||
const history = createMemoryHistory(); | ||
const walletURL = `/profiles/${getDefaultProfileId()}/wallets/${getDefaultWalletId()}`; | ||
|
||
jest.mock("electron", () => ({ | ||
ipcRenderer: { on: jest.fn(), send: jest.fn(), removeListener: jest.fn() }, | ||
})); | ||
|
||
describe("useDeeplink hook", () => { | ||
const toastSpy = jest.spyOn(toasts, "warning").mockImplementationOnce((subject) => jest.fn(subject)); | ||
|
||
const TestComponent: React.FC = () => { | ||
useDeeplink(); | ||
|
||
return <h1>Deeplink tester</h1>; | ||
}; | ||
|
||
it("should subscribe to deeplink listener", () => { | ||
ipcRenderer.on.mockImplementationOnce((event, callback) => | ||
callback( | ||
event, | ||
"ark:transfer?coin=ark&network=mainnet&recipient=DNjuJEDQkhrJ7cA9FZ2iVXt5anYiM8Jtc9&amount=1.2&memo=ARK", | ||
), | ||
); | ||
|
||
const { getByText, history } = renderWithRouter( | ||
<Route pathname="/"> | ||
<TestComponent /> | ||
</Route>, | ||
); | ||
|
||
expect(getByText("Deeplink tester")).toBeTruthy(); | ||
expect(ipcRenderer.on).toBeCalledWith("process-url", expect.any(Function)); | ||
}); | ||
|
||
it("should subscribe to deeplink listener and toast a warning to select a profile", () => { | ||
ipcRenderer.on.mockImplementationOnce((event, callback) => | ||
callback( | ||
event, | ||
"ark:transfer?coin=ark&network=mainnet&recipient=DNjuJEDQkhrJ7cA9FZ2iVXt5anYiM8Jtc9&amount=1.2&memo=ARK", | ||
), | ||
); | ||
|
||
const { getByText, history } = renderWithRouter( | ||
<Route pathname="/"> | ||
<TestComponent /> | ||
</Route>, | ||
{ | ||
routes: ["/"], | ||
}, | ||
); | ||
|
||
expect(getByText("Deeplink tester")).toBeTruthy(); | ||
expect(toastSpy).toHaveBeenCalledWith(translations.SELECT_A_PROFILE); | ||
expect(ipcRenderer.on).toBeCalledWith("process-url", expect.any(Function)); | ||
}); | ||
|
||
it("should subscribe to deeplink listener and toast a warning to select a wallet", () => { | ||
ipcRenderer.on.mockImplementationOnce((event, callback) => | ||
callback( | ||
event, | ||
"ark:transfer?coin=ark&network=mainnet&recipient=DNjuJEDQkhrJ7cA9FZ2iVXt5anYiM8Jtc9&amount=1.2&memo=ARK", | ||
), | ||
); | ||
|
||
window.history.pushState({}, "Deeplink Test", `/profiles/${getDefaultProfileId()}/dashboard`); | ||
|
||
const { getByText, history } = renderWithRouter( | ||
<Route pathname="/profiles/:profileId"> | ||
<TestComponent /> | ||
</Route>, | ||
{ | ||
routes: [`/profiles/${getDefaultProfileId()}/dashboard`], | ||
}, | ||
); | ||
|
||
expect(getByText("Deeplink tester")).toBeTruthy(); | ||
expect(toastSpy).toHaveBeenCalledWith(translations.SELECT_A_WALLET); | ||
expect(ipcRenderer.on).toBeCalledWith("process-url", expect.any(Function)); | ||
}); | ||
|
||
it("should subscribe to deeplink listener and navigate", () => { | ||
ipcRenderer.on.mockImplementationOnce((event, callback) => | ||
callback( | ||
event, | ||
"ark:transfer?coin=ark&network=mainnet&recipient=DNjuJEDQkhrJ7cA9FZ2iVXt5anYiM8Jtc9&amount=1.2&memo=ARK", | ||
), | ||
); | ||
|
||
window.history.pushState( | ||
{}, | ||
"Deeplink Test", | ||
`/profiles/${getDefaultProfileId()}/wallets/${getDefaultWalletId()}`, | ||
); | ||
|
||
const { getByText, history } = renderWithRouter( | ||
<Route pathname="/profiles/:profileId/wallets/:walletId"> | ||
<TestComponent /> | ||
</Route>, | ||
{ | ||
routes: [walletURL], | ||
}, | ||
); | ||
|
||
expect(getByText("Deeplink tester")).toBeTruthy(); | ||
expect(history.location.pathname).toEqual( | ||
`/profiles/${getDefaultProfileId()}/wallets/${getDefaultWalletId()}/send-transfer`, | ||
); | ||
expect(ipcRenderer.on).toBeCalledWith("process-url", expect.any(Function)); | ||
}); | ||
|
||
it("should subscribe to deeplink listener and navigate when no method found", () => { | ||
ipcRenderer.on.mockImplementationOnce((event, callback) => | ||
callback( | ||
event, | ||
"ark:vote?coin=ark&network=mainnet&recipient=DNjuJEDQkhrJ7cA9FZ2iVXt5anYiM8Jtc9&amount=1.2&memo=ARK", | ||
), | ||
); | ||
|
||
window.history.pushState( | ||
{}, | ||
"Deeplink Test", | ||
`/profiles/${getDefaultProfileId()}/wallets/${getDefaultWalletId()}`, | ||
); | ||
|
||
const { getByText, history } = renderWithRouter( | ||
<Route pathname="/profiles/:profileId/wallets/:walletId"> | ||
<TestComponent /> | ||
</Route>, | ||
{ | ||
routes: [walletURL], | ||
}, | ||
); | ||
|
||
expect(getByText("Deeplink tester")).toBeTruthy(); | ||
expect(history.location.pathname).toEqual("/"); | ||
expect(ipcRenderer.on).toBeCalledWith("process-url", expect.any(Function)); | ||
}); | ||
}); |
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,63 @@ | ||
import { URI } from "@arkecosystem/platform-sdk-support/dist/uri"; | ||
import { toasts } from "app/services"; | ||
import { ipcRenderer } from "electron"; | ||
import { useCallback, useEffect } from "react"; | ||
import { useTranslation } from "react-i18next"; | ||
import { useHistory } from "react-router-dom"; | ||
|
||
const useDeepLinkHandler = () => { | ||
const history = useHistory(); | ||
const { t } = useTranslation(); | ||
const uriService = new URI(); | ||
|
||
const navigate = useCallback((url: string, deeplinkSchema?: any) => history.push(url, deeplinkSchema), [history]); | ||
|
||
const handler = useCallback( | ||
(event: any, deeplink: string) => { | ||
if (deeplink) { | ||
if (window.location.pathname === "/") return toasts.warning(t("COMMON.SELECT_A_PROFILE")); | ||
|
||
if (window.location.pathname.includes("/dashboard")) return toasts.warning(t("COMMON.SELECT_A_WALLET")); | ||
|
||
const deeplinkSchema = uriService.deserialize(deeplink); | ||
const urlParts = window.location.pathname.split("/"); | ||
const activeSession = { | ||
profileId: urlParts[2], | ||
walletId: urlParts[4], | ||
}; | ||
|
||
switch (deeplinkSchema.method) { | ||
case "transfer": | ||
return navigate( | ||
`/profiles/${activeSession.profileId}/wallets/${activeSession.walletId}/send-transfer`, | ||
deeplinkSchema, | ||
); | ||
|
||
default: | ||
return navigate("/"); | ||
} | ||
} | ||
}, | ||
[t, uriService, navigate], | ||
); | ||
|
||
useEffect((): any => { | ||
ipcRenderer.on("process-url", handler); | ||
|
||
return () => ipcRenderer.removeListener("process-url", handler); | ||
}, [handler]); | ||
|
||
return { | ||
handler, | ||
}; | ||
}; | ||
|
||
export const useDeeplink = () => { | ||
const { handler } = useDeepLinkHandler(); | ||
|
||
useEffect(() => { | ||
handler(null, ""); | ||
}, [handler]); | ||
}; | ||
|
||
export default useDeeplink; |
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.