Skip to content

Commit

Permalink
Use concise bigint notation in tests (#4123)
Browse files Browse the repository at this point in the history
# Motivation

Similar to #4114 but for test
files.

`bigint`s can be written as `123n` but in many places we write
`BigInt(123)` or `BigInt("123").
The former is more concise and also more accurate if the number actually
requires a bigint for full accuracy.

# Changes

1. Replace `BigInt(???)` with `???n`.
2. Add underscore separators for numbers with more than 3 digits if they
weren't there already.

# Tests

Still pass.

# Todos

- [ ] Add entry to changelog (if necessary).
not necessary
  • Loading branch information
dskloetd authored Jan 3, 2024
1 parent 8975afc commit 0abcfc9
Show file tree
Hide file tree
Showing 154 changed files with 1,299 additions and 1,309 deletions.
2 changes: 1 addition & 1 deletion frontend/src/tests/fakes/governance-api.fake.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ async function mergeNeurons({
// merge happened.
targetNeuron.fullNeuron.cachedNeuronStake +=
sourceNeuron.fullNeuron.cachedNeuronStake;
sourceNeuron.fullNeuron.cachedNeuronStake = BigInt(0);
sourceNeuron.fullNeuron.cachedNeuronStake = 0n;
}

async function simulateMergeNeurons({
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/tests/fakes/sns-governance-api.fake.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ async function getNeuronBalance({
// the fake.
return neuron.cached_neuron_stake_e8s;
}
return BigInt(0);
return 0n;
}

async function refreshNeuron(params: {
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/tests/fakes/sns-ledger-api.fake.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ async function transactionFee({
rootCanisterId: Principal;
certified: boolean;
}): Promise<bigint> {
return BigInt(10_000);
return 10_000n;
}

async function getSnsToken({
Expand Down
30 changes: 15 additions & 15 deletions frontend/src/tests/lib/api-services/governance.api-service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ const shouldInvalidateCacheOnFailure = async <P, R>({
};

describe("neurons api-service", () => {
const neuronId = BigInt(12);
const neuronId = 12n;

beforeEach(() => {
vi.resetAllMocks();
Expand Down Expand Up @@ -184,13 +184,13 @@ describe("neurons api-service", () => {
it("should call queryNeuron api", async () => {
expect(
await governanceApiService.queryNeuron({
neuronId: BigInt(1),
neuronId: 1n,
...params,
})
).toEqual(neuron1);
expect(
await governanceApiService.queryNeuron({
neuronId: BigInt(2),
neuronId: 2n,
...params,
})
).toEqual(neuron2);
Expand All @@ -199,7 +199,7 @@ describe("neurons api-service", () => {

it("should fail if queryNeuron api fails", async () => {
expect(() =>
governanceApiService.queryNeuron({ neuronId: BigInt(999), ...params })
governanceApiService.queryNeuron({ neuronId: 999n, ...params })
).rejects.toThrow("No neuron with id 999");
expect(api.queryNeuron).toHaveBeenCalledTimes(1);
});
Expand All @@ -208,7 +208,7 @@ describe("neurons api-service", () => {
await shouldNotInvalidateCache({
apiFunc: api.queryNeuron,
apiServiceFunc: governanceApiService.queryNeuron,
params: { neuronId: BigInt(1), ...params },
params: { neuronId: 1n, ...params },
});
});
});
Expand Down Expand Up @@ -455,7 +455,7 @@ describe("neurons api-service", () => {
const rewardEvent1: RewardEvent = mockRewardEvent;
const rewardEvent2: RewardEvent = {
...rewardEvent1,
rounds_since_last_distribution: [BigInt(2_000)],
rounds_since_last_distribution: [2_000n],
};
beforeEach(() => {
vi.spyOn(api, "queryLastestRewardEvent").mockImplementation(
Expand Down Expand Up @@ -603,7 +603,7 @@ describe("neurons api-service", () => {
neuronId,
identity: mockIdentity,
toAccount: mockMainAccount.identifier,
amount: BigInt(10_000_000),
amount: 10_000_000n,
};

it("should call disburse api", () => {
Expand Down Expand Up @@ -750,8 +750,8 @@ describe("neurons api-service", () => {
describe("mergeNeurons", () => {
const params = {
identity: mockIdentity,
sourceNeuronId: BigInt(2),
targetNeuronId: BigInt(20),
sourceNeuronId: 2n,
targetNeuronId: 20n,
};

it("should call mergeNeurons api", () => {
Expand Down Expand Up @@ -812,7 +812,7 @@ describe("neurons api-service", () => {
neuronId,
identity: mockIdentity,
topic: Topic.ExchangeRate,
followees: [BigInt(2), BigInt(20)],
followees: [2n, 20n],
};

it("should call setFollowees api", () => {
Expand Down Expand Up @@ -841,8 +841,8 @@ describe("neurons api-service", () => {
describe("simulateMergeNeurons", () => {
const params = {
identity: mockIdentity,
sourceNeuronId: BigInt(3),
targetNeuronId: BigInt(21),
sourceNeuronId: 3n,
targetNeuronId: 21n,
};

it("should call simulateMergeNeurons api", () => {
Expand Down Expand Up @@ -902,7 +902,7 @@ describe("neurons api-service", () => {
const params = {
neuronId,
identity: mockIdentity,
amount: BigInt(10_000_000),
amount: 10_000_000n,
};

it("should call splitNeuron api", async () => {
Expand Down Expand Up @@ -962,7 +962,7 @@ describe("neurons api-service", () => {
describe("stakeNeuron", () => {
const params = {
identity: mockIdentity,
stake: BigInt(10_000_000),
stake: 10_000_000n,
controller: mockPrincipal,
ledgerCanisterIdentity: mockIdentity,
fromSubaccount: new Uint8Array(),
Expand Down Expand Up @@ -995,7 +995,7 @@ describe("neurons api-service", () => {
describe("stakeNeuronIcrc1", () => {
const params = {
identity: mockIdentity,
stake: BigInt(10_000_000),
stake: 10_000_000n,
controller: mockPrincipal,
ledgerCanisterIdentity: mockIdentity,
fromSubaccount: new Uint8Array(),
Expand Down
26 changes: 12 additions & 14 deletions frontend/src/tests/lib/api/canisters.api.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,13 +204,11 @@ describe("canisters-api", () => {

describe("getIcpToCyclesExchangeRate", () => {
it("should call CMC to get conversion rate", async () => {
mockCMCCanister.getIcpToCyclesConversionRate.mockResolvedValue(
BigInt(10_000)
);
mockCMCCanister.getIcpToCyclesConversionRate.mockResolvedValue(10_000n);

const response = await getIcpToCyclesExchangeRate(mockIdentity);
expect(mockCMCCanister.getIcpToCyclesConversionRate).toBeCalled();
expect(response).toEqual(BigInt(10_000));
expect(response).toEqual(10_000n);
});
});

Expand All @@ -223,7 +221,7 @@ describe("canisters-api", () => {
});

it("should make a transfer, notify and attach the canister", async () => {
mockLedgerCanister.transfer.mockResolvedValue(BigInt(10));
mockLedgerCanister.transfer.mockResolvedValue(10n);
mockCMCCanister.notifyCreateCanister.mockResolvedValue(
mockCanisterDetails.id
);
Expand All @@ -245,7 +243,7 @@ describe("canisters-api", () => {
});

it("should attach the canister if name max length", async () => {
mockLedgerCanister.transfer.mockResolvedValue(BigInt(10));
mockLedgerCanister.transfer.mockResolvedValue(10n);
mockCMCCanister.notifyCreateCanister.mockResolvedValue(
mockCanisterDetails.id
);
Expand All @@ -267,7 +265,7 @@ describe("canisters-api", () => {
});

it("should notify twice if the first call returns Processing", async () => {
mockLedgerCanister.transfer.mockResolvedValue(BigInt(10));
mockLedgerCanister.transfer.mockResolvedValue(10n);
mockCMCCanister.notifyCreateCanister
.mockRejectedValueOnce(new ProcessingError())
.mockResolvedValue(mockCanisterDetails.id);
Expand All @@ -284,7 +282,7 @@ describe("canisters-api", () => {
});

it("handles creating from subaccounts", async () => {
mockLedgerCanister.transfer.mockResolvedValue(BigInt(10));
mockLedgerCanister.transfer.mockResolvedValue(10n);
mockCMCCanister.notifyCreateCanister.mockResolvedValue(
mockCanisterDetails.id
);
Expand Down Expand Up @@ -368,8 +366,8 @@ describe("canisters-api", () => {
});

it("should make a transfer and notify", async () => {
mockLedgerCanister.transfer.mockResolvedValue(BigInt(10));
mockCMCCanister.notifyTopUp.mockResolvedValue(BigInt(10));
mockLedgerCanister.transfer.mockResolvedValue(10n);
mockCMCCanister.notifyTopUp.mockResolvedValue(10n);

await topUpCanister({
identity: mockIdentity,
Expand All @@ -384,10 +382,10 @@ describe("canisters-api", () => {
});

it("should notify twice if the first returns ProcessingError", async () => {
mockLedgerCanister.transfer.mockResolvedValue(BigInt(10));
mockLedgerCanister.transfer.mockResolvedValue(10n);
mockCMCCanister.notifyTopUp
.mockRejectedValueOnce(new ProcessingError())
.mockResolvedValue(BigInt(10));
.mockResolvedValue(10n);

await topUpCanister({
identity: mockIdentity,
Expand All @@ -401,8 +399,8 @@ describe("canisters-api", () => {
});

it("should make a transfer from subaccounts", async () => {
mockLedgerCanister.transfer.mockResolvedValue(BigInt(10));
mockCMCCanister.notifyTopUp.mockResolvedValue(BigInt(10));
mockLedgerCanister.transfer.mockResolvedValue(10n);
mockCMCCanister.notifyTopUp.mockResolvedValue(10n);

const toSubAccount = principalToSubAccount(mockCanisterDetails.id);
// To create a canister you need to send ICP to an account owned by the CMC, so that the CMC can burn those funds.
Expand Down
Loading

0 comments on commit 0abcfc9

Please sign in to comment.