-
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.
chore: Refactor setConflictHandler to avoid directly exporting let (#…
…13143) Co-authored-by: erinleigh90 <[email protected]>
- Loading branch information
1 parent
57feb5b
commit 22d042d
Showing
8 changed files
with
134 additions
and
72 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
34 changes: 9 additions & 25 deletions
34
...notifications/__tests__/inAppMessaging/providers/pinpoint/apis/setConflictHandler.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 |
---|---|---|
@@ -1,49 +1,33 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import { defaultStorage } from '@aws-amplify/core'; | ||
import { | ||
dispatchEvent, | ||
initializeInAppMessaging, | ||
setConflictHandler, | ||
} from '../../../../../src/inAppMessaging/providers/pinpoint/apis'; | ||
import { processInAppMessages } from '../../../../../src/inAppMessaging/providers/pinpoint/utils'; | ||
import { | ||
customHandledMessage, | ||
inAppMessages, | ||
simpleInAppMessagingEvent, | ||
} from '../../../../testUtils/data'; | ||
import { notifyEventListeners } from '../../../../../src/eventListeners'; | ||
import { InAppMessage } from '../../../../../src/inAppMessaging/types'; | ||
import { setConflictHandler as setConflictHandlerInteral } from '../../../../../src/inAppMessaging/providers/pinpoint/utils'; | ||
|
||
jest.mock('@aws-amplify/core'); | ||
jest.mock('@aws-amplify/core/internals/utils'); | ||
jest.mock('../../../../../src/inAppMessaging/providers/pinpoint/utils'); | ||
jest.mock('../../../../../src/eventListeners'); | ||
|
||
const mockDefaultStorage = defaultStorage as jest.Mocked<typeof defaultStorage>; | ||
const mockNotifyEventListeners = notifyEventListeners as jest.Mock; | ||
const mockProcessInAppMessages = processInAppMessages as jest.Mock; | ||
const mockSetConflictHandlerInteral = setConflictHandlerInteral as jest.Mock; | ||
|
||
describe('setConflictHandler', () => { | ||
beforeAll(() => { | ||
initializeInAppMessaging(); | ||
}); | ||
beforeEach(() => { | ||
mockDefaultStorage.setItem.mockClear(); | ||
mockNotifyEventListeners.mockClear(); | ||
|
||
afterEach(() => { | ||
mockSetConflictHandlerInteral.mockClear(); | ||
}); | ||
it('can register a custom conflict handler', async () => { | ||
const customConflictHandler = (messages: InAppMessage[]) => | ||
messages.find(message => message.id === 'custom-handled')!; | ||
mockProcessInAppMessages.mockReturnValueOnce(inAppMessages); | ||
|
||
it('can register a custom conflict handler', async () => { | ||
const customConflictHandler = jest.fn(); | ||
setConflictHandler(customConflictHandler); | ||
await dispatchEvent(simpleInAppMessagingEvent); | ||
|
||
expect(mockNotifyEventListeners).toHaveBeenCalledWith( | ||
'messageReceived', | ||
customHandledMessage, | ||
expect(mockSetConflictHandlerInteral).toHaveBeenCalledWith( | ||
customConflictHandler, | ||
); | ||
}); | ||
}); |
37 changes: 37 additions & 0 deletions
37
...ications/__tests__/inAppMessaging/providers/pinpoint/utils/conflictHandlerManager.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,37 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import { | ||
getConflictHandler, | ||
setConflictHandler, | ||
} from '../../../../../src/inAppMessaging/providers/pinpoint/utils/conflictHandlerManager'; | ||
import { | ||
closestExpiryMessage, | ||
customHandledMessage, | ||
inAppMessages, | ||
} from '../../../../testUtils/data'; | ||
|
||
describe('conflictHandlerManager', () => { | ||
const newConflictHandler = jest.fn(() => customHandledMessage); | ||
|
||
afterEach(() => { | ||
newConflictHandler.mockClear(); | ||
}); | ||
|
||
it('has a default conflict handler to start', () => { | ||
const defaultConflictHandler = getConflictHandler(); | ||
expect(defaultConflictHandler).toBeDefined(); | ||
expect(defaultConflictHandler(inAppMessages)).toStrictEqual( | ||
closestExpiryMessage, | ||
); | ||
}); | ||
|
||
it('can set and get a custom conflict handler', () => { | ||
setConflictHandler(newConflictHandler); | ||
const customConflictHandler = getConflictHandler(); | ||
expect(customConflictHandler).toBe(newConflictHandler); | ||
expect(customConflictHandler(inAppMessages)).toStrictEqual( | ||
customHandledMessage, | ||
); | ||
}); | ||
}); |
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
52 changes: 52 additions & 0 deletions
52
packages/notifications/src/inAppMessaging/providers/pinpoint/utils/conflictHandlerManager.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,52 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import { InAppMessage } from '../../../types'; | ||
import { InAppMessageConflictHandler, SetConflictHandlerInput } from '../types'; | ||
|
||
/** | ||
* The default conflict handler. Can be overridden by `setConflictHandler`. | ||
*/ | ||
let conflictHandler: InAppMessageConflictHandler = ( | ||
messages: InAppMessage[], | ||
): InAppMessage => { | ||
// default behavior is to return the message closest to expiry | ||
// this function assumes that messages processed by providers already filters out expired messages | ||
const sorted = messages.sort((a, b) => { | ||
const endDateA = a.metadata?.endDate; | ||
const endDateB = b.metadata?.endDate; | ||
// if both message end dates are falsy or have the same date string, treat them as equal | ||
if (endDateA === endDateB) { | ||
return 0; | ||
} | ||
// if only message A has an end date, treat it as closer to expiry | ||
if (endDateA && !endDateB) { | ||
return -1; | ||
} | ||
// if only message B has an end date, treat it as closer to expiry | ||
if (!endDateA && endDateB) { | ||
return 1; | ||
} | ||
// otherwise, compare them | ||
return new Date(endDateA) < new Date(endDateB) ? -1 : 1; | ||
}); | ||
// always return the top sorted | ||
return sorted[0]; | ||
}; | ||
|
||
/** | ||
* Sets conflict handler. | ||
* | ||
* @internal | ||
*/ | ||
export const setConflictHandler = (input: SetConflictHandlerInput): void => { | ||
conflictHandler = input; | ||
}; | ||
|
||
/** | ||
* Returns the current conflict handler. | ||
* | ||
* @internal | ||
*/ | ||
export const getConflictHandler = (): InAppMessageConflictHandler => | ||
conflictHandler; |
4 changes: 4 additions & 0 deletions
4
packages/notifications/src/inAppMessaging/providers/pinpoint/utils/index.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