2020-01-18 02:07:52 +05:30
|
|
|
import React, { ComponentProps } 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-01-18 02:07:52 +05:30
|
|
|
import { shallow, mount, ShallowWrapper, ReactWrapper } from 'enzyme';
|
2016-12-10 21:28:47 +05:30
|
|
|
import { IntlProvider } from 'react-intl';
|
2019-12-08 00:32:00 +05:30
|
|
|
import feedback from 'app/services/api/feedback';
|
|
|
|
import { User } from 'app/components/user';
|
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-01-18 02:07:52 +05:30
|
|
|
type ContactFormShallowType = ShallowWrapper<
|
|
|
|
ComponentProps<typeof ContactForm>,
|
|
|
|
any,
|
|
|
|
ContactForm
|
|
|
|
>;
|
|
|
|
|
2016-12-10 21:28:47 +05:30
|
|
|
describe('ContactForm', () => {
|
2019-11-27 14:33:32 +05:30
|
|
|
describe('when rendered', () => {
|
2019-12-07 16:58:52 +05:30
|
|
|
const user = {} as User;
|
2020-01-18 02:07:52 +05:30
|
|
|
let component: ContactFormShallowType;
|
2016-12-10 21:28:47 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
beforeEach(() => {
|
|
|
|
component = shallow(<ContactForm user={user} />);
|
|
|
|
});
|
2016-12-10 21:28:47 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
[
|
|
|
|
{
|
|
|
|
type: 'Input',
|
|
|
|
name: 'subject',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
type: 'Input',
|
|
|
|
name: 'email',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
type: 'Dropdown',
|
|
|
|
name: 'category',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
type: 'TextArea',
|
|
|
|
name: 'message',
|
|
|
|
},
|
2020-05-20 22:05:52 +05:30
|
|
|
].forEach((el) => {
|
2019-11-27 14:33:32 +05:30
|
|
|
it(`should have ${el.name} field`, () => {
|
|
|
|
expect(component.find(`${el.type}[name="${el.name}"]`), 'to satisfy', {
|
|
|
|
length: 1,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2016-12-10 21:28:47 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
it('should contain Form', () => {
|
|
|
|
expect(component.find('Form'), 'to satisfy', { length: 1 });
|
2016-12-10 21:28:47 +05:30
|
|
|
});
|
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
it('should contain submit Button', () => {
|
|
|
|
expect(component.find('Button[type="submit"]'), 'to satisfy', {
|
|
|
|
length: 1,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2016-12-10 21:28:47 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
describe('when rendered with user', () => {
|
|
|
|
const user = {
|
|
|
|
email: 'foo@bar.com',
|
2019-12-07 16:58:52 +05:30
|
|
|
} as User;
|
2020-01-18 02:07:52 +05:30
|
|
|
let component: ContactFormShallowType;
|
2016-12-10 21:28:47 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
beforeEach(() => {
|
|
|
|
component = shallow(<ContactForm user={user} />);
|
2016-12-10 21:28:47 +05:30
|
|
|
});
|
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
it('should render email field with user email', () => {
|
|
|
|
expect(
|
|
|
|
component.find('Input[name="email"]').prop('defaultValue'),
|
|
|
|
'to equal',
|
|
|
|
user.email,
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
2016-12-10 21:28:47 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
describe('when email was successfully sent', () => {
|
|
|
|
const user = {
|
|
|
|
email: 'foo@bar.com',
|
2019-12-07 16:58:52 +05:30
|
|
|
} as User;
|
2020-01-18 02:07:52 +05:30
|
|
|
let component: ContactFormShallowType;
|
2016-12-10 21:28:47 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
beforeEach(() => {
|
2019-12-07 16:58:52 +05:30
|
|
|
component = shallow(<ContactForm user={user} />);
|
2016-12-10 21:28:47 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
component.setState({ isSuccessfullySent: true });
|
2016-12-10 21:28:47 +05:30
|
|
|
});
|
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
it('should not contain Form', () => {
|
|
|
|
expect(component.find('Form'), 'to satisfy', { length: 0 });
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
xdescribe('validation', () => {
|
|
|
|
const user = {
|
|
|
|
email: 'foo@bar.com',
|
2019-12-07 16:58:52 +05:30
|
|
|
} as User;
|
2020-01-18 02:07:52 +05:30
|
|
|
let component: ContactForm;
|
|
|
|
let wrapper: ReactWrapper;
|
2019-11-27 14:33:32 +05:30
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
// TODO: add polyfill for from validation for jsdom
|
|
|
|
|
|
|
|
wrapper = mount(
|
|
|
|
<IntlProvider locale="en" defaultLocale="en">
|
2020-05-20 22:05:52 +05:30
|
|
|
<ContactForm user={user} ref={(el) => (component = el!)} />
|
2019-11-27 14:33:32 +05:30
|
|
|
</IntlProvider>,
|
|
|
|
);
|
|
|
|
});
|
2016-12-10 21:28:47 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
it('should require email, subject and message', () => {
|
|
|
|
// wrapper.find('[type="submit"]').simulate('click');
|
|
|
|
wrapper.find('form').simulate('submit');
|
2016-12-10 21:28:47 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
expect(component.form.hasErrors(), 'to be true');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('when user submits form', () => {
|
|
|
|
const user = {
|
|
|
|
email: 'foo@bar.com',
|
2019-12-07 16:58:52 +05:30
|
|
|
} as User;
|
2020-01-18 02:07:52 +05:30
|
|
|
let component: ContactForm;
|
|
|
|
let wrapper: ReactWrapper;
|
2019-11-27 14:33:32 +05:30
|
|
|
const requestData = {
|
|
|
|
email: user.email,
|
|
|
|
subject: 'Test subject',
|
|
|
|
message: 'Test message',
|
|
|
|
};
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
sinon.stub(feedback, 'send');
|
|
|
|
|
|
|
|
// TODO: add polyfill for from validation for jsdom
|
2019-12-07 16:58:52 +05:30
|
|
|
if (!(Element.prototype as any).checkValidity) {
|
|
|
|
(Element.prototype as any).checkValidity = () => true;
|
2019-11-27 14:33:32 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: try to rewrite with unexpected-react
|
|
|
|
wrapper = mount(
|
|
|
|
<IntlProvider locale="en" defaultLocale="en">
|
2020-05-20 22:05:52 +05:30
|
|
|
<ContactForm user={user} ref={(el) => (component = el!)} />
|
2019-11-27 14:33:32 +05:30
|
|
|
</IntlProvider>,
|
|
|
|
);
|
|
|
|
|
2020-01-18 02:07:52 +05:30
|
|
|
wrapper.find('input[name="email"]').getDOMNode<HTMLInputElement>().value =
|
2019-11-27 14:33:32 +05:30
|
|
|
requestData.email;
|
2020-01-18 02:07:52 +05:30
|
|
|
wrapper
|
|
|
|
.find('input[name="subject"]')
|
|
|
|
.getDOMNode<HTMLInputElement>().value = requestData.subject;
|
|
|
|
wrapper
|
|
|
|
.find('textarea[name="message"]')
|
|
|
|
.getDOMNode<HTMLInputElement>().value = requestData.message;
|
2016-12-10 21:28:47 +05:30
|
|
|
});
|
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
afterEach(() => {
|
2019-12-07 16:58:52 +05:30
|
|
|
(feedback.send as any).restore();
|
2019-11-27 14:33:32 +05:30
|
|
|
});
|
2016-12-10 21:28:47 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
xit('should call onSubmit', () => {
|
|
|
|
sinon.stub(component, 'onSubmit');
|
2016-12-10 21:28:47 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
wrapper.find('form').simulate('submit');
|
2016-12-10 21:28:47 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
expect(component.onSubmit, 'was called');
|
|
|
|
});
|
2016-12-10 21:28:47 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
it('should call send with required data', () => {
|
2019-12-07 16:58:52 +05:30
|
|
|
(feedback.send as any).returns(Promise.resolve());
|
2018-05-05 12:35:57 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
component.onSubmit();
|
2016-12-10 21:28:47 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
expect(feedback.send, 'to have a call satisfying', [requestData]);
|
|
|
|
});
|
2016-12-10 21:28:47 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
it('should set isSuccessfullySent', () => {
|
2019-12-07 16:58:52 +05:30
|
|
|
(feedback.send as any).returns(Promise.resolve());
|
2016-12-10 21:28:47 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
return component
|
|
|
|
.onSubmit()
|
|
|
|
.then(() =>
|
|
|
|
expect(component.state, 'to satisfy', { isSuccessfullySent: true }),
|
|
|
|
);
|
|
|
|
});
|
2016-12-10 21:28:47 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
it('should handle isLoading during request', () => {
|
2019-12-07 16:58:52 +05:30
|
|
|
(feedback.send as any).returns(Promise.resolve());
|
2016-12-10 21:28:47 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
const promise = component.onSubmit();
|
2016-12-10 21:28:47 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
expect(component.state, 'to satisfy', { isLoading: true });
|
2016-12-10 21:28:47 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
return promise.then(() =>
|
|
|
|
expect(component.state, 'to satisfy', { isLoading: false }),
|
|
|
|
);
|
|
|
|
});
|
2016-12-10 21:28:47 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
it('should render success message with user email', () => {
|
2019-12-07 16:58:52 +05:30
|
|
|
(feedback.send as any).returns(Promise.resolve());
|
2016-12-10 21:28:47 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
return component
|
|
|
|
.onSubmit()
|
|
|
|
.then(() => expect(wrapper.text(), 'to contain', user.email));
|
2016-12-10 21:28:47 +05:30
|
|
|
});
|
2019-11-27 14:33:32 +05:30
|
|
|
});
|
2016-12-10 21:28:47 +05:30
|
|
|
});
|