accounts-frontend/packages/app/components/contact/ContactForm.test.tsx

150 lines
4.2 KiB
TypeScript
Raw Normal View History

import React from 'react';
import expect from 'app/test/unexpected';
import sinon from 'sinon';
import { render, fireEvent, waitFor, screen } from '@testing-library/react';
import * as feedback from 'app/services/api/feedback';
import { User } from 'app/components/user';
import { TestContextProvider } from 'app/shell';
import ContactForm from './ContactForm';
beforeEach(() => {
2020-05-24 04:38:24 +05:30
sinon.stub(feedback, 'send').returns(Promise.resolve() as any);
});
afterEach(() => {
2020-05-24 04:38:24 +05:30
(feedback.send as any).restore();
});
describe('ContactForm', () => {
2020-05-24 04:38:24 +05:30
it('should contain Form', () => {
render(
<TestContextProvider>
<ContactForm />
2020-05-24 04:38:24 +05:30
</TestContextProvider>,
);
expect(screen.getAllByRole('textbox').length, 'to be greater than', 1);
expect(screen.getByRole('button', { name: /Send/ }), 'to have property', 'type', 'submit');
[
{
label: 'subject',
name: 'subject',
},
{
label: 'Email',
name: 'email',
},
{
label: 'message',
name: 'message',
},
].forEach((el) => {
expect(screen.getByLabelText(el.label, { exact: false }), 'to have property', 'name', el.name);
});
});
2020-05-24 04:38:24 +05:30
describe('when rendered with user', () => {
const user: Pick<User, 'email'> = {
2020-05-24 04:38:24 +05:30
email: 'foo@bar.com',
};
2020-05-24 04:38:24 +05:30
it('should render email field with user email', () => {
render(
<TestContextProvider state={{ user }}>
<ContactForm />
2020-05-24 04:38:24 +05:30
</TestContextProvider>,
);
2020-05-24 04:38:24 +05:30
expect(screen.getByDisplayValue(user.email), 'to be a', HTMLInputElement);
});
});
2020-05-24 04:38:24 +05:30
it('should submit and then hide form and display success message', async () => {
const user: Pick<User, 'email'> = {
2020-05-24 04:38:24 +05:30
email: 'foo@bar.com',
};
2020-05-24 04:38:24 +05:30
render(
<TestContextProvider state={{ user }}>
<ContactForm />
2020-05-24 04:38:24 +05:30
</TestContextProvider>,
);
2020-05-24 04:38:24 +05:30
fireEvent.change(screen.getByLabelText(/subject/i), {
target: {
value: 'subject',
},
});
2020-05-24 04:38:24 +05:30
fireEvent.change(screen.getByLabelText(/message/i), {
target: {
value: 'the message',
},
});
2020-05-24 04:38:24 +05:30
const button = screen.getByRole('button', { name: 'Send' });
2020-05-24 04:38:24 +05:30
expect(button, 'to have property', 'disabled', false);
2020-05-24 04:38:24 +05:30
fireEvent.click(button);
2020-05-24 04:38:24 +05:30
expect(button, 'to have property', 'disabled', true);
expect(feedback.send, 'to have a call exhaustively satisfying', [
{
subject: 'subject',
email: user.email,
category: '',
message: 'the message',
},
]);
await waitFor(() => {
expect(screen.getByText('Your message was received', { exact: false }), 'to be a', HTMLElement);
});
2020-05-24 04:38:24 +05:30
expect(screen.getByText(user.email), 'to be a', HTMLElement);
expect(screen.queryByRole('button', { name: /Send/ }), 'to be null');
});
2020-05-24 04:38:24 +05:30
it('should show validation messages', async () => {
const user: Pick<User, 'email'> = {
2020-05-24 04:38:24 +05:30
email: 'foo@bar.com',
};
2020-05-24 04:38:24 +05:30
(feedback.send as any).callsFake(() =>
Promise.reject({
success: false,
errors: { email: 'error.email_invalid' },
}),
);
render(
<TestContextProvider state={{ user }}>
<ContactForm />
2020-05-24 04:38:24 +05:30
</TestContextProvider>,
);
fireEvent.change(screen.getByLabelText(/subject/i), {
target: {
value: 'subject',
},
});
fireEvent.change(screen.getByLabelText(/message/i), {
target: {
value: 'the message',
},
});
fireEvent.click(screen.getByRole('button', { name: 'Send' }));
await waitFor(() => {
expect(screen.getByRole('alert'), 'to have property', 'innerHTML', 'Email is invalid');
});
});
});