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
Excluded: .git, node_modules, secrets/, compose.env, assemblyscript tgz Source: /opt/alga-psa on psa.joliet.tech
246 lines
7.4 KiB
TypeScript
246 lines
7.4 KiB
TypeScript
import React from 'react';
|
|
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
|
import { launchTimeEntryForWorkItem } from '../src/lib/timeEntryLauncher';
|
|
|
|
const getCurrentUser = vi.fn();
|
|
vi.mock('@alga-psa/users/actions', () => ({
|
|
getCurrentUser,
|
|
}));
|
|
|
|
const getCurrentTimePeriod = vi.fn();
|
|
vi.mock('../src/actions/timePeriodsActions', () => ({
|
|
getCurrentTimePeriod,
|
|
}));
|
|
|
|
const fetchOrCreateTimeSheet = vi.fn();
|
|
const saveTimeEntry = vi.fn();
|
|
vi.mock('../src/actions/timeEntryActions', () => ({
|
|
fetchOrCreateTimeSheet,
|
|
saveTimeEntry,
|
|
}));
|
|
|
|
const toastError = vi.fn();
|
|
vi.mock('react-hot-toast', () => ({
|
|
toast: { error: toastError },
|
|
}));
|
|
|
|
beforeEach(() => {
|
|
getCurrentUser.mockResolvedValue({ user_id: 'user-1' });
|
|
getCurrentTimePeriod.mockResolvedValue({
|
|
period_id: 'period-1',
|
|
start_date: '2026-01-01',
|
|
end_date: '2026-01-31',
|
|
});
|
|
fetchOrCreateTimeSheet.mockResolvedValue({ id: 'sheet-1' });
|
|
saveTimeEntry.mockResolvedValue({});
|
|
});
|
|
|
|
describe('launchTimeEntryForWorkItem', () => {
|
|
it('fetches current time period before opening the dialog', async () => {
|
|
const openDrawer = vi.fn();
|
|
await launchTimeEntryForWorkItem({
|
|
openDrawer,
|
|
closeDrawer: vi.fn(),
|
|
context: {
|
|
workItemId: 'ticket-1',
|
|
workItemType: 'ticket',
|
|
workItemName: 'Ticket 1',
|
|
},
|
|
});
|
|
|
|
expect(getCurrentTimePeriod).toHaveBeenCalled();
|
|
expect(openDrawer).toHaveBeenCalled();
|
|
});
|
|
|
|
it('creates or fetches a time sheet for the current user and period', async () => {
|
|
await launchTimeEntryForWorkItem({
|
|
openDrawer: vi.fn(),
|
|
closeDrawer: vi.fn(),
|
|
context: {
|
|
workItemId: 'ticket-1',
|
|
workItemType: 'ticket',
|
|
workItemName: 'Ticket 1',
|
|
},
|
|
});
|
|
|
|
expect(fetchOrCreateTimeSheet).toHaveBeenCalledWith('user-1', 'period-1');
|
|
});
|
|
|
|
it('builds a ticket work item with ticket context', async () => {
|
|
const openDrawer = vi.fn();
|
|
await launchTimeEntryForWorkItem({
|
|
openDrawer,
|
|
closeDrawer: vi.fn(),
|
|
context: {
|
|
workItemId: 'ticket-1',
|
|
workItemType: 'ticket',
|
|
workItemName: 'Ticket 1',
|
|
ticketNumber: 'T-123',
|
|
clientName: 'Acme',
|
|
timeDescription: 'Worked on issue',
|
|
},
|
|
});
|
|
|
|
const element = openDrawer.mock.calls[0][0] as React.ReactElement;
|
|
expect(element.props.workItem.work_item_id).toBe('ticket-1');
|
|
expect(element.props.workItem.type).toBe('ticket');
|
|
expect(element.props.workItem.name).toBe('Ticket 1');
|
|
expect(element.props.workItem.ticket_number).toBe('T-123');
|
|
expect(element.props.workItem.client_name).toBe('Acme');
|
|
expect(element.props.workItem.description).toBe('Worked on issue');
|
|
});
|
|
|
|
it('builds an interaction work item with interaction context', async () => {
|
|
const openDrawer = vi.fn();
|
|
const start = new Date('2026-02-01T09:00:00Z');
|
|
const end = new Date('2026-02-01T10:00:00Z');
|
|
|
|
await launchTimeEntryForWorkItem({
|
|
openDrawer,
|
|
closeDrawer: vi.fn(),
|
|
context: {
|
|
workItemId: 'interaction-1',
|
|
workItemType: 'interaction',
|
|
workItemName: 'Follow-up',
|
|
interactionType: 'Call',
|
|
clientName: 'Globex',
|
|
startTime: start,
|
|
endTime: end,
|
|
},
|
|
});
|
|
|
|
const element = openDrawer.mock.calls[0][0] as React.ReactElement;
|
|
expect(element.props.workItem.work_item_id).toBe('interaction-1');
|
|
expect(element.props.workItem.type).toBe('interaction');
|
|
expect(element.props.workItem.interaction_type).toBe('Call');
|
|
expect(element.props.workItem.client_name).toBe('Globex');
|
|
expect(element.props.workItem.startTime).toEqual(start);
|
|
expect(element.props.workItem.endTime).toEqual(end);
|
|
});
|
|
|
|
it('builds a project task work item with task context', async () => {
|
|
const openDrawer = vi.fn();
|
|
|
|
await launchTimeEntryForWorkItem({
|
|
openDrawer,
|
|
closeDrawer: vi.fn(),
|
|
context: {
|
|
workItemId: 'task-1',
|
|
workItemType: 'project_task',
|
|
workItemName: 'Build feature',
|
|
projectName: 'Project A',
|
|
phaseName: 'Phase 2',
|
|
taskName: 'Build feature',
|
|
serviceId: 'service-1',
|
|
serviceName: 'Implementation',
|
|
},
|
|
});
|
|
|
|
const element = openDrawer.mock.calls[0][0] as React.ReactElement;
|
|
expect(element.props.workItem.type).toBe('project_task');
|
|
expect(element.props.workItem.project_name).toBe('Project A');
|
|
expect(element.props.workItem.phase_name).toBe('Phase 2');
|
|
expect(element.props.workItem.task_name).toBe('Build feature');
|
|
expect(element.props.workItem.service_id).toBe('service-1');
|
|
expect(element.props.workItem.service_name).toBe('Implementation');
|
|
});
|
|
|
|
it('shows a toast error when no active time period exists', async () => {
|
|
const openDrawer = vi.fn();
|
|
getCurrentTimePeriod.mockResolvedValueOnce(null);
|
|
|
|
await launchTimeEntryForWorkItem({
|
|
openDrawer,
|
|
closeDrawer: vi.fn(),
|
|
context: {
|
|
workItemId: 'ticket-1',
|
|
workItemType: 'ticket',
|
|
workItemName: 'Ticket 1',
|
|
},
|
|
});
|
|
|
|
expect(toastError).toHaveBeenCalled();
|
|
expect(openDrawer).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it('opens TimeEntryDialog in drawer mode with time period and time sheet', async () => {
|
|
const openDrawer = vi.fn();
|
|
await launchTimeEntryForWorkItem({
|
|
openDrawer,
|
|
closeDrawer: vi.fn(),
|
|
context: {
|
|
workItemId: 'ticket-2',
|
|
workItemType: 'ticket',
|
|
workItemName: 'Ticket 2',
|
|
},
|
|
});
|
|
|
|
const element = openDrawer.mock.calls[0][0] as React.ReactElement;
|
|
expect(element.props.inDrawer).toBe(true);
|
|
expect(element.props.timePeriod.period_id).toBe('period-1');
|
|
expect(element.props.timeSheetId).toBe('sheet-1');
|
|
});
|
|
|
|
it('prefills service information for project tasks', async () => {
|
|
const openDrawer = vi.fn();
|
|
await launchTimeEntryForWorkItem({
|
|
openDrawer,
|
|
closeDrawer: vi.fn(),
|
|
context: {
|
|
workItemId: 'task-2',
|
|
workItemType: 'project_task',
|
|
workItemName: 'Configure service',
|
|
serviceId: 'service-99',
|
|
serviceName: 'Deployment',
|
|
},
|
|
});
|
|
|
|
const element = openDrawer.mock.calls[0][0] as React.ReactElement;
|
|
expect(element.props.workItem.service_id).toBe('service-99');
|
|
expect(element.props.workItem.service_name).toBe('Deployment');
|
|
});
|
|
|
|
it('saves time entry and closes the drawer on success', async () => {
|
|
const openDrawer = vi.fn();
|
|
const closeDrawer = vi.fn();
|
|
|
|
await launchTimeEntryForWorkItem({
|
|
openDrawer,
|
|
closeDrawer,
|
|
context: {
|
|
workItemId: 'ticket-3',
|
|
workItemType: 'ticket',
|
|
workItemName: 'Ticket 3',
|
|
},
|
|
});
|
|
|
|
const element = openDrawer.mock.calls[0][0] as React.ReactElement;
|
|
await element.props.onSave({ id: 'entry-1' });
|
|
|
|
expect(saveTimeEntry).toHaveBeenCalled();
|
|
expect(closeDrawer).toHaveBeenCalled();
|
|
});
|
|
|
|
it('invokes onComplete after successful save', async () => {
|
|
const openDrawer = vi.fn();
|
|
const closeDrawer = vi.fn();
|
|
const onComplete = vi.fn();
|
|
|
|
await launchTimeEntryForWorkItem({
|
|
openDrawer,
|
|
closeDrawer,
|
|
onComplete,
|
|
context: {
|
|
workItemId: 'ticket-4',
|
|
workItemType: 'ticket',
|
|
workItemName: 'Ticket 4',
|
|
},
|
|
});
|
|
|
|
const element = openDrawer.mock.calls[0][0] as React.ReactElement;
|
|
await element.props.onSave({ id: 'entry-2' });
|
|
|
|
expect(onComplete).toHaveBeenCalled();
|
|
});
|
|
});
|