-
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: Wait for endpoint creation to identify user
- Loading branch information
Showing
10 changed files
with
223 additions
and
33 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
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
69 changes: 69 additions & 0 deletions
69
...s/__tests__/pushNotifications/providers/pinpoint/utils/inflightDeviceRegistration.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,69 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import { | ||
getInflightDeviceRegistration, | ||
rejectInflightDeviceRegistration, | ||
resolveInflightDeviceRegistration, | ||
} from '../../../../../src/pushNotifications/providers/pinpoint/utils/inflightDeviceRegistration'; | ||
import { InflightDeviceRegistration } from '../../../../../src/pushNotifications/providers/pinpoint/types'; | ||
|
||
describe('inflightDeviceRegistration', () => { | ||
describe('resolveInflightDeviceRegistration', () => { | ||
let getInflightDeviceRegistration: () => InflightDeviceRegistration; | ||
let resolveInflightDeviceRegistration: () => void; | ||
jest.isolateModules(() => { | ||
({ | ||
getInflightDeviceRegistration, | ||
resolveInflightDeviceRegistration, | ||
} = require('../../../../../src/pushNotifications/providers/pinpoint/utils/inflightDeviceRegistration')); | ||
}); | ||
|
||
it('creates a pending promise on module load', () => { | ||
expect(getInflightDeviceRegistration()).toBeDefined(); | ||
}); | ||
|
||
it('should resolve the promise', async () => { | ||
const blockedFunction = jest.fn(); | ||
const promise = getInflightDeviceRegistration()?.then(() => { | ||
blockedFunction(); | ||
}); | ||
|
||
expect(blockedFunction).not.toHaveBeenCalled(); | ||
resolveInflightDeviceRegistration(); | ||
await promise; | ||
expect(blockedFunction).toHaveBeenCalled(); | ||
}); | ||
|
||
it('should have released the promise from memory', () => { | ||
expect(getInflightDeviceRegistration()).toBeUndefined(); | ||
}); | ||
}); | ||
|
||
describe('rejectInflightDeviceRegistration', () => { | ||
let getInflightDeviceRegistration: () => InflightDeviceRegistration; | ||
let rejectInflightDeviceRegistration: (underlyingError: unknown) => void; | ||
jest.isolateModules(() => { | ||
({ | ||
getInflightDeviceRegistration, | ||
rejectInflightDeviceRegistration, | ||
} = require('../../../../../src/pushNotifications/providers/pinpoint/utils/inflightDeviceRegistration')); | ||
}); | ||
|
||
it('creates a pending promise on module load', () => { | ||
expect(getInflightDeviceRegistration()).toBeDefined(); | ||
}); | ||
|
||
it('should reject the promise', async () => { | ||
const blockedFunction = jest.fn(); | ||
const promise = getInflightDeviceRegistration()?.then(() => { | ||
blockedFunction(); | ||
}); | ||
|
||
expect(blockedFunction).not.toHaveBeenCalled(); | ||
rejectInflightDeviceRegistration(new Error()); | ||
await expect(promise).rejects.toThrow('Failed to register device'); | ||
expect(blockedFunction).not.toHaveBeenCalled(); | ||
}); | ||
}); | ||
}); |
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
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
38 changes: 38 additions & 0 deletions
38
...otifications/src/pushNotifications/providers/pinpoint/utils/inflightDeviceRegistration.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,38 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import { PushNotificationError } from '../../../errors'; | ||
import { | ||
InflightDeviceRegistration, | ||
InflightDeviceRegistrationResolver, | ||
} from '../types'; | ||
|
||
const inflightDeviceRegistrationResolver: InflightDeviceRegistrationResolver = | ||
{}; | ||
|
||
let inflightDeviceRegistration: InflightDeviceRegistration = new Promise<void>( | ||
(resolve, reject) => { | ||
inflightDeviceRegistrationResolver.resolve = resolve; | ||
inflightDeviceRegistrationResolver.reject = reject; | ||
}, | ||
); | ||
|
||
export const getInflightDeviceRegistration = () => inflightDeviceRegistration; | ||
|
||
export const resolveInflightDeviceRegistration = () => { | ||
inflightDeviceRegistrationResolver.resolve?.(); | ||
// release promise from memory | ||
inflightDeviceRegistration = undefined; | ||
}; | ||
|
||
export const rejectInflightDeviceRegistration = (underlyingError: unknown) => { | ||
inflightDeviceRegistrationResolver.reject?.( | ||
new PushNotificationError({ | ||
name: 'DeviceRegistrationFailed', | ||
message: 'Failed to register device for push notifications.', | ||
underlyingError, | ||
}), | ||
); | ||
// release promise from memory | ||
inflightDeviceRegistration = undefined; | ||
}; |