|
1 | 1 | import { Test, TestingModule } from '@nestjs/testing'; |
2 | 2 | import { AladhanService } from './aladhan.service'; |
3 | | -import { HttpModule } from '@nestjs/axios'; |
| 3 | +import { HttpService } from '@nestjs/axios'; |
| 4 | +import { of } from 'rxjs'; |
| 5 | +import { AxiosResponse } from 'axios'; |
4 | 6 |
|
5 | 7 | describe('AladhanService', () => { |
6 | 8 | let service: AladhanService; |
| 9 | + let httpService: HttpService; |
| 10 | + |
| 11 | + const mockAladhanResponseData = { |
| 12 | + data: { |
| 13 | + '1': [ |
| 14 | + { |
| 15 | + date: { timestamp: '01 Jan 2025' }, |
| 16 | + meta: { |
| 17 | + timezone: 'Asia/Riyadh', |
| 18 | + location: { latitude: 24.7136, longitude: 46.6753 }, |
| 19 | + }, |
| 20 | + timings: { |
| 21 | + Fajr: '2025-01-01T05:23:00+03:00', |
| 22 | + Sunrise: '2025-01-01T06:46:00+03:00', |
| 23 | + Dhuhr: '2025-01-01T12:04:00+03:00', |
| 24 | + Asr: '2025-01-01T15:15:00+03:00', |
| 25 | + Sunset: '2025-01-01T17:23:00+03:00', |
| 26 | + Maghrib: '2025-01-01T17:23:00+03:00', |
| 27 | + Isha: '2025-01-01T18:53:00+03:00', |
| 28 | + Imsak: '2025-01-01T05:13:00+03:00', |
| 29 | + Midnight: '2025-01-02T00:04:00+03:00', |
| 30 | + Firstthird: '2025-01-01T21:24:00+03:00', |
| 31 | + Lastthird: '2025-01-02T02:45:00+03:00', |
| 32 | + }, |
| 33 | + }, |
| 34 | + ], |
| 35 | + }, |
| 36 | + }; |
| 37 | + |
| 38 | + const mockAxiosResponse: AxiosResponse = { |
| 39 | + data: mockAladhanResponseData, |
| 40 | + status: 200, |
| 41 | + statusText: 'OK', |
| 42 | + headers: {}, |
| 43 | + config: {} as any, |
| 44 | + }; |
7 | 45 |
|
8 | 46 | beforeEach(async () => { |
9 | 47 | const module: TestingModule = await Test.createTestingModule({ |
10 | | - imports: [HttpModule], |
11 | | - providers: [AladhanService], |
| 48 | + providers: [ |
| 49 | + AladhanService, |
| 50 | + { |
| 51 | + provide: HttpService, |
| 52 | + useValue: { |
| 53 | + get: jest.fn(), |
| 54 | + }, |
| 55 | + }, |
| 56 | + ], |
12 | 57 | }).compile(); |
13 | 58 |
|
14 | 59 | service = module.get<AladhanService>(AladhanService); |
| 60 | + httpService = module.get<HttpService>(HttpService); |
15 | 61 | }); |
16 | 62 |
|
17 | 63 | it('should be defined', () => { |
18 | 64 | expect(service).toBeDefined(); |
19 | 65 | }); |
| 66 | + |
| 67 | + describe('getPrayerTimes', () => { |
| 68 | + it('should fetch prayer times with basic parameters', async () => { |
| 69 | + jest.spyOn(httpService, 'get').mockReturnValue(of(mockAxiosResponse)); |
| 70 | + |
| 71 | + const result = await service.getPrayerTimes(24.7136, 46.6753, { |
| 72 | + method: 3, |
| 73 | + }); |
| 74 | + |
| 75 | + expect(httpService.get).toHaveBeenCalledWith( |
| 76 | + 'https://api.aladhan.com/v1/calendar', |
| 77 | + { |
| 78 | + params: { |
| 79 | + latitude: 24.7136, |
| 80 | + longitude: 46.6753, |
| 81 | + iso8601: true, |
| 82 | + annual: true, |
| 83 | + method: 3, |
| 84 | + }, |
| 85 | + }, |
| 86 | + ); |
| 87 | + |
| 88 | + expect(result.timezone).toBe('Asia/Riyadh'); |
| 89 | + expect(result.location).toEqual({ |
| 90 | + latitude: 24.7136, |
| 91 | + longitude: 46.6753, |
| 92 | + }); |
| 93 | + expect(result.months['1']).toBeDefined(); |
| 94 | + }); |
| 95 | + |
| 96 | + it('should fetch prayer times with all Aladhan parameters', async () => { |
| 97 | + jest.spyOn(httpService, 'get').mockReturnValue(of(mockAxiosResponse)); |
| 98 | + |
| 99 | + const params = { |
| 100 | + method: 3, |
| 101 | + school: 1, |
| 102 | + midnightMode: 0, |
| 103 | + shafaq: 'general', |
| 104 | + latitudeAdjustment: 1, |
| 105 | + tune: '0,5,0,0,0,0,0,-2,0', |
| 106 | + year: 2025, |
| 107 | + month: 1, |
| 108 | + }; |
| 109 | + |
| 110 | + await service.getPrayerTimes(24.7136, 46.6753, params); |
| 111 | + |
| 112 | + expect(httpService.get).toHaveBeenCalledWith( |
| 113 | + 'https://api.aladhan.com/v1/calendar', |
| 114 | + { |
| 115 | + params: { |
| 116 | + latitude: 24.7136, |
| 117 | + longitude: 46.6753, |
| 118 | + iso8601: true, |
| 119 | + annual: true, |
| 120 | + method: 3, |
| 121 | + school: 1, |
| 122 | + midnightMode: 0, |
| 123 | + shafaq: 'general', |
| 124 | + latitudeAdjustment: 1, |
| 125 | + tune: '0,5,0,0,0,0,0,-2,0', |
| 126 | + year: 2025, |
| 127 | + month: 1, |
| 128 | + }, |
| 129 | + }, |
| 130 | + ); |
| 131 | + }); |
| 132 | + |
| 133 | + it('should fetch prayer times with no optional parameters', async () => { |
| 134 | + jest.spyOn(httpService, 'get').mockReturnValue(of(mockAxiosResponse)); |
| 135 | + |
| 136 | + await service.getPrayerTimes(24.7136, 46.6753); |
| 137 | + |
| 138 | + expect(httpService.get).toHaveBeenCalledWith( |
| 139 | + 'https://api.aladhan.com/v1/calendar', |
| 140 | + { |
| 141 | + params: { |
| 142 | + latitude: 24.7136, |
| 143 | + longitude: 46.6753, |
| 144 | + iso8601: true, |
| 145 | + annual: true, |
| 146 | + }, |
| 147 | + }, |
| 148 | + ); |
| 149 | + }); |
| 150 | + }); |
20 | 151 | }); |
0 commit comments