Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

641 Validate Mnemonic Words on Wallet Import #739

Merged
Prev Previous commit
test: add MnemonicWordInput.test.tsx
  • Loading branch information
theborakompanioni committed Apr 22, 2024
commit 14b28278046761a103f3a80b77f3876b1a08c5d6
37 changes: 37 additions & 0 deletions src/components/MnemonicWordInput.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { render, screen } from '../testUtils'
import { Bip39MnemonicWordInput, MnemonicWordInputProps } from './MnemonicWordInput'

const NOOP = () => {}

describe('<Bip39MnemonicWordInput />', () => {
const validBip39MnemonicWord = 'abandon'
const invalidBip39MnemonicWord = 'not a bip39 word!'

const setup = (props: MnemonicWordInputProps) => {
render(<Bip39MnemonicWordInput {...props} />)
}

it('should render without errors', async () => {
setup({ index: 0, value: '', setValue: NOOP })

expect(await screen.findByTestId('mnemonic-word-input')).toBeVisible()
})

it('should report if input is NOT included in the BIP-39 wordlist', async () => {
setup({ index: 0, value: invalidBip39MnemonicWord, setValue: NOOP })

const input = await screen.findByTestId('mnemonic-word-input')
expect(input).toBeVisible()
expect(input).toHaveClass('is-invalid')
expect(input).not.toHaveClass('is-valid')
})

it('should report if input IS INCLUDED in the BIP-39 wordlist', async () => {
setup({ index: 0, value: validBip39MnemonicWord, setValue: NOOP })

const input = await screen.findByTestId('mnemonic-word-input')
expect(input).toBeVisible()
expect(input).toHaveClass('is-valid')
expect(input).not.toHaveClass('is-invalid')
})
})
5 changes: 3 additions & 2 deletions src/components/MnemonicWordInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { useTranslation } from 'react-i18next'
import { MNEMONIC_WORDS } from '../constants/bip39words'
import styles from './MnemonicWordInput.module.css'

interface MnemonicWordInputProps {
forwardRef: (el: HTMLInputElement) => void
export interface MnemonicWordInputProps {
forwardRef?: (el: HTMLInputElement) => void
index: number
value: string
setValue: (value: string, index: number) => void
Expand All @@ -30,6 +30,7 @@ const MnemonicWordInput = ({
<rb.InputGroup>
<rb.InputGroup.Text className={styles.seedwordIndexBackup}>{index + 1}.</rb.InputGroup.Text>
<rb.Form.Control
data-testid="mnemonic-word-input"
ref={forwardRef}
type="text"
placeholder={`${t('create_wallet.placeholder_seed_word_input')} ${index + 1}`}
Expand Down
Loading