Skip to content

Commit

Permalink
fix: update test to account for multiple symbols on page
Browse files Browse the repository at this point in the history
  • Loading branch information
stanleyjones committed Dec 7, 2023
1 parent dde9eed commit fee7aff
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 36 deletions.
27 changes: 7 additions & 20 deletions src/features/transactions/components/txn-list/txn-list.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Event, SendEvent } from "@liftedinit/many-js"
import { Event } from "@liftedinit/many-js"
import {
Button,
ChevronRightIcon,
Expand All @@ -10,9 +10,9 @@ import {
Tbody,
TableContainer,
Text,
arrayBufferToBase64,
} from "@liftedinit/ui"
import {
calculateBalances,
useAllTransactionsList,
useTransactionsList,
} from "features/transactions/queries"
Expand Down Expand Up @@ -46,26 +46,13 @@ export function TxnList({
const { data: allTxns } = useAllTransactionsList({ accounts })
const { data: balances } = useBalances({ address })

const txnBalances: Map<string, bigint> = new Map()
let txnBalances = new Map()
if (allTxns && balances) {
const running: Map<string, bigint> = new Map()
balances.ownedAssetsWithBalance.forEach(({ identity, balance }) =>
running.set(identity, balance),
txnBalances = calculateBalances(
allTxns.transactions,
balances.ownedAssetsWithBalance,
address,
)
allTxns.transactions.forEach(txn => {
switch (txn.type) {
case "send": {
const { id, symbolAddress: symbol, amount, from } = txn as SendEvent
const balance = running.get(symbol) ?? BigInt(0)
txnBalances.set(arrayBufferToBase64(id), balance)
const updated = from === address ? balance - amount : balance + amount
running.set(symbol, updated)
break
}
default:
break
}
})
}

if (isError && error) {
Expand Down
51 changes: 50 additions & 1 deletion src/features/transactions/queries.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
import { useNetworkContext } from "features/network"
import { BoundType, Event, ListOrderType, Network } from "@liftedinit/many-js"
import {
BoundType,
BurnEvent,
Event,
ListOrderType,
MintEvent,
Network,
SendEvent,
} from "@liftedinit/many-js"
import { useMutation, useQuery, useQueryClient } from "react-query"
import { useMemo, useState } from "react"
import { arrayBufferToBase64 } from "@liftedinit/ui"
import { Asset } from "features/balances"

const PAGE_SIZE = 11
const MAX_PAGE_SIZE = 100
Expand Down Expand Up @@ -304,3 +313,43 @@ export function useAllTransactionsList({
},
}
}

export function calculateBalances(
transactions: ProcessedEvent[],
balances: Asset[],
address: string,
): Map<string, bigint> {
const txnBalances = new Map()
const running = new Map()

balances.forEach(({ identity, balance }) => running.set(identity, balance))
transactions.forEach(txn => {
if (["send", "mint", "burn"].includes(txn.type)) {
const { id, symbolAddress } = txn as SendEvent
const balance = running.get(symbolAddress) ?? BigInt(0)
txnBalances.set(arrayBufferToBase64(id), balance)
let updated

switch (txn.type) {
case "send": {
const { amount, from } = txn as SendEvent
updated = from === address ? balance + amount : balance - amount
break
}
case "mint": {
const { amounts } = txn as MintEvent
updated = balance + amounts[address]
break

Check warning on line 342 in src/features/transactions/queries.ts

View check run for this annotation

Codecov / codecov/patch

src/features/transactions/queries.ts#L339-L342

Added lines #L339 - L342 were not covered by tests
}
case "burn": {
const { amounts } = txn as BurnEvent
updated = balance - amounts[address]
break

Check warning on line 347 in src/features/transactions/queries.ts

View check run for this annotation

Codecov / codecov/patch

src/features/transactions/queries.ts#L344-L347

Added lines #L344 - L347 were not covered by tests
}
}

running.set(symbolAddress, updated)
}
})
return txnBalances
}
26 changes: 11 additions & 15 deletions src/views/home/__tests__/home.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ const mockListData = {
const mockBalanceData = {
balances: new Map([
["mabc", BigInt(1000000)],
["mghi", BigInt(5000000)],
["mghi", BigInt(6000000)],
]),
}

Expand Down Expand Up @@ -114,14 +114,14 @@ describe("home page", () => {
expect(assetsTab).toBeInTheDocument()
expect(activityTab).toBeInTheDocument()
})
it("should list the balances of each token the account holds", async function () {
it("should list the balances of each token the account holds", async function() {
setupHome()

expect(screen.getByText(/abc/i)).toBeInTheDocument()
expect(screen.getAllByText(/abc/i)[0]).toBeInTheDocument()
expect(screen.getByText("0.001")).toBeInTheDocument()

expect(screen.getByText(/ghi/i)).toBeInTheDocument()
expect(screen.getByText("0.005")).toBeInTheDocument()
expect(screen.getAllByText(/ghi/i)[0]).toBeInTheDocument()
expect(screen.getByText("0.006")).toBeInTheDocument()
})
it("should display an error message", async () => {
mockNetwork.ledger.balance = jest.fn().mockImplementation(async arg => {
Expand Down Expand Up @@ -164,14 +164,12 @@ describe("home page", () => {
const assets = await screen.findAllByLabelText(/asset list item/i)
userEvent.click(assets[0])
expect(screen.getByText(/0.001 abc/i)).toBeInTheDocument()
await waitFor(() =>
expect(mockNetwork.events.list).toHaveBeenCalledTimes(1),
)
await waitFor(() => expect(mockNetwork.events.list).toHaveBeenCalled())
const rows = await screen.findAllByRole("row")
expect(rows.length).toBe(2)

const firstTxn = within(rows[0])
expect(firstTxn.getByText(/abc/i)).toBeInTheDocument()
expect(firstTxn.getAllByText(/abc/i)[0]).toBeInTheDocument()
expect(firstTxn.getByText(/send/i)).toBeInTheDocument()
expect(firstTxn.getByText(/-0.000000001/i)).toBeInTheDocument()
expect(firstTxn.getByText(/2023/i)).toBeInTheDocument()
Expand All @@ -180,7 +178,7 @@ describe("home page", () => {
expect(firstTxn.getByText(/8:03:00/i)).toBeInTheDocument()

const secondTxn = within(rows[1])
expect(secondTxn.getByText(/abc/i)).toBeInTheDocument()
expect(secondTxn.getAllByText(/abc/i)[0]).toBeInTheDocument()
expect(secondTxn.getByText(/receive/i)).toBeInTheDocument()
expect(secondTxn.getByText(/\+0.000000003/i)).toBeInTheDocument()
expect(secondTxn.getByText(/2022/i)).toBeInTheDocument()
Expand All @@ -192,9 +190,7 @@ describe("home page", () => {
const { activityTab } = setupHome()
userEvent.click(activityTab)

await waitFor(() =>
expect(mockNetwork.events.list).toHaveBeenCalledTimes(1),
)
await waitFor(() => expect(mockNetwork.events.list).toHaveBeenCalled())

const rows = screen.getAllByRole("row")

Expand All @@ -205,15 +201,15 @@ describe("home page", () => {
expect(screen.getByText(/9/i)).toBeInTheDocument()
expect(screen.getByText(/8:03:00/i)).toBeInTheDocument()
expect(screen.getByText(/-0.000000001/i)).toBeInTheDocument()
expect(screen.getByText(/abc/i)).toBeInTheDocument()
expect(screen.getAllByText(/abc/i)[0]).toBeInTheDocument()

expect(screen.getByText(/receive/i)).toBeInTheDocument()
expect(screen.getByText(/2022/i)).toBeInTheDocument()
expect(screen.getByText(/4/i)).toBeInTheDocument()
expect(screen.getByText(/9/i)).toBeInTheDocument()
expect(screen.getByText(/8:01:00/i)).toBeInTheDocument()
expect(screen.getByText(/\+0.000000003/i)).toBeInTheDocument()
expect(screen.getByText(/ghi/i)).toBeInTheDocument()
expect(screen.getAllByText(/ghi/i)[0]).toBeInTheDocument()
})
})

Expand Down

0 comments on commit fee7aff

Please sign in to comment.