Skip to content

Commit

Permalink
Moved a few tests into AntD and MUI packages
Browse files Browse the repository at this point in the history
  • Loading branch information
zaxovaiko committed May 24, 2024
1 parent 783649b commit 6e05d14
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 111 deletions.
58 changes: 58 additions & 0 deletions packages/uniforms-antd/__tests__/wrapField.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { screen } from '@testing-library/react';
import React from 'react';
import { wrapField } from 'uniforms-antd';
import { renderWithZod } from 'uniforms/__suites__';
import { z } from 'zod';

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

test('<wrapField> - renders wrapper with an error status (error)', () => {
const error = new Error();
renderWithZod({
element: wrapField({ error }, <div data-testid="x" />),
schema: z.object({}),
});
expect(
screen
.getByTestId('x')
.closest('.ant-form-item-has-feedback.ant-form-item-has-error'),
).toBeInTheDocument();
});

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 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();
});
});
1 change: 0 additions & 1 deletion packages/uniforms-bootstrap3/__tests__/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,5 +63,4 @@ describe('@RTL', () => {
suites.testTextField(theme.TextField, { testWrapClassName: true });
suites.testValidatedForm(theme.ValidatedForm);
suites.testValidatedQuickForm(theme.ValidatedQuickForm);
suites.testWrapField(theme.wrapField, { onlyForBootstrap3: true });
});
20 changes: 20 additions & 0 deletions packages/uniforms-bootstrap4/__tests__/wrapField.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { screen } from '@testing-library/react';
import React from 'react';
import { wrapField } from 'uniforms-bootstrap4';
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);
});
});
20 changes: 20 additions & 0 deletions packages/uniforms-bootstrap5/__tests__/wrapField.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { screen } from '@testing-library/react';
import React from 'react';
import { wrapField } from 'uniforms-bootstrap5';
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);
});
});
19 changes: 19 additions & 0 deletions packages/uniforms-mui/__tests__/wrapField.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { screen } from '@testing-library/react';
import React from 'react';
import { wrapField } from 'uniforms-mui';
import { renderWithZod } from 'uniforms/__suites__';
import { z } from 'zod';

describe('wrapField tests', () => {
test('<wrapField> - renders wrapper', () => {
renderWithZod({
element: wrapField({}, <div data-testid="x" />),
schema: z.object({}),
});
expect(
screen
.getByTestId('x')
.parentElement?.classList.contains('MuiFormControl-root'),
).toBe(true);
});
});
114 changes: 4 additions & 110 deletions packages/uniforms/__suites__/wrapField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,40 +10,8 @@ export function testWrapField(
options?: {
skipForMUI?: boolean;
skipForAntD?: boolean;
onlyForBootstrap3?: boolean;
},
) {
skipTestIf(!options?.onlyForBootstrap3)(
'<wrapField> - renders wrapper with (feedbackable=true)',
() => {
const error = new Error();
renderWithZod({
element: wrapField(
{ error, feedbackable: true },
<div data-testid="x" />,
),
schema: z.object({}),
});
const x = screen.getByTestId('x');
expect(x.parentElement?.classList.contains('has-feedback')).toBe(true);
expect(
x.nextElementSibling?.classList.contains('form-control-feedback'),
).toBe(true);
},
);

skipTestIf(!options?.skipForMUI)('<wrapField> - renders wrapper', () => {
renderWithZod({
element: wrapField({}, <div data-testid="x" />),
schema: z.object({}),
});
expect(
screen
.getByTestId('x')
.parentElement?.classList.contains('MuiFormControl-root'),
).toBe(true);
});

skipTestIf(options?.skipForMUI || options?.skipForAntD)(
'<wrapField> - renders wrapper with correct class',
() => {
Expand Down Expand Up @@ -86,7 +54,7 @@ export function testWrapField(
},
);

test('<wrapField> - renders error block', () => {
test('<wrapField> - renders error block (showInlineError=true)', () => {
const error = new Error();
renderWithZod({
element: wrapField(
Expand Down Expand Up @@ -140,43 +108,6 @@ export function testWrapField(
},
);

skipTestIf(!options?.skipForAntD)(
'<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();
},
);

skipTestIf(!options?.skipForAntD)(
'<wrapField> - renders wrapper with extra style',
() => {
renderWithZod({
element: wrapField(
{ wrapperStyle: { backgroundColor: 'red' } },
<div data-testid="x" />,
),
schema: z.object({}),
});
expect(
screen
.getByTestId('x')
.closest('.ant-form-item')
?.getAttribute('style'),
).toBe('background-color: red;');
},
);

skipTestIf(options?.skipForMUI)(
'<wrapField> - renders wrapper with extra text',
() => {
Expand All @@ -192,22 +123,6 @@ export function testWrapField(
},
);

skipTestIf(!options?.skipForAntD)(
'<wrapField> - renders wrapper with an error status (error)',
() => {
const error = new Error();
renderWithZod({
element: wrapField({ error }, <div data-testid="x" />),
schema: z.object({}),
});
expect(
screen
.getByTestId('x')
.closest('.ant-form-item-has-feedback.ant-form-item-has-error'),
).toBeInTheDocument();
},
);

skipTestIf(options?.skipForMUI)(
'<wrapField> - renders error block (showInlineError=false)',
() => {
Expand All @@ -220,30 +135,9 @@ export function testWrapField(
schema: z.object({}),
});
const x = screen.getByTestId('x');
if (options?.skipForAntD) {
expect(
x.closest('.ant-form-item-has-feedback.ant-form-item-has-error'),
).toBeInTheDocument();
} else {
expect(
x.parentElement?.classList.contains(
options?.onlyForBootstrap3 ? 'has-error' : 'is-invalid',
),
).toBe(true);
}
},
);

skipTestIf(!options?.skipForAntD)(
'<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',
);
expect(
x.closest('.ant-form-item-has-error, .is-invalid'),
).toBeInTheDocument();
},
);
}

0 comments on commit 6e05d14

Please sign in to comment.