|
| 1 | +import * as api from "$lib/api/governance.api"; |
| 2 | +import { MIN_DISBURSEMENT_WITH_VARIANCE } from "$lib/constants/neurons.constants"; |
| 3 | +import NnsDisburseMaturityModal from "$lib/modals/neurons/NnsDisburseMaturityModal.svelte"; |
| 4 | +import { neuronsStore } from "$lib/stores/neurons.store"; |
| 5 | +import { mockIdentity, resetIdentity } from "$tests/mocks/auth.store.mock"; |
| 6 | +import { mockMainAccount } from "$tests/mocks/icp-accounts.store.mock"; |
| 7 | +import { renderModal } from "$tests/mocks/modal.mock"; |
| 8 | +import { mockNeuron } from "$tests/mocks/neurons.mock"; |
| 9 | +import { DisburseMaturityModalPo } from "$tests/page-objects/DisburseMaturityModal.page-object"; |
| 10 | +import { JestPageObjectElement } from "$tests/page-objects/jest.page-object"; |
| 11 | +import { setAccountsForTesting } from "$tests/utils/accounts.test-utils"; |
| 12 | +import { runResolvedPromises } from "$tests/utils/timers.test-utils"; |
| 13 | +import { busyStore, toastsStore } from "@dfinity/gix-components"; |
| 14 | +import type { NeuronInfo } from "@dfinity/nns"; |
| 15 | +import { get } from "svelte/store"; |
| 16 | + |
| 17 | +vi.mock("$lib/api/governance.api"); |
| 18 | + |
| 19 | +describe("NnsDisburseMaturityModal", () => { |
| 20 | + const minMaturityForDisbursement = MIN_DISBURSEMENT_WITH_VARIANCE; |
| 21 | + const enoughMaturityToDisburse1Percent = minMaturityForDisbursement * 100n; |
| 22 | + const testNeuron = ( |
| 23 | + maturityE8sEquivalent: bigint = enoughMaturityToDisburse1Percent |
| 24 | + ): NeuronInfo => ({ |
| 25 | + ...mockNeuron, |
| 26 | + fullNeuron: { |
| 27 | + ...mockNeuron.fullNeuron, |
| 28 | + maturityE8sEquivalent, |
| 29 | + controller: mockIdentity.getPrincipal().toText(), |
| 30 | + }, |
| 31 | + }); |
| 32 | + |
| 33 | + beforeEach(() => { |
| 34 | + resetIdentity(); |
| 35 | + setAccountsForTesting({ |
| 36 | + main: mockMainAccount, |
| 37 | + hardwareWallets: [], |
| 38 | + }); |
| 39 | + }); |
| 40 | + |
| 41 | + const renderNnsDisburseMaturityModal = async ({ |
| 42 | + neuron, |
| 43 | + close, |
| 44 | + }: { |
| 45 | + neuron: NeuronInfo; |
| 46 | + close?: () => void; |
| 47 | + }): Promise<DisburseMaturityModalPo> => { |
| 48 | + const { container } = await renderModal({ |
| 49 | + component: NnsDisburseMaturityModal, |
| 50 | + props: { |
| 51 | + neuron, |
| 52 | + neuronId: neuron.neuronId, |
| 53 | + close, |
| 54 | + }, |
| 55 | + }); |
| 56 | + return DisburseMaturityModalPo.under(new JestPageObjectElement(container)); |
| 57 | + }; |
| 58 | + |
| 59 | + it("should display total maturity", async () => { |
| 60 | + const po = await renderNnsDisburseMaturityModal({ |
| 61 | + neuron: testNeuron(minMaturityForDisbursement), |
| 62 | + }); |
| 63 | + // MINIMUM_DISBURSEMENT / MATURITY_MODULATION_VARIANCE_PERCENTAGE |
| 64 | + expect(await po.getTotalMaturity()).toBe("1.05"); |
| 65 | + }); |
| 66 | + |
| 67 | + it("should disable next button when 0 selected", async () => { |
| 68 | + const po = await renderNnsDisburseMaturityModal({ neuron: testNeuron() }); |
| 69 | + await po.setPercentage(100); |
| 70 | + expect(await po.isNextButtonDisabled()).toBe(false); |
| 71 | + await po.setPercentage(0); |
| 72 | + expect(await po.isNextButtonDisabled()).toBe(true); |
| 73 | + }); |
| 74 | + |
| 75 | + it("should enable next button when enough maturity is selected", async () => { |
| 76 | + const po = await renderNnsDisburseMaturityModal({ neuron: testNeuron() }); |
| 77 | + await po.setPercentage(1); |
| 78 | + expect(await po.isNextButtonDisabled()).toBe(false); |
| 79 | + }); |
| 80 | + |
| 81 | + it("should the main address be selected by default", async () => { |
| 82 | + const po = await renderNnsDisburseMaturityModal({ neuron: testNeuron() }); |
| 83 | + await po.setPercentage(10); |
| 84 | + await po.clickNextButton(); |
| 85 | + expect(await po.getConfirmPercentage()).toEqual("10%"); |
| 86 | + expect(await po.getConfirmDestination()).toEqual("Main"); |
| 87 | + }); |
| 88 | + |
| 89 | + it("should disable next button if amount of maturity is less than enough", async () => { |
| 90 | + const po = await renderNnsDisburseMaturityModal({ |
| 91 | + neuron: testNeuron(minMaturityForDisbursement), |
| 92 | + }); |
| 93 | + await po.setPercentage(99); |
| 94 | + expect(await po.isNextButtonDisabled()).toBe(true); |
| 95 | + await po.setPercentage(100); |
| 96 | + expect(await po.isNextButtonDisabled()).toBe(false); |
| 97 | + }); |
| 98 | + |
| 99 | + it("should display summary information in the last step", async () => { |
| 100 | + const po = await renderNnsDisburseMaturityModal({ neuron: testNeuron() }); |
| 101 | + await po.setPercentage(50); |
| 102 | + await po.clickNextButton(); |
| 103 | + expect(await po.getConfirmPercentage()).toEqual("50%"); |
| 104 | + expect(await po.getConfirmTokens()).toBe("50.00-55.26 ICP"); |
| 105 | + expect(await po.getConfirmDestination()).toEqual("Main"); |
| 106 | + }); |
| 107 | + |
| 108 | + it("should successfully disburse maturity", async () => { |
| 109 | + const neuron = testNeuron(); |
| 110 | + const close = vi.fn(); |
| 111 | + let resolveDisburseMaturity; |
| 112 | + const spyDisburseMaturity = vi |
| 113 | + .spyOn(api, "disburseMaturity") |
| 114 | + .mockImplementation( |
| 115 | + () => new Promise((resolve) => (resolveDisburseMaturity = resolve)) |
| 116 | + ); |
| 117 | + const spyQueryNeurons = vi |
| 118 | + .spyOn(api, "queryNeurons") |
| 119 | + .mockResolvedValue([neuron]); |
| 120 | + // Add the neuron to the store to avoid extra query from getIdentityOfControllerByNeuronId |
| 121 | + neuronsStore.setNeurons({ |
| 122 | + neurons: [neuron], |
| 123 | + certified: true, |
| 124 | + }); |
| 125 | + const po = await renderNnsDisburseMaturityModal({ neuron, close }); |
| 126 | + |
| 127 | + await po.setPercentage(100); |
| 128 | + await po.clickNextButton(); |
| 129 | + |
| 130 | + expect( |
| 131 | + await po.getNeuronConfirmActionScreenPo().getConfirmButton().isDisabled() |
| 132 | + ).toEqual(false); |
| 133 | + expect(spyDisburseMaturity).toHaveBeenCalledTimes(0); |
| 134 | + expect(spyQueryNeurons).toHaveBeenCalledTimes(0); |
| 135 | + expect(get(busyStore)).toEqual([]); |
| 136 | + expect(get(toastsStore)).toEqual([]); |
| 137 | + |
| 138 | + await po.clickConfirmButton(); |
| 139 | + await runResolvedPromises(); |
| 140 | + |
| 141 | + expect(spyQueryNeurons).toHaveBeenCalledTimes(0); |
| 142 | + expect(spyDisburseMaturity).toHaveBeenCalledTimes(1); |
| 143 | + expect(spyDisburseMaturity).toHaveBeenCalledWith({ |
| 144 | + neuronId: neuron.neuronId, |
| 145 | + percentageToDisburse: 100, |
| 146 | + identity: mockIdentity, |
| 147 | + }); |
| 148 | + expect(close).toHaveBeenCalledTimes(0); |
| 149 | + expect(get(busyStore)).toEqual([ |
| 150 | + { |
| 151 | + initiator: "disburse-maturity", |
| 152 | + text: undefined, |
| 153 | + }, |
| 154 | + ]); |
| 155 | + expect(get(toastsStore)).toEqual([]); |
| 156 | + |
| 157 | + resolveDisburseMaturity(); |
| 158 | + await runResolvedPromises(); |
| 159 | + |
| 160 | + expect(spyQueryNeurons).toHaveBeenCalledTimes(2); |
| 161 | + expect(spyQueryNeurons).toHaveBeenCalledWith( |
| 162 | + expect.objectContaining({ |
| 163 | + certified: false, |
| 164 | + }) |
| 165 | + ); |
| 166 | + expect(spyQueryNeurons).toHaveBeenCalledWith( |
| 167 | + expect.objectContaining({ |
| 168 | + certified: true, |
| 169 | + }) |
| 170 | + ); |
| 171 | + expect(get(busyStore)).toEqual([]); |
| 172 | + expect(get(toastsStore)).toMatchObject([ |
| 173 | + { |
| 174 | + level: "success", |
| 175 | + text: "Maturity successfully disbursed.", |
| 176 | + }, |
| 177 | + ]); |
| 178 | + expect(close).toHaveBeenCalledTimes(1); |
| 179 | + }); |
| 180 | +}); |
0 commit comments