Skip to content

Commit

Permalink
Merge branch 'get_status' into selectors_with_client_status_tests
Browse files Browse the repository at this point in the history
  • Loading branch information
EmilianoSanchez committed May 23, 2024
2 parents c0834fe + c6cfa84 commit 53d41b3
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 10 deletions.
51 changes: 49 additions & 2 deletions src/__tests__/helpers.browser.test.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@
/** Mocks */
import { mockSdk } from './utils/mockBrowserSplitSdk';
import { mockSdk, Event } from './utils/mockBrowserSplitSdk';
jest.mock('@splitsoftware/splitio', () => {
return { SplitFactory: mockSdk() };
});

/** Constants, types, utils */
import { sdkBrowserConfig } from './utils/sdkConfigs';
import { STATUS_INITIAL } from './utils/storeState';
import {
getTreatments,
initSplitSdk,
splitSdk,
} from '../asyncActions';
import { ERROR_TRACK_NO_INITSPLITSDK } from '../constants';
import { ERROR_GETSTATUS_NO_INITSPLITSDK, ERROR_TRACK_NO_INITSPLITSDK, WARN_GETSTATUS_NO_CLIENT } from '../constants';

/** Test targets */
import {
getSplitNames,
getSplit,
getSplits,
track,
getStatus,
} from '../helpers';

const featureFlagNames: string[] = ['split_1', 'split_2'];
Expand Down Expand Up @@ -171,3 +174,47 @@ describe('track', () => {
});

});

describe('getStatus', () => {

beforeEach(() => {
splitSdk.factory = null;
});

it('should return the default status if the SDK was not initialized', () => {
const errorSpy = jest.spyOn(console, 'error');

expect(getStatus()).toEqual(STATUS_INITIAL);
expect(errorSpy).toBeCalledWith(ERROR_GETSTATUS_NO_INITSPLITSDK);

errorSpy.mockRestore();
});

it('should return the status of the client associated to the provided key', () => {
const logSpy = jest.spyOn(console, 'log');

initSplitSdk({ config: sdkBrowserConfig });
getTreatments({ key: 'user_2', splitNames: ['split_1'] });
(splitSdk.factory as any).client().__emitter__.emit(Event.SDK_READY);
(splitSdk.factory as any).client('user_2').__emitter__.emit(Event.SDK_READY_FROM_CACHE);

// Main client
const MAIN_CLIENT_STATUS = { ...STATUS_INITIAL, isReady: true, isOperational: true };
expect(getStatus()).toEqual(MAIN_CLIENT_STATUS);
expect(getStatus(sdkBrowserConfig.core.key)).toEqual(MAIN_CLIENT_STATUS);
expect(getStatus({ matchingKey: sdkBrowserConfig.core.key as string, bucketingKey: '' })).toEqual(MAIN_CLIENT_STATUS);

// Client for user_2
const USER_2_STATUS = { ...STATUS_INITIAL, isReadyFromCache: true, isOperational: true };
expect(getStatus('user_2')).toEqual(USER_2_STATUS);
expect(getStatus({ matchingKey: 'user_2', bucketingKey: '' })).toEqual(USER_2_STATUS);

expect(logSpy).not.toBeCalled();

// Non-existing client
expect(getStatus('non_existing_key')).toEqual(STATUS_INITIAL);
expect(logSpy).toBeCalledWith(WARN_GETSTATUS_NO_CLIENT);

logSpy.mockRestore();
});
});
24 changes: 23 additions & 1 deletion src/__tests__/helpers.node.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
/** Mocks */
import { mockSdk } from './utils/mockNodeSplitSdk';
import { mockSdk, Event } from './utils/mockNodeSplitSdk';
jest.mock('@splitsoftware/splitio', () => {
return { SplitFactory: mockSdk() };
});

/** Constants, types, utils */
import { sdkNodeConfig } from './utils/sdkConfigs';
import { STATUS_INITIAL } from './utils/storeState';
import {
initSplitSdk,
splitSdk,
Expand All @@ -18,6 +19,7 @@ import {
getSplit,
getSplits,
track,
getStatus,
} from '../helpers';

const featureFlagNames: string[] = ['split_1', 'split_2'];
Expand Down Expand Up @@ -143,3 +145,23 @@ describe('track', () => {
});

});

describe('getStatus', () => {

beforeEach(() => {
splitSdk.factory = null;
});

it('should return the default status if the SDK was not initialized', () => {
expect(getStatus()).toEqual(STATUS_INITIAL);
});

it('should return the status of the main client', () => {
initSplitSdk({ config: sdkNodeConfig });
(splitSdk.factory as any).client().__emitter__.emit(Event.SDK_READY);

const MAIN_CLIENT_STATUS = { ...STATUS_INITIAL, isReady: true, isOperational: true };
expect(getStatus()).toEqual(MAIN_CLIENT_STATUS);
expect(getStatus('ignored_key_in_server_side')).toEqual(MAIN_CLIENT_STATUS);
});
});
2 changes: 1 addition & 1 deletion src/__tests__/utils/storeState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export const STATUS_INITIAL = {
isReadyFromCache: false,
hasTimedout: false,
isDestroyed: false,
}
};

export const STATE_INITIAL: { splitio: ISplitState } = {
splitio: {
Expand Down
14 changes: 8 additions & 6 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,30 @@
// @TODO Reuse IStatus and IClientStatus when interfaces are aligned (breaking change)
/** Type for Split client and manager status */
export interface IStatus {

/**
* isReady indicates if Split SDK client is ready, i.e., if it has emitted an SDK_READY event.
* isReady indicates if Split client is ready, i.e., if it has emitted an SDK_READY event.
* @see {@link https://help.split.io/hc/en-us/articles/360020448791-JavaScript-SDK#advanced-subscribe-to-events-and-changes}
*/
isReady: boolean;

/**
* isReadyFromCache indicates if Split SDK client has emitted an SDK_READY_FROM_CACHE event, what means that the SDK is ready to
* isReadyFromCache indicates if Split client has emitted an SDK_READY_FROM_CACHE event, what means that the SDK is ready to
* evaluate using LocalStorage cached data (which might be stale).
* This flag only applies for the Browser if using LOCALSTORAGE as storage type.
* @see {@link https://help.split.io/hc/en-us/articles/360020448791-JavaScript-SDK#advanced-subscribe-to-events-and-changes}
*/
isReadyFromCache: boolean;

/**
* hasTimedout indicates if the Split SDK client has ever emitted an SDK_READY_TIMED_OUT event.
* hasTimedout indicates if the Split client has ever emitted an SDK_READY_TIMED_OUT event.
* It's meant to keep a reference that the SDK emitted a timeout at some point, not the current state.
* @see {@link https://help.split.io/hc/en-us/articles/360020448791-JavaScript-SDK#advanced-subscribe-to-events-and-changes}
*/
hasTimedout: boolean;

/**
* isDestroyed indicates if the Split SDK client has been destroyed by dispatching a `destroySplitSdk` action.
* isDestroyed indicates if the Split client has been destroyed by dispatching a `destroySplitSdk` action.
* @see {@link https://help.split.io/hc/en-us/articles/360038851551-Redux-SDK#shutdown}
*/
isDestroyed: boolean;
Expand All @@ -32,13 +34,13 @@ export interface IStatus {
export interface ISplitState extends IStatus {

/**
* isTimedout indicates if the Split SDK client has emitted an SDK_READY_TIMED_OUT event and is not ready.
* isTimedout indicates if the Split main client has emitted an SDK_READY_TIMED_OUT event and is not ready.
* @see {@link https://help.split.io/hc/en-us/articles/360020448791-JavaScript-SDK#advanced-subscribe-to-events-and-changes}
*/
isTimedout: boolean;

/**
* lastUpdate is the timestamp of the last Split SDK client event (SDK_READY, SDK_READY_TIMED_OUT or SDK_UPDATE).
* lastUpdate is the timestamp of the last Split main client event (SDK_READY, SDK_READY_TIMED_OUT or SDK_UPDATE).
* @see {@link https://help.split.io/hc/en-us/articles/360038851551-Redux-SDK#advanced-subscribe-to-events-and-changes}
*/
lastUpdate: number;
Expand Down

0 comments on commit 53d41b3

Please sign in to comment.