2018-11-01 15:15:09 +03:00
|
|
|
import React from 'react';
|
|
|
|
import { shallow } from 'enzyme';
|
|
|
|
import moment from 'moment';
|
|
|
|
import { identity } from 'ramda';
|
2018-12-18 01:11:55 +03:00
|
|
|
import createShortUrlsCreator from '../../src/short-urls/CreateShortUrl';
|
2018-11-01 15:15:09 +03:00
|
|
|
import DateInput from '../../src/utils/DateInput';
|
|
|
|
|
|
|
|
describe('<CreateShortUrl />', () => {
|
|
|
|
let wrapper;
|
2018-12-18 01:11:55 +03:00
|
|
|
const TagsSelector = () => '';
|
2018-11-01 15:15:09 +03:00
|
|
|
const shortUrlCreationResult = {
|
|
|
|
loading: false,
|
|
|
|
};
|
2019-04-19 13:41:59 +03:00
|
|
|
const createShortUrl = jest.fn();
|
2018-11-01 15:15:09 +03:00
|
|
|
|
|
|
|
beforeEach(() => {
|
2019-01-14 01:26:06 +03:00
|
|
|
const CreateShortUrl = createShortUrlsCreator(TagsSelector, () => '');
|
2018-12-18 01:11:55 +03:00
|
|
|
|
2018-11-01 15:15:09 +03:00
|
|
|
wrapper = shallow(
|
|
|
|
<CreateShortUrl shortUrlCreationResult={shortUrlCreationResult} createShortUrl={createShortUrl} />
|
|
|
|
);
|
|
|
|
});
|
|
|
|
afterEach(() => {
|
|
|
|
wrapper.unmount();
|
2019-04-19 13:41:59 +03:00
|
|
|
createShortUrl.mockReset();
|
2018-11-01 15:15:09 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('saves short URL with data set in form controls', (done) => {
|
|
|
|
const validSince = moment('2017-01-01');
|
|
|
|
const validUntil = moment('2017-01-06');
|
|
|
|
|
|
|
|
const urlInput = wrapper.find('.form-control-lg');
|
|
|
|
const tagsInput = wrapper.find(TagsSelector);
|
|
|
|
const customSlugInput = wrapper.find('#customSlug');
|
2019-10-05 10:02:02 +03:00
|
|
|
const domain = wrapper.find('#domain');
|
2018-11-01 15:15:09 +03:00
|
|
|
const maxVisitsInput = wrapper.find('#maxVisits');
|
|
|
|
const dateInputs = wrapper.find(DateInput);
|
|
|
|
const validSinceInput = dateInputs.at(0);
|
|
|
|
const validUntilInput = dateInputs.at(1);
|
|
|
|
|
|
|
|
urlInput.simulate('change', { target: { value: 'https://long-domain.com/foo/bar' } });
|
|
|
|
tagsInput.simulate('change', [ 'tag_foo', 'tag_bar' ]);
|
|
|
|
customSlugInput.simulate('change', { target: { value: 'my-slug' } });
|
2019-10-05 10:02:02 +03:00
|
|
|
domain.simulate('change', { target: { value: 'example.com' } });
|
2018-11-01 15:15:09 +03:00
|
|
|
maxVisitsInput.simulate('change', { target: { value: '20' } });
|
|
|
|
validSinceInput.simulate('change', validSince);
|
|
|
|
validUntilInput.simulate('change', validUntil);
|
|
|
|
|
|
|
|
setImmediate(() => {
|
|
|
|
const form = wrapper.find('form');
|
|
|
|
|
|
|
|
form.simulate('submit', { preventDefault: identity });
|
2019-04-19 13:41:59 +03:00
|
|
|
expect(createShortUrl).toHaveBeenCalledTimes(1);
|
2019-04-19 13:52:55 +03:00
|
|
|
expect(createShortUrl).toHaveBeenCalledWith({
|
|
|
|
longUrl: 'https://long-domain.com/foo/bar',
|
|
|
|
tags: [ 'tag_foo', 'tag_bar' ],
|
|
|
|
customSlug: 'my-slug',
|
2019-10-05 10:02:02 +03:00
|
|
|
domain: 'example.com',
|
2019-04-19 13:52:55 +03:00
|
|
|
validSince: validSince.format(),
|
|
|
|
validUntil: validUntil.format(),
|
|
|
|
maxVisits: '20',
|
|
|
|
findIfExists: false,
|
|
|
|
});
|
2018-11-01 15:15:09 +03:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|