Skip to content

armgit / TREK

Public Imported from GitHub
0

The most complete open-source travel planner: real-time collaboration, bookings, budgets, packing lists, journals, offline PWA, SSO and an AI-ready MCP server, with planet-wide place search and routing built in.

6.9 KB
Raw Blame
import { describe, it, expect, beforeEach, vi } from 'vitest';
import { http, HttpResponse } from 'msw';
import { useTripStore } from '../../../src/store/tripStore';
import { resetAllStores, seedStore } from '../../helpers/store';
import { buildPlace, buildAssignment } from '../../helpers/factories';
import { server } from '../../helpers/msw/server';

vi.mock('../../../src/api/websocket', () => ({
  connect: vi.fn(),
  disconnect: vi.fn(),
  getSocketId: vi.fn(() => null),
  joinTrip: vi.fn(),
  leaveTrip: vi.fn(),
  addListener: vi.fn(),
  removeListener: vi.fn(),
  setRefetchCallback: vi.fn(),
  setPreReconnectHook: vi.fn(),
}));

beforeEach(() => {
  resetAllStores();
});

describe('placesSlice', () => {
  describe('addPlace', () => {
    it('FE-PLACES-001: addPlace calls API and prepends place to places array', async () => {
      const existing = buildPlace({ trip_id: 1 });
      seedStore(useTripStore, { places: [existing] });

      const result = await useTripStore.getState().addPlace(1, { name: 'New Place' });

      expect(result.name).toBe('New Place');
      const places = useTripStore.getState().places;
      expect(places).toHaveLength(2);
      expect(places[0].name).toBe('New Place'); // prepended
    });

    it('FE-PLACES-002: addPlace on failure throws and places remain unchanged', async () => {
      const existing = buildPlace({ trip_id: 1 });
      seedStore(useTripStore, { places: [existing] });

      server.use(
        http.post('/api/trips/:id/places', () =>
          HttpResponse.json({ message: 'Server error' }, { status: 500 })
        ),
      );

      await expect(useTripStore.getState().addPlace(1, { name: 'Fail' })).rejects.toThrow();
      expect(useTripStore.getState().places).toEqual([existing]);
    });
  });

  describe('updatePlace', () => {
    it('FE-PLACES-003: updatePlace calls API and updates place in array', async () => {
      const place = buildPlace({ id: 10, trip_id: 1, name: 'Old Name' });
      seedStore(useTripStore, { places: [place] });

      server.use(
        http.put('/api/trips/:id/places/:placeId', async ({ params, request }) => {
          const body = await request.json() as Record<string, unknown>;
          return HttpResponse.json({ place: { ...place, ...body, id: Number(params.placeId) } });
        }),
      );

      const result = await useTripStore.getState().updatePlace(1, 10, { name: 'New Name' });

      expect(result.name).toBe('New Name');
      const updated = useTripStore.getState().places.find(p => p.id === 10);
      expect(updated?.name).toBe('New Name');
    });

    it('FE-PLACES-004: updatePlace cascades to assignments map — assignment place field updated', async () => {
      const place = buildPlace({ id: 10, trip_id: 1, name: 'Old Place' });
      const assignment = buildAssignment({ id: 100, day_id: 1, place });
      seedStore(useTripStore, {
        places: [place],
        assignments: { '1': [assignment] },
      });

      server.use(
        http.put('/api/trips/1/places/10', async ({ request }) => {
          const body = await request.json() as Record<string, unknown>;
          return HttpResponse.json({ place: { ...place, ...body } });
        }),
      );

      await useTripStore.getState().updatePlace(1, 10, { name: 'Updated Place' });

      const updatedAssignments = useTripStore.getState().assignments['1'];
      expect(updatedAssignments[0].place.name).toBe('Updated Place');
    });
  });

  describe('updatePlacesMany', () => {
    it('FE-PLACES-008: applies the patch to every listed place and cascades to assignments', async () => {
      const a = buildPlace({ id: 10, trip_id: 1, category_id: 1 });
      const b = buildPlace({ id: 20, trip_id: 1, category_id: 1 });
      const c = buildPlace({ id: 30, trip_id: 1, category_id: 9 });
      const assignment = buildAssignment({ id: 100, day_id: 1, place: a });
      seedStore(useTripStore, { places: [a, b, c], assignments: { '1': [assignment] } });

      server.use(
        http.post('/api/trips/1/places/bulk-update', () => HttpResponse.json({ updated: [10, 20], count: 2 })),
      );

      await useTripStore.getState().updatePlacesMany(1, [10, 20], { category_id: 5 });

      const places = useTripStore.getState().places;
      expect(places.find(p => p.id === 10)?.category_id).toBe(5);
      expect(places.find(p => p.id === 20)?.category_id).toBe(5);
      expect(places.find(p => p.id === 30)?.category_id).toBe(9); // untouched
      expect(useTripStore.getState().assignments['1'][0].place.category_id).toBe(5); // cascaded
    });

    it('FE-PLACES-009: no-ops on an empty id list without calling the API', async () => {
      const a = buildPlace({ id: 10, trip_id: 1, category_id: 1 });
      seedStore(useTripStore, { places: [a] });
      await useTripStore.getState().updatePlacesMany(1, [], { category_id: 5 });
      expect(useTripStore.getState().places[0].category_id).toBe(1);
    });
  });

  describe('deletePlace', () => {
    it('FE-PLACES-005: deletePlace removes place from places array', async () => {
      const place1 = buildPlace({ id: 10, trip_id: 1 });
      const place2 = buildPlace({ id: 20, trip_id: 1 });
      seedStore(useTripStore, { places: [place1, place2], assignments: {} });

      server.use(
        http.delete('/api/trips/1/places/10', () => HttpResponse.json({ success: true })),
      );

      await useTripStore.getState().deletePlace(1, 10);

      const places = useTripStore.getState().places;
      expect(places).toHaveLength(1);
      expect(places[0].id).toBe(20);
    });

    it('FE-PLACES-006: deletePlace cascades — assignments referencing the place are removed', async () => {
      const place = buildPlace({ id: 10, trip_id: 1 });
      const otherPlace = buildPlace({ id: 20, trip_id: 1 });
      const assignmentWithPlace = buildAssignment({ id: 100, day_id: 1, place });
      const assignmentOther = buildAssignment({ id: 200, day_id: 1, place: otherPlace });

      seedStore(useTripStore, {
        places: [place, otherPlace],
        assignments: { '1': [assignmentWithPlace, assignmentOther] },
      });

      server.use(
        http.delete('/api/trips/1/places/10', () => HttpResponse.json({ success: true })),
      );

      await useTripStore.getState().deletePlace(1, 10);

      const dayAssignments = useTripStore.getState().assignments['1'];
      expect(dayAssignments).toHaveLength(1);
      expect(dayAssignments[0].id).toBe(200);
    });
  });

  describe('refreshPlaces', () => {
    it('FE-PLACES-007: refreshPlaces re-fetches and replaces places array', async () => {
      const stale = buildPlace({ id: 99, trip_id: 1, name: 'Stale' });
      seedStore(useTripStore, { places: [stale] });

      const fresh = buildPlace({ trip_id: 1, name: 'Fresh' });
      server.use(
        http.get('/api/trips/1/places', () => HttpResponse.json({ places: [fresh] })),
      );

      await useTripStore.getState().refreshPlaces(1);

      const places = useTripStore.getState().places;
      expect(places).toHaveLength(1);
      expect(places[0].name).toBe('Fresh');
    });
  });
});