Skip to content

Commit 7d58055

Browse files
authored
chore: Added custom AnalyticsProvider test (#11618)
1 parent 28bc873 commit 7d58055

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

packages/analytics/__tests__/Analytics.test.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ jest.mock('../src/vendor/dom-utils', () => {
88

99
import { ClientDevice, parseAWSExports, Hub } from '@aws-amplify/core';
1010
import { AnalyticsClass as Analytics } from '../src/Analytics';
11+
import { AnalyticsProvider, PromiseHandlers } from '../src/types';
1112
import { AWSPinpointProvider as AWSAnalyticsProvider } from '../src/Providers/AWSPinpointProvider';
1213

1314
jest.mock('@aws-amplify/core');
@@ -21,6 +22,31 @@ const record_spyon = jest
2122
return handlers.resolve();
2223
});
2324

25+
/**
26+
* CAUTION: This mock class implements a publicly available interface `AnalyticsProvider` which customers can use to
27+
* implement custom providers. Exercise caution when modifying this class as additive changes to this interface can
28+
* break customers when not marked as optional.
29+
*/
30+
class TestCustomProvider implements AnalyticsProvider {
31+
getProviderName(): string {
32+
return 'CustomProvider' as const;
33+
}
34+
35+
getCategory() {
36+
return 'Analytics' as const;
37+
}
38+
39+
configure(o: object) {
40+
return o;
41+
}
42+
43+
record(params: object, handlers?: PromiseHandlers) {
44+
handlers?.resolve(true);
45+
46+
return Promise.resolve(true);
47+
}
48+
}
49+
2450
describe('Analytics test', () => {
2551
beforeEach(() => {
2652
(parseAWSExports as jest.Mock).mockReturnValueOnce({
@@ -126,6 +152,26 @@ describe('Analytics test', () => {
126152
);
127153
expect(record_spyon).toBeCalled();
128154
});
155+
156+
/**
157+
* Custom providers are a publically available feature via the `AnalyticsProvider` interface. Exercise caution when
158+
* updating these to ensure backwards compatibility.
159+
*/
160+
test('record with custom provider', async () => {
161+
const analytics = new Analytics();
162+
const customProvider = new TestCustomProvider();
163+
const customProviderRecordSpy = jest.spyOn(customProvider, 'record');
164+
165+
analytics.addPluggable(customProvider);
166+
167+
const recordResponse = await analytics.record(
168+
{ name: 'testEvent' },
169+
'CustomProvider'
170+
);
171+
172+
expect(customProviderRecordSpy).toBeCalled();
173+
expect(recordResponse).toBe(true);
174+
});
129175
});
130176

131177
describe('updateEndpoint test', () => {

0 commit comments

Comments
 (0)