|
| 1 | +import { FallbacksSanitizer } from '../fallbackSanitizer'; |
| 2 | +import { TreatmentWithConfig } from '../../../../types/splitio'; |
| 3 | + |
| 4 | +describe('FallbacksSanitizer', () => { |
| 5 | + const validTreatment: TreatmentWithConfig = { treatment: 'on', config: '{"color":"blue"}' }; |
| 6 | + const invalidTreatment: TreatmentWithConfig = { treatment: ' ', config: null }; |
| 7 | + |
| 8 | + beforeEach(() => { |
| 9 | + jest.spyOn(console, 'error').mockImplementation(() => {}); |
| 10 | + }); |
| 11 | + |
| 12 | + afterEach(() => { |
| 13 | + (console.error as jest.Mock).mockRestore(); |
| 14 | + }); |
| 15 | + |
| 16 | + describe('isValidFlagName', () => { |
| 17 | + it('returns true for a valid flag name', () => { |
| 18 | + // @ts-expect-private-access |
| 19 | + expect((FallbacksSanitizer as any).isValidFlagName('my_flag')).toBe(true); |
| 20 | + }); |
| 21 | + |
| 22 | + it('returns false for a name longer than 100 chars', () => { |
| 23 | + const longName = 'a'.repeat(101); |
| 24 | + expect((FallbacksSanitizer as any).isValidFlagName(longName)).toBe(false); |
| 25 | + }); |
| 26 | + |
| 27 | + it('returns false if the name contains spaces', () => { |
| 28 | + expect((FallbacksSanitizer as any).isValidFlagName('invalid flag')).toBe(false); |
| 29 | + }); |
| 30 | + }); |
| 31 | + |
| 32 | + describe('isValidTreatment', () => { |
| 33 | + it('returns true for a valid treatment string', () => { |
| 34 | + expect((FallbacksSanitizer as any).isValidTreatment(validTreatment)).toBe(true); |
| 35 | + }); |
| 36 | + |
| 37 | + it('returns false for null or undefined', () => { |
| 38 | + expect((FallbacksSanitizer as any).isValidTreatment(null)).toBe(false); |
| 39 | + expect((FallbacksSanitizer as any).isValidTreatment(undefined)).toBe(false); |
| 40 | + }); |
| 41 | + |
| 42 | + it('returns false for a treatment longer than 100 chars', () => { |
| 43 | + const long = { treatment: 'a'.repeat(101) }; |
| 44 | + expect((FallbacksSanitizer as any).isValidTreatment(long)).toBe(false); |
| 45 | + }); |
| 46 | + |
| 47 | + it('returns false if treatment does not match regex pattern', () => { |
| 48 | + const invalid = { treatment: 'invalid treatment!' }; |
| 49 | + expect((FallbacksSanitizer as any).isValidTreatment(invalid)).toBe(false); |
| 50 | + }); |
| 51 | + }); |
| 52 | + |
| 53 | + describe('sanitizeGlobal', () => { |
| 54 | + it('returns the treatment if valid', () => { |
| 55 | + expect(FallbacksSanitizer.sanitizeGlobal(validTreatment)).toEqual(validTreatment); |
| 56 | + expect(console.error).not.toHaveBeenCalled(); |
| 57 | + }); |
| 58 | + |
| 59 | + it('returns undefined and logs error if invalid', () => { |
| 60 | + const result = FallbacksSanitizer.sanitizeGlobal(invalidTreatment); |
| 61 | + expect(result).toBeUndefined(); |
| 62 | + expect(console.error).toHaveBeenCalledWith( |
| 63 | + expect.stringContaining('Fallback treatments - Discarded fallback') |
| 64 | + ); |
| 65 | + }); |
| 66 | + }); |
| 67 | + |
| 68 | + describe('sanitizeByFlag', () => { |
| 69 | + it('returns a sanitized map with valid entries only', () => { |
| 70 | + const input = { |
| 71 | + valid_flag: validTreatment, |
| 72 | + 'invalid flag': validTreatment, |
| 73 | + bad_treatment: invalidTreatment, |
| 74 | + }; |
| 75 | + |
| 76 | + const result = FallbacksSanitizer.sanitizeByFlag(input); |
| 77 | + |
| 78 | + expect(result).toEqual({ valid_flag: validTreatment }); |
| 79 | + expect(console.error).toHaveBeenCalledTimes(2); // invalid flag + bad_treatment |
| 80 | + }); |
| 81 | + |
| 82 | + it('returns empty object if all invalid', () => { |
| 83 | + const input = { |
| 84 | + 'invalid flag': invalidTreatment, |
| 85 | + }; |
| 86 | + |
| 87 | + const result = FallbacksSanitizer.sanitizeByFlag(input); |
| 88 | + expect(result).toEqual({}); |
| 89 | + expect(console.error).toHaveBeenCalled(); |
| 90 | + }); |
| 91 | + |
| 92 | + it('returns same object if all valid', () => { |
| 93 | + const input = { |
| 94 | + flag_one: validTreatment, |
| 95 | + flag_two: { treatment: 'valid_2', config: null }, |
| 96 | + }; |
| 97 | + |
| 98 | + const result = FallbacksSanitizer.sanitizeByFlag(input); |
| 99 | + expect(result).toEqual(input); |
| 100 | + expect(console.error).not.toHaveBeenCalled(); |
| 101 | + }); |
| 102 | + }); |
| 103 | +}); |
0 commit comments