Skip to content

Commit

Permalink
test: add more test cases for improving coverage in config manager
Browse files Browse the repository at this point in the history
  • Loading branch information
saikumarrs committed Jan 10, 2025
1 parent c9c6453 commit ef841ae
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { effect, signal } from '@preact/signals-core';
import { http, HttpResponse } from 'msw';
import type { ResponseDetails } from '@rudderstack/analytics-js-common/types/HttpClient';
import { defaultHttpClient } from '../../../src/services/HttpClient';
import { defaultErrorHandler } from '../../../src/services/ErrorHandler';
import { defaultLogger } from '../../../src/services/Logger';
Expand Down Expand Up @@ -178,8 +179,8 @@ describe('ConfigManager', () => {
);
});

it('should call the onError method of errorHandler for undefined sourceConfig response', () => {
configManagerInstance.processConfig();
it('should handle error for undefined source config response', () => {
configManagerInstance.processConfig(undefined);

expect(defaultErrorHandler.onError).toHaveBeenCalledTimes(1);
expect(defaultErrorHandler.onError).toHaveBeenCalledWith(
Expand All @@ -189,6 +190,19 @@ describe('ConfigManager', () => {
);
});

it('should handle error for source config request failures', () => {
configManagerInstance.processConfig(undefined, {
error: new Error('Request failed'),
} as unknown as ResponseDetails);

expect(defaultErrorHandler.onError).toHaveBeenCalledTimes(1);
expect(defaultErrorHandler.onError).toHaveBeenCalledWith(
new Error('Request failed'),
'ConfigManager',
'Failed to fetch the source config',
);
});

it('should handle error if the source config response is not parsable', () => {
configManagerInstance.processConfig('{"key": "value"');

Expand All @@ -201,6 +215,7 @@ describe('ConfigManager', () => {
});

it('should handle error if the source config response is not valid', () => {
// @ts-expect-error Testing invalid input
configManagerInstance.processConfig({ key: 'value' });

expect(defaultErrorHandler.onError).toHaveBeenCalledTimes(1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@ import type {
ResponseDetails,
} from '@rudderstack/analytics-js-common/types/HttpClient';
import { batch, effect } from '@preact/signals-core';
import { isFunction, isNull, isString } from '@rudderstack/analytics-js-common/utilities/checks';
import {
isDefined,
isFunction,
isNull,
isString,
} from '@rudderstack/analytics-js-common/utilities/checks';
import type { IErrorHandler } from '@rudderstack/analytics-js-common/types/ErrorHandler';
import type { Destination } from '@rudderstack/analytics-js-common/types/Destination';
import type { ILogger } from '@rudderstack/analytics-js-common/types/Logger';
Expand Down Expand Up @@ -141,11 +146,15 @@ class ConfigManager implements IConfigManager {
* A callback function that is executed once we fetch the source config response.
* Use to construct and store information that are dependent on the sourceConfig.
*/
processConfig(response?: SourceConfigResponse | string, details?: ResponseDetails) {
processConfig(response: SourceConfigResponse | string | undefined, details?: ResponseDetails) {
// TODO: add retry logic with backoff based on rejectionDetails.xhr.status
// We can use isErrRetryable utility method
if (!response) {
this.onError(new Error(SOURCE_CONFIG_FETCH_ERROR));
if (!isDefined(response)) {
if (isDefined(details)) {
this.onError((details as ResponseDetails).error, SOURCE_CONFIG_FETCH_ERROR);
} else {
this.onError(new Error(SOURCE_CONFIG_FETCH_ERROR));
}
return;
}

Expand All @@ -154,7 +163,7 @@ class ConfigManager implements IConfigManager {
if (isString(response)) {
res = JSON.parse(response);
} else {
res = response;
res = response as SourceConfigResponse;
}
} catch (err) {
this.onError(err, SOURCE_CONFIG_RESOLUTION_ERROR);
Expand Down
10 changes: 8 additions & 2 deletions packages/analytics-js/src/components/configManager/types.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import type { DestinationConfig } from '@rudderstack/analytics-js-common/types/Destination';
import type { Nullable } from '@rudderstack/analytics-js-common/types/Nullable';
import type { StatsCollection } from '@rudderstack/analytics-js-common/types/Source';
import type { IHttpClient } from '@rudderstack/analytics-js-common/types/HttpClient';
import type {
IHttpClient,
ResponseDetails,
} from '@rudderstack/analytics-js-common/types/HttpClient';
import type { IErrorHandler } from '@rudderstack/analytics-js-common/types/ErrorHandler';
import type { ILogger } from '@rudderstack/analytics-js-common/types/Logger';
import type { ConsentManagementMetadata } from '@rudderstack/analytics-js-common/types/Consent';
Expand Down Expand Up @@ -81,5 +84,8 @@ export interface IConfigManager {
logger: ILogger;
init: () => void;
getConfig: () => void;
processConfig: () => void;
processConfig: (
response: SourceConfigResponse | string | undefined,
details?: ResponseDetails,
) => void;
}

0 comments on commit ef841ae

Please sign in to comment.