-
Notifications
You must be signed in to change notification settings - Fork 120
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add a validation function to handle seconds parameter in ExtendNames
- Loading branch information
1 parent
6086c21
commit d8d2ee1
Showing
6 changed files
with
240 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,170 @@ | ||
import { it, expect, describe} from "vitest"; | ||
|
||
import { calculateRenewState } from "./useRenew"; | ||
|
||
describe('calculateRenewState', () => { | ||
it('should return connect-user if accountStatus is disconnected', () => { | ||
expect(calculateRenewState({ | ||
registrationStatus: 'gracePeriod', | ||
isRegistrationStatusLoading: false, | ||
renewSeconds: 123, | ||
connectModalOpen: false, | ||
accountStatus: 'disconnected', | ||
isAbilitiesLoading: false, | ||
isRouterReady: true, | ||
name: 'name', | ||
openedConnectModal: false, | ||
openConnectModal: () => {} | ||
})).toBe('connect-user') | ||
}) | ||
|
||
it('should return connect-user if accountStatus is connected', () => { | ||
expect(calculateRenewState({ | ||
registrationStatus: 'registered', | ||
isRegistrationStatusLoading: false, | ||
renewSeconds: 123, | ||
connectModalOpen: false, | ||
accountStatus: 'connected', | ||
isAbilitiesLoading: false, | ||
isRouterReady: true, | ||
name: 'name', | ||
openedConnectModal: false, | ||
openConnectModal: () => {} | ||
})).toBe('display-extend-names') | ||
}) | ||
|
||
it('should return idle if registration status is available', () => { | ||
expect(calculateRenewState({ | ||
registrationStatus: 'available', | ||
isRegistrationStatusLoading: false, | ||
renewSeconds: 123, | ||
connectModalOpen: false, | ||
accountStatus: 'connected', | ||
isAbilitiesLoading: false, | ||
isRouterReady: true, | ||
name: 'name', | ||
openedConnectModal: false, | ||
openConnectModal: () => {} | ||
})).toBe('idle') | ||
}) | ||
|
||
it('should return idle if registration status is loading', () => { | ||
expect(calculateRenewState({ | ||
registrationStatus: 'registered', | ||
isRegistrationStatusLoading: true, | ||
renewSeconds: 123, | ||
connectModalOpen: false, | ||
accountStatus: 'connected', | ||
isAbilitiesLoading: false, | ||
isRouterReady: true, | ||
name: 'name', | ||
openedConnectModal: false, | ||
openConnectModal: () => {} | ||
})).toBe('idle') | ||
}) | ||
|
||
it('should return idle if renewSeconds is null', () => { | ||
expect(calculateRenewState({ | ||
registrationStatus: 'registered', | ||
isRegistrationStatusLoading: false, | ||
renewSeconds: null, | ||
connectModalOpen: false, | ||
accountStatus: 'connected', | ||
isAbilitiesLoading: false, | ||
isRouterReady: true, | ||
name: 'name', | ||
openedConnectModal: false, | ||
openConnectModal: () => {} | ||
})).toBe('idle') | ||
}) | ||
|
||
it('should return idle if connectModalOpen is true', () => { | ||
expect(calculateRenewState({ | ||
registrationStatus: 'registered', | ||
isRegistrationStatusLoading: false, | ||
renewSeconds: 123, | ||
connectModalOpen: true, | ||
accountStatus: 'connected', | ||
isAbilitiesLoading: false, | ||
isRouterReady: true, | ||
name: 'name', | ||
openedConnectModal: false, | ||
openConnectModal: () => {} | ||
})).toBe('idle') | ||
}) | ||
|
||
it('should return idle if abilities is loading', () => { | ||
expect(calculateRenewState({ | ||
registrationStatus: 'registered', | ||
isRegistrationStatusLoading: false, | ||
renewSeconds: 123, | ||
connectModalOpen: false, | ||
accountStatus: 'connected', | ||
isAbilitiesLoading: true, | ||
isRouterReady: true, | ||
name: 'name', | ||
openedConnectModal: false, | ||
openConnectModal: () => {} | ||
})).toBe('idle') | ||
}) | ||
|
||
it('should return idle if isRouterReady is false', () => { | ||
expect(calculateRenewState({ | ||
registrationStatus: 'registered', | ||
isRegistrationStatusLoading: false, | ||
renewSeconds: 123, | ||
connectModalOpen: false, | ||
accountStatus: 'connected', | ||
isAbilitiesLoading: false, | ||
isRouterReady: false, | ||
name: 'name', | ||
openedConnectModal: false, | ||
openConnectModal: () => {} | ||
})).toBe('idle') | ||
}) | ||
|
||
it('should return idle if name is empty', () => { | ||
expect(calculateRenewState({ | ||
registrationStatus: 'registered', | ||
isRegistrationStatusLoading: false, | ||
renewSeconds: 123, | ||
connectModalOpen: false, | ||
accountStatus: 'connected', | ||
isAbilitiesLoading: false, | ||
isRouterReady: true, | ||
name: '', | ||
openedConnectModal: false, | ||
openConnectModal: () => {} | ||
})).toBe('idle') | ||
}) | ||
|
||
it('should return idle if openedConnectModal is true', () => { | ||
expect(calculateRenewState({ | ||
registrationStatus: 'registered', | ||
isRegistrationStatusLoading: false, | ||
renewSeconds: 123, | ||
connectModalOpen: false, | ||
accountStatus: 'connected', | ||
isAbilitiesLoading: false, | ||
isRouterReady: true, | ||
name: 'name', | ||
openedConnectModal: true, | ||
openConnectModal: () => {} | ||
})).toBe('idle') | ||
}) | ||
|
||
it('should return idle if openConnectModal is undefined and accountStatus is disconnected', () => { | ||
expect(calculateRenewState({ | ||
registrationStatus: 'registered', | ||
isRegistrationStatusLoading: false, | ||
renewSeconds: 123, | ||
connectModalOpen: false, | ||
accountStatus: 'disconnected', | ||
isAbilitiesLoading: false, | ||
isRouterReady: true, | ||
name: 'name', | ||
openedConnectModal: false, | ||
openConnectModal: undefined | ||
})).toBe('idle') | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
src/transaction-flow/input/ExtendNames/utils/validateExtendNamesDuration.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { it, describe, expect } from "vitest"; | ||
import { validateExtendNamesDuration } from "./validateExtendNamesDuration"; | ||
import { ONE_DAY, ONE_YEAR } from "@app/utils/time"; | ||
|
||
describe('validateExtendNamesDuration', () => { | ||
it('should return an integer', () => { | ||
expect(validateExtendNamesDuration({ duration: '90000'})).toBe(90000) | ||
}) | ||
|
||
it('should return an integer for a decimal', () => { | ||
expect(validateExtendNamesDuration({duration: '90000.123'})).toBe(90000) | ||
}) | ||
|
||
it('should return minimum duration for a number less than the minimum', () => { | ||
expect(validateExtendNamesDuration({duration: '0'})).toBe(ONE_DAY) | ||
}) | ||
|
||
it('should return null duration for null', () => { | ||
expect(validateExtendNamesDuration({duration: null})).toBe(null) | ||
}) | ||
|
||
it('should return default duration for undefined', () => { | ||
expect(validateExtendNamesDuration({duration: undefined})).toBe(ONE_YEAR) | ||
}) | ||
|
||
it('should return default for a string', () => { | ||
expect(validateExtendNamesDuration({duration: 'abc'})).toBe(ONE_YEAR) | ||
}) | ||
|
||
it('should return default for an empty string', () => { | ||
expect(validateExtendNamesDuration({ duration: ''})).toBe(ONE_YEAR) | ||
}) | ||
|
||
it('should return default for a negative number', () => { | ||
expect(validateExtendNamesDuration({duration: '-123'})).toBe(ONE_YEAR) | ||
}) | ||
|
||
it('should return default for an object ', () => { | ||
expect(validateExtendNamesDuration({ duration: {}})).toBe(ONE_YEAR) | ||
}) | ||
}) |
17 changes: 17 additions & 0 deletions
17
src/transaction-flow/input/ExtendNames/utils/validateExtendNamesDuration.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { ONE_DAY, ONE_YEAR } from '@app/utils/time' | ||
|
||
export const validateExtendNamesDuration = ({ | ||
duration, | ||
minDuration = ONE_DAY, | ||
defaultDuration = ONE_YEAR, | ||
}: { | ||
duration?: unknown | ||
minDuration?: number | ||
defaultDuration?: number | ||
}): number | null => { | ||
if (duration === null) return null | ||
const parsedDuration = parseInt(duration as string, 10) | ||
if (Number.isNaN(parsedDuration) || parsedDuration < 0) return defaultDuration | ||
if (parsedDuration < minDuration) return minDuration | ||
return parsedDuration | ||
} |