Fix gateway mapping (#332)

This commit is contained in:
Ildar Galeev 2024-08-26 18:52:53 +07:00 committed by GitHub
parent 6610d76ff9
commit ba5f948784
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 46 additions and 29 deletions

View File

@ -1,42 +1,57 @@
import { Locale } from 'checkout/contexts';
import { mapGatewayName } from './mapGatewayName';
describe('mapGatewayName', () => {
it('should return the gateway name if locale path is nil', () => {
const gatewayName = 'PayPal';
const locale: Locale = {} as Locale;
let locale;
expect(mapGatewayName(gatewayName, locale)).toBe(gatewayName);
beforeEach(() => {
locale = {
'p2p.gateways': {
gatewayA: 'Localized Gateway A',
gatewayB: 'Localized Gateway B',
gatewayC: 'Localized Gateway C',
},
};
});
it('should return the gateway name if locale path does not contain the gateway name', () => {
const gatewayName = 'PayPal';
const locale: Locale = { p2p: { gateways: {} } } as Locale;
expect(mapGatewayName(gatewayName, locale)).toBe(gatewayName);
test('should return the localized gateway name when it exists in the locale', () => {
expect(mapGatewayName('gatewayA', locale)).toBe('Localized Gateway A');
expect(mapGatewayName('gatewayB', locale)).toBe('Localized Gateway B');
expect(mapGatewayName('gatewayC', locale)).toBe('Localized Gateway C');
});
it('should return the mapped gateway name if locale path contains the gateway name', () => {
const gatewayName = 'PayPal';
const mappedGatewayName = 'PayPal Localized';
const locale: Locale = { p2p: { gateways: { paypal: mappedGatewayName } } } as Locale;
expect(mapGatewayName(gatewayName, locale)).toBe(mappedGatewayName);
test('should return the original gateway name when it does not exist in the locale', () => {
expect(mapGatewayName('unknownGateway', locale)).toBe('unknownGateway');
});
it('should handle gateway names case insensitively', () => {
const gatewayName = 'PAYPAL';
const mappedGatewayName = 'PayPal Localized';
const locale: Locale = { p2p: { gateways: { paypal: mappedGatewayName } } } as Locale;
expect(mapGatewayName(gatewayName, locale)).toBe(mappedGatewayName);
test('should return the original gateway name when the locale does not contain the "p2p.gateways" key', () => {
const emptyLocale = {};
expect(mapGatewayName('gatewayA', emptyLocale)).toBe('gatewayA');
});
it('should return the original gateway name if locale.p2p is nil', () => {
const gatewayName = 'PayPal';
const locale: Locale = { p2p: undefined } as Locale;
test('should return the original gateway name when the "p2p.gateways" key is an empty object', () => {
const emptyGatewaysLocale = {
'p2p.gateways': {},
};
expect(mapGatewayName('gatewayA', emptyGatewaysLocale)).toBe('gatewayA');
});
expect(mapGatewayName(gatewayName, locale)).toBe(gatewayName);
test('should return the original gateway name when locale is null or undefined', () => {
expect(mapGatewayName('gatewayA', null)).toBe('gatewayA');
expect(mapGatewayName('gatewayA', undefined)).toBe('gatewayA');
});
test('should return the original gateway name when gatewayName is an empty string', () => {
expect(mapGatewayName('', locale)).toBe('');
});
test('should handle locale with other unrelated keys', () => {
const complexLocale = {
'p2p.gateways': {
gatewayA: 'Localized Gateway A',
},
'other.key': 'some value',
};
expect(mapGatewayName('gatewayA', complexLocale)).toBe('Localized Gateway A');
expect(mapGatewayName('gatewayB', complexLocale)).toBe('gatewayB');
});
});

View File

@ -1,4 +1,6 @@
import { Locale } from 'checkout/contexts';
export const mapGatewayName = (gatewayName: string, { p2p: { gateways = {} } = {} }: Locale): string =>
gateways[gatewayName.toLowerCase()] ?? gatewayName;
export const mapGatewayName = (gatewayName: string, l: Locale): string => {
const gateways = l?.['p2p.gateways'] ?? {};
return gateways[gatewayName] ?? gatewayName;
};