-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(PAYMENTS-15245): add legal component
- Loading branch information
a.kornienko
authored and
a.kornienko
committed
Jul 17, 2023
1 parent
5f8ead8
commit b295418
Showing
39 changed files
with
975 additions
and
129 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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 @@ | ||
declare module '*.svg'; |
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,16 @@ | ||
import { Message } from '../../core/message.interface'; | ||
import { isEventMessage } from './event-message.guard'; | ||
import { EventName } from '../../core/event-name.enum'; | ||
import { LegalComponentConfig } from '../../features/headless-checkout/web-components/legal/legal-component.config.interface'; | ||
|
||
export const isLegalConfigEventMessage = ( | ||
messageData: unknown | ||
): messageData is Message<{ config: LegalComponentConfig }> => { | ||
if (isEventMessage(messageData)) { | ||
return ( | ||
messageData.name === EventName.getLegalComponentConfig && | ||
(messageData.data as { [key: string]: unknown })?.config !== undefined | ||
); | ||
} | ||
return false; | ||
}; |
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
22 changes: 22 additions & 0 deletions
22
...tures/headless-checkout/post-messages-handlers/get-legal-component-config.handler.spec.ts
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,22 @@ | ||
import { EventName } from '../../../core/event-name.enum'; | ||
import { Message } from '../../../core/message.interface'; | ||
import { getLegalComponentConfigHandler } from './get-legal-component-config.handler'; | ||
import { LegalComponentConfig } from '../web-components/legal/legal-component.config.interface'; | ||
|
||
const mockMessage: Message<{ config: LegalComponentConfig }> = { | ||
name: EventName.getLegalComponentConfig, | ||
data: { config: {} as unknown as LegalComponentConfig }, | ||
}; | ||
describe('getLegalComponentConfigHandler', () => { | ||
it('Should handle data', () => { | ||
expect(getLegalComponentConfigHandler(mockMessage)).toEqual({ | ||
isHandled: true, | ||
value: {} as unknown as LegalComponentConfig, | ||
}); | ||
}); | ||
it('Should return null', () => { | ||
expect( | ||
getLegalComponentConfigHandler({ name: EventName.initPayment }) | ||
).toBeNull(); | ||
}); | ||
}); |
21 changes: 21 additions & 0 deletions
21
src/features/headless-checkout/post-messages-handlers/get-legal-component-config.handler.ts
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,21 @@ | ||
import { Handler } from '../../../core/post-messages-client/handler.type'; | ||
import { Message } from '../../../core/message.interface'; | ||
import { EventName } from '../../../core/event-name.enum'; | ||
import { LegalComponentConfig } from '../web-components/legal/legal-component.config.interface'; | ||
import { isLegalConfigEventMessage } from '../../../core/guards/legal-config-event-message.guard'; | ||
|
||
export const getLegalComponentConfigHandler: Handler<LegalComponentConfig> = ( | ||
message: Message | ||
): { isHandled: boolean; value: LegalComponentConfig } | null => { | ||
if ( | ||
isLegalConfigEventMessage(message) && | ||
message.name === EventName.getLegalComponentConfig | ||
) { | ||
const config = message.data?.config; | ||
return { | ||
isHandled: true, | ||
value: config!, | ||
}; | ||
} | ||
return null; | ||
}; |
10 changes: 10 additions & 0 deletions
10
src/features/headless-checkout/web-components/legal/legal-component.config.interface.ts
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,10 @@ | ||
export interface LegalComponentConfig { | ||
isJapanUser: boolean; | ||
refundPolicyUrl: string; | ||
sctlPolicyUrl?: string; | ||
secureConnection: { | ||
secureConnectionUrl?: string; | ||
isWhiteLabel?: boolean; | ||
}; | ||
disclaimer?: string; | ||
} |
167 changes: 167 additions & 0 deletions
167
src/features/headless-checkout/web-components/legal/legal.component.spec.ts
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,167 @@ | ||
import { container } from 'tsyringe'; | ||
import { WebComponentTagName } from '../../../../core/web-components/web-component-tag-name.enum'; | ||
import { HeadlessCheckoutSpy } from '../../../../core/headless-checkout-spy/headless-checkout-spy'; | ||
import { noopStub } from '../../../../tests/stubs/noop.stub'; | ||
import { HeadlessCheckout } from '../../headless-checkout'; | ||
import { LegalComponent } from './legal.component'; | ||
import { PostMessagesClient } from '../../../../core/post-messages-client/post-messages-client'; | ||
import { EventName } from '../../../../core/event-name.enum'; | ||
import { LegalComponentConfig } from './legal-component.config.interface'; | ||
|
||
function createComponent(): void { | ||
const element = document.createElement(WebComponentTagName.LegalComponent); | ||
element.setAttribute('id', 'test'); | ||
(document.getElementById('container')! as HTMLElement).appendChild(element); | ||
} | ||
|
||
const mockConfig: LegalComponentConfig = { | ||
isJapanUser: false, | ||
refundPolicyUrl: 'refundPolicyUrl', | ||
secureConnection: { | ||
secureConnectionUrl: 'secureConnectionUrl', | ||
}, | ||
}; | ||
|
||
const delay = async (): Promise<boolean> => | ||
new Promise((resolve) => { | ||
setTimeout(() => { | ||
resolve(true); | ||
}); | ||
}); | ||
|
||
describe('LegalComponent', () => { | ||
let headlessCheckout: HeadlessCheckout; | ||
let headlessCheckoutSpy: HeadlessCheckoutSpy; | ||
let postMessagesClient: PostMessagesClient; | ||
let windowService: Window; | ||
|
||
window.customElements.define( | ||
WebComponentTagName.LegalComponent, | ||
LegalComponent | ||
); | ||
|
||
beforeEach(() => { | ||
document.body.innerHTML = '<div id="container"></div>'; | ||
|
||
headlessCheckout = { | ||
getRegularMethods: noopStub, | ||
} as unknown as HeadlessCheckout; | ||
|
||
headlessCheckoutSpy = { | ||
listenAppInit: noopStub, | ||
get appWasInit() { | ||
return; | ||
}, | ||
} as unknown as HeadlessCheckoutSpy; | ||
|
||
postMessagesClient = { | ||
send: noopStub, | ||
} as unknown as PostMessagesClient; | ||
|
||
windowService = window; | ||
|
||
container | ||
.register<HeadlessCheckoutSpy>(HeadlessCheckoutSpy, { | ||
useValue: headlessCheckoutSpy, | ||
}) | ||
.register<HeadlessCheckout>(HeadlessCheckout, { | ||
useValue: headlessCheckout, | ||
}) | ||
.register<PostMessagesClient>(PostMessagesClient, { | ||
useValue: postMessagesClient, | ||
}) | ||
.register<Window>(Window, { useValue: windowService }); | ||
}); | ||
|
||
afterEach(() => { | ||
document.body.innerHTML = ''; | ||
}); | ||
|
||
it('Should create component', () => { | ||
createComponent(); | ||
expect( | ||
document.querySelector(WebComponentTagName.LegalComponent) | ||
).toBeDefined(); | ||
}); | ||
|
||
it('Should load legal component config', () => { | ||
const spy = spyOn(postMessagesClient, 'send').and.returnValue( | ||
Promise.resolve({}) | ||
); | ||
spyOnProperty(headlessCheckoutSpy, 'appWasInit', 'get').and.returnValue( | ||
true | ||
); | ||
createComponent(); | ||
expect(spy).toHaveBeenCalled(); | ||
}); | ||
|
||
it('Should load legal component config after init', () => { | ||
const spy = spyOn(postMessagesClient, 'send').and.returnValue( | ||
Promise.resolve({}) | ||
); | ||
const appWasInitSpy = spyOnProperty( | ||
headlessCheckoutSpy, | ||
'appWasInit', | ||
'get' | ||
); | ||
const listenAppInitSpy = spyOn(headlessCheckoutSpy, 'listenAppInit'); | ||
listenAppInitSpy.and.callFake((callback: () => void) => { | ||
appWasInitSpy.and.returnValue(true); | ||
callback(); | ||
}); | ||
appWasInitSpy.and.returnValue(false); | ||
createComponent(); | ||
expect(spy).toHaveBeenCalled(); | ||
}); | ||
|
||
it('Should call addEventListener', async () => { | ||
spyOn(postMessagesClient, 'send').and.returnValue( | ||
Promise.resolve(mockConfig) | ||
); | ||
spyOnProperty(headlessCheckoutSpy, 'appWasInit', 'get').and.returnValue( | ||
true | ||
); | ||
const spy = spyOn(windowService, 'addEventListener'); | ||
createComponent(); | ||
await delay(); | ||
expect(spy).toHaveBeenCalled(); | ||
}); | ||
|
||
it('Should not call addEventListener', async () => { | ||
spyOn(postMessagesClient, 'send').and.returnValue(Promise.resolve(null)); | ||
spyOnProperty(headlessCheckoutSpy, 'appWasInit', 'get').and.returnValue( | ||
true | ||
); | ||
const spy = spyOn(windowService, 'addEventListener'); | ||
createComponent(); | ||
await delay(); | ||
expect(spy).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it('Should send pong message', async () => { | ||
spyOn(postMessagesClient, 'send').and.returnValue( | ||
Promise.resolve(mockConfig) | ||
); | ||
spyOnProperty(headlessCheckoutSpy, 'appWasInit', 'get').and.returnValue( | ||
true | ||
); | ||
spyOn(windowService, 'addEventListener').and.callFake( | ||
(name: string, callback: (event: unknown) => void) => { | ||
const messageEvent = { | ||
data: JSON.stringify({ | ||
name: EventName.legalComponentPing, | ||
}), | ||
origin: '', | ||
source: { | ||
postMessage: noopStub, | ||
}, | ||
}; | ||
const spy = spyOn(messageEvent.source, 'postMessage'); | ||
callback(messageEvent); | ||
expect(spy).toHaveBeenCalled(); | ||
} | ||
); | ||
createComponent(); | ||
await delay(); | ||
}); | ||
}); |
78 changes: 78 additions & 0 deletions
78
src/features/headless-checkout/web-components/legal/legal.component.tempate.ts
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,78 @@ | ||
import { LegalComponentConfig } from './legal-component.config.interface'; | ||
import { getSecureConnectionTemplate } from './secure-connection.component.template'; | ||
import i18next from 'i18next'; | ||
|
||
export const getLegalComponentTemplate = ( | ||
config: LegalComponentConfig | ||
): string => { | ||
const { | ||
isJapanUser, | ||
refundPolicyUrl, | ||
sctlPolicyUrl, | ||
secureConnection, | ||
disclaimer, | ||
} = config; | ||
return ` | ||
${ | ||
disclaimer | ||
? ` | ||
<div class="disclaimer"> | ||
${i18next.t('disclaimer')} | ||
</div>` | ||
: '' | ||
} | ||
${getSecureConnectionTemplate(secureConnection)} | ||
<div class="legal-links"> | ||
<a | ||
class="link link-legal" | ||
href="https://xsolla.com/legal-agreements" | ||
target="_blank" | ||
> | ||
${i18next.t('legal')} | ||
</a> | ||
<div class="divider"></div> | ||
<a | ||
class="link link-legal" | ||
href="https://xsolla.com/cookie" | ||
target="_blank" | ||
> | ||
${i18next.t('cookie-policy')} | ||
</a> | ||
<div class="divider"></div> | ||
<a | ||
class="link link-legal" | ||
href="https://xsolla.com/privacypolicy" | ||
target="_blank" | ||
> | ||
${i18next.t('privacy-policy')} | ||
</a> | ||
<div class="divider"></div> | ||
<a | ||
class="link link-refund" | ||
href="${refundPolicyUrl}" | ||
target="_blank" | ||
> | ||
${i18next.t('refund-policy')} | ||
</a> | ||
${ | ||
isJapanUser && sctlPolicyUrl | ||
? ` | ||
<div class="divider"></div> | ||
<a | ||
class="link sctl-link" | ||
href="${sctlPolicyUrl}" | ||
target="_blank" | ||
> | ||
${i18next.t('sctl-indications')} | ||
</a>` | ||
: '' | ||
} | ||
</div>`; | ||
}; |
Oops, something went wrong.