-
Notifications
You must be signed in to change notification settings - Fork 358
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(customer): CHECKOUT-4641 Add Privacy Policy feature
- Loading branch information
Luis Sanchez
authored
Feb 7, 2020
1 parent
676028e
commit 525fc4b
Showing
18 changed files
with
301 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import parseAnchor from './parseAnchor'; | ||
|
||
describe('parseAnchor()', () => { | ||
it('returns empty prefix and suffix if there is just an anchor element', () => { | ||
expect(parseAnchor('<a>text</a>')) | ||
.toEqual(['', 'text', '']); | ||
|
||
expect(parseAnchor('<a href="something">text</a>')) | ||
.toEqual(['', 'text', '']); | ||
}); | ||
|
||
it('returns prefix and suffix if anchor is surrounded by text', () => { | ||
expect(parseAnchor('foo <a>text</a> bar')) | ||
.toEqual(['foo ', 'text', ' bar']); | ||
|
||
expect(parseAnchor('foo <a href="something">text</a> bar')) | ||
.toEqual(['foo ', 'text', ' bar']); | ||
}); | ||
|
||
it('returns first anchor if theres more than one', () => { | ||
expect(parseAnchor('foo <a>text</a> s <a>x</a> bar')) | ||
.toEqual(['foo ', 'text', ' s <a>x</a> bar']); | ||
}); | ||
|
||
it('returns empty array if no anchor', () => { | ||
expect(parseAnchor('foo text bar')) | ||
.toEqual([]); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
export default function parseAnchor(text: string): string[] { | ||
const div = document.createElement('div'); | ||
div.innerHTML = text; | ||
|
||
const anchor = div.querySelector('a'); | ||
|
||
if (!anchor) { | ||
return []; | ||
} | ||
|
||
const anchorSiblings = div.innerHTML.split(anchor.outerHTML); | ||
|
||
return [ anchorSiblings[0], anchor.text, anchorSiblings[1] ]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { mount } from 'enzyme'; | ||
import { Formik } from 'formik'; | ||
import { noop } from 'lodash'; | ||
import React from 'react'; | ||
|
||
import { getStoreConfig } from '../config/config.mock'; | ||
import { createLocaleContext, LocaleContext, LocaleContextType, TranslatedHtml } from '../locale'; | ||
import { CheckboxFormField } from '../ui/form'; | ||
|
||
import PrivacyPolicyField from './PrivacyPolicyField'; | ||
|
||
describe('PrivacyPolicyField', () => { | ||
let localeContext: LocaleContextType; | ||
let initialValues: { terms: boolean }; | ||
|
||
beforeEach(() => { | ||
initialValues = { terms: false }; | ||
localeContext = createLocaleContext(getStoreConfig()); | ||
}); | ||
|
||
it('renders checkbox with external link', () => { | ||
const component = mount( | ||
<LocaleContext.Provider value={ localeContext }> | ||
<Formik | ||
initialValues={ initialValues } | ||
onSubmit={ noop } | ||
> | ||
<PrivacyPolicyField url="foo" /> | ||
</Formik> | ||
</LocaleContext.Provider> | ||
); | ||
|
||
expect(component.find(CheckboxFormField)).toHaveLength(1); | ||
expect(component.find(TranslatedHtml).props()).toMatchObject({ | ||
data: { url: 'foo' }, | ||
id: 'privacy_policy.label', | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import React, { memo, FunctionComponent } from 'react'; | ||
|
||
import { TranslatedHtml } from '../locale'; | ||
import { CheckboxFormField, Fieldset } from '../ui/form'; | ||
|
||
const PrivacyPolicyCheckboxFieldLink: FunctionComponent<{ url: string }> = ({ | ||
url, | ||
}) => ( | ||
<CheckboxFormField | ||
labelContent={ <TranslatedHtml data={ { url } } id="privacy_policy.label" /> } | ||
name="privacyPolicy" | ||
/> | ||
); | ||
|
||
const PrivacyPolicyFieldset: FunctionComponent<{ url: string }> = ({ | ||
url, | ||
}) => ( | ||
<Fieldset additionalClassName="checkout-privacy-policy"> | ||
<PrivacyPolicyCheckboxFieldLink url={ url } /> | ||
</Fieldset> | ||
); | ||
|
||
export default memo(PrivacyPolicyFieldset); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { LanguageService } from '@bigcommerce/checkout-sdk'; | ||
import { boolean, object, BooleanSchema, ObjectSchema } from 'yup'; | ||
|
||
export interface PrivacyPolicyValidatonSchemaProps { | ||
isRequired: boolean; | ||
language: LanguageService; | ||
} | ||
|
||
export default function getPrivacyPolicyValidationSchema({ | ||
isRequired, | ||
language, | ||
}: PrivacyPolicyValidatonSchemaProps): ObjectSchema<{ privacyPolicy?: boolean }> { | ||
const schemaFields: { | ||
privacyPolicy?: BooleanSchema; | ||
} = {}; | ||
|
||
if (isRequired) { | ||
schemaFields.privacyPolicy = boolean() | ||
.oneOf([true], language.translate('privacy_policy.required_error')); | ||
} | ||
|
||
return object(schemaFields); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export { default as PrivacyPolicyField } from './PrivacyPolicyField'; | ||
export { default as getPrivacyPolicyValidationSchema } from './getPrivacyPolicyValidationSchema'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.