shlink-web-client/test/short-urls/helpers/EditMetaModal.test.tsx

84 lines
2.7 KiB
TypeScript
Raw Normal View History

2020-08-26 21:03:23 +03:00
import { shallow, ShallowWrapper } from 'enzyme';
import { FormGroup } from 'reactstrap';
import { Mock } from 'ts-mockery';
2020-01-19 22:59:01 +03:00
import EditMetaModal from '../../../src/short-urls/helpers/EditMetaModal';
2020-08-26 21:03:23 +03:00
import { ShortUrl } from '../../../src/short-urls/data';
import { ShortUrlMetaEdition } from '../../../src/short-urls/reducers/shortUrlMeta';
2020-01-19 22:59:01 +03:00
describe('<EditMetaModal />', () => {
2020-08-26 21:03:23 +03:00
let wrapper: ShallowWrapper;
const editShortUrlMeta = jest.fn(async () => Promise.resolve());
2020-01-19 22:59:01 +03:00
const resetShortUrlMeta = jest.fn();
const toggle = jest.fn();
2020-08-26 21:03:23 +03:00
const createWrapper = (shortUrlMeta: Partial<ShortUrlMetaEdition>) => {
2020-01-19 22:59:01 +03:00
wrapper = shallow(
<EditMetaModal
isOpen={true}
2020-08-26 21:03:23 +03:00
shortUrl={Mock.all<ShortUrl>()}
shortUrlMeta={Mock.of<ShortUrlMetaEdition>(shortUrlMeta)}
2020-01-19 22:59:01 +03:00
toggle={toggle}
editShortUrlMeta={editShortUrlMeta}
resetShortUrlMeta={resetShortUrlMeta}
2020-08-22 09:10:31 +03:00
/>,
2020-01-19 22:59:01 +03:00
);
return wrapper;
};
2020-08-26 21:03:23 +03:00
afterEach(() => wrapper?.unmount());
afterEach(jest.clearAllMocks);
2020-01-19 22:59:01 +03:00
it('properly renders form with components', () => {
2020-08-26 21:03:23 +03:00
const wrapper = createWrapper({ saving: false, error: false });
2020-01-19 22:59:01 +03:00
const error = wrapper.find('.bg-danger');
const form = wrapper.find('form');
const formGroup = form.find(FormGroup);
expect(form).toHaveLength(1);
expect(formGroup).toHaveLength(3);
expect(error).toHaveLength(0);
});
it.each([
2020-01-19 22:59:01 +03:00
[ true, 'Saving...' ],
[ false, 'Save' ],
])('renders submit button on expected state', (saving, expectedText) => {
2020-08-26 21:03:23 +03:00
const wrapper = createWrapper({ saving, error: false });
2020-01-19 22:59:01 +03:00
const button = wrapper.find('[type="submit"]');
expect(button.prop('disabled')).toEqual(saving);
expect(button.text()).toContain(expectedText);
});
it('renders error message on error', () => {
2020-08-26 21:03:23 +03:00
const wrapper = createWrapper({ saving: false, error: true });
2020-01-19 22:59:01 +03:00
const error = wrapper.find('.bg-danger');
expect(error).toHaveLength(1);
});
it('saves meta when form is submit', () => {
const preventDefault = jest.fn();
2020-08-26 21:03:23 +03:00
const wrapper = createWrapper({ saving: false, error: false });
2020-01-19 22:59:01 +03:00
const form = wrapper.find('form');
form.simulate('submit', { preventDefault });
expect(preventDefault).toHaveBeenCalled();
expect(editShortUrlMeta).toHaveBeenCalled();
});
it.each([
2020-01-19 22:59:01 +03:00
[ '.btn-link', 'onClick' ],
2020-08-26 21:03:23 +03:00
[ 'Modal', 'toggle' ],
[ 'ModalHeader', 'toggle' ],
])('resets meta when modal is toggled in any way', (componentToFind, propToCall) => {
2020-08-26 21:03:23 +03:00
const wrapper = createWrapper({ saving: false, error: false });
2020-01-19 22:59:01 +03:00
const component = wrapper.find(componentToFind);
2020-08-26 21:03:23 +03:00
(component.prop(propToCall) as Function)(); // eslint-disable-line @typescript-eslint/no-unnecessary-type-assertion
2020-01-19 22:59:01 +03:00
expect(resetShortUrlMeta).toHaveBeenCalled();
});
});