Skip to content

Commit

Permalink
chore(analytics): Re-enable hub dispatch on record (#12224)
Browse files Browse the repository at this point in the history
Co-authored-by: Jim Blanchard <[email protected]>
  • Loading branch information
cshfang and jimblanc authored Oct 6, 2023
1 parent 99a7af3 commit 2dc407c
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Hub } from '@aws-amplify/core';
import { record as pinpointRecord } from '@aws-amplify/core/internals/providers/pinpoint';
import { ConsoleLogger as Logger } from '@aws-amplify/core/internals/utils';
import { record } from '../../../../src/providers/pinpoint';
Expand All @@ -20,35 +21,43 @@ import {
config,
} from './testUtils/data';

jest.mock('@aws-amplify/core');
jest.mock('@aws-amplify/core/internals/providers/pinpoint');
jest.mock('../../../../src/utils');
jest.mock('../../../../src/providers/pinpoint/utils');
jest.mock('@aws-amplify/core/internals/providers/pinpoint');

describe('Pinpoint API: record', () => {
// create spies
const loggerWarnSpy = jest.spyOn(Logger.prototype, 'warn');
// create mocks
const mockPinpointRecord = pinpointRecord as jest.Mock;
const mockResolveConfig = resolveConfig as jest.Mock;
const mockResolveCredentials = resolveCredentials as jest.Mock;
const mockIsAnalyticsEnabled = isAnalyticsEnabled as jest.Mock;
const mockGetAnalyticsUserAgentString =
getAnalyticsUserAgentString as jest.Mock;
const loggerWarnSpy = jest.spyOn(Logger.prototype, 'warn');
const mockHubDispatch = Hub.dispatch as jest.Mock;

beforeEach(() => {
mockPinpointRecord.mockReset();
mockPinpointRecord.mockResolvedValue(undefined);
mockResolveConfig.mockReset();
mockResolveConfig.mockReturnValue(config);
mockIsAnalyticsEnabled.mockReset();
mockIsAnalyticsEnabled.mockReturnValue(true);
mockGetAnalyticsUserAgentString.mockReset();
mockGetAnalyticsUserAgentString.mockReturnValue('mock-user-agent');
mockResolveCredentials.mockReset();
mockResolveCredentials.mockResolvedValue({
credentials,
identityId,
});
});

afterEach(() => {
mockPinpointRecord.mockReset();
mockResolveConfig.mockReset();
mockIsAnalyticsEnabled.mockReset();
mockGetAnalyticsUserAgentString.mockReset();
mockResolveCredentials.mockReset();
mockHubDispatch.mockClear();
});

it('invokes the core record implementation', async () => {
record(event);

Expand Down Expand Up @@ -101,4 +110,17 @@ describe('Pinpoint API: record', () => {

expect(mockPinpointRecord).not.toBeCalled();
});

it('should dispatch a Hub event', async () => {
record(event);

await new Promise(process.nextTick);

expect(mockHubDispatch).toBeCalledWith(
'analytics',
{ event: 'record', data: event, message: 'Recording Analytics event' },
'Analytics',
expect.anything()
);
});
});
8 changes: 8 additions & 0 deletions packages/analytics/src/providers/pinpoint/apis/record.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

import { Hub } from '@aws-amplify/core';
import {
AMPLIFY_SYMBOL,
AnalyticsAction,
ConsoleLogger as Logger,
} from '@aws-amplify/core/internals/utils';
Expand Down Expand Up @@ -59,6 +61,12 @@ export const record = (input: RecordInput): void => {

resolveCredentials()
.then(({ credentials, identityId }) => {
Hub.dispatch(
'analytics',
{ event: 'record', data: input, message: 'Recording Analytics event' },
'Analytics',
AMPLIFY_SYMBOL
);
recordCore({
appId,
category: 'Analytics',
Expand Down

0 comments on commit 2dc407c

Please sign in to comment.