fix(lib): fix tests for locateControl (#324)

* improved locate control

* fix(lib): clean up setTimeout and fix Chrome modal layout issue

- Add proper cleanup for setTimeout in LocateControl to prevent memory leaks
- Replace modal-open class with direct overflow style to fix Chrome scrollbar issue
- Add timeout reference tracking for better component unmount handling

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>

* fix linting

* set distance rule

* optimized locatecontrol

* working for new users without profile now

* test(lib): add comprehensive tests for LocateControl component

- Add 9 comprehensive unit tests covering all LocateControl functionality
- Test modal display logic for new and existing users
- Test profile creation and position updates
- Test navigation after successful operations
- Test error handling with proper toast notifications
- Mock all external dependencies (React Router, Leaflet, APIs)
- Verify dialog behavior prevents re-appearance after decline
- Include snapshot tests for UI consistency
- All tests pass with proper TypeScript typing

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>

* removed unused code, add 1s delay

* updated tests

* fixed tests

* fix test for LocateControl

* fix linting

---------

Co-authored-by: Claude <noreply@anthropic.com>
This commit is contained in:
Anton Tranelis 2025-08-20 16:53:08 +02:00 committed by GitHub
parent f7f758bb00
commit db423b26f3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -274,6 +274,69 @@ describe('<LocateControl />', () => {
// Check if modal appears after timeout
expect(screen.getByText(/place your profile at your current location/i)).toBeInTheDocument()
})
it('does not show modal when distance is less than 100m', () => {
const profileWithPosition = {
...mockProfile,
position: {
type: 'Point' as const,
coordinates: [10.0, 50.0],
},
}
mockUseMyProfile.mockReturnValue({ myProfile: profileWithPosition, isMyProfileLoaded: true })
render(
<TestWrapper>
<LocateControl />
</TestWrapper>,
)
// Mock distanceTo to return a distance < 100m
const locationEvent = {
latlng: {
lat: 50.001, // Very close to current position
lng: 10.001,
distanceTo: vi.fn(() => 50), // Distance less than 100m
},
}
act(() => {
;(global as any).mockMapEventHandlers?.locationfound?.(locationEvent)
})
act(() => {
vi.runAllTimers()
})
// Modal should not appear
expect(
screen.queryByText(/place your profile at your current location/i),
).not.toBeInTheDocument()
})
it('does not show modal when location error occurs', () => {
mockUseMyProfile.mockReturnValue({ myProfile: null, isMyProfileLoaded: true })
render(
<TestWrapper>
<LocateControl />
</TestWrapper>,
)
// Simulate location error
act(() => {
;(global as any).mockMapEventHandlers?.locationerror?.()
})
act(() => {
vi.runAllTimers()
})
// Modal should not appear
expect(
screen.queryByText(/create your profile at your current location/i),
).not.toBeInTheDocument()
})
})
describe('Profile Creation', () => {