|
1 | 1 | import * as React from 'react'; |
2 | | -import { Text, View, Button } from 'react-native'; |
3 | | -import { fireEvent, render, screen, userEvent } from '@testing-library/react-native'; |
| 2 | +import { Text, View, Pressable, TextInput } from 'react-native'; |
| 3 | +import { render, screen, userEvent } from '@testing-library/react-native'; |
4 | 4 |
|
5 | 5 | function Counter() { |
6 | 6 | const [count, setCount] = React.useState(0); |
7 | 7 |
|
8 | 8 | return ( |
9 | 9 | <View> |
10 | 10 | <Text>{count}</Text> |
11 | | - <Button title="Increment" onPress={() => setCount(count + 1)} /> |
| 11 | + <Pressable role="button" onPress={() => setCount(count + 1)}> |
| 12 | + <Text>Increment</Text> |
| 13 | + </Pressable> |
12 | 14 | </View> |
13 | 15 | ); |
14 | 16 | } |
15 | 17 |
|
16 | | -describe('Counter', () => { |
17 | | - it('should increment the count', async () => { |
18 | | - const user = userEvent.setup(); |
| 18 | +test('Counter should increment the count when the button is pressed', async () => { |
| 19 | + const user = userEvent.setup(); |
19 | 20 |
|
20 | | - render(<Counter />); |
21 | | - expect(screen.getByText('0')).toBeOnTheScreen(); |
| 21 | + render(<Counter />); |
| 22 | + expect(screen.getByText('0')).toBeOnTheScreen(); |
22 | 23 |
|
23 | | - const button = screen.getByRole('button', { name: 'Increment' }); |
24 | | - expect(button).toBeOnTheScreen(); |
| 24 | + const button = screen.getByRole('button', { name: 'Increment' }); |
| 25 | + expect(button).toBeOnTheScreen(); |
25 | 26 |
|
26 | | - await user.press(button); |
27 | | - expect(screen.getByText('1')).toBeOnTheScreen(); |
28 | | - }); |
| 27 | + await user.press(button); |
| 28 | + expect(screen.getByText('1')).toBeOnTheScreen(); |
| 29 | +}); |
| 30 | + |
| 31 | +function LoginForm() { |
| 32 | + const [email, setEmail] = React.useState(''); |
| 33 | + const [password, setPassword] = React.useState(''); |
| 34 | + const [state, setState] = React.useState('idle'); |
29 | 35 |
|
30 | | - it('should increment the count when the button is pressed', () => { |
31 | | - render(<Counter />); |
32 | | - expect(screen.getByText('0')).toBeOnTheScreen(); |
| 36 | + const handleLogin = () => { |
| 37 | + if (email === '[email protected]' && password === 'password') { |
| 38 | + setState('success'); |
| 39 | + } else { |
| 40 | + setState('error'); |
| 41 | + } |
| 42 | + }; |
| 43 | + |
| 44 | + if (state === 'success') { |
| 45 | + return ( |
| 46 | + <View> |
| 47 | + <Text role="heading">Login successful</Text> |
| 48 | + </View> |
| 49 | + ); |
| 50 | + } |
| 51 | + return ( |
| 52 | + <View> |
| 53 | + <TextInput placeholder="Email" value={email} onChangeText={setEmail} /> |
| 54 | + <TextInput placeholder="Password" value={password} onChangeText={setPassword} /> |
| 55 | + {state === 'error' && ( |
| 56 | + <Text accessible role="alert"> |
| 57 | + Invalid credentials |
| 58 | + </Text> |
| 59 | + )} |
| 60 | + |
| 61 | + <Pressable role="button" onPress={handleLogin}> |
| 62 | + <Text>Login</Text> |
| 63 | + </Pressable> |
| 64 | + </View> |
| 65 | + ); |
| 66 | +} |
| 67 | + |
| 68 | +test('should login with valid credentials', async () => { |
| 69 | + const user = userEvent.setup(); |
| 70 | + render(<LoginForm />); |
| 71 | + |
| 72 | + await user.type(screen.getByPlaceholderText('Email'), '[email protected]'); |
| 73 | + await user.type(screen.getByPlaceholderText('Password'), 'password'); |
| 74 | + await user.press(screen.getByRole('button', { name: 'Login' })); |
| 75 | + |
| 76 | + expect(screen.getByRole('heading', { name: 'Login successful' })).toBeOnTheScreen(); |
| 77 | +}); |
33 | 78 |
|
34 | | - const button = screen.getByRole('button', { name: 'Increment' }); |
35 | | - expect(button).toBeOnTheScreen(); |
| 79 | +test('should show error message with invalid credentials', async () => { |
| 80 | + const user = userEvent.setup(); |
| 81 | + render(<LoginForm />); |
36 | 82 |
|
37 | | - fireEvent.press(button); |
38 | | - expect(screen.getByText('1')).toBeOnTheScreen(); |
39 | | - }); |
| 83 | + await user.type(screen.getByPlaceholderText('Email'), '[email protected]'); |
| 84 | + await user.type(screen.getByPlaceholderText('Password'), 'wrong-password'); |
| 85 | + await user.press(screen.getByRole('button', { name: 'Login' })); |
| 86 | + expect(screen.getByRole('alert', { name: 'Invalid credentials' })).toBeOnTheScreen(); |
40 | 87 | }); |
0 commit comments