Skip to content

Commit

Permalink
test fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
apaleslimghost committed Nov 6, 2018
1 parent ac21318 commit 2db67a7
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 92 deletions.
6 changes: 3 additions & 3 deletions __tests__/__snapshots__/snapshots.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ exports[`@financial-times/x-gift-article renders a default With gift credits x-g
<a
className="o-share__icon o-share__icon--facebook MobileShareButtons_facebook__1ji2o"
data-trackable="facebook"
href="http://www.facebook.com/sharer.php?u=https%3A%2F%2Fwww.ft.com%2Fcontent%2Fblahblahblah&amp;t=Title%20Title%20Title%20Title"
href="http://www.facebook.com/sharer.php?u=https%3A%2F%2Fwww.ft.com%2Fcontent%2Fblahblahblah&t=Title%20Title%20Title%20Title"
rel="noopener"
>
Facebook
Expand All @@ -192,7 +192,7 @@ exports[`@financial-times/x-gift-article renders a default With gift credits x-g
<a
className="o-share__icon o-share__icon--twitter MobileShareButtons_twitter__1QRsw"
data-trackable="twitter"
href="https://twitter.com/intent/tweet?url=https%3A%2F%2Fwww.ft.com%2Fcontent%2Fblahblahblah&amp;text=Title%20Title%20Title%20Title&amp;via=financialtimes"
href="https://twitter.com/intent/tweet?url=https%3A%2F%2Fwww.ft.com%2Fcontent%2Fblahblahblah&text=Title%20Title%20Title%20Title&via=financialtimes"
rel="noopener"
>
Twitter
Expand All @@ -210,7 +210,7 @@ exports[`@financial-times/x-gift-article renders a default With gift credits x-g
<a
className="o-share__icon o-share__icon--linkedin MobileShareButtons_linkedin__1-2-Y"
data-trackable="linkedin"
href="http://www.linkedin.com/shareArticle?mini=true&amp;url=https%3A%2F%2Fwww.ft.com%2Fcontent%2Fblahblahblah&amp;title=Title%20Title%20Title%20Title&amp;source=Financial+Times"
href="http://www.linkedin.com/shareArticle?mini=true&url=https%3A%2F%2Fwww.ft.com%2Fcontent%2Fblahblahblah&title=Title%20Title%20Title%20Title&source=Financial+Times"
rel="noopener"
>
LinkedIn
Expand Down
15 changes: 5 additions & 10 deletions components/x-gift-article/src/Buttons.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,17 @@ export default ({
shareType,
isGiftUrlCreated,
mailtoUrl,
createGiftUrl,
copyGiftUrl,
copyNonGiftUrl,
emailGiftUrl,
emailNonGiftUrl,
showCopyButton,
showNativeShareButton,
shareByNativeShare
actions
}) => {

if (isGiftUrlCreated || shareType === ShareType.nonGift) {

if (showNativeShareButton) {
return (
<div className={ ButtonsClassName }>
<button className={ ButtonWithGapClassNames } type="button" onClick={ shareByNativeShare }>Share link</button>
<button className={ ButtonWithGapClassNames } type="button" onClick={ actions.shareByNativeShare }>Share link</button>
</div>
);
}
Expand All @@ -47,19 +42,19 @@ export default ({
<button
className={ ButtonWithGapClassNames }
type="button"
onClick={ shareType === ShareType.gift ? copyGiftUrl : copyNonGiftUrl }
onClick={ shareType === ShareType.gift ? actions.copyGiftUrl : actions.copyNonGiftUrl }
>
Copy link
</button>
}
<a className={ ButtonClassNames } href={ mailtoUrl } target="_blank" rel="noopener noreferrer" onClick={ shareType === ShareType.gift ? emailGiftUrl : emailNonGiftUrl }>Email link</a>
<a className={ ButtonClassNames } href={ mailtoUrl } target="_blank" rel="noopener noreferrer" onClick={ shareType === ShareType.gift ? actions.emailGiftUrl : actions.emailNonGiftUrl }>Email link</a>
</div>
);
}

return (
<div className={ ButtonsClassName }>
<button className={ ButtonClassNames } type="button" onClick={ createGiftUrl }>
<button className={ ButtonClassNames } type="button" onClick={ actions.createGiftUrl }>
Create gift link
</button>
</div>
Expand Down
8 changes: 4 additions & 4 deletions components/x-gift-article/src/Form.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ export default (props) => (
<Title title={ props.title }/>

{ !props.isFreeArticle && <RadioButtonsSection
shareType={ props.shareType }
showGiftUrlSection={ props.actions.showGiftUrlSection }
showNonGiftUrlSection={ props.actions.showNonGiftUrlSection }/>
shareType={ props.shareType }
showGiftUrlSection={ props.actions.showGiftUrlSection }
showNonGiftUrlSection={ props.actions.showNonGiftUrlSection }/>
}

<UrlSection {...props} />
Expand All @@ -30,7 +30,7 @@ export default (props) => (
{ props.showCopyConfirmation &&
<CopyConfirmation hideCopyConfirmation={ props.actions.hideCopyConfirmation }/> }

{ props.showShareButtons &&
{ props.showMobileShareLinks &&
<MobileShareButtons mobileShareLinks={ props.mobileShareLinks }/> }
</div>
);
132 changes: 74 additions & 58 deletions components/x-gift-article/src/GiftArticle.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ const isCopySupported = typeof document !== 'undefined'
&& document.queryCommandSupported('copy');

const withGiftFormActions = withActions(
props => {
initialProps => {
const api = new ApiClient({
protocol: props.apiProtocol,
domain: props.apiDomain
protocol: initialProps.apiProtocol,
domain: initialProps.apiDomain
});

return {
Expand All @@ -40,7 +40,7 @@ const withGiftFormActions = withActions(
},

async createGiftUrl() {
const { redemptionUrl, redemptionLimit } = await api.getGiftUrl(props.articleId);
const { redemptionUrl, redemptionLimit } = await api.getGiftUrl(initialProps.articleId);

if (redemptionUrl) {
const { url, isShortened } = await api.getShorterUrl(redemptionUrl);
Expand All @@ -53,27 +53,37 @@ const withGiftFormActions = withActions(
},

copyGiftUrl(event) {
const giftUrl = updaters.urls.gift;
copyToClipboard(event);
tracking.copyLink('giftLink', giftUrl);

return { showCopyConfirmation: true };
return state => {
const giftUrl = state.urls.gift;
tracking.copyLink('giftLink', giftUrl);

return { showCopyConfirmation: true };
};
},

copyNonGiftUrl(event) {
const nonGiftUrl = updaters.urls.nonGift;
copyToClipboard(event);
tracking.copyLink('nonGiftLink', nonGiftUrl);

return { showCopyConfirmation: true };
return state => {
const nonGiftUrl = state.urls.nonGift;
tracking.copyLink('nonGiftLink', nonGiftUrl);

return { showCopyConfirmation: true };
}
},

emailGiftUrl() {
tracking.emailLink('giftLink', updaters.urls.gift);
return state => {
tracking.emailLink('giftLink', state.urls.gift);
};
},

emailNonGiftUrl() {
tracking.emailLink('nonGiftLink', updaters.urls.nonGift);
return state => {
tracking.emailLink('nonGiftLink', state.urls.nonGift);
};
},

hideCopyConfirmation() {
Expand All @@ -84,60 +94,66 @@ const withGiftFormActions = withActions(
throw new Error(`shareByNativeShare should be implemented by x-gift-article's consumers`);
},

async activate() {
if (props.isFreeArticle) {
const { url, isShortened } = await api.getShorterUrl(updaters.urls.nonGift);

if (isShortened) {
return updaters.setShortenedNonGiftUrl(url);
}
} else {
const { giftCredits, monthlyAllowance, nextRenewalDate } = await api.getGiftArticleAllowance();
activate() {
return async state => {
if (initialProps.isFreeArticle) {
const { url, isShortened } = await api.getShorterUrl(state.urls.nonGift);

// avoid to use giftCredits >= 0 because it returns true when null and ""
if (giftCredits > 0 || giftCredits === 0) {
return updaters.setAllowance(giftCredits, monthlyAllowance, nextRenewalDate);
if (isShortened) {
return updaters.setShortenedNonGiftUrl(url)(state);
}
} else {
// TODO do something
const { giftCredits, monthlyAllowance, nextRenewalDate } = await api.getGiftArticleAllowance();

// avoid to use giftCredits >= 0 because it returns true when null and ""
if (giftCredits > 0 || giftCredits === 0) {
return updaters.setAllowance(giftCredits, monthlyAllowance, nextRenewalDate);
} else {
// TODO do something
}
}
}
}
}
},
props => ({
title: 'Share this article',
giftCredits: undefined,
monthlyAllowance: undefined,
showCopyButton: isCopySupported,
isGiftUrlCreated: false,
isGiftUrlShortened: false,
isNonGiftUrlShortened: false,

urls: {
dummy: 'https://on.ft.com/gift_link',
gift: undefined,
nonGift: `${props.articleUrl}?shareType=nongift`
},

mailtoUrls: {
gift: undefined,
nonGift: createMailtoUrl(props.articleTitle, `${props.articleUrl}?shareType=nongift`)
},

mobileShareLinks: props.showMobileShareLinks
? {
facebook: `http://www.facebook.com/sharer.php?u=${encodeURIComponent(props.articleUrl)}&t=${encodeURIComponent(props.articleTitle)}`,
twitter: `https://twitter.com/intent/tweet?url=${encodeURIComponent(props.articleUrl)}&text=${encodeURIComponent(props.articleTitle)}&via=financialtimes`,
linkedin: `http://www.linkedin.com/shareArticle?mini=true&url=${encodeURIComponent(props.articleUrl)}&title=${encodeURIComponent(props.articleTitle)}&source=Financial+Times`,
whatsapp: `whatsapp://send?text=${encodeURIComponent(props.articleTitle)}%20-%20${encodeURIComponent(props.articleUrl)}`
}
: undefined,
props => {
const initialState = {
title: 'Share this article',
giftCredits: undefined,
monthlyAllowance: undefined,
showCopyButton: isCopySupported,
isGiftUrlCreated: false,
isGiftUrlShortened: false,
isNonGiftUrlShortened: false,

urls: {
dummy: 'https://on.ft.com/gift_link',
gift: undefined,
nonGift: `${props.articleUrl}?shareType=nongift`
},

mailtoUrls: {
gift: undefined,
nonGift: createMailtoUrl(props.articleTitle, `${props.articleUrl}?shareType=nongift`)
},

mobileShareLinks: props.showMobileShareLinks
? {
facebook: `http://www.facebook.com/sharer.php?u=${encodeURIComponent(props.articleUrl)}&t=${encodeURIComponent(props.articleTitle)}`,
twitter: `https://twitter.com/intent/tweet?url=${encodeURIComponent(props.articleUrl)}&text=${encodeURIComponent(props.articleTitle)}&via=financialtimes`,
linkedin: `http://www.linkedin.com/shareArticle?mini=true&url=${encodeURIComponent(props.articleUrl)}&title=${encodeURIComponent(props.articleTitle)}&source=Financial+Times`,
whatsapp: `whatsapp://send?text=${encodeURIComponent(props.articleTitle)}%20-%20${encodeURIComponent(props.articleUrl)}`
}
: undefined
};

const expandedProps = Object.assign({}, props, initialState);
const sectionProps = props.isFreeArticle
? updaters.showNonGiftUrlSection(expandedProps)
: updaters.showGiftUrlSection(expandedProps);

...(props.isFreeArticle
? updaters.showNonGiftUrlSection(props)
: updaters.showGiftUrlSection(props)
),
})
return Object.assign(initialState, sectionProps);
}
);

const BaseGiftArticle = (props) => {
Expand Down
16 changes: 3 additions & 13 deletions components/x-gift-article/src/UrlSection.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,10 @@ export default ({
monthlyAllowance,
nextRenewalDateText,
mailtoUrl,
createGiftUrl,
copyGiftUrl,
copyNonGiftUrl,
emailGiftUrl,
emailNonGiftUrl,
redemptionLimit,
showCopyButton,
showNativeShareButton,
shareByNativeShare
actions
}) => {

const hideUrlShareElements = ( giftCredits === 0 && shareType === ShareType.gift );
Expand Down Expand Up @@ -61,15 +56,10 @@ export default ({
shareType,
isGiftUrlCreated,
mailtoUrl,
createGiftUrl,
copyGiftUrl,
copyNonGiftUrl,
emailGiftUrl,
emailNonGiftUrl,
showCopyButton,
showNativeShareButton,
shareByNativeShare,
}} />}
actions,
}} /> }

</div>
);
Expand Down
8 changes: 4 additions & 4 deletions components/x-gift-article/src/lib/updaters.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ export const showNonGiftUrlSection = (props) => ({
showCopyConfirmation: false
});

export const setGiftUrl = (url, redemptionLimit, isShortened) => {
const mailtoUrl = createMailtoUrl(this.articleTitle, url);
export const setGiftUrl = (url, redemptionLimit, isShortened) => props => {
const mailtoUrl = createMailtoUrl(props.articleTitle, url);

return props => ({
return {
url,
mailtoUrl,
redemptionLimit,
Expand All @@ -37,7 +37,7 @@ export const setGiftUrl = (url, redemptionLimit, isShortened) => {
mailtoUrls: Object.assign(props.mailtoUrls, {
gift: mailtoUrl,
})
});
};
};

export const setAllowance = (giftCredits, monthlyAllowance, nextRenewalDate) => {
Expand Down

0 comments on commit 2db67a7

Please sign in to comment.