2025-10-29 22:16:33 -06:00
|
|
|
import React from 'react';
|
2025-12-06 14:49:46 -07:00
|
|
|
import {
|
|
|
|
|
createMockDomain,
|
|
|
|
|
createTestForm,
|
|
|
|
|
hasInvertedStyling,
|
|
|
|
|
render,
|
|
|
|
|
renderInDarkMode,
|
|
|
|
|
renderInLightMode,
|
|
|
|
|
screen
|
|
|
|
|
} from '../../test-utils';
|
2025-10-29 22:16:33 -06:00
|
|
|
import {CollectionEditForm} from './CollectionEditForm';
|
|
|
|
|
|
2025-12-05 18:26:36 -07:00
|
|
|
// Mock the TagsContext
|
2025-10-29 22:16:33 -06:00
|
|
|
jest.mock('../../Tags', () => ({
|
|
|
|
|
TagsContext: {
|
|
|
|
|
_currentValue: {
|
|
|
|
|
SingleTag: ({name}) => <span data-testid="applied-tag">{name}</span>
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
// Mock Common components
|
|
|
|
|
jest.mock('../Common', () => ({
|
|
|
|
|
...jest.requireActual('../Common'),
|
2025-12-05 18:26:36 -07:00
|
|
|
WROLModeMessage: ({content}) => <div data-testid="wrol-mode-message">{content}</div>,
|
2025-10-29 22:16:33 -06:00
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
describe('CollectionEditForm', () => {
|
|
|
|
|
const mockCollection = createMockDomain();
|
|
|
|
|
|
|
|
|
|
describe('Form Rendering', () => {
|
2025-12-05 18:26:36 -07:00
|
|
|
it('renders children and Save button', () => {
|
2025-10-29 22:16:33 -06:00
|
|
|
const form = createTestForm(mockCollection);
|
|
|
|
|
|
|
|
|
|
render(
|
2025-12-05 18:26:36 -07:00
|
|
|
<CollectionEditForm form={form}>
|
|
|
|
|
<div data-testid="child-content">Form fields here</div>
|
|
|
|
|
</CollectionEditForm>
|
2025-10-29 22:16:33 -06:00
|
|
|
);
|
|
|
|
|
|
2025-12-05 18:26:36 -07:00
|
|
|
expect(screen.getByTestId('child-content')).toBeInTheDocument();
|
2025-10-29 22:16:33 -06:00
|
|
|
expect(screen.getByRole('button', {name: /save/i})).toBeInTheDocument();
|
|
|
|
|
});
|
|
|
|
|
|
2025-12-05 18:26:36 -07:00
|
|
|
it('renders title when provided', () => {
|
2025-10-29 22:16:33 -06:00
|
|
|
const form = createTestForm(mockCollection);
|
|
|
|
|
|
|
|
|
|
render(
|
2025-12-05 18:26:36 -07:00
|
|
|
<CollectionEditForm form={form} title="Edit Domain: example.com">
|
|
|
|
|
<div>Form content</div>
|
|
|
|
|
</CollectionEditForm>
|
2025-10-29 22:16:33 -06:00
|
|
|
);
|
|
|
|
|
|
2025-12-05 18:26:36 -07:00
|
|
|
expect(screen.getByRole('heading', {level: 1})).toHaveTextContent('Edit Domain: example.com');
|
2025-10-29 22:16:33 -06:00
|
|
|
});
|
|
|
|
|
|
2025-12-05 18:26:36 -07:00
|
|
|
it('renders WROL mode message when provided', () => {
|
2025-10-29 22:16:33 -06:00
|
|
|
const form = createTestForm(mockCollection);
|
|
|
|
|
|
|
|
|
|
render(
|
|
|
|
|
<CollectionEditForm
|
|
|
|
|
form={form}
|
2025-12-05 18:26:36 -07:00
|
|
|
wrolModeContent="Editing disabled in WROL Mode"
|
|
|
|
|
>
|
|
|
|
|
<div>Form content</div>
|
|
|
|
|
</CollectionEditForm>
|
2025-10-29 22:16:33 -06:00
|
|
|
);
|
|
|
|
|
|
2025-12-05 18:26:36 -07:00
|
|
|
expect(screen.getByTestId('wrol-mode-message')).toHaveTextContent('Editing disabled in WROL Mode');
|
2025-10-29 22:16:33 -06:00
|
|
|
});
|
|
|
|
|
|
2025-12-05 18:26:36 -07:00
|
|
|
it('renders action buttons when provided', () => {
|
2025-10-29 22:16:33 -06:00
|
|
|
const form = createTestForm(mockCollection);
|
|
|
|
|
|
|
|
|
|
render(
|
|
|
|
|
<CollectionEditForm
|
|
|
|
|
form={form}
|
2025-12-05 18:26:36 -07:00
|
|
|
actionButtons={<button data-testid="delete-button">Delete</button>}
|
|
|
|
|
>
|
|
|
|
|
<div>Form content</div>
|
|
|
|
|
</CollectionEditForm>
|
2025-10-29 22:16:33 -06:00
|
|
|
);
|
|
|
|
|
|
2025-12-05 18:26:36 -07:00
|
|
|
expect(screen.getByTestId('delete-button')).toBeInTheDocument();
|
2025-10-29 22:16:33 -06:00
|
|
|
});
|
|
|
|
|
|
2025-12-05 18:26:36 -07:00
|
|
|
it('renders appliedTagName when provided', () => {
|
2025-10-29 22:16:33 -06:00
|
|
|
const form = createTestForm(mockCollection);
|
|
|
|
|
|
|
|
|
|
render(
|
2025-12-05 18:26:36 -07:00
|
|
|
<CollectionEditForm form={form} appliedTagName="News">
|
|
|
|
|
<div>Form content</div>
|
|
|
|
|
</CollectionEditForm>
|
2025-10-29 22:16:33 -06:00
|
|
|
);
|
|
|
|
|
|
2025-12-05 18:26:36 -07:00
|
|
|
expect(screen.getByTestId('applied-tag')).toHaveTextContent('News');
|
2025-10-29 22:16:33 -06:00
|
|
|
});
|
|
|
|
|
|
2025-12-05 18:26:36 -07:00
|
|
|
it('does not render appliedTagName when not provided', () => {
|
|
|
|
|
const form = createTestForm(mockCollection);
|
2025-10-29 22:16:33 -06:00
|
|
|
|
|
|
|
|
render(
|
2025-12-05 18:26:36 -07:00
|
|
|
<CollectionEditForm form={form}>
|
|
|
|
|
<div>Form content</div>
|
|
|
|
|
</CollectionEditForm>
|
2025-10-29 22:16:33 -06:00
|
|
|
);
|
|
|
|
|
|
2025-12-05 18:26:36 -07:00
|
|
|
expect(screen.queryByTestId('applied-tag')).not.toBeInTheDocument();
|
2025-10-29 22:16:33 -06:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe('Save/Cancel Actions', () => {
|
|
|
|
|
it('shows Cancel button when onCancel provided', () => {
|
|
|
|
|
const form = createTestForm(mockCollection);
|
|
|
|
|
const mockOnCancel = jest.fn();
|
|
|
|
|
|
|
|
|
|
render(
|
2025-12-05 18:26:36 -07:00
|
|
|
<CollectionEditForm form={form} onCancel={mockOnCancel}>
|
|
|
|
|
<div>Form content</div>
|
|
|
|
|
</CollectionEditForm>
|
2025-10-29 22:16:33 -06:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
expect(screen.getByRole('button', {name: /cancel/i})).toBeInTheDocument();
|
|
|
|
|
});
|
|
|
|
|
|
2025-12-05 18:26:36 -07:00
|
|
|
it('does not show Cancel button when onCancel not provided', () => {
|
|
|
|
|
const form = createTestForm(mockCollection);
|
2025-10-29 22:16:33 -06:00
|
|
|
|
|
|
|
|
render(
|
2025-12-05 18:26:36 -07:00
|
|
|
<CollectionEditForm form={form}>
|
|
|
|
|
<div>Form content</div>
|
|
|
|
|
</CollectionEditForm>
|
2025-10-29 22:16:33 -06:00
|
|
|
);
|
|
|
|
|
|
2025-12-05 18:26:36 -07:00
|
|
|
expect(screen.queryByRole('button', {name: /cancel/i})).not.toBeInTheDocument();
|
2025-10-29 22:16:33 -06:00
|
|
|
});
|
|
|
|
|
|
2025-12-05 18:26:36 -07:00
|
|
|
it('disables Save button when form is disabled', () => {
|
|
|
|
|
const form = createTestForm(mockCollection, {
|
|
|
|
|
overrides: {disabled: true}
|
|
|
|
|
});
|
2025-10-29 22:16:33 -06:00
|
|
|
|
|
|
|
|
render(
|
2025-12-05 18:26:36 -07:00
|
|
|
<CollectionEditForm form={form}>
|
|
|
|
|
<div>Form content</div>
|
|
|
|
|
</CollectionEditForm>
|
2025-10-29 22:16:33 -06:00
|
|
|
);
|
|
|
|
|
|
2025-12-05 18:26:36 -07:00
|
|
|
expect(screen.getByRole('button', {name: /save/i})).toBeDisabled();
|
2025-12-05 17:12:21 -07:00
|
|
|
});
|
|
|
|
|
|
2025-12-05 18:26:36 -07:00
|
|
|
it('disables Cancel button when form is disabled', () => {
|
|
|
|
|
const form = createTestForm(mockCollection, {
|
|
|
|
|
overrides: {disabled: true}
|
|
|
|
|
});
|
|
|
|
|
const mockOnCancel = jest.fn();
|
2025-12-05 17:12:21 -07:00
|
|
|
|
|
|
|
|
render(
|
2025-12-05 18:26:36 -07:00
|
|
|
<CollectionEditForm form={form} onCancel={mockOnCancel}>
|
|
|
|
|
<div>Form content</div>
|
|
|
|
|
</CollectionEditForm>
|
2025-12-05 17:12:21 -07:00
|
|
|
);
|
|
|
|
|
|
2025-12-05 18:26:36 -07:00
|
|
|
expect(screen.getByRole('button', {name: /cancel/i})).toBeDisabled();
|
2025-10-29 22:16:33 -06:00
|
|
|
});
|
2025-12-05 18:26:36 -07:00
|
|
|
});
|
2025-12-02 15:33:46 -07:00
|
|
|
|
2025-12-05 18:26:36 -07:00
|
|
|
describe('Error States', () => {
|
2025-12-02 15:33:46 -07:00
|
|
|
it('displays form-level errors', () => {
|
|
|
|
|
const form = createTestForm(mockCollection, {
|
|
|
|
|
overrides: {error: 'Something went wrong'}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
render(
|
2025-12-05 18:26:36 -07:00
|
|
|
<CollectionEditForm form={form}>
|
|
|
|
|
<div>Form content</div>
|
|
|
|
|
</CollectionEditForm>
|
2025-12-02 15:33:46 -07:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
expect(screen.getByText(/something went wrong/i)).toBeInTheDocument();
|
|
|
|
|
});
|
|
|
|
|
|
2025-12-05 18:26:36 -07:00
|
|
|
it('does not display error message when no error', () => {
|
|
|
|
|
const form = createTestForm(mockCollection);
|
2025-12-02 15:33:46 -07:00
|
|
|
|
2025-12-05 18:26:36 -07:00
|
|
|
render(
|
|
|
|
|
<CollectionEditForm form={form}>
|
|
|
|
|
<div>Form content</div>
|
|
|
|
|
</CollectionEditForm>
|
2025-12-02 15:33:46 -07:00
|
|
|
);
|
|
|
|
|
|
2025-12-05 18:26:36 -07:00
|
|
|
expect(screen.queryByText(/error/i)).not.toBeInTheDocument();
|
2025-12-02 15:33:46 -07:00
|
|
|
});
|
2025-12-05 18:26:36 -07:00
|
|
|
});
|
2025-12-02 15:33:46 -07:00
|
|
|
|
2025-12-05 18:26:36 -07:00
|
|
|
describe('Form Submission', () => {
|
|
|
|
|
it('calls form.onSubmit when Save button is clicked', () => {
|
|
|
|
|
const form = createTestForm(mockCollection);
|
|
|
|
|
form.onSubmit = jest.fn();
|
2025-12-02 15:33:46 -07:00
|
|
|
|
2025-12-05 18:26:36 -07:00
|
|
|
render(
|
|
|
|
|
<CollectionEditForm form={form}>
|
|
|
|
|
<div>Form content</div>
|
|
|
|
|
</CollectionEditForm>
|
2025-12-02 15:33:46 -07:00
|
|
|
);
|
|
|
|
|
|
2025-12-05 18:26:36 -07:00
|
|
|
screen.getByRole('button', {name: /save/i}).click();
|
|
|
|
|
expect(form.onSubmit).toHaveBeenCalled();
|
2025-12-02 15:33:46 -07:00
|
|
|
});
|
2025-10-29 22:16:33 -06:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe('Theme Integration', () => {
|
|
|
|
|
it('applies inverted styling to Segment in dark mode', () => {
|
|
|
|
|
const form = createTestForm(mockCollection);
|
|
|
|
|
|
|
|
|
|
const {container} = renderInDarkMode(
|
2025-12-05 18:26:36 -07:00
|
|
|
<CollectionEditForm form={form}>
|
|
|
|
|
<div>Form content</div>
|
|
|
|
|
</CollectionEditForm>
|
2025-10-29 22:16:33 -06:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const segment = container.querySelector('.ui.segment');
|
|
|
|
|
expect(segment).toBeInTheDocument();
|
|
|
|
|
expect(hasInvertedStyling(segment)).toBe(true);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('does not apply inverted styling in light mode', () => {
|
|
|
|
|
const form = createTestForm(mockCollection);
|
|
|
|
|
|
|
|
|
|
const {container} = renderInLightMode(
|
2025-12-05 18:26:36 -07:00
|
|
|
<CollectionEditForm form={form}>
|
|
|
|
|
<div>Form content</div>
|
|
|
|
|
</CollectionEditForm>
|
2025-10-29 22:16:33 -06:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const segment = container.querySelector('.ui.segment');
|
|
|
|
|
expect(segment).toBeInTheDocument();
|
|
|
|
|
expect(hasInvertedStyling(segment)).toBe(false);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('applies dark theme styling to Header in dark mode', () => {
|
|
|
|
|
const form = createTestForm(mockCollection);
|
|
|
|
|
|
|
|
|
|
const {container} = renderInDarkMode(
|
2025-12-05 18:26:36 -07:00
|
|
|
<CollectionEditForm form={form} title="Test Collection">
|
|
|
|
|
<div>Form content</div>
|
|
|
|
|
</CollectionEditForm>
|
2025-10-29 22:16:33 -06:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const header = container.querySelector('.ui.header');
|
|
|
|
|
expect(header).toBeInTheDocument();
|
|
|
|
|
expect(header.style.color).toBe('rgb(238, 238, 238)'); // #eeeeee
|
|
|
|
|
});
|
|
|
|
|
});
|
2025-12-02 15:33:46 -07:00
|
|
|
|
|
|
|
|
describe('CSS Classes', () => {
|
|
|
|
|
it('uses action-button-spacing class for cancel button', () => {
|
|
|
|
|
const form = createTestForm(mockCollection);
|
|
|
|
|
const mockOnCancel = jest.fn();
|
|
|
|
|
|
2025-12-05 18:26:36 -07:00
|
|
|
render(
|
|
|
|
|
<CollectionEditForm form={form} onCancel={mockOnCancel}>
|
|
|
|
|
<div>Form content</div>
|
|
|
|
|
</CollectionEditForm>
|
2025-12-02 15:33:46 -07:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const cancelButton = screen.getByRole('button', {name: /cancel/i});
|
|
|
|
|
expect(cancelButton).toHaveClass('action-button-spacing');
|
|
|
|
|
});
|
|
|
|
|
});
|
2025-10-29 22:16:33 -06:00
|
|
|
});
|