-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjest.setup.js
More file actions
154 lines (145 loc) · 4.39 KB
/
Copy pathjest.setup.js
File metadata and controls
154 lines (145 loc) · 4.39 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import '@testing-library/jest-dom';
// Mock Geolocation API
const mockGeolocation = {
getCurrentPosition: jest.fn().mockImplementationOnce((success) =>
Promise.resolve(
success({
coords: {
latitude: 51.1,
longitude: 45.3,
altitude: null,
accuracy: 1,
altitudeAccuracy: null,
heading: null,
speed: null,
},
timestamp: Date.now(),
})
)
),
watchPosition: jest.fn().mockReturnValue(1), // Return a watchId
clearWatch: jest.fn(),
};
global.navigator.geolocation = mockGeolocation;
// Mock localStorage
const localStorageMock = (function () {
let store = {};
return {
getItem: function (key) {
return store[key] || null;
},
setItem: function (key, value) {
store[key] = value.toString();
},
removeItem: function (key) {
delete store[key];
},
clear: function () {
store = {};
},
};
})();
Object.defineProperty(window, 'localStorage', {
value: localStorageMock,
});
// Mock matchMedia
Object.defineProperty(window, 'matchMedia', {
writable: true,
value: jest.fn().mockImplementation(query => ({
matches: false,
media: query,
onchange: null,
addListener: jest.fn(), // deprecated
removeListener: jest.fn(), // deprecated
addEventListener: jest.fn(),
removeEventListener: jest.fn(),
dispatchEvent: jest.fn(),
})),
});
// Mock Supabase client
// This is a very basic mock. For more complex scenarios, you might want a more elaborate mock.
jest.mock('@/lib/supabaseClient', () => ({
createClient: jest.fn(() => ({
auth: {
getSession: jest.fn(() => Promise.resolve({ data: { session: null }, error: null })),
onAuthStateChange: jest.fn(() => ({ data: { subscription: { unsubscribe: jest.fn() } } })),
signInWithPassword: jest.fn(() => Promise.resolve({ data: { user: { id: 'mock-user-id', email: 'test@example.com' }, session: {} }, error: null })),
signUp: jest.fn(() => Promise.resolve({ data: { user: { id: 'mock-user-id', email: 'test@example.com' }, session: {} }, error: null })),
signOut: jest.fn(() => Promise.resolve({ error: null })),
},
from: jest.fn(() => ({
select: jest.fn(() => ({
eq: jest.fn(() => ({
single: jest.fn(() => Promise.resolve({ data: null, error: null })),
})),
single: jest.fn(() => Promise.resolve({ data: null, error: null })),
})),
update: jest.fn(() => ({
eq: jest.fn(() => Promise.resolve({ error: null })),
})),
insert: jest.fn(() => Promise.resolve({ error: null })),
})),
})),
}));
// Mock next/navigation
jest.mock('next/navigation', () => ({
useRouter: () => ({
push: jest.fn(),
replace: jest.fn(),
refresh: jest.fn(),
back: jest.fn(),
forward: jest.fn(),
}),
useParams: () => ({
orderId: 'mock-order-id',
}),
usePathname: () => '/',
useSearchParams: () => new URLSearchParams(),
}));
// Mock next-themes
jest.mock('next-themes', () => ({
useTheme: () => ({
theme: 'light',
setTheme: jest.fn(),
}),
ThemeProvider: ({ children }) => jest.fn(() => <>{children}</>),
}));
// Mock socket.io-client
jest.mock('socket.io-client', () => {
const mockSocket = {
on: jest.fn(),
off: jest.fn(),
emit: jest.fn(),
connect: jest.fn(),
disconnect: jest.fn(),
connected: false,
};
return {
io: jest.fn(() => mockSocket),
};
});
// Mock custom hooks
jest.mock('@/hooks/useAuth', () => ({
useAuth: jest.fn(() => ({
user: null,
login: jest.fn().mockResolvedValue({ success: true }),
logout: jest.fn().mockResolvedValue({ error: null }),
signUp: jest.fn().mockResolvedValue({ success: true }),
isLoading: false,
})),
}));
jest.mock('@/hooks/useOrderData', () => ({
useOrderData: jest.fn(() => ({
orders: [],
deliveryPartners: [],
vendors: [],
isLoadingData: false,
assignOrder: jest.fn().mockResolvedValue(undefined),
updateOrderStatus: jest.fn().mockResolvedValue(undefined),
updateOrderLocation: jest.fn().mockResolvedValue(undefined),
getVendorOrders: jest.fn(() => []),
getDeliveryPartnerOrders: jest.fn(() => []),
getOrderById: jest.fn(() => undefined),
fetchOrderById: jest.fn().mockResolvedValue(null),
})),
}));