Skip to content

Commit

Permalink
Increase test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
lubej committed Sep 13, 2023
1 parent ccb5147 commit deda6af
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 4 deletions.
32 changes: 31 additions & 1 deletion src/app/lib/__tests__/ledger.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
import { Ledger, LedgerSigner, requestDevice } from '../ledger'
import { canAccessBle, Ledger, LedgerSigner, requestDevice } from '../ledger'
import OasisApp from '@oasisprotocol/ledger'
import { WalletError, WalletErrors } from 'types/errors'
import { Wallet, WalletType } from 'app/state/wallet/types'
import { isSupported, requestLedgerDevice } from '@ledgerhq/hw-transport-webusb/lib-es/webusb'
import BleTransport from '@oasisprotocol/ionic-ledger-hw-transport-ble/lib'

jest.mock('@ledgerhq/hw-transport-webusb/lib-es/webusb')
jest.mock('@oasisprotocol/ionic-ledger-hw-transport-ble/lib', () => {
return {
isEnabled: jest.fn(),
}
})

jest.mock('@oasisprotocol/ledger', () => ({
...(jest.createMockFromModule('@oasisprotocol/ledger') as any),
Expand All @@ -31,6 +37,30 @@ describe('Ledger Library', () => {
jest.resetAllMocks()
})

describe('BLE Ledger', () => {
it('should support Bluetooth', async () => {
;(BleTransport.isEnabled as jest.Mock).mockResolvedValue(true)
Object.defineProperty(window.navigator, 'bluetooth', {
writable: true,
value: {
requestLEScan: jest.fn(),
},
})

const canAccessBluetooth = await canAccessBle()
expect(canAccessBluetooth).toBe(true)
})

it('should not throw if platform does not support Bluetooth', async () => {
;(BleTransport.isEnabled as jest.Mock).mockRejectedValue(
new Error('Platform does not support Bluetooth'),
)

const canAccessBluetooth = await canAccessBle()
expect(canAccessBluetooth).toBe(false)
})
})

describe('Ledger', () => {
it('enumerateAccounts should pass when Oasis App is open', async () => {
mockAppIsOpen('Oasis')
Expand Down
59 changes: 56 additions & 3 deletions src/app/state/transaction/saga.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,22 @@ import { DeepPartialRootState } from 'types/RootState'
import { WalletErrors } from 'types/errors'
import { transactionActions as actions } from '.'
import { TransactionTypes } from '../paratimes/types'
import { selectAddress, selectActiveWallet } from '../wallet/selectors'
import { selectActiveWallet, selectAddress } from '../wallet/selectors'
import { Wallet, WalletType } from '../wallet/types'
import { doTransaction, submitParaTimeTransaction, getAllowanceDifference, setAllowance } from './saga'
import { doTransaction, getAllowanceDifference, setAllowance, submitParaTimeTransaction } from './saga'
import { addressToPublicKey } from '../../lib/helpers'
import BleTransport from '@oasisprotocol/ionic-ledger-hw-transport-ble/lib'

const makeState = (wallet: Partial<Wallet>): DeepPartialRootState => {
const makeState = (
wallet: Partial<Wallet>,
rootState: Partial<DeepPartialRootState> = {},
): DeepPartialRootState => {
return {
wallet: {
wallets: { [wallet.address!]: wallet },
selectedWallet: wallet.address,
},
...rootState,
}
}

Expand Down Expand Up @@ -52,6 +58,7 @@ describe('Transaction Sagas', () => {
[matchers.call.fn(OasisTransaction.sign), {}],
[matchers.call.fn(OasisTransaction.signParaTime), {}],
[matchers.call.fn(OasisTransaction.submit), {}],
[matchers.call.fn(OasisTransaction.signUsingLedger), {}],
]

const providers: (EffectProviders | StaticProvider)[] = [
Expand Down Expand Up @@ -79,6 +86,52 @@ describe('Transaction Sagas', () => {
.run()
})

it('Should send transactions from a Bluetooth Ledger Wallet', async () => {
const wallet = {

Check failure on line 90 in src/app/state/transaction/saga.test.ts

View workflow job for this annotation

GitHub Actions / lint

Conversion of type '{ type: WalletType.BleLedger; path: number[]; publicKey: Uint8Array; address?: string | undefined; pathDisplay?: string | undefined; privateKey?: string | undefined; balance?: BalanceDetails | undefined; }' to type 'Partial<Wallet>' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert the expression to 'unknown' first.
...validKeyWallet,
type: WalletType.BleLedger,
path: [44, 474, 0, 0, 0],
publicKey: await addressToPublicKey(matchingAddress),
} as Partial<Wallet>

const selectedBleDevice = {
device: {
deviceId: 'xx:xx:xx:xx:xx:xx',
name: 'Nano X ABCD',
},
localName: 'Nano X ABCD',
rssi: -50,
txPower: 100,
}

return expectSaga(
doTransaction,
actions.sendTransaction({ type: 'transfer', amount: '10000000000', to: validAddress }),
)
.withState(
makeState(wallet, {
importAccounts: {
selectedBleDevice,
},
}),
)
.provide(providers)
.provide([
...sendProviders,
[matchers.call.fn(BleTransport.isSupported), true],
[
matchers.call.fn(BleTransport.open),
{
close: () => {},
},
],
])
.dispatch(actions.sendTransaction({ type: 'transfer', amount: '123000000000', to: 'testaddress' }))
.dispatch(actions.confirmTransaction())
.put.actionType(actions.transactionSent.type)
.run()
})

const runtime = {
address: 'oasis1qrnu9yhwzap7rqh6tdcdcpz0zf86hwhycchkhvt8',
id: '000000000000000000000000000000000000000000000000e199119c992377cb',
Expand Down

0 comments on commit deda6af

Please sign in to comment.