-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(message overrides): extract message content with Platform-Specifi…
…c Overrides (#12917) * extract message content with Platform-Specific Overrides * added default platform when getClientInfo returns an undefined platform * mocking getClientInfo shouldn't override mock utils * moved logic to extractContent fn, updated tests * exposed internal type InAppMessageButton * added empty line * return immediately if os is falsy * fixed lint warning * pinpoint internal type exports, mergeOverride test util, simplified data object * removed unnecessary spread operator * refactored merging fn * prettier * flatten test structure * minor update * minor update * updated merge override fns signature and associated tests * extracted getButtonConfig fn and refactored to add data existence checks. * removed unnecessary optional chaining operators * fixed lint issues --------- Co-authored-by: ManojNB <[email protected]>
- Loading branch information
1 parent
1625424
commit cb91437
Showing
8 changed files
with
342 additions
and
9 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
73 changes: 73 additions & 0 deletions
73
...es/notifications/__tests__/inAppMessaging/providers/pinpoint/utils/helpers.native.test.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,73 @@ | ||
/** | ||
* @jest-environment node | ||
*/ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import { | ||
extractContent, | ||
mapOSPlatform, | ||
} from '../../../../../src/inAppMessaging/providers/pinpoint/utils/helpers'; | ||
|
||
import { | ||
nonBrowserConfigTestCases, | ||
pinpointInAppMessage, | ||
extractedContent, | ||
nativeButtonOverrides, | ||
} from '../../../../testUtils/data'; | ||
import { mergeExpectedContentWithExpectedOverride, mergeInAppMessageWithOverrides } from '../../../../testUtils/mergeInAppMessageWithOverrides'; | ||
|
||
jest.mock('@aws-amplify/core'); | ||
|
||
jest.mock('@aws-amplify/core/internals/utils', () => { | ||
const originalModule = jest.requireActual( | ||
'@aws-amplify/core/internals/utils', | ||
); | ||
return { | ||
...originalModule, | ||
getClientInfo: jest.fn(), // Setup as a Jest mock function without implementation | ||
}; | ||
}); | ||
|
||
describe('InAppMessaging Provider Utils (running natively)', () => { | ||
describe('mapOSPlatform method', () => { | ||
nonBrowserConfigTestCases.forEach(({ os, expectedPlatform }) => { | ||
test(`correctly maps OS "${os}" to ConfigPlatformType "${expectedPlatform}"`, () => { | ||
const result = mapOSPlatform(os); | ||
expect(result).toBe(expectedPlatform); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('extractContent with overrides', () => { | ||
nativeButtonOverrides.forEach( | ||
({ buttonOverrides, configPlatform, mappedPlatform }) => { | ||
const message = mergeInAppMessageWithOverrides( | ||
pinpointInAppMessage, | ||
mappedPlatform, | ||
buttonOverrides, | ||
); | ||
const expectedContent = mergeExpectedContentWithExpectedOverride( | ||
extractedContent[0], | ||
buttonOverrides, | ||
); | ||
|
||
test(`correctly extracts content for ${configPlatform}`, () => { | ||
const utils = require('@aws-amplify/core/internals/utils'); | ||
// Dynamically override the mock for getClientInfo | ||
utils.getClientInfo.mockImplementation(() => ({ | ||
platform: configPlatform, | ||
})); | ||
|
||
const [firstContent] = extractContent(message); | ||
expect(firstContent.primaryButton).toStrictEqual( | ||
expectedContent.primaryButton, | ||
); | ||
expect(firstContent.secondaryButton).toStrictEqual( | ||
expectedContent.secondaryButton, | ||
); | ||
}); | ||
}, | ||
); | ||
}); | ||
}); |
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
59 changes: 59 additions & 0 deletions
59
packages/notifications/__tests__/testUtils/mergeInAppMessageWithOverrides.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,59 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import { cloneDeep } from 'lodash'; | ||
import { | ||
InAppMessageCampaign, | ||
OverrideButtonConfiguration, | ||
} from '@aws-amplify/core/internals/aws-clients/pinpoint'; | ||
import { | ||
ButtonConfigPlatform, | ||
InAppMessageButton, | ||
InAppMessageContent, | ||
} from '../../src/inAppMessaging/types/message'; | ||
|
||
export const mergeInAppMessageWithOverrides = ( | ||
pinpointInAppMessage: InAppMessageCampaign, | ||
mappedPlatform: ButtonConfigPlatform, | ||
buttonOverrides?: { | ||
primaryButton: OverrideButtonConfiguration; | ||
secondaryButton: OverrideButtonConfiguration; | ||
}, | ||
): InAppMessageCampaign => { | ||
const message = cloneDeep(pinpointInAppMessage); | ||
if (message?.InAppMessage?.Content) { | ||
message.InAppMessage.Content[0] = { | ||
...message.InAppMessage.Content[0], | ||
PrimaryBtn: { | ||
...message.InAppMessage.Content[0].PrimaryBtn, | ||
[mappedPlatform]: buttonOverrides?.primaryButton, | ||
}, | ||
SecondaryBtn: { | ||
...message.InAppMessage.Content[0].SecondaryBtn, | ||
[mappedPlatform]: buttonOverrides?.secondaryButton, | ||
}, | ||
}; | ||
} | ||
return message; | ||
}; | ||
|
||
export const mergeExpectedContentWithExpectedOverride = ( | ||
inAppMessage: InAppMessageContent, | ||
expectedButtonConfig: { | ||
primaryButton: OverrideButtonConfiguration; | ||
secondaryButton: OverrideButtonConfiguration; | ||
}, | ||
): InAppMessageContent => { | ||
let expectedContent = cloneDeep(inAppMessage); | ||
expectedContent.primaryButton = { | ||
...expectedContent.primaryButton, | ||
action: expectedButtonConfig.primaryButton.ButtonAction, | ||
url: expectedButtonConfig.primaryButton.Link, | ||
} as InAppMessageButton; | ||
expectedContent.secondaryButton = { | ||
...expectedContent.secondaryButton, | ||
action: expectedButtonConfig.secondaryButton.ButtonAction, | ||
url: expectedButtonConfig.secondaryButton.Link, | ||
} as InAppMessageButton; | ||
return expectedContent; | ||
}; |
Oops, something went wrong.