|
| 1 | +import React from 'react'; |
| 2 | +import { render, fireEvent } from '@testing-library/react-native'; |
| 3 | +import SeedPhrase from '../components/SeedPhrase'; |
| 4 | + |
| 5 | +// Mocking Reanimated because it often has issues in Jest environments |
| 6 | +jest.mock('react-native-reanimated', () => { |
| 7 | + const Reanimated = require('react-native-reanimated/mock'); |
| 8 | + Reanimated.default.call = () => {}; |
| 9 | + return Reanimated; |
| 10 | +}); |
| 11 | + |
| 12 | +describe('<SeedPhrase />', () => { |
| 13 | + const mockPhrase = ['apple', 'banana', 'cherry', 'date', 'elderberry', 'fig', 'grape', 'honeydew', 'iceberg', 'jackfruit', 'kiwi', 'lemon']; |
| 14 | + const primaryColor = '#3B82F6'; |
| 15 | + |
| 16 | + it('renders all words when showSeed is true', () => { |
| 17 | + const { getByText } = render( |
| 18 | + <SeedPhrase |
| 19 | + recoveryPhrase={mockPhrase} |
| 20 | + showSeed={true} |
| 21 | + primaryColor={primaryColor} |
| 22 | + /> |
| 23 | + ); |
| 24 | + |
| 25 | + mockPhrase.forEach((word) => { |
| 26 | + expect(getByText(word)).toBeTruthy(); |
| 27 | + }); |
| 28 | + }); |
| 29 | + |
| 30 | + it('hides words when showSeed is false and no validation is active', () => { |
| 31 | + const { queryByText } = render( |
| 32 | + <SeedPhrase |
| 33 | + recoveryPhrase={mockPhrase} |
| 34 | + showSeed={false} |
| 35 | + primaryColor={primaryColor} |
| 36 | + /> |
| 37 | + ); |
| 38 | + |
| 39 | + // It should NOT show the words |
| 40 | + mockPhrase.forEach((word) => { |
| 41 | + expect(queryByText(word)).toBeNull(); |
| 42 | + }); |
| 43 | + }); |
| 44 | + |
| 45 | + it('renders input fields for words specified in validateWords', () => { |
| 46 | + const validateWords = { 0: '', 5: '' }; |
| 47 | + const { getByPlaceholderText } = render( |
| 48 | + <SeedPhrase |
| 49 | + recoveryPhrase={mockPhrase} |
| 50 | + showSeed={false} |
| 51 | + validateWords={validateWords} |
| 52 | + primaryColor={primaryColor} |
| 53 | + /> |
| 54 | + ); |
| 55 | + |
| 56 | + expect(getByPlaceholderText('Word #1')).toBeTruthy(); |
| 57 | + expect(getByPlaceholderText('Word #6')).toBeTruthy(); |
| 58 | + }); |
| 59 | + |
| 60 | + it('calls onInputChange when typing in validation fields', () => { |
| 61 | + const validateWords = { 0: '' }; |
| 62 | + const onInputChange = jest.fn(); |
| 63 | + const { getByPlaceholderText } = render( |
| 64 | + <SeedPhrase |
| 65 | + recoveryPhrase={mockPhrase} |
| 66 | + showSeed={false} |
| 67 | + validateWords={validateWords} |
| 68 | + onInputChange={onInputChange} |
| 69 | + primaryColor={primaryColor} |
| 70 | + /> |
| 71 | + ); |
| 72 | + |
| 73 | + const input = getByPlaceholderText('Word #1'); |
| 74 | + fireEvent.changeText(input, 'test'); |
| 75 | + |
| 76 | + expect(onInputChange).toHaveBeenCalledWith(0, 'test'); |
| 77 | + }); |
| 78 | +}); |
0 commit comments