-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Expand file tree
/
Copy pathflushEvents.test.ts
More file actions
79 lines (70 loc) · 2.24 KB
/
Copy pathflushEvents.test.ts
File metadata and controls
79 lines (70 loc) · 2.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
import { ConsoleLogger } from '@aws-amplify/core';
import {
getEventBuffer,
resolveConfig,
} from '../../../../src/providers/kinesis-firehose/utils';
import { resolveCredentials } from '../../../../src/utils';
import {
mockCredentialConfig,
mockKinesisConfig,
} from '../../../testUtils/mockConstants';
import { flushEvents } from '../../../../src/providers/kinesis-firehose/apis';
import {
setupGlobalContext,
teardownGlobalContext,
} from '../../../testUtils/mockAmplifyContext';
jest.mock('../../../../src/utils');
jest.mock('../../../../src/providers/kinesis-firehose/utils');
describe('Analytics Kinesis Firehose API: flushEvents', () => {
beforeAll(() => {
setupGlobalContext();
});
afterAll(() => {
teardownGlobalContext();
});
const mockResolveConfig = resolveConfig as jest.Mock;
const mockResolveCredentials = resolveCredentials as jest.Mock;
const mockGetEventBuffer = getEventBuffer as jest.Mock;
const mockFlushAll = jest.fn();
const loggerWarnSpy = jest.spyOn(ConsoleLogger.prototype, 'warn');
beforeEach(() => {
mockResolveConfig.mockReturnValue(mockKinesisConfig);
mockResolveCredentials.mockReturnValue(
Promise.resolve(mockCredentialConfig),
);
mockGetEventBuffer.mockImplementation(() => ({
flushAll: mockFlushAll,
}));
});
afterEach(() => {
mockResolveConfig.mockReset();
mockResolveCredentials.mockReset();
mockFlushAll.mockReset();
mockGetEventBuffer.mockReset();
});
it('trigger flushAll on event buffer', async () => {
flushEvents();
await new Promise(process.nextTick);
expect(mockResolveConfig).toHaveBeenCalledTimes(1);
expect(mockResolveCredentials).toHaveBeenCalledTimes(1);
expect(mockGetEventBuffer).toHaveBeenNthCalledWith(
1,
expect.objectContaining({
...mockKinesisConfig,
...mockCredentialConfig,
}),
);
expect(mockFlushAll).toHaveBeenCalledTimes(1);
});
it('logs an error when credentials can not be fetched', async () => {
mockResolveCredentials.mockRejectedValue(new Error('Mock Error'));
flushEvents();
await new Promise(process.nextTick);
expect(loggerWarnSpy).toHaveBeenCalledWith(
expect.any(String),
expect.any(Error),
);
});
});