Skip to content

Commit

Permalink
fix(customer): CHECKOUT-4712 Do not send consent if not required
Browse files Browse the repository at this point in the history
  • Loading branch information
Luis Sanchez committed Feb 27, 2020
1 parent d416c9f commit 1d9fcb5
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 5 deletions.
33 changes: 30 additions & 3 deletions src/app/customer/Customer.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ import { getStoreConfig } from '../config/config.mock';
import { createLocaleContext, LocaleContext, LocaleContextType } from '../locale';

import { getGuestCustomer } from './customers.mock';
import Customer, { CustomerProps } from './Customer';
import Customer, { CustomerProps, WithCheckoutCustomerProps } from './Customer';
import CustomerViewType from './CustomerViewType';
import GuestForm, { GuestFormProps } from './GuestForm';
import LoginForm, { LoginFormProps } from './LoginForm';

describe('Customer', () => {
let CustomerTest: FunctionComponent<CustomerProps>;
let CustomerTest: FunctionComponent<CustomerProps & Partial<WithCheckoutCustomerProps>>;
let billingAddress: BillingAddress;
let checkout: Checkout;
let checkoutService: CheckoutService;
Expand Down Expand Up @@ -108,13 +108,40 @@ describe('Customer', () => {
});
});

it('continues checkout as guest and subscribes to newsletter when "continue as guest" event is received', () => {
it('continues checkout as guest and does not send consent if not required', () => {
jest.spyOn(checkoutService, 'continueAsGuest')
.mockReturnValue(Promise.resolve(checkoutService.getState()));

const subscribeToNewsletter = jest.fn();
const component = mount(
<CustomerTest
requiresMarketingConsent={ false }
subscribeToNewsletter={ subscribeToNewsletter }
viewType={ CustomerViewType.Guest }
/>
);

(component.find(GuestForm) as ReactWrapper<GuestFormProps>)
.prop('onContinueAsGuest')({
email: ' test@bigcommerce.com ',
shouldSubscribe: true,
});

expect(checkoutService.continueAsGuest)
.toHaveBeenCalledWith({
email: 'test@bigcommerce.com',
marketingEmailConsent: undefined,
});
});

it('continues checkout as guest, subscribes to newsletter and sends marketing consent', () => {
jest.spyOn(checkoutService, 'continueAsGuest')
.mockReturnValue(Promise.resolve(checkoutService.getState()));

const subscribeToNewsletter = jest.fn();
const component = mount(
<CustomerTest
requiresMarketingConsent={ true }
subscribeToNewsletter={ subscribeToNewsletter }
viewType={ CustomerViewType.Guest }
/>
Expand Down
5 changes: 3 additions & 2 deletions src/app/customer/Customer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export interface CustomerProps {
subscribeToNewsletter?(data: { email: string; firstName?: string }): void;
}

interface WithCheckoutCustomerProps {
export interface WithCheckoutCustomerProps {
canSubscribe: boolean;
checkoutButtonIds: string[];
createAccountUrl: string;
Expand Down Expand Up @@ -135,6 +135,7 @@ class Customer extends Component<CustomerProps & WithCheckoutCustomerProps> {
onContinueAsGuest = noop,
onContinueAsGuestError = noop,
subscribeToNewsletter = noop,
requiresMarketingConsent,
} = this.props;

const email = formValues.email.trim();
Expand All @@ -146,7 +147,7 @@ class Customer extends Component<CustomerProps & WithCheckoutCustomerProps> {
try {
await continueAsGuest({
email,
marketingEmailConsent: formValues.shouldSubscribe ? true : undefined,
marketingEmailConsent: requiresMarketingConsent && formValues.shouldSubscribe ? true : undefined,
});
onContinueAsGuest();

Expand Down

0 comments on commit 1d9fcb5

Please sign in to comment.