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

Emailfield to Muilink #4866

Merged
merged 2 commits into from
May 27, 2020
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
54 changes: 29 additions & 25 deletions packages/ra-ui-materialui/src/field/EmailField.spec.js
Original file line number Diff line number Diff line change
@@ -1,53 +1,57 @@
import React from 'react';
import assert from 'assert';
import expect from 'expect';
import { render, cleanup } from '@testing-library/react';
import EmailField from './EmailField';

const url = 'foo@bar.com';

describe('<EmailField />', () => {
afterEach(cleanup);

it('should render as an email link', () => {
const record = { foo: 'foo@bar.com' };
const { container } = render(
it('should render as Mui Link', () => {
const record = { foo: url };
const { getByText } = render(
<EmailField record={record} source="foo" />
);
assert.equal(
container.innerHTML,
'<a href="mailto:foo@bar.com">foo@bar.com</a>'
);
const link = getByText(url);
expect(link.tagName).toEqual('A');
expect(link.href).toEqual(`mailto:${url}`);
expect(link.innerHTML).toEqual(url);
});

it('should handle deep fields', () => {
const record = { foo: { bar: 'foo@bar.com' } };
const { container } = render(
const record = { foo: { bar: url } };
const { getByText } = render(
<EmailField record={record} source="foo.bar" />
);
assert.equal(
container.innerHTML,
'<a href="mailto:foo@bar.com">foo@bar.com</a>'
);
const link = getByText(url);
expect(link.tagName).toEqual('A');
expect(link.href).toEqual(`mailto:${url}`);
expect(link.innerHTML).toEqual(url);
});

it('should display an email (mailto) link', () => {
const record = { email: 'hal@kubrickcorp.com' };
const { container } = render(
const halUrl = 'hal@kubrickcorp.com';
const record = { email: halUrl };
const { getByText } = render(
<EmailField record={record} source="email" />
);
assert.equal(
container.innerHTML,
'<a href="mailto:hal@kubrickcorp.com">hal@kubrickcorp.com</a>'
);
const link = getByText(halUrl);
expect(link.tagName).toEqual('A');
expect(link.href).toEqual(`mailto:${halUrl}`);
expect(link.innerHTML).toEqual(halUrl);
});

it('should use custom className', () => {
const { container } = render(
const { getByText } = render(
<EmailField
record={{ email: true }}
record={{ email: url }}
source="email"
className="foo"
/>
);
assert.ok(container.firstChild.classList.contains('foo'));
const link = getByText(url);
expect(link.className).toContain('foo');
});

it.each([null, undefined])(
Expand All @@ -56,12 +60,12 @@ describe('<EmailField />', () => {
const { queryByText } = render(
<EmailField record={{ foo }} source="foo" emptyText="NA" />
);
assert.notEqual(queryByText('NA'), null);
expect(queryByText('NA')).not.toBeNull();
}
);

it('should return null when the record has no value for the source and no emptyText', () => {
const { container } = render(<EmailField record={{}} source="foo" />);
assert.equal(container.firstChild, null);
expect(container.firstChild).toBeNull();
});
});
5 changes: 3 additions & 2 deletions packages/ra-ui-materialui/src/field/EmailField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import Typography from '@material-ui/core/Typography';

import sanitizeRestProps from './sanitizeRestProps';
import { FieldProps, InjectedFieldProps, fieldPropTypes } from './types';
import { Link } from '@material-ui/core';

// useful to prevent click bubbling in a datagrid with rowClick
const stopPropagation = e => e.stopPropagation();
Expand All @@ -28,14 +29,14 @@ const EmailField: FunctionComponent<
}

return (
<a
<Link
className={className}
href={`mailto:${value}`}
onClick={stopPropagation}
{...sanitizeRestProps(rest)}
>
{value}
</a>
</Link>
);
};

Expand Down