|
| 1 | + |
| 2 | +import * as LabelConstants from '../../labels'; |
| 3 | + |
| 4 | +import { loggerMock } from '../../../logger/__tests__/sdkLogger.mock'; |
| 5 | + |
| 6 | +import { validateDefinitionExistence } from '../definitionExistence'; |
| 7 | +import { IReadinessManager } from '../../../readiness/types'; |
| 8 | +import { WARN_NOT_EXISTENT_DEFINITION } from '../../../logger/constants'; |
| 9 | + |
| 10 | +describe('Split existence (special case)', () => { |
| 11 | + |
| 12 | + afterEach(() => { loggerMock.mockClear(); }); |
| 13 | + |
| 14 | + test('Should return a boolean indicating if the SDK was ready and there was no Split object or "definition not found" label', () => { |
| 15 | + // @ts-expect-error |
| 16 | + let readinessManagerMock = { |
| 17 | + isReady: jest.fn(() => false) // Fake the signal for the non ready SDK |
| 18 | + } as IReadinessManager; |
| 19 | + |
| 20 | + expect(validateDefinitionExistence(loggerMock, readinessManagerMock, 'some_split', {}, 'test_method')).toBe(true); // Should always return true when the SDK is not ready. |
| 21 | + expect(validateDefinitionExistence(loggerMock, readinessManagerMock, 'some_split', null, 'test_method')).toBe(true); // Should always return true when the SDK is not ready. |
| 22 | + expect(validateDefinitionExistence(loggerMock, readinessManagerMock, 'some_split', undefined, 'test_method')).toBe(true); // Should always return true when the SDK is not ready. |
| 23 | + expect(validateDefinitionExistence(loggerMock, readinessManagerMock, 'some_split', 'a label', 'test_method')).toBe(true); // Should always return true when the SDK is not ready. |
| 24 | + expect(validateDefinitionExistence(loggerMock, readinessManagerMock, 'some_split', LabelConstants.DEFINITION_NOT_FOUND, 'test_method')).toBe(true); // Should always return true when the SDK is not ready. |
| 25 | + |
| 26 | + expect(loggerMock.warn).not.toBeCalled(); // There should have been no warning logs since the SDK was not ready yet. |
| 27 | + expect(loggerMock.error).not.toBeCalled(); // There should have been no error logs since the SDK was not ready yet. |
| 28 | + |
| 29 | + // Prepare the mock to fake that the SDK is ready now. |
| 30 | + (readinessManagerMock.isReady as jest.Mock).mockImplementation(() => true); |
| 31 | + |
| 32 | + expect(validateDefinitionExistence(loggerMock, readinessManagerMock, 'other_split', {}, 'other_method')).toBe(true); // Should return true if it receives a Split Object instead of null (when the object is not found, for manager). |
| 33 | + expect(validateDefinitionExistence(loggerMock, readinessManagerMock, 'other_split', 'a label', 'other_method')).toBe(true); // Should return true if it receives a Label and it is not split not found (when the Split was not found on the storage, for client). |
| 34 | + |
| 35 | + expect(loggerMock.warn).not.toBeCalled(); // There should have been no warning logs since the values we used so far were considered valid. |
| 36 | + expect(loggerMock.error).not.toBeCalled(); // There should have been no error logs since the values we used so far were considered valid. |
| 37 | + |
| 38 | + expect(validateDefinitionExistence(loggerMock, readinessManagerMock, 'other_split', null, 'other_method')).toBe(false); // Should return false if it receives a non-truthy value as a split object or label |
| 39 | + expect(validateDefinitionExistence(loggerMock, readinessManagerMock, 'other_split', undefined, 'other_method')).toBe(false); // Should return false if it receives a non-truthy value as a split object or label |
| 40 | + expect(validateDefinitionExistence(loggerMock, readinessManagerMock, 'other_split', LabelConstants.DEFINITION_NOT_FOUND, 'other_method')).toBe(false); // Should return false if it receives a label but it is the split not found one. |
| 41 | + |
| 42 | + expect(loggerMock.warn).toBeCalledTimes(3); // It should have logged 3 warnings, one per each time we called it |
| 43 | + loggerMock.warn.mock.calls.forEach(call => expect(call).toEqual([WARN_NOT_EXISTENT_DEFINITION, ['other_method', 'other_split']])); // Warning logs should have the correct message. |
| 44 | + |
| 45 | + expect(loggerMock.error).not.toBeCalled(); // We log warnings, not errors. |
| 46 | + }); |
| 47 | +}); |
0 commit comments