-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprofileScreen.test.tsx
More file actions
115 lines (97 loc) · 2.92 KB
/
profileScreen.test.tsx
File metadata and controls
115 lines (97 loc) · 2.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import { render, screen, waitFor } from '@testing-library/react-native';
import ProfileScreen from '../../app/(tabs)/profileScreen';
jest.mock('react-native-safe-area-context', () => {
const { View } = require('react-native');
return {
SafeAreaProvider: ({ children }: any) => <View>{children}</View>,
SafeAreaView: ({ children }: any) => <View>{children}</View>,
useSafeAreaInsets: () => ({ top: 0, bottom: 0, left: 0, right: 0 }),
};
});
jest.mock('@react-native-firebase/auth', () => () => ({
currentUser: { uid: 'test-uid-123', email: 'test@example.com' },
}));
jest.mock('expo-image', () => {
const { Image } = require('react-native');
return { Image };
});
jest.mock('expo-image-picker', () => ({
launchImageLibraryAsync: jest.fn(),
}));
jest.mock('../../services/api', () => ({
searchGames: jest.fn().mockResolvedValue([]),
}));
jest.mock('react-native-input-spinner', () => {
const { View } = require('react-native');
return ({ value, onChange }: any) => <View />;
});
jest.mock('../../components/VideoPromptAnswer', () => {
const { View } = require('react-native');
return {
VideoPromptAnswer: () => <View />,
};
});
jest.mock('../../lib/supabase', () => ({
supabase: {
storage: {
from: () => ({
upload: jest.fn().mockResolvedValue({ error: null }),
getPublicUrl: jest.fn().mockReturnValue({
data: { publicUrl: 'https://example.com/avatar.jpg' },
}),
}),
},
},
}));
jest.mock('../../app/loginScreen', () => ({
handleLogout: jest.fn(),
}));
global.fetch = jest.fn().mockResolvedValue({
ok: true,
json: async () => ({
platforms: [],
games: [],
prompts: [],
profilePicture: null,
}),
text: async () => JSON.stringify({
platforms: [],
games: [],
prompts: [],
}),
});
// --- Tests ---
describe('Profile Screen', () => {
beforeEach(() => {
jest.clearAllMocks();
});
it('renders loading indicator on mount', async () => {
render(<ProfileScreen />);
await waitFor(() => {
expect(screen.getByText('Loading your profile…')).toBeTruthy();
});
});
it('renders profile screen after data loads', async () => {
render(<ProfileScreen />);
await waitFor(() => {
expect(screen.queryByText('Loading your profile…')).toBeNull();
});
});
it('renders error message if fetch fails', async () => {
(global.fetch as jest.Mock).mockResolvedValueOnce({
ok: false,
json: async () => ({ message: 'Failed to fetch profile' }),
});
render(<ProfileScreen />);
await waitFor(() => {
expect(screen.getByText('Failed to fetch profile')).toBeTruthy();
});
});
it('renders server error if fetch throws', async () => {
(global.fetch as jest.Mock).mockRejectedValueOnce(new Error('Network error'));
render(<ProfileScreen />);
await waitFor(() => {
expect(screen.getByText('Server error when fetching profile')).toBeTruthy();
});
});
});