|
| 1 | +/** |
| 2 | + * @file AddPoint.test.jsx |
| 3 | + */ |
| 4 | +import React from 'react'; |
| 5 | +import { render, screen, fireEvent, waitFor, act } from '@testing-library/react'; |
| 6 | +import AddPoint from './AddPoint'; |
| 7 | + |
| 8 | +// Mock APIs |
| 9 | +jest.mock('../lib/api', () => ({ |
| 10 | + authAPI: { |
| 11 | + isAuthenticated: jest.fn(), |
| 12 | + getProfile: jest.fn(), |
| 13 | + }, |
| 14 | + pointsAPI: { |
| 15 | + getUserPoints: jest.fn(), |
| 16 | + }, |
| 17 | +})); |
| 18 | + |
| 19 | +// Mock ThemeManager |
| 20 | +jest.mock('../utils/themeManager', () => ({ |
| 21 | + ThemeManager: { |
| 22 | + getTheme: jest.fn(() => 'light'), |
| 23 | + addThemeChangeListener: jest.fn(() => jest.fn()), // returns cleanup fn |
| 24 | + }, |
| 25 | +})); |
| 26 | + |
| 27 | +const { authAPI, pointsAPI } = require('../lib/api'); |
| 28 | + |
| 29 | +beforeAll(() => { |
| 30 | + jest.useFakeTimers(); |
| 31 | +}); |
| 32 | +afterAll(() => { |
| 33 | + jest.useRealTimers(); |
| 34 | +}); |
| 35 | + |
| 36 | +describe('AddPoint Component', () => { |
| 37 | + beforeEach(() => { |
| 38 | + jest.clearAllMocks(); |
| 39 | + // Mock location |
| 40 | + delete window.location; |
| 41 | + window.location = { search: '', href: '' }; |
| 42 | + // Mock geolocation |
| 43 | + global.navigator.geolocation = { |
| 44 | + getCurrentPosition: jest.fn((success) => |
| 45 | + success({ coords: { latitude: 10, longitude: 20 } }) |
| 46 | + ), |
| 47 | + }; |
| 48 | + }); |
| 49 | + |
| 50 | + function mockAuthAndRender() { |
| 51 | + authAPI.isAuthenticated.mockReturnValue(true); |
| 52 | + authAPI.getProfile.mockResolvedValue({ |
| 53 | + success: true, |
| 54 | + data: { user: { firstName: 'Test', email: 'test@example.com' } }, |
| 55 | + }); |
| 56 | + return render(<AddPoint />); |
| 57 | + } |
| 58 | + |
| 59 | + it('shows loading state initially and then renders form', async () => { |
| 60 | + mockAuthAndRender(); |
| 61 | + |
| 62 | + // Loading spinner |
| 63 | + expect(screen.getByText(/Loading/i)).toBeInTheDocument(); |
| 64 | + |
| 65 | + // Wait for form after profile resolves |
| 66 | + await waitFor(() => |
| 67 | + expect(screen.getByText(/Add a temperature point/i)).toBeInTheDocument() |
| 68 | + ); |
| 69 | + }); |
| 70 | + |
| 71 | + it('handles input changes and form submission success', async () => { |
| 72 | + mockAuthAndRender(); |
| 73 | + await screen.findByText(/Add a temperature point/i); |
| 74 | + |
| 75 | + localStorage.setItem('authToken', 'mock-token'); |
| 76 | + global.fetch = jest.fn().mockResolvedValue({ |
| 77 | + ok: true, |
| 78 | + json: async () => ({ success: true }), |
| 79 | + }); |
| 80 | + pointsAPI.getUserPoints.mockResolvedValue({ |
| 81 | + success: true, |
| 82 | + data: [ |
| 83 | + { lat: 10, lon: 20, temp: 30, timestamp: new Date().toISOString() }, |
| 84 | + ], |
| 85 | + }); |
| 86 | + |
| 87 | + fireEvent.change(screen.getByLabelText(/Latitude/i), { target: { value: '10' } }); |
| 88 | + fireEvent.change(screen.getByLabelText(/Longitude/i), { target: { value: '20' } }); |
| 89 | + fireEvent.change(screen.getByLabelText(/Temperature/i), { target: { value: '30' } }); |
| 90 | + |
| 91 | + fireEvent.click(screen.getByText(/Add Temperature Point/i)); |
| 92 | + |
| 93 | + // Fast-forward the 1.5s delay |
| 94 | + await act(() => jest.advanceTimersByTime(1500)); |
| 95 | + // Wait for modal |
| 96 | + expect(await screen.findByText('Temperature point added successfully')).toBeInTheDocument(); |
| 97 | + expect(screen.getByText(/Success!/i)).toBeInTheDocument(); |
| 98 | + }); |
| 99 | + |
| 100 | + it('shows error if no token is present', async () => { |
| 101 | + mockAuthAndRender(); |
| 102 | + await screen.findByText(/Add a temperature point/i); |
| 103 | + |
| 104 | + localStorage.removeItem('authToken'); |
| 105 | + fireEvent.change(screen.getByLabelText(/Latitude/i), { target: { value: '10' } }); |
| 106 | + fireEvent.change(screen.getByLabelText(/Longitude/i), { target: { value: '20' } }); |
| 107 | + fireEvent.change(screen.getByLabelText(/Temperature/i), { target: { value: '30' } }); |
| 108 | + |
| 109 | + fireEvent.click(screen.getByText(/Add Temperature Point/i)); |
| 110 | + |
| 111 | + await screen.findByText(/You must be logged in to add a point/i); |
| 112 | + expect(screen.getByText(/You must be logged in to add a point/i)).toBeInTheDocument(); |
| 113 | + }); |
| 114 | + |
| 115 | + it('handles API failure gracefully', async () => { |
| 116 | + mockAuthAndRender(); |
| 117 | + await screen.findByText(/Add a temperature point/i); |
| 118 | + |
| 119 | + localStorage.setItem('authToken', 'mock-token'); |
| 120 | + global.fetch = jest.fn().mockResolvedValue({ |
| 121 | + ok: false, |
| 122 | + json: async () => ({ message: 'Server error' }), |
| 123 | + }); |
| 124 | + |
| 125 | + fireEvent.change(screen.getByLabelText(/Latitude/i), { target: { value: '10' } }); |
| 126 | + fireEvent.change(screen.getByLabelText(/Longitude/i), { target: { value: '20' } }); |
| 127 | + fireEvent.change(screen.getByLabelText(/Temperature/i), { target: { value: '30' } }); |
| 128 | + |
| 129 | + fireEvent.click(screen.getByText(/Add Temperature Point/i)); |
| 130 | + |
| 131 | + // Fast-forward the 1.5s delay |
| 132 | + await act(() => jest.advanceTimersByTime(1500)); |
| 133 | + // Now the error banner should appear |
| 134 | + expect(await screen.findByText(/Server error/i)).toBeInTheDocument(); |
| 135 | + }); |
| 136 | +}); |
0 commit comments