|
| 1 | +// tests/regulationAPI.test.js - Unit tests for Regulation API module |
| 2 | + |
| 3 | +import RegulationAPI from '../src/space/regulationAPI'; |
| 4 | +import axios from 'axios'; |
| 5 | + |
| 6 | +jest.mock('axios'); // Mock axios for testing |
| 7 | + |
| 8 | +describe('RegulationAPI', () => { |
| 9 | + let regulationAPI; |
| 10 | + const apiEndpoints = [ |
| 11 | + 'https://api.example.com/regulations', |
| 12 | + 'https://api.anotherexample.com/regulations' |
| 13 | + ]; |
| 14 | + |
| 15 | + beforeEach(() => { |
| 16 | + regulationAPI = new RegulationAPI(apiEndpoints); |
| 17 | + }); |
| 18 | + |
| 19 | + test('should fetch regulations from multiple APIs', async () => { |
| 20 | + const mockResponse1 = { data: [{ id: 1, title: 'Regulation 1' }] }; |
| 21 | + const mockResponse2 = { data: [{ id: 2, title: 'Regulation 2' }] }; |
| 22 | + |
| 23 | + axios.get.mockImplementationOnce(() => Promise.resolve(mockResponse1)); |
| 24 | + axios.get.mockImplementationOnce(() => Promise.resolve(mockResponse2)); |
| 25 | + |
| 26 | + const regulations = await regulationAPI.fetchRegulations(); |
| 27 | + expect(regulations).toEqual([ |
| 28 | + { id: 1, title: 'Regulation 1' }, |
| 29 | + { id: 2, title: 'Regulation 2' } |
| 30 | + ]); |
| 31 | + expect(axios.get).toHaveBeenCalledTimes(2); |
| 32 | + expect(axios.get).toHaveBeenCalledWith(apiEndpoints[0]); |
| 33 | + expect(axios.get).toHaveBeenCalledWith(apiEndpoints[1]); |
| 34 | + }); |
| 35 | + |
| 36 | + test('should cache fetched regulations', async () => { |
| 37 | + const mockResponse = { data: [{ id: 1, title: 'Regulation 1' }] }; |
| 38 | + axios.get.mockImplementationOnce(() => Promise.resolve(mockResponse)); |
| 39 | + |
| 40 | + const regulationsFirstFetch = await regulationAPI.fetchRegulations(); |
| 41 | + const regulationsSecondFetch = await regulationAPI.fetchRegulations(); |
| 42 | + |
| 43 | + expect(regulationsFirstFetch).toEqual(regulationsSecondFetch); |
| 44 | + expect(axios.get).toHaveBeenCalledTimes(1); // Should only call once |
| 45 | + }); |
| 46 | + |
| 47 | + test('should throw an error if fetching regulations fails', async () => { |
| 48 | + axios.get.mockImplementationOnce(() => Promise.reject(new Error('Network Error'))); |
| 49 | + |
| 50 | + await expect(regulationAPI.fetchRegulations()).rejects.toThrow('Failed to fetch regulations'); |
| 51 | + }); |
| 52 | + |
| 53 | + test('should fetch regulations from cache if available', async () => { |
| 54 | + const mockResponse = { data: [{ id: 1, title: 'Regulation 1' }] }; |
| 55 | + axios.get.mockImplementationOnce(() => Promise.resolve(mockResponse)); |
| 56 | + |
| 57 | + await regulationAPI.fetchRegulations(); // First fetch to populate cache |
| 58 | + const cachedRegulations = await regulationAPI.fetchRegulations(); // Second fetch should hit cache |
| 59 | + |
| 60 | + expect(cachedRegulations).toEqual([{ id: 1, title: 'Regulation 1' }]); |
| 61 | + expect(axios.get).toHaveBeenCalledTimes(1); // Should only call once |
| 62 | + }); |
| 63 | + |
| 64 | + test('should throw an error when trying to get regulation by ID that does not exist', async () => { |
| 65 | + const mockResponse = { data: [{ id: 1, title: 'Regulation 1' }] }; |
| 66 | + axios.get.mockImplementationOnce(() => Promise.resolve(mockResponse)); |
| 67 | + |
| 68 | + await regulationAPI.fetchRegulations(); // Populate cache |
| 69 | + |
| 70 | + await expect(regulationAPI.getRegulationById(999)).rejects.toThrow('Regulation not found'); |
| 71 | + }); |
| 72 | + |
| 73 | + test('should return regulation by ID if it exists', async () => { |
| 74 | + const mockResponse = { data: [{ id: 1, title: 'Regulation 1' }] }; |
| 75 | + axios.get.mockImplementationOnce(() => Promise.resolve(mockResponse)); |
| 76 | + |
| 77 | + await regulationAPI.fetchRegulations(); // Populate cache |
| 78 | + |
| 79 | + const regulation = await regulationAPI.getRegulationById(1); |
| 80 | + expect(regulation).toEqual({ id: 1, title: 'Regulation 1' }); |
| 81 | + }); |
| 82 | +}); |
0 commit comments