Skip to content

Commit

Permalink
feat(payment): CHECKOUT-4223 Add GiftCertificate components
Browse files Browse the repository at this point in the history
  • Loading branch information
Luis Sanchez committed Aug 8, 2019
1 parent cd75c64 commit 35d3214
Show file tree
Hide file tree
Showing 7 changed files with 142 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/app/giftCertificate/AppliedGiftCertificate.spec.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { shallow, ShallowWrapper } from 'enzyme';
import React from 'react';

import { getGiftCertificate } from './giftCertificate.mock';
import AppliedGiftCertificate from './AppliedGiftCertificate';

describe('AppliedGiftCertificate', () => {
let component: ShallowWrapper;

beforeEach(() => {
component = shallow(<AppliedGiftCertificate giftCertificate={ getGiftCertificate() } />);
});

it('renders markup that matches snapshot', () => {
expect(component).toMatchSnapshot();
});
});
36 changes: 36 additions & 0 deletions src/app/giftCertificate/AppliedGiftCertificate.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { GiftCertificate } from '@bigcommerce/checkout-sdk';
import React, { FunctionComponent } from 'react';

import { ShopperCurrency } from '../currency';
import { TranslatedString } from '../language';

export interface AppliedGiftCertificateProps {
giftCertificate: GiftCertificate;
}

const AppliedGiftCertificate: FunctionComponent<AppliedGiftCertificateProps> = ({ giftCertificate }) => (
<div
className="redeemable-column redeemable-info"
data-test="redeemable-item--giftCertificate"
>
<span className="redeemable-info-header">
<span className="redeemable-info-header--highlight" data-test="giftCertificate-amount">
<ShopperCurrency amount={ giftCertificate.used } />
</span>
<TranslatedString id="redeemable.gift_certificate_text" />
</span>
<span className="redeemable-info-subHeader">
{ giftCertificate.remaining > 0 && <span className="redeemable-info-subHeader--remaining">
<TranslatedString id="redeemable.gift_certificate_remaining_text" />
<span data-test="giftCertificate-remaining">
<ShopperCurrency amount={ giftCertificate.remaining } />
</span>
</span> }
<span data-test="giftCertificate-code">
{ giftCertificate.code }
</span>
</span>
</div>
);

export default AppliedGiftCertificate;
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`AppliedGiftCertificate renders markup that matches snapshot 1`] = `
<div
className="redeemable-column redeemable-info"
data-test="redeemable-item--giftCertificate"
>
<span
className="redeemable-info-header"
>
<span
className="redeemable-info-header--highlight"
data-test="giftCertificate-amount"
>
<WithCurrency(ShopperCurrency)
amount={80}
/>
</span>
<WithLanguage(TranslatedString)
id="redeemable.gift_certificate_text"
/>
</span>
<span
className="redeemable-info-subHeader"
>
<span
className="redeemable-info-subHeader--remaining"
>
<WithLanguage(TranslatedString)
id="redeemable.gift_certificate_remaining_text"
/>
<span
data-test="giftCertificate-remaining"
>
<WithCurrency(ShopperCurrency)
amount={20}
/>
</span>
</span>
<span
data-test="giftCertificate-code"
>
savebig2015
</span>
</span>
</div>
`;
3 changes: 3 additions & 0 deletions src/app/giftCertificate/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export { default as AppliedGiftCertificate } from './AppliedGiftCertificate';
export { default as mapFromPayments } from './mapFromPayments';
export { default as isGiftCertificatePayment } from './isGiftCertificatePayment';
6 changes: 6 additions & 0 deletions src/app/giftCertificate/isGiftCertificatePayment.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { CheckoutPayment, GiftCertificateOrderPayment, OrderPayment } from '@bigcommerce/checkout-sdk';

export default function isGiftCertificatePayment(payment: OrderPayment | CheckoutPayment):
payment is GiftCertificateOrderPayment {
return payment.providerId === 'giftcertificate';
}
18 changes: 18 additions & 0 deletions src/app/giftCertificate/mapFromPayments.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { getOrder } from '../order/orders.mock';

import mapFromPayments from './mapFromPayments';

describe('mapFromPayments()', () => {
it('returns mapped gift certificates', () => {
expect(mapFromPayments(getOrder().payments || []))
.toEqual([
{
code: 'gc',
remaining: 3,
used: 7,
balance: 10,
purchaseDate: '',
},
]);
});
});
15 changes: 15 additions & 0 deletions src/app/giftCertificate/mapFromPayments.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { GiftCertificate, OrderPayments } from '@bigcommerce/checkout-sdk';

import isGiftCertificatePayment from './isGiftCertificatePayment';

export default function mapFromPayments(payments: OrderPayments): GiftCertificate[] {
return payments
.filter(isGiftCertificatePayment)
.map(({ amount, detail }) => ({
code: detail.code,
remaining: detail.remaining,
used: amount,
balance: amount + detail.remaining,
purchaseDate: '',
}));
}

0 comments on commit 35d3214

Please sign in to comment.