forked from anoma/namada
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* Add some unit tests to shielded account actions * Configure to run in CI * Adjusted a failing test to reflect a change in a dependency * Removed a hard-coded value that was used during debugging * Adjusted setting of the state based on the change in the type of actions result * Changed the faulty test file name based on PR review * removed unnecessary import * Investigating a weird issue where the paths resolve locally but not in CI * corrected the paths after renaming
- Loading branch information
1 parent
f4325a2
commit bb7946b
Showing
20 changed files
with
590 additions
and
18 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 |
---|---|---|
|
@@ -10,6 +10,45 @@ on: | |
env: | ||
CI: false | ||
jobs: | ||
unit-tests: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Checkout repository | ||
uses: actions/checkout@v2 | ||
|
||
- name: Install dependencies | ||
working-directory: ./packages/anoma-wallet | ||
run: yarn | ||
|
||
- name: Install wasm-pack | ||
uses: jetli/[email protected] | ||
with: | ||
version: "v0.10.3" | ||
|
||
- name: Run unit tests | ||
id: run-unit-tests | ||
working-directory: ./packages/anoma-wallet | ||
run: yarn test:coverage | ||
env: | ||
REACT_APP_ALIAS: "Namada Mainnet" | ||
REACT_APP_CHAIN_ID: "anoma-masp-1.5.32ccad5356012a7" | ||
REACT_APP_LEDGER_URL: "https://d3brk13lbhxfdb.cloudfront.net/anoma-masp-1.5.32ccad5356012a7" | ||
REACT_APP_FAUCET: "atest1v4ehgw36gc6yxvpjxccyzvphxycrxw2xxsuyydesxgcnjs3cg9znwv3cxgmnj32yxy6rssf5tcqjm3" | ||
|
||
- name: report success | ||
if: steps.run-unit-tests.outcome == 'success' | ||
run: | | ||
curl --header "Content-Type: application/json" \ | ||
--request POST \ | ||
--data '{"message":"Unit tests succeeded ✅\n \n${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}\n \nReview\nhttps://pull-request-${{ github.event.number }}--wallet-development-heliax-dev.netlify.app\n \nthe PR\nhttps://github.com/anoma/namada-interface/pull/${{ github.event.number }}"}' \ | ||
${{ secrets.SLACK_WEBHOOK_WALLET_PR }} | ||
- name: report failure | ||
if: steps.run-unit-tests.outcome != 'success' | ||
run: | | ||
curl --header "Content-Type: application/json" \ | ||
--request POST \ | ||
--data '{"message":"Unit tests failed ⛔️ \n \n${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}\n \nReview\nhttps://pull-request-${{ github.event.number }}--wallet-development-heliax-dev.netlify.app\n \nthe PR\nhttps://github.com/anoma/namada-interface/pull/${{ github.event.number }}"}' \ | ||
${{ secrets.SLACK_WEBHOOK_WALLET_PR }} | ||
build: | ||
runs-on: ubuntu-latest | ||
steps: | ||
|
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
File renamed without changes.
257 changes: 257 additions & 0 deletions
257
packages/anoma-wallet/src/slices/AccountsNew/__tests__/AccountsNew.actions.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,257 @@ | ||
import { jest } from "@jest/globals"; | ||
import { AnyAction } from "redux"; | ||
import createMockStore from "redux-mock-store"; | ||
import thunk, { ThunkDispatch } from "redux-thunk"; | ||
import { RootState } from "store/store"; | ||
import { mockAppState } from "store/mocks"; | ||
import { updateShieldedBalances } from "../actions"; | ||
import { ShieldedAccount } from "slices/accounts"; | ||
import { AccountErrors } from "../types"; | ||
|
||
// imports containing mocked units | ||
import * as shieldedTransfer from "slices/shieldedTransfer"; | ||
jest.mock("slices/shieldedTransfer"); | ||
|
||
type DispatchExts = ThunkDispatch<RootState, void, AnyAction>; | ||
const middleware = [thunk]; | ||
const mockStore = createMockStore<RootState, DispatchExts>(middleware); | ||
const store = mockStore(mockAppState); | ||
|
||
describe("shielded balances", () => { | ||
describe("fetchContactPageContent", () => { | ||
beforeEach(() => { | ||
store.clearActions(); | ||
}); | ||
|
||
// this is not a focused case, but a very generic one just testing a happy path | ||
// of updateShieldedBalances. | ||
it("should behave as expected in happy path of updateShieldedBalances", async () => { | ||
// setup data | ||
const testChainId1 = "testChainId1"; | ||
const testspendingKey1 = "testspendingKey1"; | ||
const testBalanceAmount = 100; | ||
const shieldedAccountUuidInClient = "shieldedAccountUuidInClient1"; | ||
const shieldedAccounts: { | ||
[key: string]: { [key: string]: ShieldedAccount }; | ||
} = { | ||
testChainId1: { | ||
[shieldedAccountUuidInClient]: { | ||
chainId: "testChainId1", | ||
shieldedKeysAndPaymentAddress: { | ||
viewingKey: "viewingKey", | ||
spendingKey: testspendingKey1, | ||
paymentAddress: "paymentAddress", | ||
}, | ||
isShielded: true, | ||
alias: "Namada", | ||
balance: 0, | ||
address: "this_should_be_the_current_masp_address", | ||
tokenType: "ETH", | ||
signingKey: "signingKey", | ||
publicKey: "publicKey", | ||
id: shieldedAccountUuidInClient, | ||
}, | ||
}, | ||
}; | ||
// we create this here and set in the mocked function below to be able to assert that | ||
// it was called with the right data | ||
let expectedChainId; | ||
let expectedSpendingKey; | ||
|
||
const store = mockStore({ | ||
...mockAppState, | ||
settings: { ...mockAppState.settings, chainId: testChainId1 }, | ||
accounts: { | ||
...mockAppState.accounts, | ||
shieldedAccounts: shieldedAccounts, | ||
}, | ||
}); | ||
// let's define a mock function that our action is calling | ||
jest | ||
.spyOn(shieldedTransfer, "getShieldedBalance") | ||
.mockImplementation( | ||
async ( | ||
chainId: string, | ||
inputAddress: string, | ||
_tokenAddress: string | ||
): Promise<string> => { | ||
// we want to assert later that the correct values are being passed from the action | ||
expectedChainId = chainId; | ||
expectedSpendingKey = inputAddress; | ||
|
||
// we return the test balance amount | ||
return Promise.resolve(`${testBalanceAmount}`); | ||
} | ||
); | ||
|
||
// run unit under test | ||
await store.dispatch(updateShieldedBalances()); | ||
|
||
// assert results | ||
// ensure that the parameter was correctly passed from action to a dependency | ||
expect(expectedChainId).toEqual(testChainId1); | ||
expect(expectedSpendingKey).toEqual(testspendingKey1); | ||
|
||
// then ensure that the actions was resolving with expected results | ||
const actionsReceivedByStore = await store.getActions(); | ||
expect(actionsReceivedByStore[0].type).toEqual( | ||
updateShieldedBalances.pending.type | ||
); | ||
expect(actionsReceivedByStore[1].type).toEqual( | ||
updateShieldedBalances.fulfilled.type | ||
); | ||
|
||
// lets get the returned balance of the account from the payload | ||
const balanceOfShieldedAccountUuidInClient = | ||
actionsReceivedByStore[1].payload.shieldedBalances[ | ||
shieldedAccountUuidInClient | ||
]; | ||
expect(balanceOfShieldedAccountUuidInClient).toEqual(testBalanceAmount); | ||
expect(balanceOfShieldedAccountUuidInClient).not.toEqual( | ||
`${testBalanceAmount}` | ||
); | ||
}); | ||
|
||
it("should behave as expected in faulty response from wasm call in updateShieldedBalances", async () => { | ||
// setup data | ||
const testChainId1 = "testChainId1"; | ||
const testBalanceAmount = "aaa"; | ||
const shieldedAccountUuidInClient = "shieldedAccountUuidInClient1"; | ||
const shieldedAccounts: { | ||
[key: string]: { [key: string]: ShieldedAccount }; | ||
} = { | ||
testChainId1: { | ||
[shieldedAccountUuidInClient]: { | ||
chainId: testChainId1, | ||
shieldedKeysAndPaymentAddress: { | ||
viewingKey: "viewingKey", | ||
spendingKey: "testspendingKey1", | ||
paymentAddress: "paymentAddress", | ||
}, | ||
isShielded: true, | ||
alias: "Namada", | ||
balance: 0, | ||
address: "this_should_be_the_current_masp_address", | ||
tokenType: "ETH", | ||
signingKey: "signingKey", | ||
publicKey: "publicKey", | ||
id: shieldedAccountUuidInClient, | ||
}, | ||
}, | ||
}; | ||
// we create this here and set in the mocked function below to be able to assert that | ||
// it was called with the right data | ||
|
||
const store = mockStore({ | ||
...mockAppState, | ||
settings: { ...mockAppState.settings, chainId: testChainId1 }, | ||
accounts: { | ||
...mockAppState.accounts, | ||
shieldedAccounts: shieldedAccounts, | ||
}, | ||
}); | ||
// let's define a mock function that our action is calling | ||
jest | ||
.spyOn(shieldedTransfer, "getShieldedBalance") | ||
.mockImplementation( | ||
async ( | ||
_chainId: string, | ||
_inputAddress: string, | ||
_tokenAddress: string | ||
): Promise<string> => { | ||
// we return the test balance amount | ||
return Promise.resolve(`${testBalanceAmount}`); | ||
} | ||
); | ||
|
||
// run unit under test | ||
await store.dispatch(updateShieldedBalances()); | ||
|
||
// then ensure that the actions was resolving with expected results | ||
const actionsReceivedByStore = await store.getActions(); | ||
expect(actionsReceivedByStore[0].type).toEqual( | ||
updateShieldedBalances.pending.type | ||
); | ||
expect(actionsReceivedByStore[1].type).toEqual( | ||
updateShieldedBalances.fulfilled.type | ||
); | ||
|
||
// lets get the returned balance of the account from the payload | ||
const balanceOfShieldedAccountUuidInClient = | ||
actionsReceivedByStore[1].payload.shieldedBalances[ | ||
shieldedAccountUuidInClient | ||
]; | ||
expect(balanceOfShieldedAccountUuidInClient).toEqual( | ||
AccountErrors.NonNumericShieldedBalanceReturned | ||
); | ||
}); | ||
|
||
it("should behave as expected when the action throws", async () => { | ||
// setup data | ||
const testChainId1 = "testChainId1"; | ||
const testBalanceAmount = "aaa"; | ||
const shieldedAccountUuidInClient = "shieldedAccountUuidInClient1"; | ||
const shieldedAccounts: { | ||
[key: string]: { [key: string]: ShieldedAccount }; | ||
} = { | ||
[testChainId1 + "wrong_key"]: { | ||
[shieldedAccountUuidInClient]: { | ||
chainId: testChainId1, | ||
shieldedKeysAndPaymentAddress: { | ||
viewingKey: "viewingKey", | ||
spendingKey: "testspendingKey1", | ||
paymentAddress: "paymentAddress", | ||
}, | ||
isShielded: true, | ||
alias: "Namada", | ||
balance: 0, | ||
address: "this_should_be_the_current_masp_address", | ||
tokenType: "ETH", | ||
signingKey: "signingKey", | ||
publicKey: "publicKey", | ||
id: shieldedAccountUuidInClient, | ||
}, | ||
}, | ||
}; | ||
// we create this here and set in the mocked function below to be able to assert that | ||
// it was called with the right data | ||
|
||
const store = mockStore({ | ||
...mockAppState, | ||
settings: { ...mockAppState.settings, chainId: testChainId1 }, | ||
accounts: { | ||
...mockAppState.accounts, | ||
shieldedAccounts: shieldedAccounts, | ||
}, | ||
}); | ||
// let's define a mock function that our action is calling | ||
jest | ||
.spyOn(shieldedTransfer, "getShieldedBalance") | ||
.mockImplementation( | ||
async ( | ||
_chainId: string, | ||
_inputAddress: string, | ||
_tokenAddress: string | ||
): Promise<string> => { | ||
// we return the test balance amount | ||
return Promise.resolve(`${testBalanceAmount}`); | ||
} | ||
); | ||
|
||
// run unit under test | ||
await store.dispatch(updateShieldedBalances()); | ||
|
||
// then ensure that the actions was resolving with expected results | ||
const actionsReceivedByStore = await store.getActions(); | ||
expect(actionsReceivedByStore[0].type).toEqual( | ||
updateShieldedBalances.pending.type | ||
); | ||
expect(actionsReceivedByStore[1].type).toEqual( | ||
updateShieldedBalances.rejected.type | ||
); | ||
expect(actionsReceivedByStore[1].payload).toEqual( | ||
AccountErrors.RetrievingShieldedBalancesFailed | ||
); | ||
}); | ||
}); | ||
}); |
5 changes: 5 additions & 0 deletions
5
packages/anoma-wallet/src/slices/AccountsNew/__tests__/AccountsNew.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,5 @@ | ||
import ReactTestUtils from "react-dom/test-utils"; | ||
|
||
describe("ShieldedTransfer", () => { | ||
it.skip("testing the component's behavior", async () => {}); | ||
}); |
Oops, something went wrong.