|
| 1 | +import {describe, expect, it, jest, test} from '@jest/globals'; |
| 2 | + |
| 3 | +import {supabase} from '../../src/db/setupDb'; |
| 4 | +import getOfferings from '../../src/services/getOfferings'; |
| 5 | + |
| 6 | +jest.mock('../../src/db/setupDb', () => ({ |
| 7 | + supabase: { |
| 8 | + schema: jest.fn(), |
| 9 | + }, |
| 10 | + })); |
| 11 | + |
| 12 | +type SupabaseQueryResult = Promise<{data: any; error: any;}>; |
| 13 | + |
| 14 | +describe('getOfferings', () => { |
| 15 | + it('returns offering data for a valid course and semester', async () => { |
| 16 | + const mockData = [ |
| 17 | + { |
| 18 | + id: 1, |
| 19 | + course_id: 123, |
| 20 | + meeting_section: 'L01', |
| 21 | + offering: 'Fall 2025', |
| 22 | + day: 'Mon', |
| 23 | + start: '10:00', |
| 24 | + end: '11:00', |
| 25 | + location: 'Room 101', |
| 26 | + current: 30, |
| 27 | + max: 40, |
| 28 | + is_waitlisted: false, |
| 29 | + delivery_mode: 'In-Person', |
| 30 | + instructor: 'Dr. Smith', |
| 31 | + notes: '', |
| 32 | + code: 'ABC123', |
| 33 | + }, |
| 34 | + ]; |
| 35 | + |
| 36 | + // Build the method chain mock |
| 37 | + const eqMock2 = jest.fn<() => SupabaseQueryResult>().mockResolvedValue({ |
| 38 | + data: mockData, |
| 39 | + error: null, |
| 40 | + }); |
| 41 | + const eqMock1 = jest.fn(() => ({eq: eqMock2})); |
| 42 | + const selectMock = jest.fn(() => ({eq: eqMock1})); |
| 43 | + const fromMock = jest.fn(() => ({select: selectMock})); |
| 44 | + const schemaMock = jest.fn(() => ({from: fromMock})); |
| 45 | + |
| 46 | + // Replace supabase.schema with our chain |
| 47 | + (supabase.schema as jest.Mock).mockImplementation(schemaMock); |
| 48 | + |
| 49 | + // Act |
| 50 | + const result = await getOfferings(123, 'Fall 2025'); |
| 51 | + |
| 52 | + // Assert |
| 53 | + expect(schemaMock).toHaveBeenCalledWith('course'); |
| 54 | + expect(fromMock).toHaveBeenCalledWith('offerings'); |
| 55 | + expect(selectMock).toHaveBeenCalled(); |
| 56 | + expect(eqMock1).toHaveBeenCalledWith('course_id', 123); |
| 57 | + expect(eqMock2).toHaveBeenCalledWith('offering', 'Fall 2025'); |
| 58 | + expect(result).toEqual(mockData); |
| 59 | + }); |
| 60 | +}); |
0 commit comments