File size: 3,598 Bytes
af1bee9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import { Table } from '@mantine/core';
import { fireEvent, screen } from '@testing-library/react';
import { describe, expect, it, vi } from 'vitest';
import { renderUi } from '../test/utils';
import { Pager } from './common';
import { NavTabs } from './NavTabs';
import { DataTable, LoadingState, QueryState } from './states';

const rows = <Table.Tr><Table.Td>alice</Table.Td><Table.Td>admin</Table.Td></Table.Tr>;

describe('shared states', () => {
  it('loading is announced once, with words', () => {
    renderUi(<LoadingState label="Loading the users" />);
    expect(screen.getByRole('status')).toHaveTextContent('Loading the users');
  });

  it('QueryState shows loading, then the error with Try again, then empty, then the content', () => {
    const refetch = vi.fn();
    let view = renderUi(<QueryState q={{ isLoading: true }}>content</QueryState>);
    expect(screen.getByTestId('loading-state')).toBeInTheDocument();

    view.unmount();
    view = renderUi(<QueryState q={{ error: new Error('boom'), refetch }}>content</QueryState>);
    fireEvent.click(screen.getByTestId('error-retry'));
    expect(refetch).toHaveBeenCalledOnce();

    view.unmount();
    view = renderUi(<QueryState q={{}} isEmpty empty={<p>No users yet.</p>}>content</QueryState>);
    expect(screen.getByText('No users yet.')).toBeInTheDocument();

    view.unmount();
    view = renderUi(<QueryState q={{}}>content</QueryState>);
    expect(screen.getByText('content')).toBeInTheDocument();
  });

  it('DataTable is a named, keyboard-reachable region with loading and empty rows', () => {
    let view = renderUi(<DataTable label="Users" columns={['User', { label: 'Actions', hidden: true }]} q={{ isLoading: true }} rowCount={0}>{rows}</DataTable>);
    const region = screen.getByRole('region', { name: 'Users' });
    expect(region).toHaveAttribute('tabindex', '0');
    expect(screen.getByRole('table', { name: 'Users' })).toBeInTheDocument();
    expect(screen.getByRole('columnheader', { name: 'Actions' })).toBeInTheDocument();   // hidden visually, still named
    expect(screen.getAllByTestId('table-loading')).toHaveLength(3);

    view.unmount();
    view = renderUi(<DataTable label="Users" columns={['User', 'Role']} q={{}} rowCount={0} empty="No users yet.">{rows}</DataTable>);
    expect(screen.getByTestId('table-empty')).toHaveTextContent('No users yet.');

    view.unmount();
    view = renderUi(<DataTable label="Users" columns={['User', 'Role']} q={{}} rowCount={1}>{rows}</DataTable>);
    expect(screen.getByRole('cell', { name: 'alice' })).toBeInTheDocument();
  });

  it('pagination controls have names', () => {
    renderUi(<Pager total={120} limit={50} offset={0} onChange={() => {}} />);
    expect(screen.getByRole('navigation', { name: 'Pagination' })).toBeInTheDocument();
    expect(screen.getByRole('button', { name: 'Page 2 of 3' })).toBeInTheDocument();
    expect(screen.getByRole('button', { name: 'Next page' })).toBeInTheDocument();
  });

  it('NavTabs marks the current view for screen readers', () => {
    renderUi(<NavTabs label="Views of this test run" items={[{ to: '/runs/1/scenarios', label: 'Scenarios', active: true }, { to: '/runs/1/report', label: 'Report', active: false }]} />);
    expect(screen.getByRole('navigation', { name: 'Views of this test run' })).toBeInTheDocument();
    expect(screen.getByRole('link', { name: 'Scenarios' })).toHaveAttribute('aria-current', 'page');
    expect(screen.getByRole('link', { name: 'Report' })).not.toHaveAttribute('aria-current');
  });
});