Skip to content

Commit

Permalink
Migrated wrapField tests to @testing-library/react (#1318)
Browse files Browse the repository at this point in the history
Co-authored-by: Adrian Mucha <[email protected]>
  • Loading branch information
zaxovaiko and kestarumper authored Jun 3, 2024
1 parent 1436d4c commit 5667df5
Show file tree
Hide file tree
Showing 12 changed files with 248 additions and 370 deletions.
6 changes: 6 additions & 0 deletions packages/uniforms-antd/__tests__/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,10 @@ describe('@RTL', () => {
suites.testTextField(theme.TextField);
suites.testValidatedForm(theme.ValidatedForm);
suites.testValidatedQuickForm(theme.ValidatedQuickForm);
suites.testWrapField(theme.wrapField, {
helpPropsName: 'help',
withoutWrapClassName: true,
withoutHelpClassName: true,
withoutLabelClassName: true,
});
});
121 changes: 49 additions & 72 deletions packages/uniforms-antd/__tests__/wrapField.tsx
Original file line number Diff line number Diff line change
@@ -1,75 +1,52 @@
import Form from 'antd/lib/form';
import Tooltip from 'antd/lib/tooltip';
import { screen } from '@testing-library/react';
import React from 'react';
import { wrapField } from 'uniforms-antd';

import mount from './_mount';

test('<wrapField> - renders wrapper with label', () => {
const element = wrapField({ label: 'Label' }, <div />);
const wrapper = mount(element);

// @ts-expect-error Correct label type.
expect(wrapper.find(Form.Item).prop('label').props.children[0]).toBe('Label');
});

test('<wrapField> - renders wrapper with label and info', () => {
const element = wrapField({ label: 'Label', info: 'Info' }, <div />);
const wrapper = mount(element);

expect(wrapper.find(Tooltip).prop('title')).toBe('Info');
});

test('<wrapField> - renders wrapper with an error message', () => {
const error = new Error();
const element = wrapField(
{ error, showInlineError: true, errorMessage: 'Error' },
<div />,
);
const wrapper = mount(element);

expect(wrapper.find(Form.Item).prop('help')).toBe('Error');
});

test('<wrapField> - renders wrapper with an error status', () => {
const element = wrapField({}, <div />);
const wrapper = mount(element);

expect(wrapper.find(Form.Item).prop('validateStatus')).toBe(undefined);
});

test('<wrapField> - renders wrapper with an error status (error)', () => {
const error = new Error();
const element = wrapField({ error }, <div />);
const wrapper = mount(element);

expect(wrapper.find(Form.Item).prop('validateStatus')).toBe('error');
});

test('<wrapField> - renders wrapper with help text', () => {
const element = wrapField({ help: 'Help' }, <div />);
const wrapper = mount(element);

expect(wrapper.find(Form.Item).prop('help')).toBe('Help');
});

test('<wrapField> - renders wrapper with extra text', () => {
const element = wrapField({ extra: 'Extra' }, <div />);
const wrapper = mount(element);

expect(wrapper.find(Form.Item).prop('extra')).toBe('Extra');
});

test('<wrapField> - renders wrapper with extra style', () => {
const element = wrapField({ wrapperStyle: {} }, <div />);
const wrapper = mount(element);

expect(wrapper.find(Form.Item).prop('style')).toEqual({});
});

test('<wrapField> - renders wrapper with a custom validateStatus', () => {
const element = wrapField({ validateStatus: 'success' }, <div />);
const wrapper = mount(element);

expect(wrapper.find(Form.Item).prop('validateStatus')).toBe('success');
import { renderWithZod } from 'uniforms/__suites__';
import { z } from 'zod';

describe('wrapField tests', () => {
test('<wrapField> - renders wrapper with extra style', () => {
const { container } = renderWithZod({
element: wrapField(
{ wrapperStyle: { backgroundColor: 'red' } },
<div data-testid="x" />,
),
schema: z.object({}),
});
const element = container.getElementsByClassName('ant-form-item')[0];
expect(element?.getAttribute('style')).toBe('background-color: red;');
});

test('<wrapField> - renders wrapper with label and info', () => {
renderWithZod({
element: wrapField({ label: 'Label', info: 'Info' }, <div />),
schema: z.object({}),
});
expect(screen.getByRole('img').getAttribute('aria-label')).toBe(
'question-circle',
);
});

test('<wrapField> - renders wrapper with extra text', () => {
renderWithZod({
element: wrapField({ extra: 'Extra' }, <div data-testid="x" />),
schema: z.object({}),
});
expect(screen.getByText('Extra')).toBeInTheDocument();
});

test('<wrapField> - renders wrapper with a custom validateStatus', () => {
renderWithZod({
element: wrapField(
{ validateStatus: 'success' },
<div data-testid="x" />,
),
schema: z.object({}),
});
expect(
screen
.getByTestId('x')
.closest('.ant-form-item-has-feedback.ant-form-item-has-success'),
).toBeInTheDocument();
});
});
84 changes: 0 additions & 84 deletions packages/uniforms-bootstrap3/__tests__/wrapField.tsx

This file was deleted.

1 change: 1 addition & 0 deletions packages/uniforms-bootstrap4/__tests__/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,5 @@ describe('@RTL', () => {
suites.testTextField(theme.TextField, { testWrapClassName: true });
suites.testValidatedForm(theme.ValidatedForm);
suites.testValidatedQuickForm(theme.ValidatedQuickForm);
suites.testWrapField(theme.wrapField);
});
90 changes: 17 additions & 73 deletions packages/uniforms-bootstrap4/__tests__/wrapField.tsx
Original file line number Diff line number Diff line change
@@ -1,76 +1,20 @@
import { screen } from '@testing-library/react';
import React from 'react';
import { wrapField } from 'uniforms-bootstrap4';

import mount from './_mount';

test('<wrapField> - renders wrapper with correct class', () => {
const element = wrapField({ wrapClassName: 'container' }, <div />);
const wrapper = mount(element);

expect(wrapper.find('.container')).toHaveLength(1);
});

test('<wrapField> - renders help block', () => {
const element = wrapField({ help: 'Hint' }, <div />);
const wrapper = mount(element);

expect(wrapper.find('.form-text').text()).toBe('Hint');
});

test('<wrapField> - renders help block with specified class', () => {
const element = wrapField(
{ help: 'Hint', helpClassName: 'text-hint' },
<div />,
);
const wrapper = mount(element);

expect(wrapper.find('.form-text.text-hint')).toHaveLength(1);
});

test('<wrapField> - renders error block', () => {
const error = new Error();
const element = wrapField(
{ error, showInlineError: true, errorMessage: 'Error' },
<div />,
);
const wrapper = mount(element);

expect(wrapper.find('.text-danger').text()).toBe('Error');
});

test('<wrapField> - renders error block (showInlineError=false)', () => {
const error = new Error();
const element = wrapField(
{ error, showInlineError: false, errorMessage: 'Error' },
<div />,
);
const wrapper = mount(element);

expect(wrapper.find('.text-danger')).toHaveLength(0);
});

test('<wrapField> - label has custom class (String)', () => {
const element = wrapField(
{
label: 'A field label',
labelClassName: 'custom-label-class',
},
<div />,
);
const wrapper = mount(element);

expect(wrapper.find('label.custom-label-class')).toHaveLength(1);
});

test('<wrapField> - label has custom class (Array[String])', () => {
const element = wrapField(
{
label: 'A field label',
labelClassName: ['custom-1', 'custom-2'],
},
<div />,
);
const wrapper = mount(element);

expect(wrapper.find('label.custom-1.custom-2')).toHaveLength(1);
import { renderWithZod } from 'uniforms/__suites__';
import { z } from 'zod';

describe('wrapField tests', () => {
test('<wrapField> - renders wrapper with correct class', () => {
renderWithZod({
element: wrapField(
{ wrapClassName: 'container' },
<div data-testid="x" />,
),
schema: z.object({}),
});
expect(
screen.getByTestId('x').parentElement?.classList.contains('container'),
).toBe(true);
});
});
1 change: 1 addition & 0 deletions packages/uniforms-bootstrap5/__tests__/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,5 @@ describe('@RTL', () => {
});
suites.testValidatedForm(theme.ValidatedForm);
suites.testValidatedQuickForm(theme.ValidatedQuickForm);
suites.testWrapField(theme.wrapField);
});
Loading

0 comments on commit 5667df5

Please sign in to comment.