PSA/packages/scheduling/tests/agentScheduleView.test.tsx
Hermes 284313f908
Some checks are pending
Bidi Control Character Guard / bidi-control-guard (push) Waiting to run
Circular Dependency Check / Check for new circular dependencies (push) Waiting to run
Citus Migration Smoke / Combined migrations on single-node Citus (push) Waiting to run
E2E Fresh Install Tests / fresh-install-e2e (push) Waiting to run
ext-v2 guardrails / Run ext-v2 guard and ESLint (push) Waiting to run
Integration Tests / Check for relevant changes (push) Waiting to run
Integration Tests / ${{ (github.event_name == 'schedule' || github.event.inputs.suite == 'full') && 'Full integration suite' || 'Tier-1 integration subset' }} (push) Blocked by required conditions
Mobile checks / Mobile lint + typecheck (push) Waiting to run
Mobile checks / Mobile unit tests (push) Waiting to run
Mobile checks / Mobile dependency audit (report) (push) Waiting to run
Mobile checks / Mobile reproducibility checks (push) Waiting to run
Secrets guard (env backups) / Ensure no tracked env backup files (push) Waiting to run
Temporal Readiness / fast-readiness (push) Waiting to run
Temporal Readiness / docker-parity (push) Waiting to run
TypeScript Type Check / Nx affected typecheck (push) Waiting to run
Unit Tests / Skipped-test budget (push) Waiting to run
Unit Tests / Nx affected unit tests (push) Waiting to run
Unit Tests / Server unit coverage (informational) (push) Waiting to run
Validate Tenant Management Schema / Check for relevant changes (push) Waiting to run
Validate Tenant Management Schema / Validate Tenant Management Schema (push) Blocked by required conditions
EE Workflows Build Guard / ee-workflows-build-guard (push) Waiting to run
Initial import of AlgaPSA codebase from PSA server
Excluded: .git, node_modules, secrets/, compose.env, assemblyscript tgz

Source: /opt/alga-psa on psa.joliet.tech
2026-06-22 16:12:17 -05:00

174 lines
5.7 KiB
TypeScript

// @vitest-environment jsdom
import React from 'react';
import { describe, it, expect, vi, beforeEach } from 'vitest';
import { act, render, waitFor } from '@testing-library/react';
import AgentScheduleView from '../src/components/schedule/AgentScheduleView';
const calendarSpy = vi.fn();
vi.mock('next/dynamic', () => ({
default: () => (props: any) => {
calendarSpy(props);
return <div data-testid="calendar" />;
}
}));
const getScheduleEntries = vi.fn();
vi.mock('@alga-psa/scheduling/actions', () => ({
getScheduleEntries,
}));
const getCurrentUser = vi.fn();
const getCurrentUserPermissions = vi.fn();
vi.mock('@alga-psa/users/actions', () => ({
getCurrentUser,
getCurrentUserPermissions,
}));
const useUsers = vi.fn(() => ({ users: [] }));
vi.mock('@alga-psa/users/hooks', () => ({
useUsers,
}));
const entryPopupSpy = vi.fn();
vi.mock('../src/components/schedule/EntryPopup', () => ({
default: (props: any) => {
entryPopupSpy(props);
return <div data-testid="entry-popup" />;
},
}));
vi.mock('../src/components/schedule/CalendarStyleProvider', () => ({
CalendarStyleProvider: () => <div data-testid="calendar-style-provider" />,
}));
vi.mock('../src/components/schedule/AgentScheduleDrawerStyles', () => ({
AgentScheduleDrawerStyles: () => <div data-testid="agent-drawer-styles" />,
}));
beforeEach(() => {
calendarSpy.mockClear();
getScheduleEntries.mockResolvedValue({ success: true, entries: [] });
getCurrentUser.mockResolvedValue({ user_id: 'user-1' });
getCurrentUserPermissions.mockResolvedValue(['user_schedule:read:all']);
});
describe('AgentScheduleView', () => {
it('renders without error for a valid agent', () => {
const { getByTestId } = render(<AgentScheduleView agentId="agent-1" />);
expect(getByTestId('calendar')).toBeTruthy();
});
it('calls getScheduleEntries with the agent ID and date range', async () => {
render(<AgentScheduleView agentId="agent-1" />);
await waitFor(() => expect(getScheduleEntries).toHaveBeenCalled());
const [start, end, technicianIds] = getScheduleEntries.mock.calls[0];
expect(technicianIds).toEqual(['agent-1']);
expect(start).toBeInstanceOf(Date);
expect(end).toBeInstanceOf(Date);
expect((end as Date).getTime()).toBeGreaterThan((start as Date).getTime());
});
it('shows a loading state while fetching events', async () => {
let resolvePromise: (value: any) => void = () => {};
const pending = new Promise((resolve) => {
resolvePromise = resolve as (value: any) => void;
});
getScheduleEntries.mockReturnValueOnce(pending);
const { getByText } = render(<AgentScheduleView agentId="agent-1" />);
expect(getByText(/Loading/i)).toBeTruthy();
resolvePromise({ success: true, entries: [] });
});
it('defaults to week view', () => {
render(<AgentScheduleView agentId="agent-1" />);
const props = calendarSpy.mock.calls[0][0];
expect(props.defaultView).toBe('week');
expect(props.view).toBe('week');
});
it('allows switching between day, week, and month views', () => {
render(<AgentScheduleView agentId="agent-1" />);
const initialProps = calendarSpy.mock.calls[0][0];
act(() => {
initialProps.onView('day');
});
const dayProps = calendarSpy.mock.calls.at(-1)[0];
expect(dayProps.view).toBe('day');
act(() => {
dayProps.onView('month');
});
const monthProps = calendarSpy.mock.calls.at(-1)[0];
expect(monthProps.view).toBe('month');
});
it('applies work item colors to events', () => {
render(<AgentScheduleView agentId="agent-1" />);
const props = calendarSpy.mock.calls[0][0];
const result = props.eventPropGetter({ work_item_type: 'ticket' });
expect(result.style.backgroundColor).toBe('rgb(var(--color-primary-200))');
});
it('opens EntryPopup when an event is clicked', () => {
const { getByTestId } = render(<AgentScheduleView agentId="agent-1" />);
const props = calendarSpy.mock.calls[0][0];
act(() => {
props.onSelectEvent({
entry_id: 'entry-1',
scheduled_start: new Date(),
scheduled_end: new Date(),
work_item_type: 'ticket',
assigned_user_ids: [],
});
});
expect(getByTestId('entry-popup')).toBeTruthy();
});
it('sets scrollToTime to 8 AM', () => {
render(<AgentScheduleView agentId="agent-1" />);
const props = calendarSpy.mock.calls[0][0];
const scrollToTime = props.scrollToTime as Date;
expect(scrollToTime.getHours()).toBe(8);
expect(scrollToTime.getMinutes()).toBe(0);
});
it('restricts users with user_schedule:read to their own schedule', async () => {
getCurrentUser.mockResolvedValueOnce({ user_id: 'user-1' });
getCurrentUserPermissions.mockResolvedValueOnce(['user_schedule:read']);
const { getByText } = render(<AgentScheduleView agentId="user-2" />);
await waitFor(() => expect(getByText(/permission/i)).toBeTruthy());
expect(getScheduleEntries).not.toHaveBeenCalled();
});
it('allows users with user_schedule:read:all to view any agent', async () => {
getCurrentUser.mockResolvedValueOnce({ user_id: 'user-1' });
getCurrentUserPermissions.mockResolvedValueOnce(['user_schedule:read:all']);
render(<AgentScheduleView agentId="user-2" />);
await waitFor(() => expect(getScheduleEntries).toHaveBeenCalled());
const [, , technicianIds] = getScheduleEntries.mock.calls[0];
expect(technicianIds).toEqual(['user-2']);
});
it('renders CalendarStyleProvider and AgentScheduleDrawerStyles', () => {
const { getByTestId } = render(<AgentScheduleView agentId="agent-1" />);
expect(getByTestId('calendar-style-provider')).toBeTruthy();
expect(getByTestId('agent-drawer-styles')).toBeTruthy();
});
});