From 10c9f7dabd6759aff16d60f7ff6dc553a72ae109 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Sat, 27 Mar 2021 17:56:46 +0100 Subject: [PATCH] Added header to EditShortUrl and created EditSHortUrl test --- src/short-urls/EditShortUrl.tsx | 18 +++++ test/short-urls/EditShortUrl.test.tsx | 105 ++++++++++++++++++++++++++ 2 files changed, 123 insertions(+) create mode 100644 test/short-urls/EditShortUrl.test.tsx diff --git a/src/short-urls/EditShortUrl.tsx b/src/short-urls/EditShortUrl.tsx index 4d83a81e..ff21254e 100644 --- a/src/short-urls/EditShortUrl.tsx +++ b/src/short-urls/EditShortUrl.tsx @@ -1,5 +1,9 @@ import { FC, useEffect } from 'react'; import { RouteComponentProps } from 'react-router'; +import { Button, Card } from 'reactstrap'; +import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'; +import { faArrowLeft } from '@fortawesome/free-solid-svg-icons'; +import { ExternalLink } from 'react-external-link'; import { SelectedServer } from '../servers/data'; import { Settings, ShortUrlCreationSettings } from '../settings/reducers/settings'; import { OptionalString } from '../utils/utils'; @@ -41,6 +45,7 @@ const getInitialState = (shortUrl?: ShortUrl, settings?: ShortUrlCreationSetting }; export const EditShortUrl = (ShortUrlForm: FC) => ({ + history: { goBack }, match: { params }, location: { search }, settings: { shortUrlCreation: shortUrlCreationSettings }, @@ -70,8 +75,21 @@ export const EditShortUrl = (ShortUrlForm: FC) => ({ ); } + const title = Edit ; + return ( <> +
+ +

+ + {title} + +

+
+
', () => { + let wrapper: ShallowWrapper; + const ShortUrlForm = () => null; + const goBack = jest.fn(); + const getShortUrlDetail = jest.fn(); + const editShortUrl = jest.fn(); + const shortUrlCreation = { validateUrls: true }; + const createWrapper = (detail: Partial = {}, edition: Partial = {}) => { + const EditSHortUrl = createEditShortUrl(ShortUrlForm); + + wrapper = shallow( + ({ shortUrlCreation })} + selectedServer={null} + shortUrlDetail={Mock.of(detail)} + shortUrlEdition={Mock.of(edition)} + getShortUrlDetail={getShortUrlDetail} + editShortUrl={editShortUrl} + history={Mock.of({ goBack })} + location={Mock.all()} + match={Mock.of>({ + params: { shortCode: 'the_base_url' }, + })} + />, + ); + + return wrapper; + }; + + beforeEach(jest.clearAllMocks); + afterEach(() => wrapper?.unmount()); + + it('renders loading message while loading detail', () => { + const wrapper = createWrapper({ loading: true }); + + expect(wrapper.prop('loading')).toEqual(true); + }); + + it('renders error when loading detail fails', () => { + const wrapper = createWrapper({ error: true }); + const form = wrapper.find(ShortUrlForm); + const apiError = wrapper.find(ShlinkApiError); + + expect(form).toHaveLength(0); + expect(apiError).toHaveLength(1); + expect(apiError.prop('fallbackMessage')).toEqual('An error occurred while loading short URL detail :('); + }); + + it.each([ + [ undefined, { longUrl: '', validateUrl: true }, true ], + [ + Mock.of({ meta: {} }), + { + longUrl: undefined, + tags: undefined, + title: undefined, + domain: undefined, + validSince: undefined, + validUntil: undefined, + maxVisits: undefined, + validateUrl: true, + }, + false, + ], + ])('renders form when detail properly loads', (shortUrl, expectedInitialState, saving) => { + const wrapper = createWrapper({ shortUrl }, { saving }); + const form = wrapper.find(ShortUrlForm); + const apiError = wrapper.find(ShlinkApiError); + + expect(form).toHaveLength(1); + expect(apiError).toHaveLength(0); + expect(form.prop('initialState')).toEqual(expectedInitialState); + expect(form.prop('saving')).toEqual(saving); + expect(editShortUrl).not.toHaveBeenCalled(); + + form.simulate('save', {}); + + if (shortUrl) { + expect(editShortUrl).toHaveBeenCalledWith(shortUrl.shortCode, shortUrl.domain, {}); + } else { + expect(editShortUrl).not.toHaveBeenCalled(); + } + }); + + it('shows error when saving data has failed', () => { + const wrapper = createWrapper({}, { error: true }); + const form = wrapper.find(ShortUrlForm); + const apiError = wrapper.find(ShlinkApiError); + + expect(form).toHaveLength(1); + expect(apiError).toHaveLength(1); + expect(apiError.prop('fallbackMessage')).toEqual('An error occurred while updating short URL :('); + }); +});