regexper-static/src/components/Form/test.js

94 lines
2.9 KiB
JavaScript
Raw Normal View History

2018-02-14 02:23:49 +00:00
import React from 'react';
2018-02-21 01:57:13 +00:00
import { shallow } from 'enzyme';
2018-02-14 02:23:49 +00:00
import { Form } from 'components/Form';
2018-02-21 01:57:13 +00:00
import { translate } from '__mocks__/i18n';
2018-02-14 02:23:49 +00:00
const syntaxes = {
js: 'Javascript',
pcre: 'PCRE'
};
describe('Form', () => {
test('rendering', () => {
const component = shallow(
<Form t={ translate } syntaxes={ syntaxes }/>
);
expect(component).toMatchSnapshot();
});
2018-02-15 22:35:45 +00:00
test('rendering with download URLs', () => {
const downloadUrls = [
{ url: '#svg', filename: 'image.svg', type: 'image/svg+xml', label: 'Download SVG' },
{ url: '#png', filename: 'image.png', type: 'image/png', label: 'Download PNG' }
];
const component = shallow(
<Form t={ translate } syntaxes={ syntaxes } downloadUrls={ downloadUrls }/>
);
expect(component).toMatchSnapshot();
});
test('rendering with permalink URL', () => {
const permalinkUrl = '#permalink';
const component = shallow(
<Form t={ translate } syntaxes={ syntaxes } permalinkUrl={ permalinkUrl }/>
);
expect(component).toMatchSnapshot();
});
describe('submitting expression', () => {
test('submitting form', () => {
const onSubmit = jest.fn();
2018-02-21 01:57:13 +00:00
const component = shallow(
<Form t={ translate } syntaxes={ syntaxes } onSubmit={ onSubmit }/>
);
2018-06-09 01:05:35 +00:00
const exprInput = component.find('[name="expr"]');
const syntaxInput = component.find('[name="syntax"]');
exprInput.simulate('change', { target: { name: 'expr', value: 'Test expression' } });
syntaxInput.simulate('change', { target: { name: 'syntax', value: 'test' } });
2018-02-15 22:35:45 +00:00
const eventObj = { preventDefault: jest.fn() };
component.find('form').simulate('submit', eventObj);
2018-02-14 02:23:49 +00:00
2018-02-15 22:35:45 +00:00
expect(eventObj.preventDefault).toHaveBeenCalled();
expect(onSubmit).toHaveBeenCalledWith({
2018-02-19 22:29:08 +00:00
expr: 'Test expression',
syntax: 'test'
2018-02-15 22:35:45 +00:00
});
});
2018-02-14 02:23:49 +00:00
2018-02-15 22:35:45 +00:00
test('submitting form with Shift+Enter', () => {
2018-02-21 01:57:13 +00:00
const component = shallow(
<Form t={ translate } syntaxes={ syntaxes } onSubmit={ Function.prototype }/>
);
const form = component.instance();
const eventObj = {
preventDefault: Function.prototype,
charCode: 13,
shiftKey: true
};
2018-02-15 22:35:45 +00:00
jest.spyOn(form, 'handleSubmit');
component.find('textarea').simulate('keypress', eventObj);
2018-02-14 02:23:49 +00:00
2018-02-15 22:35:45 +00:00
expect(form.handleSubmit).toHaveBeenCalled();
});
test('not submitting with just Enter', () => {
2018-02-21 01:57:13 +00:00
const component = shallow(
<Form t={ translate } syntaxes={ syntaxes } onSubmit={ Function.protoytpe }/>
);
const form = component.instance();
const eventObj = {
preventDefault: Function.prototype,
charCode: 13,
shiftKey: false
};
2018-02-15 22:35:45 +00:00
jest.spyOn(form, 'handleSubmit');
component.find('textarea').simulate('keypress', eventObj);
expect(form.handleSubmit).not.toHaveBeenCalled();
});
});
2018-02-14 02:23:49 +00:00
});