Skip to content

Commit

Permalink
test: client level tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Mercy811 committed Oct 11, 2023
1 parent b382f73 commit bdd8bed
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 74 deletions.
62 changes: 61 additions & 1 deletion packages/analytics-browser/test/browser-client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { AmplitudeBrowser } from '../src/browser-client';
import * as core from '@amplitude/analytics-core';
import * as Config from '../src/config';
import * as CookieMigration from '../src/cookie-migration';
import { UserSession } from '@amplitude/analytics-types';
import { Diagnostic, Status, UserSession } from '@amplitude/analytics-types';
import {
CookieStorage,
FetchTransport,
Expand Down Expand Up @@ -261,6 +261,66 @@ describe('browser-client', () => {
}).promise;
expect(webAttributionPluginPlugin).toHaveBeenCalledTimes(1);
});

test.each([
['api_key', undefined, core.INVALID_OR_MISSING_FIELDS_DIAGNOSTIC_MESSAGE],
[undefined, { time: [0] }, core.EVENT_ERROR_DIAGNOSTIC_MESSAGE],
])('should diagnostic track when 400 invalid response', async (missingField, eventsWithInvalidFields, message) => {
const transportProvider = {
send: jest.fn().mockImplementationOnce(() => {
return Promise.resolve({
status: Status.Invalid,
statusCode: 400,
body: {
error: 'error',
missingField: missingField,
eventsWithInvalidFields: eventsWithInvalidFields,
eventsWithMissingFields: {},
eventsWithInvalidIdLengths: {},
silencedEvents: [],
},
});
}),
};

await client.init(apiKey, {
defaultTracking: false,
}).promise;
const diagnosticTrack = jest.spyOn(client.config.diagnosticProvider as Diagnostic, 'track');
client.config.transportProvider = transportProvider;
await client.track('event_type', { userId: 'user_0' }).promise;

expect(diagnosticTrack).toHaveBeenCalledTimes(1);
expect(diagnosticTrack).toHaveBeenCalledWith(1, 400, message);
});

test('should diagnostic track when 429 rate limit when flush', async () => {
const transportProvider = {
send: jest.fn().mockImplementationOnce(() => {
return Promise.resolve({
status: Status.RateLimit,
statusCode: 429,
body: {
exceededDailyQuotaUsers: { user_0: 1 },
exceededDailyQuotaDevices: {},
throttledEvents: [],
},
});
}),
};

await client.init(apiKey, {
defaultTracking: false,
}).promise;
const diagnosticTrack = jest.spyOn(client.config.diagnosticProvider as Diagnostic, 'track');
client.config.transportProvider = transportProvider;
client.track('event_type', { userId: 'user_0' });
// flush() calls destination.flush(useRetry: false)
await client.flush().promise;

expect(diagnosticTrack).toHaveBeenCalledTimes(1);
expect(diagnosticTrack).toHaveBeenCalledWith(1, 429, core.EXCEEDED_DAILY_QUOTA_DIAGNOSTIC_MESSAGE);
});
});

describe('getUserId', () => {
Expand Down
9 changes: 9 additions & 0 deletions packages/analytics-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,15 @@ export { Identify } from './identify';
export { Revenue } from './revenue';
export { Destination } from './plugins/destination';
export { Diagnostic } from './diagnostics/diagnostic';
export {
EXCEEDED_MAX_RETRY_DIAGNOSTIC_MESSAGE,
MISSING_API_KEY_DIAGNOSTIC_MESSAGE,
UNEXPECTED_DIAGNOSTIC_MESSAGE,
INVALID_OR_MISSING_FIELDS_DIAGNOSTIC_MESSAGE,
EVENT_ERROR_DIAGNOSTIC_MESSAGE,
PAYLOAD_TOO_LARGE_DIAGNOSTIC_MESSAGE,
EXCEEDED_DAILY_QUOTA_DIAGNOSTIC_MESSAGE,
} from './diagnostics/constants';
export { Config } from './config';
export { Logger } from './logger';
export { AMPLITUDE_PREFIX, STORAGE_PREFIX } from './constants';
Expand Down
73 changes: 0 additions & 73 deletions packages/analytics-core/test/plugins/destination.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -621,79 +621,6 @@ describe('destination', () => {
expect(diagnosticProvider.track).toHaveBeenCalledWith(dropCount, 429, EXCEEDED_DAILY_QUOTA_DIAGNOSTIC_MESSAGE);
expect(destination.queue.length).toBe(queueCount);
});
// const destination = new Destination();
// const callback = jest.fn();
// const event = {
// event_type: 'event_type',
// };
// const context = {
// attempts: 0,
// callback,
// event,
// timeout: 0,
// };
// const transportProvider = {
// send: jest.fn().mockImplementationOnce(() => {
// return Promise.resolve({
// status: Status.RateLimit,
// statusCode: 429,
// });
// }),
// };
// await destination.setup({
// ...useDefaultConfig(),
// transportProvider,
// apiKey: API_KEY,
// diagnosticProvider,
// });
// await destination.send([context], false);
// expect(callback).toHaveBeenCalledTimes(1);
// expect(callback).toHaveBeenCalledWith({
// event,
// code: 429,
// message: Status.RateLimit,
// });
// expect(diagnosticProvider.track).toHaveBeenCalledTimes(1);
// expect(diagnosticProvider.track).toHaveBeenCalledWith(1, 429, 'exceeded daily quota users or devices');
// expect(destination.queue.length).toBe(0);
// });

// test('should track diagnostic when 429 and retry', async () => {
// const destination = new Destination();
// const callback = jest.fn();
// const event = {
// event_type: 'event_type',
// user_id: 'user_0',
// };
// const context = {
// attempts: 0,
// callback,
// event,
// timeout: 0,
// };
// const transportProvider = {
// send: jest.fn().mockImplementationOnce(() => {
// return Promise.resolve({
// status: Status.RateLimit,
// statusCode: 429,
// body: {
// exceededDailyQuotaUsers: { user_0: 1 },
// exceededDailyQuotaDevices: {},
// throttledEvents: [],
// },
// });
// }),
// };
// await destination.setup({
// ...useDefaultConfig(),
// transportProvider,
// apiKey: API_KEY,
// diagnosticProvider,
// });
// await destination.send([context]);
// expect(diagnosticProvider.track).toHaveBeenCalledTimes(1);
// expect(diagnosticProvider.track).toHaveBeenCalledWith(1, 429, 'exceeded daily quota users or devices');
// });
});

describe('saveEvents', () => {
Expand Down

0 comments on commit bdd8bed

Please sign in to comment.