Skip to content

Commit

Permalink
Rollback selector logs, to avoid noise if they are used before action…
Browse files Browse the repository at this point in the history
…s are dispatched
  • Loading branch information
EmilianoSanchez committed May 24, 2024
1 parent db4ddd7 commit d76335c
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 37 deletions.
10 changes: 2 additions & 8 deletions src/__tests__/selectors.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
17 changes: 7 additions & 10 deletions src/__tests__/selectorsWithStatus.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -21,31 +21,30 @@ 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({
treatment: DEFAULT_TREATMENT,
...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<any>(initSplitSdk({ config: sdkBrowserConfig }));
(splitSdk.factory as any).client().__emitter__.emit(Event.SDK_READY);
Expand All @@ -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<any>(getTreatments({ key: USER_1, splitNames: [SPLIT_2] }));
(splitSdk.factory as any).client(USER_1).__emitter__.emit(Event.SDK_READY_FROM_CACHE);
Expand All @@ -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', () => {
Expand All @@ -96,7 +94,6 @@ describe('selectTreatmentAndStatus & selectTreatmentWithConfigAndStatus', () =>
isReadyFromCache: true, isOperational: true,
});

expect(logSpy).not.toHaveBeenCalled();
expect(errorSpy).not.toHaveBeenCalled();
});

Expand Down
2 changes: 1 addition & 1 deletion src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
26 changes: 8 additions & 18 deletions src/selectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down

0 comments on commit d76335c

Please sign in to comment.