mirror of
https://github.com/shlinkio/shlink-web-client.git
synced 2025-01-11 02:37:22 +03:00
Added tests for elements regarding short URL edition
This commit is contained in:
parent
a6ad3c2d4d
commit
19337d6c05
2 changed files with 153 additions and 0 deletions
79
test/short-urls/helpers/EditShortUrlModal.test.js
Normal file
79
test/short-urls/helpers/EditShortUrlModal.test.js
Normal file
|
@ -0,0 +1,79 @@
|
|||
import React from 'react';
|
||||
import { shallow } from 'enzyme';
|
||||
import { FormGroup, Modal, ModalHeader } from 'reactstrap';
|
||||
import EditShortUrlModal from '../../../src/short-urls/helpers/EditShortUrlModal';
|
||||
|
||||
describe('<EditShortUrlModal />', () => {
|
||||
let wrapper;
|
||||
const editShortUrl = jest.fn(() => Promise.resolve());
|
||||
const toggle = jest.fn();
|
||||
const createWrapper = (shortUrl, shortUrlEdition) => {
|
||||
wrapper = shallow(
|
||||
<EditShortUrlModal
|
||||
isOpen={true}
|
||||
shortUrl={shortUrl}
|
||||
shortUrlEdition={shortUrlEdition}
|
||||
toggle={toggle}
|
||||
editShortUrl={editShortUrl}
|
||||
/>
|
||||
);
|
||||
|
||||
return wrapper;
|
||||
};
|
||||
|
||||
afterEach(() => {
|
||||
wrapper && wrapper.unmount();
|
||||
jest.clearAllMocks();
|
||||
});
|
||||
|
||||
it.each([
|
||||
[ false, 0 ],
|
||||
[ true, 1 ],
|
||||
])('properly renders form with expected components', (error, expectedErrorLength) => {
|
||||
const wrapper = createWrapper({}, { saving: false, error });
|
||||
const errorElement = wrapper.find('.bg-danger');
|
||||
const form = wrapper.find('form');
|
||||
const formGroup = form.find(FormGroup);
|
||||
|
||||
expect(form).toHaveLength(1);
|
||||
expect(formGroup).toHaveLength(1);
|
||||
expect(errorElement).toHaveLength(expectedErrorLength);
|
||||
});
|
||||
|
||||
it.each([
|
||||
[ true, 'Saving...', 'something', true ],
|
||||
[ true, 'Saving...', undefined, true ],
|
||||
[ false, 'Save', 'something', false ],
|
||||
[ false, 'Save', undefined, true ],
|
||||
])('renders submit button on expected state', (saving, expectedText, longUrl, expectedDisabled) => {
|
||||
const wrapper = createWrapper({ longUrl }, { saving, error: false });
|
||||
const button = wrapper.find('[color="primary"]');
|
||||
|
||||
expect(button.prop('disabled')).toEqual(expectedDisabled);
|
||||
expect(button.html()).toContain(expectedText);
|
||||
});
|
||||
|
||||
it('saves data when form is submit', () => {
|
||||
const preventDefault = jest.fn();
|
||||
const wrapper = createWrapper({}, { saving: false, error: false });
|
||||
const form = wrapper.find('form');
|
||||
|
||||
form.simulate('submit', { preventDefault });
|
||||
|
||||
expect(preventDefault).toHaveBeenCalled();
|
||||
expect(editShortUrl).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it.each([
|
||||
[ '[color="link"]', 'onClick' ],
|
||||
[ Modal, 'toggle' ],
|
||||
[ ModalHeader, 'toggle' ],
|
||||
])('toggles modal with different mechanisms', (componentToFind, propToCall) => {
|
||||
const wrapper = createWrapper({}, { saving: false, error: false });
|
||||
const component = wrapper.find(componentToFind);
|
||||
|
||||
component.prop(propToCall)();
|
||||
|
||||
expect(toggle).toHaveBeenCalled();
|
||||
});
|
||||
});
|
74
test/short-urls/reducers/shortUrlEdition.test.js
Normal file
74
test/short-urls/reducers/shortUrlEdition.test.js
Normal file
|
@ -0,0 +1,74 @@
|
|||
import reducer, {
|
||||
EDIT_SHORT_URL_START,
|
||||
EDIT_SHORT_URL_ERROR,
|
||||
SHORT_URL_EDITED,
|
||||
editShortUrl,
|
||||
} from '../../../src/short-urls/reducers/shortUrlEdition';
|
||||
|
||||
describe('shortUrlEditionReducer', () => {
|
||||
const longUrl = 'https://shlink.io';
|
||||
const shortCode = 'abc123';
|
||||
|
||||
describe('reducer', () => {
|
||||
it('returns loading on EDIT_SHORT_URL_START', () => {
|
||||
expect(reducer({}, { type: EDIT_SHORT_URL_START })).toEqual({
|
||||
saving: true,
|
||||
error: false,
|
||||
});
|
||||
});
|
||||
|
||||
it('returns error on EDIT_SHORT_URL_ERROR', () => {
|
||||
expect(reducer({}, { type: EDIT_SHORT_URL_ERROR })).toEqual({
|
||||
saving: false,
|
||||
error: true,
|
||||
});
|
||||
});
|
||||
|
||||
it('returns provided tags and shortCode on SHORT_URL_EDITED', () => {
|
||||
expect(reducer({}, { type: SHORT_URL_EDITED, longUrl, shortCode })).toEqual({
|
||||
longUrl,
|
||||
shortCode,
|
||||
saving: false,
|
||||
error: false,
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('editShortUrl', () => {
|
||||
const updateShortUrlMeta = jest.fn().mockResolvedValue({});
|
||||
const buildShlinkApiClient = jest.fn().mockReturnValue({ updateShortUrlMeta });
|
||||
const dispatch = jest.fn();
|
||||
|
||||
afterEach(jest.clearAllMocks);
|
||||
|
||||
it.each([[ undefined ], [ null ], [ 'example.com' ]])('dispatches long URL on success', async (domain) => {
|
||||
await editShortUrl(buildShlinkApiClient)(shortCode, domain, longUrl)(dispatch);
|
||||
|
||||
expect(buildShlinkApiClient).toHaveBeenCalledTimes(1);
|
||||
expect(updateShortUrlMeta).toHaveBeenCalledTimes(1);
|
||||
expect(updateShortUrlMeta).toHaveBeenCalledWith(shortCode, domain, { longUrl });
|
||||
expect(dispatch).toHaveBeenCalledTimes(2);
|
||||
expect(dispatch).toHaveBeenNthCalledWith(1, { type: EDIT_SHORT_URL_START });
|
||||
expect(dispatch).toHaveBeenNthCalledWith(2, { type: SHORT_URL_EDITED, longUrl, shortCode, domain });
|
||||
});
|
||||
|
||||
it('dispatches error on failure', async () => {
|
||||
const error = new Error();
|
||||
|
||||
updateShortUrlMeta.mockRejectedValue(error);
|
||||
|
||||
try {
|
||||
await editShortUrl(buildShlinkApiClient)(shortCode, undefined, longUrl)(dispatch);
|
||||
} catch (e) {
|
||||
expect(e).toBe(error);
|
||||
}
|
||||
|
||||
expect(buildShlinkApiClient).toHaveBeenCalledTimes(1);
|
||||
expect(updateShortUrlMeta).toHaveBeenCalledTimes(1);
|
||||
expect(updateShortUrlMeta).toHaveBeenCalledWith(shortCode, undefined, { longUrl });
|
||||
expect(dispatch).toHaveBeenCalledTimes(2);
|
||||
expect(dispatch).toHaveBeenNthCalledWith(1, { type: EDIT_SHORT_URL_START });
|
||||
expect(dispatch).toHaveBeenNthCalledWith(2, { type: EDIT_SHORT_URL_ERROR });
|
||||
});
|
||||
});
|
||||
});
|
Loading…
Reference in a new issue