-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add tests for providers package
- Loading branch information
Showing
12 changed files
with
390 additions
and
28 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
module.exports = { | ||
preset: "../../jest-preset.js", | ||
displayName: "@cryptkeeperzk/providers", | ||
setupFilesAfterEnv: ["<rootDir>/src/setupTests.ts"], | ||
moduleNameMapper: { | ||
"@src/(.*)$": "<rootDir>/src/$1", | ||
}, | ||
moduleFileExtensions: ["ts", "js"], | ||
collectCoverageFrom: ["src/**/*.{ts,js}"], | ||
coveragePathIgnorePatterns: ["/node_modules/", "/test/", "/__tests__/", "./src/index.ts"], | ||
coverageThreshold: { | ||
global: { | ||
statements: 95, | ||
branches: 95, | ||
functions: 95, | ||
lines: 95, | ||
}, | ||
}, | ||
}; |
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
78 changes: 78 additions & 0 deletions
78
packages/providers/src/sdk/__tests__/CryptKeeperInjectedProvider.test.ts
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,78 @@ | ||
/** | ||
* @jest-environment jsdom | ||
*/ | ||
import { RPCExternalAction } from "@src/constants"; | ||
|
||
import type { IInjectedMessageData } from "@cryptkeeperzk/types"; | ||
|
||
import { CryptKeeperInjectedProvider } from ".."; | ||
import { EventName, Handler } from "../../services"; | ||
|
||
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(), | ||
eventResponser: jest.fn(), | ||
on: jest.fn(), | ||
emit: jest.fn(), | ||
cleanListeners: jest.fn(), | ||
getConnectedOrigin: jest.fn(), | ||
}; | ||
|
||
beforeEach(() => { | ||
(Handler as jest.Mock).mockReturnValue(defaultHandler); | ||
}); | ||
|
||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
test("should connect properly", async () => { | ||
const provider = new CryptKeeperInjectedProvider(); | ||
|
||
await provider.connect(); | ||
|
||
expect(defaultHandler.request).toHaveBeenCalledTimes(1); | ||
expect(defaultHandler.request).toHaveBeenCalledWith({ | ||
method: RPCExternalAction.CONNECT, | ||
payload: { | ||
isChangeIdentity: false, | ||
urlOrigin: undefined, | ||
}, | ||
}); | ||
}); | ||
|
||
test("should request rpc properly", async () => { | ||
const provider = new CryptKeeperInjectedProvider(); | ||
|
||
await provider.request({ method: RPCExternalAction.GET_CONNECTED_IDENTITY_DATA }); | ||
|
||
expect(defaultHandler.request).toHaveBeenCalledTimes(1); | ||
expect(defaultHandler.request).toHaveBeenCalledWith({ | ||
method: RPCExternalAction.GET_CONNECTED_IDENTITY_DATA, | ||
}); | ||
}); | ||
|
||
test("should handle events properly", () => { | ||
const provider = new CryptKeeperInjectedProvider(); | ||
|
||
provider.eventResponser({} as MessageEvent<IInjectedMessageData>); | ||
provider.on(EventName.CONNECT, jest.fn()); | ||
provider.emit(EventName.CONNECT, { data: true }); | ||
provider.cleanListeners(); | ||
|
||
expect(defaultHandler.eventResponser).toHaveBeenCalledTimes(1); | ||
expect(defaultHandler.on).toHaveBeenCalledTimes(1); | ||
expect(defaultHandler.emit).toHaveBeenCalledTimes(1); | ||
expect(defaultHandler.emit).toHaveBeenCalledWith(EventName.CONNECT, { data: true }); | ||
expect(defaultHandler.cleanListeners).toHaveBeenCalledTimes(1); | ||
}); | ||
}); |
65 changes: 65 additions & 0 deletions
65
packages/providers/src/sdk/__tests__/initializeInjectedProvider.test.ts
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,65 @@ | ||
/** | ||
* @jest-environment jsdom | ||
*/ | ||
|
||
import { CryptKeeperInjectedProvider, initializeCryptKeeper, initializeCryptKeeperProvider } from ".."; | ||
|
||
jest.mock("nanoevents", (): unknown => ({ | ||
createNanoEvents: jest.fn(), | ||
})); | ||
|
||
jest.mock("../CryptKeeperInjectedProvider", (): unknown => ({ | ||
...jest.requireActual("../CryptKeeperInjectedProvider"), | ||
CryptKeeperInjectedProvider: jest.fn(), | ||
})); | ||
|
||
describe("sdk/initializeInjectedProvider", () => { | ||
const defaultProvider = { | ||
request: jest.fn(), | ||
eventResponser: jest.fn(), | ||
on: jest.fn(), | ||
emit: jest.fn(), | ||
cleanListeners: jest.fn(), | ||
getConnectedOrigin: jest.fn(), | ||
}; | ||
|
||
beforeEach(() => { | ||
(CryptKeeperInjectedProvider as jest.Mock).mockReturnValue(defaultProvider); | ||
}); | ||
|
||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
window.isCryptkeeperInjected = true; | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
// @ts-expect-error | ||
window.cryptkeeper = undefined; | ||
}); | ||
|
||
test("should initialize cryptkeeper properly", () => { | ||
const provider = initializeCryptKeeper(); | ||
|
||
expect(provider).toStrictEqual(defaultProvider); | ||
expect(window.cryptkeeper).toStrictEqual(provider); | ||
expect(window.dispatchEvent).toHaveBeenCalledTimes(1); | ||
expect(window.addEventListener).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
test("should initialize cryptkeeper for extension properly", () => { | ||
const provider = initializeCryptKeeperProvider(); | ||
|
||
expect(provider).toStrictEqual(defaultProvider); | ||
expect(window.cryptkeeper).toStrictEqual(provider); | ||
expect(window.dispatchEvent).toHaveBeenCalledTimes(1); | ||
expect(window.addEventListener).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
test("should not initialize if cryptkeeper is not injected", () => { | ||
window.isCryptkeeperInjected = false; | ||
const provider = initializeCryptKeeper(); | ||
|
||
expect(provider).toBeUndefined(); | ||
expect(window.cryptkeeper).toBeUndefined(); | ||
expect(window.dispatchEvent).toHaveBeenCalledTimes(0); | ||
expect(window.addEventListener).toHaveBeenCalledTimes(0); | ||
}); | ||
}); |
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
41 changes: 41 additions & 0 deletions
41
packages/providers/src/services/event/__tests__/EventEmitter.test.ts
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,41 @@ | ||
/** | ||
* @jest-environment jsdom | ||
*/ | ||
|
||
/* eslint-disable @typescript-eslint/unbound-method */ | ||
import { type Emitter, createNanoEvents } from "nanoevents"; | ||
|
||
import { EventEmitter, EventName } from ".."; | ||
|
||
jest.mock("nanoevents", (): unknown => ({ | ||
createNanoEvents: jest.fn(), | ||
})); | ||
|
||
describe("services/event", () => { | ||
const defaultEventEmitter: Emitter = { | ||
events: {}, | ||
on: jest.fn(), | ||
emit: jest.fn(), | ||
}; | ||
|
||
const defaultHandler = jest.fn(); | ||
|
||
beforeEach(() => { | ||
(createNanoEvents as jest.Mock).mockReturnValue(defaultEventEmitter); | ||
}); | ||
|
||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
test("should handle events properly", () => { | ||
const eventEmitter = new EventEmitter(); | ||
|
||
eventEmitter.on(EventName.CONNECT, defaultHandler); | ||
eventEmitter.emit(EventName.CONNECT, { data: true }); | ||
eventEmitter.cleanListeners(); | ||
|
||
expect(defaultEventEmitter.on).toHaveBeenCalledTimes(1); | ||
expect(defaultEventEmitter.emit).toHaveBeenCalledTimes(1); | ||
}); | ||
}); |
Oops, something went wrong.