diff --git a/src/__tests__/selectors.test.ts b/src/__tests__/selectors.test.ts index 5c87085..0583e9b 100644 --- a/src/__tests__/selectors.test.ts +++ b/src/__tests__/selectors.test.ts @@ -25,20 +25,14 @@ describe('selectTreatmentValue', () => { expect(selectTreatmentValue(STATE_READY.splitio, SPLIT_2, { matchingKey: USER_1 })).toBe(OFF); }); - it('returns "control" value and logs error if the given feature flag name or key are invalid (were not evaluated with getTreatment action)', () => { - const logSpy = jest.spyOn(console, 'log'); + it('returns "control" value if the given feature flag name or key are invalid (were not evaluated with getTreatment, or returned "control"', () => { expect(selectTreatmentValue(STATE_READY.splitio, SPLIT_1, USER_INVALID)).toBe(CONTROL); - expect(logSpy).toHaveBeenLastCalledWith(`[WARN] Treatment not found by selector. Check you have dispatched a "getTreatments" action for the feature flag "${SPLIT_1}" and key "${USER_INVALID}"`); expect(selectTreatmentValue(STATE_READY.splitio, SPLIT_INVALID, USER_1)).toBe(CONTROL); - expect(logSpy).toHaveBeenLastCalledWith(`[WARN] Treatment not found by selector. Check you have dispatched a "getTreatments" action for the feature flag "${SPLIT_INVALID}" and key "${USER_1}"`); }); - it('returns the passed default treatment value and logs error if the given feature flag name or key are invalid', () => { - const logSpy = jest.spyOn(console, 'log'); + it('returns the passed default treatment value insteaad of "control" if the given feature flag name or key are invalid', () => { expect(selectTreatmentValue(STATE_READY.splitio, SPLIT_1, USER_INVALID, 'some_value')).toBe('some_value'); - expect(logSpy).toHaveBeenLastCalledWith(`[WARN] Treatment not found by selector. Check you have dispatched a "getTreatments" action for the feature flag "${SPLIT_1}" and key "${USER_INVALID}"`); expect(selectTreatmentValue(STATE_READY.splitio, SPLIT_INVALID, USER_1, 'some_value')).toBe('some_value'); - expect(logSpy).toHaveBeenLastCalledWith(`[WARN] Treatment not found by selector. Check you have dispatched a "getTreatments" action for the feature flag "${SPLIT_INVALID}" and key "${USER_1}"`); }); it('returns "control" and logs error if the given splitState is invalid', () => { diff --git a/src/__tests__/selectorsWithStatus.test.ts b/src/__tests__/selectorsWithStatus.test.ts index dc40a26..bf3a7d6 100644 --- a/src/__tests__/selectorsWithStatus.test.ts +++ b/src/__tests__/selectorsWithStatus.test.ts @@ -11,7 +11,7 @@ import { sdkBrowserConfig } from './utils/sdkConfigs'; import { initSplitSdk, getTreatments, splitSdk } from '../asyncActions'; /** Constants */ -import { ON, CONTROL, CONTROL_WITH_CONFIG, ERROR_SELECTOR_NO_SPLITSTATE, ERROR_GETSTATUS_NO_INITSPLITSDK } from '../constants'; +import { ON, CONTROL, CONTROL_WITH_CONFIG, ERROR_SELECTOR_NO_SPLITSTATE } from '../constants'; /** Test targets */ import { @@ -21,15 +21,13 @@ import { describe('selectTreatmentAndStatus & selectTreatmentWithConfigAndStatus', () => { - const logSpy = jest.spyOn(console, 'log'); const errorSpy = jest.spyOn(console, 'error'); beforeEach(() => { - logSpy.mockClear(); errorSpy.mockClear(); }); - it('if Split SDK was not initialized, logs error and returns default treatment and initial status', () => { + it('if Split state is invalid or SDK was not initialized, returns default treatment and initial status', () => { const DEFAULT_TREATMENT = { treatment: 'some_value', config: 'some_config' }; expect(selectTreatmentWithConfigAndStatus({} as any, SPLIT_1, USER_1, DEFAULT_TREATMENT)).toEqual({ @@ -37,15 +35,16 @@ describe('selectTreatmentAndStatus & selectTreatmentWithConfigAndStatus', () => ...STATUS_INITIAL, }); expect(errorSpy).toHaveBeenCalledWith(ERROR_SELECTOR_NO_SPLITSTATE); + errorSpy.mockClear(); expect(selectTreatmentAndStatus(STATE_INITIAL.splitio, SPLIT_1, USER_1, 'default_value')).toEqual({ treatment: 'default_value', ...STATUS_INITIAL, }); - expect(errorSpy).toHaveBeenCalledWith(ERROR_GETSTATUS_NO_INITSPLITSDK); + expect(errorSpy).not.toHaveBeenCalled(); }); - it('if getTreatments action was not dispatched for the provided feature flag and key, logs error and returns default treatment and client status', () => { + it('if getTreatments action was not dispatched for the provided feature flag and key, returns default treatment and client status', () => { const store = mockStore(STATE_INITIAL); store.dispatch(initSplitSdk({ config: sdkBrowserConfig })); (splitSdk.factory as any).client().__emitter__.emit(Event.SDK_READY); @@ -55,14 +54,12 @@ describe('selectTreatmentAndStatus & selectTreatmentWithConfigAndStatus', () => // status of main client: ...STATUS_INITIAL, isReady: true, isOperational: true, }); - expect(logSpy).toHaveBeenCalledWith('[WARN] Treatment not found by selector. Check you have dispatched a "getTreatments" action for the feature flag "split_1" '); expect(selectTreatmentAndStatus(STATE_INITIAL.splitio, SPLIT_1, USER_1, 'some_value')).toEqual({ treatment: 'some_value', // USER_1 client has not been initialized yet: ...STATUS_INITIAL, }); - expect(logSpy).toHaveBeenCalledWith('[WARN] Treatment not found by selector. Check you have dispatched a "getTreatments" action for the feature flag "split_1" and key "user_1"'); store.dispatch(getTreatments({ key: USER_1, splitNames: [SPLIT_2] })); (splitSdk.factory as any).client(USER_1).__emitter__.emit(Event.SDK_READY_FROM_CACHE); @@ -72,7 +69,8 @@ describe('selectTreatmentAndStatus & selectTreatmentWithConfigAndStatus', () => // status of shared client: ...STATUS_INITIAL, isReadyFromCache: true, isOperational: true, }); - expect(logSpy).toHaveBeenCalledWith('[WARN] Treatment not found by selector. Check you have dispatched a "getTreatments" action for the feature flag "split_2" and key "user_1"'); + + expect(errorSpy).not.toHaveBeenCalled(); }); it('happy path: returns the treatment value and status of the client', () => { @@ -96,7 +94,6 @@ describe('selectTreatmentAndStatus & selectTreatmentWithConfigAndStatus', () => isReadyFromCache: true, isOperational: true, }); - expect(logSpy).not.toHaveBeenCalled(); expect(errorSpy).not.toHaveBeenCalled(); }); diff --git a/src/helpers.ts b/src/helpers.ts index 740afce..1b35fcf 100644 --- a/src/helpers.ts +++ b/src/helpers.ts @@ -109,7 +109,7 @@ export function getStatus(key?: SplitIO.SplitKey): IStatus { if (client) return __getStatus(client); } - // Default status if SDK is not initialized or client is not found + // Default status if SDK is not initialized or client is not found. No warning logs for now, in case the helper is used before actions are dispatched return { isReady: false, isReadyFromCache: false, diff --git a/src/selectors.ts b/src/selectors.ts index 6b4a2c0..65750d9 100644 --- a/src/selectors.ts +++ b/src/selectors.ts @@ -32,25 +32,15 @@ export function selectTreatmentValue(splitState: ISplitState, featureFlagName: s * @param {SplitIO.TreatmentWithConfig} defaultValue */ export function selectTreatmentWithConfig(splitState: ISplitState, featureFlagName: string, key?: SplitIO.SplitKey, defaultValue: SplitIO.TreatmentWithConfig = CONTROL_WITH_CONFIG): SplitIO.TreatmentWithConfig { - if (!splitState || !splitState.treatments) { - console.error(ERROR_SELECTOR_NO_SPLITSTATE); - return defaultValue; - } + const splitTreatments = splitState && splitState.treatments ? splitState.treatments[featureFlagName] : console.error(ERROR_SELECTOR_NO_SPLITSTATE); + const treatment = + splitTreatments ? + key ? + splitTreatments[matching(key)] : + Object.values(splitTreatments)[0] : + undefined; - const splitTreatments = splitState.treatments[featureFlagName]; - - const treatment = splitTreatments ? - key ? - splitTreatments[matching(key)] : - Object.values(splitTreatments)[0] : - undefined; - - if (!treatment) { - console.log(`[WARN] Treatment not found by selector. Check you have dispatched a "getTreatments" action for the feature flag "${featureFlagName}" ${key ? `and key "${matching(key)}"` : ''}`); - return defaultValue; - } - - return treatment; + return treatment ? treatment : defaultValue; } /**