Skip to content

Commit

Permalink
chore: added test cases, fixed clipboard issue, refactored icons to s…
Browse files Browse the repository at this point in the history
…ocials
  • Loading branch information
ameerul-deriv committed Jul 27, 2023
1 parent 9aa35e4 commit faec3f1
Show file tree
Hide file tree
Showing 13 changed files with 218 additions and 44 deletions.
7 changes: 3 additions & 4 deletions packages/components/src/components/clipboard/clipboard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,16 @@ const Clipboard = ({
alignment={popoverAlignment}
classNameBubble={classNames('dc-clipboard__popover', popoverClassName)}
message={is_copied ? success_message : info_message}
relative_render
{...popover_props}
zIndex='9999'
>
{is_copied && (
{is_copied ? (
<Icon
icon='IcCheckmarkCircle'
custom_color='var(--status-success)'
className={classNames('dc-clipboard', className)}
/>
)}
{!is_copied && (
) : (
<Icon
icon={icon || 'IcClipboard'}
custom_color='var(--text-less-prominent)'
Expand Down
2 changes: 1 addition & 1 deletion packages/components/src/components/modal/modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ const ModalElement = ({
)}
{has_close_icon && (
<div onClick={toggleModal} className='dc-modal-header__close' role='button'>
<Icon icon='IcCross' color={close_icon_color} />
<Icon icon='IcCross' color={close_icon_color} data_testid='dt-close-modal-icon' />
</div>
)}
</div>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
import React from 'react';
import { toPng } from 'html-to-image';
import { act, render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { base64_images } from 'Constants/base64-images';
import ShareMyAdsModal from '../share-my-ads-modal';

const el_modal = document.createElement('div');

const mock_advert = {
account_currency: 'USD',
id: '128812781',
max_order_amount_limit_display: '2.00',
min_order_amount_limit_display: '1.00',
rate_display: '+0.16',
type: 'buy',
};

const mock_modal_manager = {
hideModal: jest.fn(),
is_modal_open: true,
};

jest.mock('html-to-image', () => ({
toPng: jest.fn(),
}));

jest.mock('react-qrcode-logo', () => ({ QRCode: () => <div>QR code</div> }));

jest.mock('@deriv/components', () => ({
...jest.requireActual('@deriv/components'),
DesktopWrapper: jest.fn(({ children }) => children),
MobileWrapper: jest.fn(({ children }) => children),
}));

jest.mock('Components/modal-manager/modal-manager-context', () => ({
...jest.requireActual('Components/modal-manager/modal-manager-context'),
useModalManagerContext: jest.fn(() => mock_modal_manager),
}));

describe('<ShareMyAdsModal />', () => {
beforeAll(() => {
el_modal.setAttribute('id', 'modal_root');
document.body.appendChild(el_modal);
});

afterAll(() => {
document.body.removeChild(el_modal);
});

it('should render the ShareMyAdsModal', () => {
render(<ShareMyAdsModal advert={mock_advert} />);

expect(screen.getByText('Share this ad')).toBeInTheDocument();
expect(screen.getByText('Promote your ad by sharing the QR code and link.')).toBeInTheDocument();
expect(screen.getByText('QR code')).toBeInTheDocument();
});

it('should toggle the modal', () => {
render(<ShareMyAdsModal advert={mock_advert} />);

const close_icon = screen.getByTestId('dt-close-modal-icon');
userEvent.click(close_icon);

expect(mock_modal_manager.hideModal).toHaveBeenCalled();
});

it('should call setShowPopup when clicking on Share link', () => {
render(<ShareMyAdsModal advert={mock_advert} />);

const share_link_button = screen.getByRole('button', { name: 'Share link' });

userEvent.click(share_link_button);

expect(screen.getByTestId('dt_share_my-ads_popup')).toBeInTheDocument();
});

it('should call onCopy function when clicking on copy icon', async () => {
const writeText = jest.fn();

Object.assign(navigator, {
clipboard: {
writeText,
},
});

jest.useFakeTimers();

render(<ShareMyAdsModal advert={mock_advert} />);

const copy_button = screen.getByRole('button', { name: 'Copy link' });

userEvent.click(copy_button);

await act(async () => {
jest.runAllTimers();
await Promise.resolve();
});

expect(navigator.clipboard.writeText).toHaveBeenCalledWith(window.location.href);
});

it('should call handleGenerateImage function when clicking on Download this QR code button', async () => {
const dataUrl = base64_images.dp2p_logo;

(toPng as jest.MockedFunction<typeof toPng>).mockResolvedValueOnce(dataUrl);

render(<ShareMyAdsModal advert={mock_advert} />);

const download_button = screen.getByRole('button', { name: 'Download this QR code' });
userEvent.click(download_button);

expect(toPng).toHaveBeenCalled();
});
});

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@
.dc-btn {
width: 50%;
}

&--bubble {
top: 31px;
left: -70px;
}
}

&-details {
Expand Down Expand Up @@ -146,6 +151,12 @@
display: flex;
margin: 1.5rem 1rem;
}

&--popover {
display: flex;
right: 0;
top: 0;
}
}

&-link {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { useModalManagerContext } from 'Components/modal-manager/modal-manager-c
import MyProfileSeparatorContainer from 'Components/my-profile/my-profile-separator-container';
import { base64_images } from 'Constants/base64-images';
import { TAdvertProps } from 'Types';
import ShareMyAdsIcons from './share-my-ads-icons';
import ShareMyAdsIcons from './share-my-ads-socials';
import ShareMyAdsPopup from './share-my-ads-popup';

const ShareMyAdsModal = ({ advert }: TAdvertProps) => {
Expand All @@ -27,10 +27,11 @@ const ShareMyAdsModal = ({ advert }: TAdvertProps) => {

const isMounted = useIsMounted();
const divRef = React.useRef(null);
// TODO: replace with proper url when available
const advert_url = window.location.href;

const { hideModal, is_modal_open } = useModalManagerContext();
const { id, min_order_amount_limit_display, max_order_amount_limit_display, account_currency, type, rate_display } =
const { account_currency, id, max_order_amount_limit_display, min_order_amount_limit_display, rate_display, type } =
advert;

const options = {
Expand Down Expand Up @@ -58,16 +59,12 @@ const ShareMyAdsModal = ({ advert }: TAdvertProps) => {

const handleGenerateImage = () => {
if (divRef.current) {
toPng(divRef.current)
.then(dataUrl => {
const link = document.createElement('a');
link.download = 'test.png';
link.href = dataUrl;
link.click();
})
.catch(error => {
throw new Error(error);
});
toPng(divRef.current).then(dataUrl => {
const link = document.createElement('a');
link.download = 'test.png';
link.href = dataUrl;
link.click();
});
}
};

Expand All @@ -88,9 +85,7 @@ const ShareMyAdsModal = ({ advert }: TAdvertProps) => {
width='71rem'
>
<Modal.Body className='share-my-ads-modal__body'>
<MobileWrapper>
{show_popup && <ShareMyAdsPopup onClose={() => setShowPopup(false)} />}
</MobileWrapper>
<MobileWrapper>{show_popup && <ShareMyAdsPopup onClose={setShowPopup} />}</MobileWrapper>
<DesktopWrapper>
<Text>
<Localize i18n_default_text='Promote your ad by sharing the QR code and link.' />
Expand Down Expand Up @@ -174,7 +169,7 @@ const ShareMyAdsModal = ({ advert }: TAdvertProps) => {
is_copied ? (
<Icon icon='IcCheckmarkCircle' custom_color='var(--status-success)' />
) : (
<Icon icon='IcShareLink' />
<Icon icon='IcShareLink' data_testid='dt-copy-link-icon' />
)
}
secondary
Expand Down Expand Up @@ -205,8 +200,11 @@ const ShareMyAdsModal = ({ advert }: TAdvertProps) => {
<div className='share-my-ads-modal__copy-clipboard'>
<Clipboard
className='share-my-ads-modal__copy-clipboard--icon'
text_copy={advert_url}
popoverAlignment='top'
popoverClassName='share-my-ads-modal__copy-clipboard--popover'
size='18'
success_message={localize('Copied!')}
text_copy={advert_url}
/>
</div>
</div>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import React from 'react';
import { screen, render } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import ShareMyAdsPopup from '../share-my-ads-popup';

const mock_on_close = jest.fn();

describe('<ShareMyAdsPopup />', () => {
it('should call closePopup function when clicking on cross icon', () => {
jest.useFakeTimers();

const el_modal = document.createElement('div');
el_modal.setAttribute('id', 'modal_root');
document.body.appendChild(el_modal);

render(<ShareMyAdsPopup onClose={mock_on_close} />);

const close_icon = screen.getByTestId('dt-close-popup-icon');
userEvent.click(close_icon);

jest.runAllTimers();

expect(mock_on_close).toHaveBeenCalled();
document.body.removeChild(el_modal);
});

it('should return null if modal_root is not found', () => {
render(<ShareMyAdsPopup onClose={mock_on_close} />);

expect(screen.queryByText('Share my ads')).not.toBeInTheDocument();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

&__container {
background: #fff;
height: 45%;
height: 33%;
width: 100%;
border-radius: 5% 5% 0 0;
animation: popup 0.5s ease-in;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import classNames from 'classnames';
import { createPortal } from 'react-dom';
import { Icon, Text } from '@deriv/components';
import { Localize } from 'Components/i18next';
import ShareMyAdsIcons from '../share-my-ads-icons';
import ShareMyAdsIcons from '../share-my-ads-socials';

type TShareMyAdsPopupProps = {
onClose: () => void;
onClose: (value: boolean) => void;
};

const ShareMyAdsPopup = ({ onClose }: TShareMyAdsPopupProps) => {
Expand All @@ -15,12 +15,12 @@ const ShareMyAdsPopup = ({ onClose }: TShareMyAdsPopupProps) => {

const closePopup = () => {
setIsClosing(true);
setTimeout(() => onClose(), 500);
setTimeout(() => onClose(false), 500);
};

if (popup) {
return createPortal(
<div className='share-my-ads-popup'>
<div className='share-my-ads-popup' data-testid='dt_share_my-ads_popup'>
<div
className={classNames('share-my-ads-popup__container', {
closing: is_closing,
Expand All @@ -30,7 +30,13 @@ const ShareMyAdsPopup = ({ onClose }: TShareMyAdsPopupProps) => {
<Text>
<Localize i18n_default_text='Share my ads' />
</Text>
<Icon className='share-my-ads-popup__container-icon' icon='IcCross' onClick={closePopup} />
<Icon
className='share-my-ads-popup__container-icon'
custom_color='less-prominent'
data_testid='dt-close-popup-icon'
icon='IcCross'
onClick={closePopup}
/>
</div>
<ShareMyAdsIcons />
</div>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import React from 'react';
import { screen, render } from '@testing-library/react';
import ShareMyAdsSocials from '../share-my-ads-socials';

describe('<ShareMyAdsSocials />', () => {
it('should render all the social media platforms', () => {
render(<ShareMyAdsSocials />);

expect(screen.getByText('WhatsApp')).toBeInTheDocument();
expect(screen.getByText('Facebook')).toBeInTheDocument();
expect(screen.getByText('Telegram')).toBeInTheDocument();
expect(screen.getByText('Twitter')).toBeInTheDocument();
expect(screen.getByText('Gmail')).toBeInTheDocument();
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import ShareMyAdsSocials from './share-my-ads-socials';
import './share-my-ads-socials.scss';

export default ShareMyAdsSocials;
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@
margin: 2.5rem 0;

@include mobile {
display: grid;
grid-template-columns: repeat(4, 1fr);
grid-row-gap: 2rem;
margin: 1rem 2.5rem;
margin: 2rem;
}

.react-share__ShareButton {
Expand Down
Loading

0 comments on commit faec3f1

Please sign in to comment.