Skip to content

Commit

Permalink
Move uniforms-material LongTextField test to suite
Browse files Browse the repository at this point in the history
  • Loading branch information
kestarumper committed Feb 9, 2024
1 parent 5b1ba1c commit 9cd479a
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 75 deletions.
73 changes: 0 additions & 73 deletions packages/uniforms-material/__tests__/LongTextField.tsx

This file was deleted.

15 changes: 14 additions & 1 deletion packages/uniforms-material/__tests__/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,20 @@ describe('@RTL', () => {
suites.testListField(theme.ListField, {
getListAddField: screen => screen.getByText(/\+/),
});
suites.testLongTextField(theme.LongTextField);
suites.testLongTextField(theme.LongTextField, {
testPassThemeProps: {
ThemeProvider({
themeOptions,
...props
}: PropsWithChildren<{ themeOptions: ThemeOptions }>) {
return (
<ThemeProvider {...props} theme={createMuiTheme(themeOptions)}>
{props.children}
</ThemeProvider>
);
},
},
});
suites.testNestField(theme.NestField, { skipInMuiTests: true });
suites.testNumField(theme.NumField);
suites.testQuickForm(theme.QuickForm);
Expand Down
74 changes: 73 additions & 1 deletion packages/uniforms/__suites__/LongTextField.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import React, { ComponentType } from 'react';
import React, { ComponentType, PropsWithChildren } from 'react';
import z from 'zod';

import { renderWithZod } from './render-zod';
Expand All @@ -10,6 +10,11 @@ export function testLongTextField(
options?: {
skipShowInlineErrorTests?: boolean;
testMinMaxLength?: boolean;
testPassThemeProps?: {
ThemeProvider: (
props: PropsWithChildren<{ themeOptions: any }>,
) => JSX.Element;
};
},
) {
test('<LongTextField> - renders a textarea with correct disabled state', () => {
Expand Down Expand Up @@ -135,6 +140,73 @@ export function testLongTextField(
});
}

if (options?.testPassThemeProps) {
const { ThemeProvider } = options.testPassThemeProps;

test('<LongTextField> - default props are not passed when MUI theme props are specified', () => {
const themeOptions = {
props: { MuiTextField: { fullWidth: false, margin: 'normal' } },
};
const { container } = renderWithZod({
element: (
<ThemeProvider themeOptions={themeOptions}>
<LongTextField name="x" />
</ThemeProvider>
),
schema: z.object({ x: z.string() }),
});

const elements = container.getElementsByClassName(
'MuiFormControl-marginNormal',
);
expect(elements).toHaveLength(1);
expect(elements[0]).not.toHaveClass('MuiFormControl-fullWidth');
});

test('<LongTextField> - default props are passed when MUI theme props are absent', () => {
const themeOptions = {};
const { container } = renderWithZod({
element: (
<ThemeProvider themeOptions={themeOptions}>
<LongTextField name="x" />
</ThemeProvider>
),
schema: z.object({ x: z.string() }),
});

const elements = container.getElementsByClassName(
'MuiFormControl-marginDense',
);
expect(elements).toHaveLength(1);
expect(elements[0]).toHaveClass('MuiFormControl-fullWidth');
});

test('<LongTextField> - explicit props are passed when MUI theme props are specified', () => {
const themeOptions = {
props: { MuiTextField: { fullWidth: true, margin: 'dense' } },
};
const explicitProps = {
fullWidth: false,
margin: 'normal' as const,
};

const { container } = renderWithZod({
element: (
<ThemeProvider themeOptions={themeOptions}>
<LongTextField name="x" {...explicitProps} />
</ThemeProvider>
),
schema: z.object({ x: z.string() }),
});

const elements = container.getElementsByClassName(
'MuiFormControl-marginNormal',
);
expect(elements).toHaveLength(1);
expect(elements[0]).not.toHaveClass('MuiFormControl-fullWidth');
});
}

if (options?.testMinMaxLength) {
test('<LongTextField> - renders a textarea with minLength and maxLength', () => {
renderWithZod({
Expand Down

0 comments on commit 9cd479a

Please sign in to comment.