Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UIU-2430 test UserForm #2265

Merged
merged 1 commit into from
Oct 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/views/UserEdit/UserForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ import getProxySponsorWarning from '../../components/util/getProxySponsorWarning

import css from './UserForm.css';

function validate(values) {
export function validate(values) {
const errors = {};
errors.personal = {};

Expand Down
112 changes: 112 additions & 0 deletions src/views/UserEdit/UserForm.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
import React from 'react';
import { screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { FormattedMessage } from 'react-intl';

import renderWithRouter from '../../../test/jest/helpers/renderWithRouter';
import UserForm, { validate } from './UserForm';

describe('UserForm', () => {
describe('validate', () => {
it('validates correctly-shaped data', () => {
const result = validate({
personal: {
lastName: 'lastName',
preferredContactTypeId: 'preferredContactTypeId',
addresses: [
{ addressType: 'addressType' },
],
},
username: 'username',
creds: {
password: 'password',
},
patronGroup: 'patronGroup',
servicePoints: ['a', 'b'],
preferredServicePoint: 'preferredServicePoint',
});

expect(result.personal).toMatchObject({ addresses: [{}] });
expect(result.personal.addresses.length).toBe(1);
});

describe('returns errors if required fields are missing', () => {
it('requires personal', () => {
const result = validate({});
expect(result.personal.lastName).toMatchObject(<FormattedMessage id="ui-users.errors.missingRequiredField" />);
});

it('requires personal.lastName', () => {
const result = validate({ personal: {} });
expect(result.personal.lastName).toMatchObject(<FormattedMessage id="ui-users.errors.missingRequiredField" />);
});

it('requires personal.preferredContactTypeId', () => {
const result = validate({ personal: {} });
expect(result.personal.preferredContactTypeId).toMatchObject(<FormattedMessage id="ui-users.errors.missingRequiredContactType" />);
});

it('requires username when password is present', () => {
const result = validate({ creds: { password: 'thunder-chicken' } });
expect(result.username).toMatchObject(<FormattedMessage id="ui-users.errors.missingRequiredUsername" />);
});

it('requires patronGroup', () => {
const result = validate({ });
expect(result.patronGroup).toMatchObject(<FormattedMessage id="ui-users.errors.missingRequiredPatronGroup" />);
});

it('requires personal.addresses to have an addressType', () => {
const result = validate({ personal: { addresses: [{ 'monkey': 'bagel' }] } });
expect(result.personal.addresses[0].addressType).toMatchObject(<FormattedMessage id="ui-users.errors.missingRequiredAddressType" />);
});

it('requires a preferred service point when service points are assigned', () => {
const result = validate({ servicePoints: ['a', 'b', 'c'] });
expect(result.preferredServicePoint).toMatchObject(<FormattedMessage id="ui-users.errors.missingRequiredPreferredServicePoint" />);
});
});
});

// this fails:
// Element type is invalid: expected a string (for built-in components)
// or a class/function (for composite components) but got: undefined.
// You likely forgot to export your component from the file it's defined
// in, or you might have mixed up default and named imports.
// not sure exactly what the problem is. Maybe we need to mock stripesFinalForm?
describe.skip('UserForm calls submit handler', () => {
const submitHandler = jest.fn();
const props = {
formData: {
patronGroups: [],
},
initialValues: {
id: 'id',
creds: {
password: 'password',
},
patronGroup: 'patronGroup',
personal: {
lastName: 'lastName',
preferredContactTypeId: 'preferredContactTypeId',
addresses: [
{ addressType: 'addressType' },
],
},
preferredServicePoint: 'preferredServicePoint',
servicePoints: ['a', 'b'],
username: 'username',
},
onCancel: jest.fn(),
onSubmit: jest.fn(),
uniquenessValidator: {
reset: jest.fn(),
GET: jest.fn(),
}
};

renderWithRouter(<UserForm {...props} />);
userEvent.click(screen.getByText('ui-users.saveAndClose'));
expect(submitHandler).toHaveBeenCalled();
});
});
5 changes: 5 additions & 0 deletions test/jest/__mock__/stripesComponents.mock.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ jest.mock('@folio/stripes/components', () => ({
AccordionSet: jest.fn(({ children, ...rest }) => (
<span {...rest}>{children}</span>
)),
AccordionStatus: jest.fn(({ children, ...rest }) => (
<span {...rest}>{children}</span>
)),
Badge: jest.fn((props) => (
<span>
<span>{props.children}</span>
Expand Down Expand Up @@ -160,7 +163,9 @@ jest.mock('@folio/stripes/components', () => ({
{lastMenu ?? null}
</div>
)),
PaneHeaderIconButton: jest.fn(({ children }) => <div className="paneHeaderIconButton">{ children }</div>),
PaneMenu: jest.fn((props) => <div>{props.children}</div>),
PaneSet: jest.fn((props) => <div>{props.children}</div>),
RadioButton: jest.fn(({ label, name, ...rest }) => (
<div>
<label htmlFor="male">{label}</label>
Expand Down