Skip to content

Commit 3fa4e0e

Browse files
committed
chore: add events
1 parent 26ee927 commit 3fa4e0e

File tree

2 files changed

+160
-21
lines changed

2 files changed

+160
-21
lines changed
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
import * as React from 'react';
2+
import { html } from 'react-strict-dom';
3+
import { render, screen, userEvent } from '@testing-library/react-native';
4+
5+
function Counter() {
6+
const [count, setCount] = React.useState(0);
7+
8+
return (
9+
<html.div>
10+
<html.p>{count}</html.p>
11+
<html.button role="button" onClick={() => setCount(count + 1)}>
12+
Increment
13+
</html.button>
14+
</html.div>
15+
);
16+
}
17+
18+
test('Counter should increment the count when the button is pressed', async () => {
19+
const user = userEvent.setup();
20+
21+
render(<Counter />);
22+
expect(screen.getByText('0')).toBeOnTheScreen();
23+
24+
const button = screen.getByRole('button', { name: 'Increment' });
25+
expect(button).toBeOnTheScreen();
26+
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');
35+
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+
<html.div>
47+
<html.h1>Login successful</html.h1>
48+
</html.div>
49+
);
50+
}
51+
return (
52+
<html.div>
53+
<html.input
54+
placeholder="Email"
55+
value={email}
56+
onChange={(e: React.ChangeEvent<HTMLInputElement>) => setEmail(e.target.value)}
57+
/>
58+
<html.input
59+
placeholder="Password"
60+
value={password}
61+
onChange={(e: React.ChangeEvent<HTMLInputElement>) => setPassword(e.target.value)}
62+
/>
63+
{state === 'error' && <html.p role="alert">Invalid credentials</html.p>}
64+
65+
<html.button role="button" onClick={handleLogin}>
66+
Login
67+
</html.button>
68+
</html.div>
69+
);
70+
}
71+
72+
test('should login with valid credentials', async () => {
73+
const user = userEvent.setup();
74+
render(<LoginForm />);
75+
76+
await user.type(screen.getByPlaceholderText('Email'), '[email protected]');
77+
await user.type(screen.getByPlaceholderText('Password'), 'password');
78+
await user.press(screen.getByRole('button', { name: 'Login' }));
79+
80+
expect(screen.getByRole('heading', { name: 'Login successful' })).toBeOnTheScreen();
81+
});
82+
83+
test('should show error message with invalid credentials', async () => {
84+
const user = userEvent.setup();
85+
render(<LoginForm />);
86+
87+
await user.type(screen.getByPlaceholderText('Email'), '[email protected]');
88+
await user.type(screen.getByPlaceholderText('Password'), 'wrong-password');
89+
await user.press(screen.getByRole('button', { name: 'Login' }));
90+
91+
expect(screen.getByRole('alert', { name: 'Invalid credentials' })).toBeOnTheScreen();
92+
});
Lines changed: 68 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,87 @@
11
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';
44

55
function Counter() {
66
const [count, setCount] = React.useState(0);
77

88
return (
99
<View>
1010
<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>
1214
</View>
1315
);
1416
}
1517

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();
1920

20-
render(<Counter />);
21-
expect(screen.getByText('0')).toBeOnTheScreen();
21+
render(<Counter />);
22+
expect(screen.getByText('0')).toBeOnTheScreen();
2223

23-
const button = screen.getByRole('button', { name: 'Increment' });
24-
expect(button).toBeOnTheScreen();
24+
const button = screen.getByRole('button', { name: 'Increment' });
25+
expect(button).toBeOnTheScreen();
2526

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');
2935

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+
});
3378

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 />);
3682

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();
4087
});

0 commit comments

Comments
 (0)