Skip to content

Commit

Permalink
test: add unit tests - refs #254313
Browse files Browse the repository at this point in the history
  • Loading branch information
ana-oprea authored Sep 8, 2023
1 parent fa47506 commit ec53971
Show file tree
Hide file tree
Showing 6 changed files with 643 additions and 1 deletion.
129 changes: 129 additions & 0 deletions src/Widget/AttachedFileWidget.test.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
import React from 'react';
import { render, fireEvent, screen, waitFor } from '@testing-library/react';
import '@testing-library/jest-dom/extend-expect';
import AttachedFileWidget from './AttachedFileWidget';
import { Provider } from 'react-intl-redux';
import configureStore from 'redux-mock-store';

jest.mock('promise-file-reader', () => ({
readAsDataURL: jest.fn(() =>
Promise.resolve('data:text/plain;base64,SGVsbG8sIFdvcmxkIQ=='),
),
}));

const mockStore = configureStore([]);
const store = mockStore({
intl: {
locale: 'en',
messages: {},
formatMessage: jest.fn(),
subrequests: {},
},
router: {
location: { pathname: '/test' },
subrequests: {},
},
});

describe('AttachedFileWidget', () => {
const mockOnChange = jest.fn();
const mockOpenObjectBrowser = jest.fn();

beforeEach(() => {
jest.clearAllMocks();
});

it('renders without crashing', () => {
const { container } = render(
<Provider store={store}>
<AttachedFileWidget
id="testId"
title="Test Title"
onChange={mockOnChange}
openObjectBrowser={mockOpenObjectBrowser}
/>
</Provider>,
);
expect(container).toBeInTheDocument();
});

it('calls onChange when a file is dropped', async () => {
const { container } = render(
<Provider store={store}>
<AttachedFileWidget
id="testId"
title="Test Title"
onChange={mockOnChange}
openObjectBrowser={mockOpenObjectBrowser}
/>
</Provider>,
);

let dropzone;
await waitFor(() => {
dropzone = container.querySelector('.file-widget-dropzone input');
expect(dropzone).toBeInTheDocument();
});
expect(dropzone).toBeInTheDocument();

const file = new File(['hello'], 'hello.png', { type: 'image/png' });
Object.defineProperty(dropzone, 'files', {
value: [file],
});
fireEvent.drop(dropzone);

await waitFor(() => expect(mockOnChange).toHaveBeenCalled());
});

it('calls onChange when delete button is clicked', () => {
render(
<Provider store={store}>
<AttachedFileWidget
id="testId"
title="Test Title"
value="someValue"
onChange={mockOnChange}
openObjectBrowser={mockOpenObjectBrowser}
/>
</Provider>,
);

const deleteButton = screen.getByLabelText('delete file');
fireEvent.click(deleteButton);

expect(mockOnChange).toHaveBeenCalledWith('testId', null);
});

it('renders without crashing', async () => {
const { container } = render(
<Provider store={store}>
<AttachedFileWidget
id="testId"
title="Test Title"
onChange={mockOnChange}
openObjectBrowser={mockOpenObjectBrowser}
/>
</Provider>,
);

let dropzone;
await waitFor(() => {
dropzone = container.querySelector('.file-widget-dropzone input');
expect(dropzone).toBeInTheDocument();
});
expect(dropzone).toBeInTheDocument();
expect(container.querySelector('.file-picker-toolbar')).toBeInTheDocument();
expect(
container.querySelector('.file-picker-toolbar button'),
).toBeInTheDocument();
fireEvent.change(container.querySelector('.input-toolbar input'), {
target: { value: 'test' },
});

fireEvent.click(
container.querySelector(
'.file-picker-toolbar button.ui.basic.icon.primary.button',
),
);
});
});
187 changes: 186 additions & 1 deletion src/Widget/AttachedImageWidget.test.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react';
import { render, fireEvent } from '@testing-library/react';
import { render, fireEvent, waitFor } from '@testing-library/react';
import { AttachedImageWidget } from './AttachedImageWidget';
import { Provider } from 'react-intl-redux';
import configureStore from 'redux-mock-store';
Expand All @@ -17,7 +17,20 @@ const store = mockStore({
messages: {},
formatMessage: jest.fn(),
},
content: {
subrequests: {},
},
router: {
location: { pathname: '/test' },
},
});

jest.mock('promise-file-reader', () => ({
readAsDataURL: jest.fn(() =>
Promise.resolve('data:text/plain;base64,SGVsbG8sIFdvcmxkIQ=='),
),
}));

describe('AttachedImageWidget', () => {
const mockCreateContent = jest.fn();
const mockOpenObjectBrowser = jest.fn();
Expand Down Expand Up @@ -62,4 +75,176 @@ describe('AttachedImageWidget', () => {

expect(mockOnChange).toHaveBeenCalledWith('imageId', '');
});

it('should render the component and handle URL input and cancel button', async () => {
const { container } = render(
<Provider store={store}>
<AttachedImageWidget {...props} value={null} />
</Provider>,
);

let dropzone;
await waitFor(() => {
dropzone = container.querySelector('div[tabindex="0"]');
expect(dropzone).toBeInTheDocument();
});
expect(dropzone).toBeInTheDocument();

fireEvent.change(
container.querySelector('.toolbar-inner .ui.input input[type="text"]'),
{ target: { value: 'test' } },
);
fireEvent.click(
container.querySelector(
'.toolbar-inner .ui.buttons button.ui.basic.icon.primary.button',
),
);

fireEvent.change(
container.querySelector('.toolbar-inner .ui.input input[type="text"]'),
{ target: { value: 'test' } },
);
fireEvent.click(
container.querySelector(
'.toolbar-inner .ui.buttons button.ui.basic.icon.secondary.button.cancel',
),
);
});

it('should render the component handle object browser button click', async () => {
const { container } = render(
<Provider store={store}>
<AttachedImageWidget {...props} value={null} />
</Provider>,
);

let dropzone;
await waitFor(() => {
dropzone = container.querySelector('div[tabindex="0"]');
expect(dropzone).toBeInTheDocument();
});
expect(dropzone).toBeInTheDocument();

fireEvent.click(
container.querySelector('.toolbar-inner .ui.buttons button'),
);
});

it('should handle file input and rerender based on request loading and loaded states', async () => {
const { rerender, container } = render(
<Provider store={store}>
<AttachedImageWidget
{...props}
request={{
loading: true,
loaded: true,
}}
value={null}
/>
</Provider>,
);

let dropzone;
await waitFor(() => {
dropzone = container.querySelector('div[tabindex="0"]');
expect(dropzone).toBeInTheDocument();
});
expect(dropzone).toBeInTheDocument();

const file = new File(['hello'], 'hello.png', { type: 'image/png' });
Object.defineProperty(
container.querySelector('label[role="button"] input[type="file"]'),
'files',
{
value: [file],
},
);
fireEvent.change(
container.querySelector('label[role="button"] input[type="file"]'),
{ target: { value: '' } },
);

rerender(
<Provider store={store}>
<AttachedImageWidget
{...props}
request={{
loading: false,
loaded: true,
}}
value={null}
content={{
'@id': 'imageId',
}}
/>
</Provider>,
);

rerender(
<Provider store={store}>
<AttachedImageWidget
{...props}
request={{
loading: false,
loaded: false,
}}
value={null}
content={{
'@id': 'imageId',
}}
/>
</Provider>,
);
});

it('should handle file input via label button ', async () => {
const { container } = render(
<Provider store={store}>
<AttachedImageWidget {...props} value={null} />
</Provider>,
);

let dropzone;
await waitFor(() => {
dropzone = container.querySelector('div[tabindex="0"]');
expect(dropzone).toBeInTheDocument();
});
expect(dropzone).toBeInTheDocument();

const file = new File(['hello'], 'hello.png', { type: 'image/png' });
Object.defineProperty(
container.querySelector('label[role="button"] input[type="file"]'),
'files',
{
value: [file],
},
);
fireEvent.change(
container.querySelector('label[role="button"] input[type="file"]'),
{ target: { value: '' } },
);
});

it('should handle drag and drop events ', async () => {
const { container } = render(
<Provider store={store}>
<AttachedImageWidget {...props} value={null} />
</Provider>,
);

let dropzone;
await waitFor(() => {
dropzone = container.querySelector('div[tabindex="0"]');
expect(dropzone).toBeInTheDocument();
});
expect(dropzone).toBeInTheDocument();

const file = new File(['hello'], 'hello.png', { type: 'image/png' });
Object.defineProperty(dropzone, 'files', {
value: [file],
});
fireEvent.dragEnter(dropzone);
fireEvent.dragLeave(dropzone);
fireEvent.drop(dropzone);
});
});
56 changes: 56 additions & 0 deletions src/Widget/MappingWidget.test.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import React from 'react';
import { render, fireEvent } from '@testing-library/react';
import MappingWidget from './MappingWidget';
import '@testing-library/jest-dom/extend-expect';

jest.mock('@plone/volto/components', () => ({
Field: ({ id, value, onChange }) => (
<input
data-testid={`field-${id}`}
value={value || ''}
onChange={(e) => onChange(id, e.target.value)}
/>
),
}));

describe('MappingWidget', () => {
beforeEach(() => {
jest.clearAllMocks();
});
const options = [
{ id: 'option1', title: 'Option 1' },
{ id: 'option2', title: 'Option 2' },
];

it('renders without crashing', () => {
const { container } = render(
<MappingWidget id="test" title="Test Title" options={options} />,
);
expect(container).toBeTruthy();
});

it('renders correct number of fields', () => {
const { getAllByTestId } = render(
<MappingWidget id="test" title="Test Title" options={options} />,
);
expect(getAllByTestId(/field-/).length).toBe(2);
});

it('updates value on field change', () => {
const onChange = jest.fn();
const { getByTestId } = render(
<MappingWidget
id="test"
title="Test Title"
options={options}
onChange={onChange}
/>,
);

fireEvent.change(getByTestId('field-test-option1'), {
target: { value: 'new value' },
});

expect(onChange).toHaveBeenCalledWith('test', { option1: 'new value' });
});
});
Loading

0 comments on commit ec53971

Please sign in to comment.