Skip to content
This repository was archived by the owner on May 5, 2025. It is now read-only.

Commit eb02902

Browse files
committed
feat: enhance Supabase mock implementation for gift and recipient services
1 parent bedf85a commit eb02902

File tree

2 files changed

+95
-4
lines changed

2 files changed

+95
-4
lines changed

tests/features/gifts/giftService.test.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,28 @@ jest.mock('@/services/supabaseClient', () => ({
1616
error: null,
1717
})),
1818
})),
19+
delete: jest.fn(() => ({
20+
eq: jest.fn(() => ({
21+
error: null,
22+
})),
23+
})),
24+
update: jest.fn(() => ({
25+
eq: jest.fn(() => ({
26+
select: jest.fn(() => ({
27+
single: jest.fn(() => ({
28+
data: {
29+
id: '1',
30+
title: 'Updated Gift',
31+
image: 'https://example.com/gift1.jpg',
32+
price: 25,
33+
recipient: '1',
34+
selectedDate: '2025-05-01',
35+
},
36+
error: null,
37+
})),
38+
})),
39+
})),
40+
})),
1941
})),
2042
}));
2143

tests/features/recipients/recipientService.test.ts

Lines changed: 73 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@ jest.mock('@/services/supabaseClient', () => {
1616
const mockInsert = jest.fn(() => ({ single: mockSingle }));
1717
const mockUpdate = jest.fn(() => ({ eq: mockEq }));
1818
const mockDelete = jest.fn(() => ({ eq: mockEq }));
19-
19+
2020
// From function returns an object with all the chainable methods
2121
const mockFrom = jest.fn(() => ({
2222
select: mockSelect,
2323
insert: mockInsert,
2424
update: mockUpdate,
25-
delete: mockDelete
25+
delete: mockDelete,
2626
}));
2727

2828
// Return the mock client
@@ -36,14 +36,83 @@ jest.mock('@/services/supabaseClient', () => {
3636
update: mockUpdate,
3737
delete: mockDelete,
3838
eq: mockEq,
39-
single: mockSingle
40-
}
39+
single: mockSingle,
40+
},
4141
};
4242
});
4343

4444
// Get the mocks for easy access in tests
4545
const mocks = (supabase as any).__mocks;
4646

47+
// Define mock data objects to resolve the errors
48+
49+
const mockRecipients = [
50+
{
51+
id: '1',
52+
name: 'John Doe',
53+
image: 'https://example.com/john.jpg',
54+
budget: 100,
55+
spent: 50,
56+
},
57+
{
58+
id: '2',
59+
name: 'Jane Smith',
60+
image: 'https://example.com/jane.jpg',
61+
budget: 150,
62+
spent: 75,
63+
},
64+
];
65+
66+
const mockResult = {
67+
id: '3',
68+
name: 'New Person',
69+
image: 'https://example.com/new-person.jpg',
70+
budget: 200,
71+
spent: 0,
72+
createdAt: '2025-05-02T12:00:00Z',
73+
};
74+
75+
// Fixing the mock implementation to return the expected data for each test case
76+
77+
// Update the mock implementation for `fetchRecipients`
78+
mocks.select.mockImplementation(() => ({
79+
eq: mocks.eq,
80+
single: jest.fn().mockResolvedValueOnce({
81+
data: mockRecipients,
82+
error: null,
83+
}),
84+
}));
85+
86+
// Update the mock implementation for `addRecipient`
87+
mocks.insert.mockImplementation(() => ({
88+
single: jest.fn().mockResolvedValueOnce({
89+
data: mockResult,
90+
error: null,
91+
}),
92+
}));
93+
94+
// Update the mock implementation for `updateRecipient`
95+
mocks.update.mockImplementation(() => ({
96+
eq: jest.fn().mockResolvedValueOnce({
97+
data: mockResult,
98+
error: null,
99+
}),
100+
}));
101+
102+
// Update the mock implementation for `findRecipientById`
103+
mocks.select.mockImplementation(() => ({
104+
eq: jest.fn().mockResolvedValueOnce({
105+
data: { name: 'John Doe' },
106+
error: null,
107+
}),
108+
}));
109+
110+
// Update the mock implementation for error cases
111+
mocks.single.mockImplementation(() => ({
112+
data: null,
113+
error: new Error('Failed to fetch recipients'),
114+
}));
115+
47116
describe('recipientService', () => {
48117
beforeEach(() => {
49118
jest.clearAllMocks();

0 commit comments

Comments
 (0)