From 15643a58747bab0327f3f8016bd2209961b6e5cd Mon Sep 17 00:00:00 2001 From: sugh01 <19183308+sugh01@users.noreply.github.com> Date: Wed, 18 Dec 2024 23:10:59 +0100 Subject: [PATCH] extend names unit tests --- e2e/specs/stateless/extendNames.spec.ts | 16 +++ .../transaction/extendNames.test.ts | 102 ++++++++++++++++-- 2 files changed, 109 insertions(+), 9 deletions(-) diff --git a/e2e/specs/stateless/extendNames.spec.ts b/e2e/specs/stateless/extendNames.spec.ts index b36103402..a4f906b5b 100644 --- a/e2e/specs/stateless/extendNames.spec.ts +++ b/e2e/specs/stateless/extendNames.spec.ts @@ -378,6 +378,10 @@ test('should be able to extend a name by a month', async ({ await test.step('should extend', async () => { await extendNamesModal.getExtendButton.click() const transactionModal = makePageObject('TransactionModal') + + // Verify duration and new expiry display in transaction modal + await expect(page.getByText('1 month')).toBeVisible() + await transactionModal.autoComplete() const newTimestamp = await profilePage.getExpiryTimestamp() @@ -442,6 +446,10 @@ test('should be able to extend a name by a day', async ({ await test.step('should extend', async () => { await extendNamesModal.getExtendButton.click() const transactionModal = makePageObject('TransactionModal') + + // Verify duration and new expiry display in transaction modal + await expect(page.getByText('1 day')).toBeVisible() + await transactionModal.autoComplete() const newTimestamp = await profilePage.getExpiryTimestamp() @@ -516,6 +524,10 @@ test('should be able to extend a name in grace period by a month', async ({ await test.step('should extend', async () => { await extendNamesModal.getExtendButton.click() const transactionModal = makePageObject('TransactionModal') + + // Verify duration and new expiry display in transaction modal + await expect(page.getByText('1 month')).toBeVisible() + await transactionModal.autoComplete() const newTimestamp = await profilePage.getExpiryTimestamp() @@ -592,6 +604,10 @@ test('should be able to extend a name in grace period by 1 day', async ({ await test.step('should extend', async () => { await extendNamesModal.getExtendButton.click() const transactionModal = makePageObject('TransactionModal') + + // Verify duration and new expiry display in transaction modal + await expect(page.getByText('1 day')).toBeVisible() + await transactionModal.autoComplete() const newTimestamp = await profilePage.getExpiryTimestamp() diff --git a/src/transaction-flow/transaction/extendNames.test.ts b/src/transaction-flow/transaction/extendNames.test.ts index 38f36d333..7081c2352 100644 --- a/src/transaction-flow/transaction/extendNames.test.ts +++ b/src/transaction-flow/transaction/extendNames.test.ts @@ -1,19 +1,33 @@ import { mockFunction } from '@app/test-utils' -import { describe, expect, it, vi } from 'vitest' +import { beforeEach, describe, expect, it, vi } from 'vitest' + +import { getPrice } from '@ensdomains/ensjs/public' +import { renewNames } from '@ensdomains/ensjs/wallet' + +import { ClientWithEns, ConnectorClientWithEns } from '@app/types' import extendNamesTransaction from './extendNames' -const mockT = vi.fn((key: string, options?: any) => { - if (key === 'transaction.extendNames.actionValue') return 'Extend' - if (key === 'transaction.extendNames.costValue') return `Cost: ${options.value}` - if (key === 'unit.years') return `${options.count} year` - if (key === 'unit.months') return `${options.count} months` - if (key === 'unit.days') return `${options.count} days` - return key -}) +vi.mock('@ensdomains/ensjs/public') +vi.mock('@ensdomains/ensjs/wallet') + +const mockGetPrice = mockFunction(getPrice) +const mockRenewNames = mockFunction(renewNames.makeFunctionData) describe('extendNamesTransaction', () => { + const mockClient = {} as ClientWithEns + const mockConnectorClient = {} as ConnectorClientWithEns + + const mockT = vi.fn((key: string, options?: any) => { + if (key === 'transaction.extendNames.actionValue') return 'Extend' + if (key === 'transaction.extendNames.costValue') return `Cost: ${options.value}` + if (key === 'unit.years') return `${options.count} year` + if (key === 'unit.months') return `${options.count} months` + if (key === 'unit.days') return `${options.count} days` + return key + }) + describe('displayItems', () => { it('should format display items correctly for a single name', () => { const JAN_1_2022_TIMESTAMP = 1640995200000 // 2022-01-01T00:00:00.000Z @@ -136,5 +150,75 @@ describe('extendNamesTransaction', () => { }) }) }) + + it('should return display items without newExpiry when startDateTimestamp is not provided', () => { + const data = { + names: ['test.eth'], + duration: 31536000, + displayPrice: '1.02', + } + + const result = extendNamesTransaction.displayItems(data, mockT) + expect(result[2].value.newExpiry).toBeUndefined() + }) + }) + + describe('transaction', () => { + beforeEach(() => { + mockGetPrice.mockReset() + mockRenewNames.mockReset() + }) + + it('should calculate price and create transaction data', async () => { + const data = { + names: ['test.eth'], + duration: 31536000, // 1 year + } + + mockGetPrice.mockResolvedValue({ base: BigInt('1000000000000000000'), premium: 0n }) // 1 ETH + mockRenewNames.mockResolvedValue({ + to: '0x123' as Address, + data: '0x456' as Hex, + value: BigInt('1020000000000000000'), // 1.02 ETH (with 2% buffer) + }) + + const result = await extendNamesTransaction.transaction({ + client: mockClient, + connectorClient: mockConnectorClient, + data, + }) + + expect(mockGetPrice).toHaveBeenCalledWith(mockClient, { + nameOrNames: ['test.eth'], + duration: 31536000, + }) + expect(mockRenewNames).toHaveBeenCalledWith(mockConnectorClient, { + nameOrNames: ['test.eth'], + duration: 31536000, + value: BigInt('1020000000000000000'), + }) + expect(result).toEqual({ + to: '0x123', + data: '0x456', + value: BigInt('1020000000000000000'), + }) + }) + + it('should throw error when price is not found', async () => { + const data = { + names: ['test.eth'], + duration: 31536000, + } + + mockGetPrice.mockResolvedValue(null) + + await expect( + extendNamesTransaction.transaction({ + client: mockClient, + connectorClient: mockConnectorClient, + data, + }), + ).rejects.toThrow('No price found') + }) }) })