Skip to content

Commit

Permalink
extend names unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sugh01 committed Dec 18, 2024
1 parent dffb4ce commit 15643a5
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 9 deletions.
16 changes: 16 additions & 0 deletions e2e/specs/stateless/extendNames.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down
102 changes: 93 additions & 9 deletions src/transaction-flow/transaction/extendNames.test.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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')
})
})
})

0 comments on commit 15643a5

Please sign in to comment.