diff --git a/src/components/ViewContainer/ApiExtensionView/utils/mapGatewayName.test.ts b/src/components/ViewContainer/ApiExtensionView/utils/mapGatewayName.test.ts index 9a0d2586..2f23db58 100644 --- a/src/components/ViewContainer/ApiExtensionView/utils/mapGatewayName.test.ts +++ b/src/components/ViewContainer/ApiExtensionView/utils/mapGatewayName.test.ts @@ -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'); }); }); diff --git a/src/components/ViewContainer/ApiExtensionView/utils/mapGatewayName.ts b/src/components/ViewContainer/ApiExtensionView/utils/mapGatewayName.ts index 6566c232..00086e52 100644 --- a/src/components/ViewContainer/ApiExtensionView/utils/mapGatewayName.ts +++ b/src/components/ViewContainer/ApiExtensionView/utils/mapGatewayName.ts @@ -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; +};