2020-05-21 23:38:47 +05:30
|
|
|
|
import React from 'react';
|
2019-12-08 00:32:00 +05:30
|
|
|
|
import expect from 'app/test/unexpected';
|
2016-12-10 21:28:47 +05:30
|
|
|
|
import sinon from 'sinon';
|
2020-05-21 23:38:47 +05:30
|
|
|
|
import { render, fireEvent, waitFor, screen } from '@testing-library/react';
|
2019-12-08 00:32:00 +05:30
|
|
|
|
import feedback from 'app/services/api/feedback';
|
|
|
|
|
import { User } from 'app/components/user';
|
2020-05-21 23:38:47 +05:30
|
|
|
|
import { TestContextProvider } from 'app/shell';
|
2016-12-10 21:28:47 +05:30
|
|
|
|
|
2019-12-07 16:58:52 +05:30
|
|
|
|
import { ContactForm } from './ContactForm';
|
2016-12-10 21:28:47 +05:30
|
|
|
|
|
2020-05-21 23:38:47 +05:30
|
|
|
|
beforeEach(() => {
|
2020-05-24 04:38:24 +05:30
|
|
|
|
sinon.stub(feedback, 'send').returns(Promise.resolve() as any);
|
2020-05-21 23:38:47 +05:30
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
afterEach(() => {
|
2020-05-24 04:38:24 +05:30
|
|
|
|
(feedback.send as any).restore();
|
2020-05-21 23:38:47 +05:30
|
|
|
|
});
|
2020-01-18 02:07:52 +05:30
|
|
|
|
|
2016-12-10 21:28:47 +05:30
|
|
|
|
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: 'E‑mail',
|
|
|
|
|
name: 'email',
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
label: 'message',
|
|
|
|
|
name: 'message',
|
|
|
|
|
},
|
|
|
|
|
].forEach((el) => {
|
|
|
|
|
expect(screen.getByLabelText(el.label, { exact: false }), 'to have property', 'name', el.name);
|
|
|
|
|
});
|
2019-11-27 14:33:32 +05:30
|
|
|
|
});
|
2016-12-10 21:28:47 +05:30
|
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
|
describe('when rendered with user', () => {
|
|
|
|
|
const user = {
|
|
|
|
|
email: 'foo@bar.com',
|
|
|
|
|
} as User;
|
2016-12-10 21:28:47 +05:30
|
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
|
it('should render email field with user email', () => {
|
|
|
|
|
render(
|
|
|
|
|
<TestContextProvider>
|
|
|
|
|
<ContactForm user={user} />
|
|
|
|
|
</TestContextProvider>,
|
|
|
|
|
);
|
2020-05-21 23:38:47 +05:30
|
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
|
expect(screen.getByDisplayValue(user.email), 'to be a', HTMLInputElement);
|
|
|
|
|
});
|
2016-12-10 21:28:47 +05:30
|
|
|
|
});
|
|
|
|
|
|
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;
|
2019-11-27 14:33:32 +05:30
|
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
|
render(
|
|
|
|
|
<TestContextProvider>
|
|
|
|
|
<ContactForm user={user} />
|
|
|
|
|
</TestContextProvider>,
|
|
|
|
|
);
|
2020-05-21 23:38:47 +05:30
|
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
|
fireEvent.change(screen.getByLabelText(/subject/i), {
|
|
|
|
|
target: {
|
|
|
|
|
value: 'subject',
|
|
|
|
|
},
|
|
|
|
|
});
|
2019-11-27 14:33:32 +05:30
|
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
|
fireEvent.change(screen.getByLabelText(/message/i), {
|
|
|
|
|
target: {
|
|
|
|
|
value: 'the message',
|
|
|
|
|
},
|
|
|
|
|
});
|
2019-11-27 14:33:32 +05:30
|
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
|
const button = screen.getByRole('button', { name: 'Send' });
|
2020-05-21 23:38:47 +05:30
|
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
|
expect(button, 'to have property', 'disabled', false);
|
2016-12-10 21:28:47 +05:30
|
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
|
fireEvent.click(button);
|
2016-12-10 21:28:47 +05:30
|
|
|
|
|
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);
|
|
|
|
|
});
|
2016-12-10 21:28:47 +05:30
|
|
|
|
|
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');
|
|
|
|
|
});
|
2016-12-10 21:28:47 +05:30
|
|
|
|
|
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', 'E‑mail is invalid');
|
|
|
|
|
});
|
2019-11-27 14:33:32 +05:30
|
|
|
|
});
|
2016-12-10 21:28:47 +05:30
|
|
|
|
});
|