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

152 lines
4.1 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 feedback from 'app/services/api/feedback';
import { User } from 'app/components/user';
import { TestContextProvider } from 'app/shell';
2019-12-07 16:58:52 +05:30
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', () => {
const user = {} as User;
render(
<TestContextProvider>
<ContactForm user={user} />
</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 = {
email: 'foo@bar.com',
} as User;
2020-05-24 04:38:24 +05:30
it('should render email field with user email', () => {
render(
<TestContextProvider>
<ContactForm user={user} />
</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 = {
email: 'foo@bar.com',
} as User;
2020-05-24 04:38:24 +05:30
render(
<TestContextProvider>
<ContactForm user={user} />
</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 = {
email: 'foo@bar.com',
} as User;
(feedback.send as any).callsFake(() =>
Promise.reject({
success: false,
errors: { email: 'error.email_invalid' },
}),
);
render(
<TestContextProvider>
<ContactForm user={user} />
</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');
});
});
});