test(atlas): cover the new Atlas from engine to hub views

SonarCloud measured 16.9% coverage on this PR's new code. Tests now cover the WebGL globe engine (boot, caching, input, camera, regions, prefetch) and renderer (against a fake WebGL2 context that reads the real shaders), the paint, labels, sky and pins, the central useAtlasGlobe hook and the hub hooks, the snapshot, account queue and Atlas repos, every desktop and phone hub view, the server's people, flights and mesh routes, the shared badge rules, and the new view transition, warm bags and kept planner maps.
This commit is contained in:
Maurice
2026-11-29 10:21:28 +01:00
parent 10ad92574d
commit 9018ee8980
69 changed files with 14979 additions and 0 deletions
@@ -0,0 +1,87 @@
import userEvent from '@testing-library/user-event';
import { http, HttpResponse } from 'msw';
import { afterEach, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest';
import { server } from '../../../tests/helpers/msw/server';
import { render, screen, waitFor } from '../../../tests/helpers/render';
import { resetAllStores, seedStore } from '../../../tests/helpers/store';
import { useAddonStore } from '../../store/addonStore';
import { useAuthStore } from '../../store/authStore';
import { useSettingsStore } from '../../store/settingsStore';
import { ToastContainer } from '../shared/Toast';
import AddonManager from './AddonManager';
// FE-ATLAS2-REST-040 to 042: the Atlas ranking row under the Atlas addon on the desktop admin page.
const RANKING = 'Ranking and profiles';
function atlasAddon(enabled: boolean) {
return { id: 'atlas', name: 'Atlas', description: 'World map', icon: 'Globe', type: 'global', enabled };
}
function isOn(toggle: HTMLElement): boolean {
return toggle.getAttribute('aria-pressed') === 'true';
}
beforeAll(() => {
Object.defineProperty(window, 'matchMedia', {
writable: true,
value: vi.fn(() => ({ matches: false, addEventListener: vi.fn(), removeEventListener: vi.fn() })),
});
});
beforeEach(() => {
resetAllStores();
seedStore(useSettingsStore, { settings: { dark_mode: false } });
useAuthStore.setState({ atlasCompareEnabled: true });
vi.spyOn(useAddonStore.getState(), 'loadAddons').mockResolvedValue(undefined);
server.use(
http.get('/api/admin/tours-wanderer', () => HttpResponse.json({ enabled: false })),
http.get('/api/admin/tours-hiking-map', () => HttpResponse.json({ enabled: false })),
http.get('/api/admin/tours-cycle-map', () => HttpResponse.json({ enabled: false })),
http.get('/api/admin/tours-ski-routing', () => HttpResponse.json({ enabled: false }))
);
});
afterEach(() => {
vi.restoreAllMocks();
});
describe('AddonManager: Atlas ranking', () => {
it('FE-ATLAS2-REST-040: an enabled Atlas shows the ranking row, on as the app config says', async () => {
server.use(http.get('/api/admin/addons', () => HttpResponse.json({ addons: [atlasAddon(true)] })));
render(<AddonManager />);
const toggle = await screen.findByRole('button', { name: RANKING });
expect(isOn(toggle)).toBe(true);
expect(screen.getByText(RANKING)).toHaveAttribute('title', expect.stringContaining('Atlas ranking'));
});
it('FE-ATLAS2-REST-041: the row stays away while Atlas itself is off', async () => {
server.use(http.get('/api/admin/addons', () => HttpResponse.json({ addons: [atlasAddon(false)] })));
render(<AddonManager />);
await screen.findByRole('button', { name: 'Atlas' });
expect(screen.queryByRole('button', { name: RANKING })).not.toBeInTheDocument();
});
it('FE-ATLAS2-REST-042: flipping the row saves atlas_compare_enabled and shows it off', async () => {
const user = userEvent.setup();
const bodies: unknown[] = [];
server.use(
http.get('/api/admin/addons', () => HttpResponse.json({ addons: [atlasAddon(true)] })),
http.put('/api/auth/app-settings', async ({ request }) => {
bodies.push(await request.json());
return HttpResponse.json({ success: true });
})
);
render(
<>
<ToastContainer />
<AddonManager />
</>
);
await user.click(await screen.findByRole('button', { name: RANKING }));
await screen.findByText('Addon updated');
expect(bodies).toEqual([{ atlas_compare_enabled: false }]);
await waitFor(() => expect(isOn(screen.getByRole('button', { name: RANKING }))).toBe(false));
expect(useAuthStore.getState().atlasCompareEnabled).toBe(false);
});
});
@@ -0,0 +1,74 @@
import { act, renderHook, waitFor } from '@testing-library/react';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import { adminApi, authApi } from '../../../api/client';
import { cycleMapAdminApi, hikingMapAdminApi, skiRoutingAdminApi } from '../../../api/tourBaseMaps';
import { wandererAdminApi } from '../../../api/wanderer';
import { useAddonStore } from '../../../store/addonStore';
import { useAuthStore } from '../../../store/authStore';
import { useAddonManager } from './useAddonManager';
// FE-ATLAS2-REST-030 to 032: the Atlas ranking switch the admin flips under the Atlas addon.
const toast = vi.hoisted(() => ({ success: vi.fn(), error: vi.fn() }));
vi.mock('../../shared/Toast', () => ({ useToast: () => toast }));
vi.mock('../../../i18n', () => ({ useTranslation: () => ({ t: (k: string) => k }) }));
let loadAddons: ReturnType<typeof vi.fn<() => Promise<void>>>;
beforeEach(() => {
toast.success.mockReset();
toast.error.mockReset();
loadAddons = vi.fn<() => Promise<void>>().mockResolvedValue(undefined);
useAddonStore.setState({ loadAddons });
useAuthStore.setState({ atlasCompareEnabled: true });
vi.spyOn(adminApi, 'addons').mockResolvedValue({ addons: [] });
vi.spyOn(wandererAdminApi, 'get').mockResolvedValue({ enabled: false });
vi.spyOn(hikingMapAdminApi, 'get').mockResolvedValue({ enabled: false });
vi.spyOn(cycleMapAdminApi, 'get').mockResolvedValue({ enabled: false });
vi.spyOn(skiRoutingAdminApi, 'get').mockResolvedValue({ enabled: false });
});
afterEach(() => vi.restoreAllMocks());
async function loaded() {
const hook = renderHook(() => useAddonManager());
await waitFor(() => expect(hook.result.current.loading).toBe(false));
return hook;
}
describe('useAddonManager: Atlas ranking switch', () => {
it('FE-ATLAS2-REST-030: reads the switch from the app config the auth store holds', async () => {
useAuthStore.setState({ atlasCompareEnabled: false });
const { result } = await loaded();
expect(result.current.atlasCompare).toBe(false);
act(() => useAuthStore.setState({ atlasCompareEnabled: true }));
expect(result.current.atlasCompare).toBe(true);
});
it('FE-ATLAS2-REST-031: a flip turns it off at once, saves the app setting, refreshes the addons and toasts', async () => {
const save = vi.spyOn(authApi, 'updateAppSettings').mockResolvedValue({});
const { result } = await loaded();
await act(() => result.current.toggleAtlasCompare());
expect(save).toHaveBeenCalledWith({ atlas_compare_enabled: false });
expect(useAuthStore.getState().atlasCompareEnabled).toBe(false);
expect(result.current.atlasCompare).toBe(false);
expect(loadAddons).toHaveBeenCalledTimes(1);
expect(toast.success).toHaveBeenCalledWith('admin.addons.toast.updated');
// And on again from off.
await act(() => result.current.toggleAtlasCompare());
expect(save).toHaveBeenLastCalledWith({ atlas_compare_enabled: true });
expect(useAuthStore.getState().atlasCompareEnabled).toBe(true);
});
it('FE-ATLAS2-REST-032: a failed save puts the switch back and says so, without refreshing', async () => {
vi.spyOn(authApi, 'updateAppSettings').mockRejectedValue(new Error('down'));
const { result } = await loaded();
await act(() => result.current.toggleAtlasCompare());
expect(useAuthStore.getState().atlasCompareEnabled).toBe(true);
expect(result.current.atlasCompare).toBe(true);
expect(toast.error).toHaveBeenCalledWith('admin.addons.toast.error');
expect(toast.success).not.toHaveBeenCalled();
expect(loadAddons).not.toHaveBeenCalled();
});
});