import React from 'react';
import { vi, describe, it, expect, beforeEach } from 'vitest';
import { render, screen } from '@testing-library/react';
import { getDefaultsFromConfigSchema, useConfig } from '@openmrs/esm-framework';
import { configSchema, type ConfigObject } from '../config-schema';
import { type Queue } from '../types';
import QueueTablesForAllStatuses from './queue-tables-for-all-statuses.component';
vi.mock('../queue-table/queue-table-metrics.component', () => ({
__esModule: true,
default: vi.fn(() =>
),
}));
vi.mock('../attending-patients/attending-patients.component', () => ({
__esModule: true,
default: vi.fn(({ queueUuid }) => attending:{queueUuid}
),
}));
vi.mock('../queue-table/default-queue-table.component', () => ({
__esModule: true,
default: vi.fn(({ queueUuid, status }) => (
{status.display}:{queueUuid}
)),
}));
const config = getDefaultsFromConfigSchema(configSchema);
const inServiceUuid = config.concepts.defaultTransitionStatus;
// The concept set in the dictionary returns the statuses as [Finished service, In Service, Waiting].
// The view reverses the list, so the tables show Waiting first.
const queue = {
uuid: 'q1',
display: 'Outpatient Triage',
allowedStatuses: [
{ uuid: 'finished-uuid', display: 'Finished service' },
{ uuid: inServiceUuid, display: 'In Service' },
{ uuid: 'waiting-uuid', display: 'Waiting' },
],
} as unknown as Queue;
describe('QueueTablesForAllStatuses', () => {
beforeEach(() => {
vi.mocked(useConfig).mockReturnValue(config);
});
it('gives every status except in-service a table, in workflow order', () => {
render();
const tables = screen.getAllByTestId('status-table');
expect(tables.map((table) => table.textContent)).toEqual(['Waiting:q1', 'Finished service:q1']);
});
it('shows in-service patients as cards rather than a table', () => {
render();
expect(screen.getByTestId('attending-patients')).toHaveTextContent('attending:q1');
expect(screen.queryByText(/^In Service:/)).not.toBeInTheDocument();
});
it('names the queue in the header and shows its metrics', () => {
render();
expect(screen.getByText('Outpatient Triage')).toBeInTheDocument();
expect(screen.getByTestId('queue-metrics')).toBeInTheDocument();
});
// In-service patients are the Attending cards, so such a queue has no table but is configured fine.
it('treats a queue that allows only the in-service status as configured', () => {
render(
,
);
expect(screen.queryByText(/no status configured/i)).not.toBeInTheDocument();
expect(screen.getByTestId('attending-patients')).toHaveTextContent('attending:q1');
expect(screen.queryByTestId('status-table')).not.toBeInTheDocument();
});
it('asks for a status to be configured when the queue allows none', () => {
render(
,
);
expect(screen.getByText(/no status configured/i)).toBeInTheDocument();
expect(screen.queryByTestId('status-table')).not.toBeInTheDocument();
});
it('reports an unusable queue instead of rendering empty tables', () => {
render(
,
);
expect(screen.getByText(/invalid queue/i)).toBeInTheDocument();
expect(screen.queryByTestId('status-table')).not.toBeInTheDocument();
});
it('reports a queue that resolved to nothing without erroring', () => {
render();
expect(screen.getByText(/invalid queue/i)).toBeInTheDocument();
expect(screen.queryByTestId('status-table')).not.toBeInTheDocument();
});
it('shows the skeleton, not an invalid-queue error, while the queue is still loading', () => {
render();
expect(screen.queryByText(/invalid queue/i)).not.toBeInTheDocument();
expect(screen.queryByTestId('status-table')).not.toBeInTheDocument();
expect(screen.getAllByRole('table').length).toBeGreaterThan(0);
});
});