Skip to content

Commit

Permalink
fix: types for tests
Browse files Browse the repository at this point in the history
  • Loading branch information
0xmad committed Nov 13, 2023
1 parent 178c7ef commit fda2c23
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 37 deletions.
6 changes: 3 additions & 3 deletions packages/providers/src/sdk/CryptKeeperInjectedProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { ICryptKeeperInjectedProvider } from "./interface";
import type { IInjectedMessageData, IInjectedProviderRequest } from "@cryptkeeperzk/types";

import { RPCExternalAction } from "../constants";
import { type EventHandler, type EventName, type IHandler, Handler } from "../services";
import { type EventHandler, type EventName, type IHandler } from "../services";

/**
* Represents the CryptKeeper provider that is injected into the application.
Expand All @@ -26,8 +26,8 @@ export class CryptKeeperInjectedProvider implements ICryptKeeperInjectedProvider
*
* @constructor
*/
constructor(connectedOrigin?: string) {
this.handler = new Handler(connectedOrigin);
constructor(handler: IHandler) {
this.handler = handler;
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/**
* @jest-environment jsdom
*/
/* eslint-disable @typescript-eslint/unbound-method */
import { RPCExternalAction } from "@src/constants";

import type { IInjectedMessageData } from "@cryptkeeperzk/types";
Expand All @@ -12,11 +13,6 @@ jest.mock("nanoevents", (): unknown => ({
createNanoEvents: jest.fn(),
}));

jest.mock("../../services", (): unknown => ({
...jest.requireActual("../../services"),
Handler: jest.fn(),
}));

describe("sdk/CryptKeeperInjectedProvider", () => {
const defaultHandler = {
request: jest.fn(),
Expand All @@ -25,18 +21,14 @@ describe("sdk/CryptKeeperInjectedProvider", () => {
emit: jest.fn(),
cleanListeners: jest.fn(),
getConnectedOrigin: jest.fn(),
};

beforeEach(() => {
(Handler as jest.Mock).mockReturnValue(defaultHandler);
});
} as unknown as Handler;

afterEach(() => {
jest.clearAllMocks();
});

test("should connect properly", async () => {
const provider = new CryptKeeperInjectedProvider();
const provider = new CryptKeeperInjectedProvider(defaultHandler);

await provider.connect();

Expand All @@ -51,7 +43,7 @@ describe("sdk/CryptKeeperInjectedProvider", () => {
});

test("should request rpc properly", async () => {
const provider = new CryptKeeperInjectedProvider();
const provider = new CryptKeeperInjectedProvider(defaultHandler);

await provider.request({ method: RPCExternalAction.GET_CONNECTED_IDENTITY_DATA });

Expand All @@ -62,7 +54,7 @@ describe("sdk/CryptKeeperInjectedProvider", () => {
});

test("should handle events properly", () => {
const provider = new CryptKeeperInjectedProvider();
const provider = new CryptKeeperInjectedProvider(defaultHandler);

provider.eventResponser({} as MessageEvent<IInjectedMessageData>);
provider.on(EventName.CONNECT, jest.fn());
Expand Down
25 changes: 4 additions & 21 deletions packages/providers/src/sdk/initializeInjectedProvider.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,17 @@
import type { ICryptKeeperInjectedProvider } from "./interface";

import { CryptKeeperInjectedProvider } from "./CryptKeeperInjectedProvider";

/**
* Extends the global Window interface to include CryptKeeper-related properties.
*/
declare global {
/**
* Represents the CryptKeeperInjectedProvider instance.
*/
interface Window {
/**
* The CryptKeeperInjectedProvider instance.
*/
cryptkeeper: CryptKeeperInjectedProvider;
import { Handler } from "../services";

/**
* Indicates whether CryptKeeper is injected.
*/
isCryptkeeperInjected?: boolean;
}
}
import { CryptKeeperInjectedProvider } from "./CryptKeeperInjectedProvider";

/**
* Initializes the CryptKeeper provider within the CryptKeeper extension.
* This function is meant to be used exclusively within the CryptKeeper extension.
* @returns {CryptKeeperInjectedProvider | undefined} The initialized CryptKeeperInjectedProvider instance or undefined.
*/
export function initializeCryptKeeperProvider(connectedOrigin?: string): ICryptKeeperInjectedProvider {
const cryptkeeperInjectedProvider = new CryptKeeperInjectedProvider(connectedOrigin);
const handler = new Handler(connectedOrigin);
const cryptkeeperInjectedProvider = new CryptKeeperInjectedProvider(handler);
window.cryptkeeper = cryptkeeperInjectedProvider;
window.dispatchEvent(new Event(`cryptkeeper#initialized`));
window.addEventListener("message", cryptkeeperInjectedProvider.eventResponser);
Expand Down
20 changes: 20 additions & 0 deletions packages/providers/src/sdk/interface.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,26 @@
import type { EventHandler, EventName } from "../services";
import type { IInjectedMessageData, IInjectedProviderRequest } from "@cryptkeeperzk/types";

/**
* Extends the global Window interface to include CryptKeeper-related properties.
*/
declare global {
/**
* Represents the CryptKeeperInjectedProvider instance.
*/
interface Window {
/**
* The CryptKeeperInjectedProvider instance.
*/
cryptkeeper: ICryptKeeperInjectedProvider;

/**
* Indicates whether CryptKeeper is injected.
*/
isCryptkeeperInjected?: boolean;
}
}

/**
* Represents the CryptKeeper provider that is injected into the application.
* This class is responsible for handling interactions with the CryptKeeper extension.
Expand Down

0 comments on commit fda2c23

Please sign in to comment.