Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
182 changes: 174 additions & 8 deletions packages/raystack/components/data-table/__tests__/data-table.test.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { describe, expect, it, vi } from 'vitest';
import { beforeAll, describe, expect, it, vi } from 'vitest';
import { DataTable } from '../data-table';
import styles from '../data-table.module.css';
import { DataTableColumnDef } from '../data-table.types';

beforeAll(() => {
global.IntersectionObserver = vi.fn().mockImplementation(() => ({
observe: vi.fn(),
unobserve: vi.fn(),
disconnect: vi.fn()
}));
});

interface TestData {
id: number;
name: string;
Expand Down Expand Up @@ -43,7 +51,11 @@ describe('DataTable', () => {
describe('Basic Rendering', () => {
it('renders data table with content', () => {
render(
<DataTable data={mockData} columns={mockColumns}>
<DataTable
data={mockData}
columns={mockColumns}
defaultSort={{ name: 'name', order: 'asc' }}
>
<DataTable.Content />
</DataTable>
);
Expand All @@ -58,7 +70,11 @@ describe('DataTable', () => {
};

render(
<DataTable data={mockData} columns={mockColumns}>
<DataTable
data={mockData}
columns={mockColumns}
defaultSort={{ name: 'name', order: 'asc' }}
>
<TestComponent />
</DataTable>
);
Expand All @@ -68,7 +84,11 @@ describe('DataTable', () => {

it('renders with empty data', () => {
render(
<DataTable data={[]} columns={mockColumns}>
<DataTable
data={[]}
columns={mockColumns}
defaultSort={{ name: 'name', order: 'asc' }}
>
<DataTable.Content />
</DataTable>
);
Expand All @@ -80,7 +100,11 @@ describe('DataTable', () => {
describe('Data Display', () => {
it('displays table data in content', () => {
render(
<DataTable data={mockData} columns={mockColumns}>
<DataTable
data={mockData}
columns={mockColumns}
defaultSort={{ name: 'name', order: 'asc' }}
>
<DataTable.Content />
</DataTable>
);
Expand All @@ -94,7 +118,11 @@ describe('DataTable', () => {

it('displays column headers', () => {
render(
<DataTable data={mockData} columns={mockColumns}>
<DataTable
data={mockData}
columns={mockColumns}
defaultSort={{ name: 'name', order: 'asc' }}
>
<DataTable.Content />
</DataTable>
);
Expand All @@ -114,6 +142,7 @@ describe('DataTable', () => {
<DataTable
data={mockData}
columns={mockColumns}
defaultSort={{ name: 'name', order: 'asc' }}
onRowClick={onRowClick}
>
<DataTable.Content />
Expand All @@ -129,7 +158,11 @@ describe('DataTable', () => {
describe('Component Composition', () => {
it('renders with toolbar', () => {
const { container } = render(
<DataTable data={mockData} columns={mockColumns}>
<DataTable
data={mockData}
columns={mockColumns}
defaultSort={{ name: 'name', order: 'asc' }}
>
<DataTable.Toolbar />
<DataTable.Content />
</DataTable>
Expand All @@ -142,7 +175,11 @@ describe('DataTable', () => {

it('renders with search', () => {
render(
<DataTable data={mockData} columns={mockColumns}>
<DataTable
data={mockData}
columns={mockColumns}
defaultSort={{ name: 'name', order: 'asc' }}
>
<DataTable.Search />
<DataTable.Content />
</DataTable>
Expand Down Expand Up @@ -409,4 +446,133 @@ describe('DataTable', () => {
expect(screen.getByText('John Doe')).toBeInTheDocument();
});
});

describe('Display Settings Reset', () => {
const columnsWithSortAndGroup: DataTableColumnDef<TestData, unknown>[] = [
{
id: 'name',
accessorKey: 'name',
header: 'Name',
cell: ({ getValue }) => getValue(),
enableSorting: true,
enableGrouping: true
},
{
id: 'email',
accessorKey: 'email',
header: 'Email',
cell: ({ getValue }) => getValue(),
enableSorting: true
},
{
id: 'status',
accessorKey: 'status',
header: 'Status',
cell: ({ getValue }) => getValue(),
enableSorting: true,
enableGrouping: true
}
];

it('resets sort and group to defaults on reset click', async () => {
const onTableQueryChange = vi.fn();
const user = userEvent.setup();

render(
<DataTable
data={mockData}
columns={columnsWithSortAndGroup}
defaultSort={{ name: 'name', order: 'asc' }}
mode='server'
onTableQueryChange={onTableQueryChange}
query={{
sort: [{ name: 'email', order: 'desc' }],
group_by: ['status']
}}
>
<DataTable.Toolbar />
<DataTable.Content />
</DataTable>
);

// Open Display popover and click reset
await user.click(screen.getByText('Display'));
await user.click(screen.getByText('Reset to default'));

// Verify onTableQueryChange was called with default sort and no group
expect(onTableQueryChange).toHaveBeenLastCalledWith(
expect.objectContaining({
sort: [{ name: 'name', order: 'asc' }],
group_by: []
})
);
});

it('does not show zero state when sort or group changes in client mode', () => {
render(
<DataTable
data={mockData}
columns={columnsWithSortAndGroup}
defaultSort={{ name: 'name', order: 'asc' }}
mode='client'
query={{
sort: [{ name: 'email', order: 'desc' }],
group_by: ['status']
}}
>
<DataTable.Content
zeroState={<div data-testid='zero-state'>No data</div>}
emptyState={<div data-testid='empty-state'>No results</div>}
/>
</DataTable>
);

// Data should still be visible, not zero/empty state
expect(screen.getByText('John Doe')).toBeInTheDocument();
expect(screen.queryByTestId('zero-state')).not.toBeInTheDocument();
expect(screen.queryByTestId('empty-state')).not.toBeInTheDocument();
});

it('shows empty state when sort is changed and no data', () => {
render(
<DataTable
data={[]}
columns={columnsWithSortAndGroup}
defaultSort={{ name: 'name', order: 'asc' }}
query={{
sort: [{ name: 'email', order: 'desc' }]
}}
>
<DataTable.Content
zeroState={<div data-testid='zero-state'>No data</div>}
emptyState={<div data-testid='empty-state'>No results</div>}
/>
</DataTable>
);

expect(screen.getByTestId('empty-state')).toBeInTheDocument();
expect(screen.queryByTestId('zero-state')).not.toBeInTheDocument();
});

it('shows empty state when group is changed and no data', () => {
render(
<DataTable
data={[]}
columns={columnsWithSortAndGroup}
defaultSort={{ name: 'name', order: 'asc' }}
query={{
group_by: ['status']
}}
>
<DataTable.Content
zeroState={<div data-testid='zero-state'>No data</div>}
emptyState={<div data-testid='empty-state'>No results</div>}
/>
</DataTable>
);

expect(screen.getByTestId('empty-state')).toBeInTheDocument();
expect(screen.queryByTestId('zero-state')).not.toBeInTheDocument();
});
});
});
12 changes: 6 additions & 6 deletions packages/raystack/components/data-table/components/content.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
GroupedData
} from '../data-table.types';
import { useDataTable } from '../hooks/useDataTable';
import { hasActiveQuery } from '../utils';

function Headers<TData>({
headerGroups = [],
Expand Down Expand Up @@ -172,7 +173,8 @@ export function Content({
isLoading,
loadMoreData,
loadingRowCount = 3,
tableQuery
tableQuery,
defaultSort
} = useDataTable();

const headerGroups = table?.getHeaderGroups();
Expand Down Expand Up @@ -219,12 +221,10 @@ export function Content({

const hasData = rows?.length > 0 || isLoading;

const hasFiltersOrSearch =
(tableQuery?.filters && tableQuery.filters.length > 0) ||
Boolean(tableQuery?.search && tableQuery.search.trim() !== '');
const hasChanges = hasActiveQuery(tableQuery || {}, defaultSort);

const isZeroState = !hasData && !hasFiltersOrSearch;
const isEmptyState = !hasData && hasFiltersOrSearch;
const isZeroState = !hasData && !hasChanges;
const isEmptyState = !hasData && hasChanges;

const stateToShow: React.ReactNode = isZeroState
? (zeroState ?? emptyState ?? <DefaultEmptyComponent />)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
VirtualizedContentProps
} from '../data-table.types';
import { useDataTable } from '../hooks/useDataTable';
import { hasActiveQuery } from '../utils';

function VirtualHeaders<TData>({
headerGroups = [],
Expand Down Expand Up @@ -220,6 +221,7 @@ export function VirtualizedContent({
isLoading,
loadMoreData,
tableQuery,
defaultSort,
loadingRowCount = 3
} = useDataTable();

Expand Down Expand Up @@ -255,12 +257,10 @@ export function VirtualizedContent({

const hasData = rows?.length > 0 || isLoading;

const hasFiltersOrSearch =
(tableQuery?.filters && tableQuery.filters.length > 0) ||
Boolean(tableQuery?.search && tableQuery.search.trim() !== '');
const hasChanges = hasActiveQuery(tableQuery || {}, defaultSort);

const isZeroState = !hasData && !hasFiltersOrSearch;
const isEmptyState = !hasData && hasFiltersOrSearch;
const isZeroState = !hasData && !hasChanges;
const isEmptyState = !hasData && hasChanges;

const stateToShow: React.ReactNode = isZeroState
? (zeroState ?? emptyState ?? <DefaultEmptyComponent />)
Expand Down
Loading