-
Notifications
You must be signed in to change notification settings - Fork 121
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* test: approvals handler test * test: approvals messages and service tests * test: approvals service test cd
- Loading branch information
1 parent
268ae4b
commit 120f9e6
Showing
7 changed files
with
735 additions
and
7 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
/* eslint-disable @typescript-eslint/no-explicit-any */ | ||
import { TxType } from "@namada/shared"; | ||
import { AccountType } from "@namada/types"; | ||
import createMockInstance from "jest-create-mock-instance"; | ||
import { | ||
ApproveConnectInterfaceMsg, | ||
ApproveSignArbitraryMsg, | ||
ApproveTxMsg, | ||
} from "provider"; | ||
import { Message } from "router"; | ||
import { getHandler } from "./handler"; | ||
import { | ||
ConnectInterfaceResponseMsg, | ||
RejectSignatureMsg, | ||
RejectTxMsg, | ||
RevokeConnectionMsg, | ||
SubmitApprovedSignatureMsg, | ||
SubmitApprovedTxMsg, | ||
} from "./messages"; | ||
import { ApprovalsService } from "./service"; | ||
|
||
jest.mock("webextension-polyfill", () => ({})); | ||
|
||
class UnknownMsg extends Message<unknown> { | ||
validate(): void {} | ||
route(): string { | ||
return "unknown"; | ||
} | ||
type(): string { | ||
return "unknown"; | ||
} | ||
} | ||
|
||
describe.only("approvals handler", () => { | ||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
test("handlers switch", () => { | ||
const service: jest.Mocked<ApprovalsService> = createMockInstance( | ||
ApprovalsService as any | ||
); | ||
const handler = getHandler(service); | ||
const env = { | ||
isInternalMsg: true, | ||
senderTabId: 1, | ||
requestInteraction: () => {}, | ||
}; | ||
|
||
const approveTxMsg = new ApproveTxMsg( | ||
TxType.Bond, | ||
"txMsg", | ||
"specificMsg", | ||
AccountType.Mnemonic | ||
); | ||
handler(env, approveTxMsg); | ||
expect(service.approveTx).toBeCalled(); | ||
|
||
const rejectTxMsg = new RejectTxMsg("msgId"); | ||
handler(env, rejectTxMsg); | ||
expect(service.rejectTx).toBeCalled(); | ||
|
||
const submitApprovedTxMsg = new SubmitApprovedTxMsg(TxType.Bond, "msgId"); | ||
handler(env, submitApprovedTxMsg); | ||
expect(service.submitTx).toBeCalled(); | ||
|
||
const approveConnectInterfaceMsg = new ApproveConnectInterfaceMsg(); | ||
handler(env, approveConnectInterfaceMsg); | ||
expect(service.approveConnection).toBeCalled(); | ||
|
||
const connectInterfaceResponseMsg = new ConnectInterfaceResponseMsg( | ||
0, | ||
"", | ||
true | ||
); | ||
handler(env, connectInterfaceResponseMsg); | ||
expect(service.approveConnectionResponse).toBeCalled(); | ||
|
||
const revokeConnectionMsg = new RevokeConnectionMsg(""); | ||
handler(env, revokeConnectionMsg); | ||
expect(service.revokeConnection).toBeCalled(); | ||
|
||
const approveSignArbitraryMsg = new ApproveSignArbitraryMsg("", ""); | ||
handler(env, approveSignArbitraryMsg); | ||
expect(service.approveSignature).toBeCalled(); | ||
|
||
const rejectSignatureMsg = new RejectSignatureMsg(""); | ||
handler(env, rejectSignatureMsg); | ||
expect(service.rejectSignature).toBeCalled(); | ||
|
||
const submitApprovedSignatureMsg = new SubmitApprovedSignatureMsg("", ""); | ||
handler(env, submitApprovedSignatureMsg); | ||
expect(service.submitSignature).toBeCalled(); | ||
|
||
const unknownMsg = new UnknownMsg(); | ||
expect(() => handler(env, unknownMsg)).toThrow(); | ||
}); | ||
}); |
128 changes: 128 additions & 0 deletions
128
apps/extension/src/background/approvals/messages.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,128 @@ | ||
/* eslint-disable @typescript-eslint/no-explicit-any */ | ||
import { TxType } from "@namada/shared"; | ||
import { ROUTE } from "./constants"; | ||
import { | ||
ConnectInterfaceResponseMsg, | ||
MessageType, | ||
RejectSignatureMsg, | ||
RejectTxMsg, | ||
RevokeConnectionMsg, | ||
SubmitApprovedSignatureMsg, | ||
SubmitApprovedTxMsg, | ||
} from "./messages"; | ||
|
||
jest.mock("webextension-polyfill", () => ({})); | ||
|
||
describe("approvals messages", () => { | ||
test("valid RejectTxMsg", () => { | ||
const msg = new RejectTxMsg("msgId"); | ||
|
||
expect(msg.type()).toBe(MessageType.RejectTx); | ||
expect(msg.route()).toBe(ROUTE); | ||
expect(msg.validate()).toBeUndefined(); | ||
}); | ||
|
||
test("invalid RejectTxMsg", () => { | ||
const msg = new RejectTxMsg("msgId"); | ||
(msg as any).msgId = undefined; | ||
|
||
expect(() => msg.validate()).toThrow(); | ||
}); | ||
|
||
test("valid SubmitApprovedTxMsg", () => { | ||
const msg = new SubmitApprovedTxMsg(TxType.Bond, "msgId"); | ||
|
||
expect(msg.type()).toBe(MessageType.SubmitApprovedTx); | ||
expect(msg.route()).toBe(ROUTE); | ||
expect(msg.validate()).toBeUndefined(); | ||
}); | ||
|
||
test("invalid SubmitApprovedTxMsg", () => { | ||
const msg = new SubmitApprovedTxMsg(TxType.Bond, "msgId"); | ||
(msg as any).msgId = undefined; | ||
|
||
expect(() => msg.validate()).toThrow(); | ||
|
||
const msg2 = new SubmitApprovedTxMsg(TxType.Bond, "msgId"); | ||
(msg2 as any).txType = undefined; | ||
|
||
expect(() => msg2.validate()).toThrow(); | ||
}); | ||
|
||
test("valid RejectSignatureMsg", () => { | ||
const msg = new RejectSignatureMsg("msgId"); | ||
|
||
expect(msg.type()).toBe(MessageType.RejectSignature); | ||
expect(msg.route()).toBe(ROUTE); | ||
expect(msg.validate()).toBeUndefined(); | ||
}); | ||
|
||
test("invalid RejectSignatureMsg", () => { | ||
const msg = new RejectSignatureMsg("msgId"); | ||
(msg as any).msgId = undefined; | ||
|
||
expect(() => msg.validate()).toThrow(); | ||
}); | ||
|
||
test("valid SubmitApprovedSignatureMsg", () => { | ||
const msg = new SubmitApprovedSignatureMsg("msgId", "signer"); | ||
|
||
expect(msg.type()).toBe(MessageType.SubmitApprovedSignature); | ||
expect(msg.route()).toBe(ROUTE); | ||
expect(msg.validate()).toBeUndefined(); | ||
}); | ||
|
||
test("invalid SubmitApprovedSignatureMsg", () => { | ||
const msg = new SubmitApprovedSignatureMsg("msgId", "signer"); | ||
(msg as any).msgId = undefined; | ||
|
||
expect(() => msg.validate()).toThrow(); | ||
|
||
const msg2 = new SubmitApprovedSignatureMsg("msgId", "signer"); | ||
(msg2 as any).signer = undefined; | ||
|
||
expect(() => msg2.validate()).toThrow(); | ||
}); | ||
|
||
test("valid ConnectInterfaceResponseMsg", () => { | ||
const msg = new ConnectInterfaceResponseMsg(0, "interface", true); | ||
|
||
expect(msg.type()).toBe(MessageType.ConnectInterfaceResponse); | ||
expect(msg.route()).toBe(ROUTE); | ||
expect(msg.validate()).toBeUndefined(); | ||
}); | ||
|
||
test("invalid ConnectInterfaceResponseMsg", () => { | ||
const msg = new ConnectInterfaceResponseMsg(0, "interface", true); | ||
|
||
(msg as any).interfaceTabId = undefined; | ||
|
||
expect(() => msg.validate()).toThrow(); | ||
|
||
const msg2 = new ConnectInterfaceResponseMsg(0, "interface", true); | ||
(msg2 as any).interfaceOrigin = undefined; | ||
|
||
expect(() => msg2.validate()).toThrow(); | ||
|
||
const msg3 = new ConnectInterfaceResponseMsg(0, "interface", true); | ||
(msg3 as any).allowConnection = undefined; | ||
|
||
expect(() => msg3.validate()).toThrow(); | ||
}); | ||
|
||
test("valid RevokeConnectionMsg", () => { | ||
const msg = new RevokeConnectionMsg(""); | ||
|
||
expect(msg.type()).toBe(MessageType.RevokeConnection); | ||
expect(msg.route()).toBe(ROUTE); | ||
expect(msg.validate()).toBeUndefined(); | ||
}); | ||
|
||
test("invalid RevokeConnectionMsg", () => { | ||
const msg = new RevokeConnectionMsg(""); | ||
|
||
(msg as any).originToRevoke = undefined; | ||
|
||
expect(() => msg.validate()).toThrow(); | ||
}); | ||
}); |
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.