import userEvent from '@testing-library/user-event';
import { http, HttpResponse } from 'msw';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import MAdminAddonManager from '../../../../src/mobile/screens/admin/MAdminAddonManager';
import { useAddonStore } from '../../../../src/store/addonStore';
import { useAuthStore } from '../../../../src/store/authStore';
import { useSettingsStore } from '../../../../src/store/settingsStore';
import { buildSettings } from '../../../helpers/factories';
import { server } from '../../../helpers/msw/server';
import { render, screen, waitFor } from '../../../helpers/render';
import { resetAllStores, seedStore } from '../../../helpers/store';
// FE-ATLAS2-REST-050 to 052: the Atlas ranking row under the Atlas addon on the phone admin screen.
const RANKING = 'Ranking and profiles';
function atlasAddon(enabled: boolean) {
return { id: 'atlas', name: 'Atlas', description: 'World map', icon: 'Globe', type: 'trip', enabled };
}
beforeEach(() => {
resetAllStores();
seedStore(useSettingsStore, { settings: buildSettings({ dark_mode: false }) });
useAuthStore.setState({ atlasCompareEnabled: false });
vi.spyOn(useAddonStore.getState(), 'loadAddons').mockResolvedValue(undefined);
});
afterEach(() => {
vi.restoreAllMocks();
});
describe('MAdminAddonManager: Atlas ranking', () => {
it('FE-ATLAS2-REST-050: an enabled Atlas shows the ranking switch, off as the app config says', async () => {
server.use(http.get('/api/admin/addons', () => HttpResponse.json({ addons: [atlasAddon(true)] })));
render();
const toggle = await screen.findByRole('switch', { name: RANKING });
expect(toggle).toHaveAttribute('aria-checked', 'false');
});
it('FE-ATLAS2-REST-051: the switch stays away while Atlas itself is off', async () => {
server.use(http.get('/api/admin/addons', () => HttpResponse.json({ addons: [atlasAddon(false)] })));
render();
await waitFor(() => expect(screen.getAllByRole('switch').length).toBeGreaterThan(0));
expect(screen.queryByRole('switch', { name: RANKING })).not.toBeInTheDocument();
});
it('FE-ATLAS2-REST-052: flipping it saves atlas_compare_enabled and shows it on', 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();
await user.click(await screen.findByRole('switch', { name: RANKING }));
await waitFor(() => expect(bodies).toEqual([{ atlas_compare_enabled: true }]));
await waitFor(() => expect(screen.getByRole('switch', { name: RANKING })).toHaveAttribute('aria-checked', 'true'));
expect(useAuthStore.getState().atlasCompareEnabled).toBe(true);
});
});