|
| 1 | +/** |
| 2 | + * Copyright 2025, Optimizely |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +import { describe, it, expect } from 'vitest'; |
| 18 | +import * as attributesValidator from './'; |
| 19 | +import { INVALID_ATTRIBUTES, UNDEFINED_ATTRIBUTE } from 'error_message'; |
| 20 | +import { OptimizelyError } from '../../error/optimizly_error'; |
| 21 | + |
| 22 | +describe('validate', () => { |
| 23 | + it('should validate the given attributes if attributes is an object', () => { |
| 24 | + expect(attributesValidator.validate({ testAttribute: 'testValue' })).toBe(true); |
| 25 | + }); |
| 26 | + |
| 27 | + it('should throw an error if attributes is an array', () => { |
| 28 | + const attributesArray = ['notGonnaWork']; |
| 29 | + |
| 30 | + expect(() => attributesValidator.validate(attributesArray)).toThrow(OptimizelyError); |
| 31 | + |
| 32 | + try { |
| 33 | + attributesValidator.validate(attributesArray); |
| 34 | + } catch (err) { |
| 35 | + expect(err).toBeInstanceOf(OptimizelyError); |
| 36 | + expect(err.baseMessage).toBe(INVALID_ATTRIBUTES); |
| 37 | + } |
| 38 | + }); |
| 39 | + |
| 40 | + it('should throw an error if attributes is null', () => { |
| 41 | + expect(() => attributesValidator.validate(null)).toThrowError(OptimizelyError); |
| 42 | + |
| 43 | + try { |
| 44 | + attributesValidator.validate(null); |
| 45 | + } catch (err) { |
| 46 | + expect(err).toBeInstanceOf(OptimizelyError); |
| 47 | + expect(err.baseMessage).toBe(INVALID_ATTRIBUTES); |
| 48 | + } |
| 49 | + }); |
| 50 | + |
| 51 | + it('should throw an error if attributes is a function', () => { |
| 52 | + function invalidInput() { |
| 53 | + console.log('This is an invalid input!'); |
| 54 | + } |
| 55 | + |
| 56 | + expect(() => attributesValidator.validate(invalidInput)).toThrowError(OptimizelyError); |
| 57 | + |
| 58 | + try { |
| 59 | + attributesValidator.validate(invalidInput); |
| 60 | + } catch(err) { |
| 61 | + expect(err).toBeInstanceOf(OptimizelyError); |
| 62 | + expect(err.baseMessage).toBe(INVALID_ATTRIBUTES); |
| 63 | + } |
| 64 | + }); |
| 65 | + |
| 66 | + it('should throw an error if attributes contains a key with an undefined value', () => { |
| 67 | + const attributeKey = 'testAttribute'; |
| 68 | + const attributes: Record<string, unknown> = {}; |
| 69 | + attributes[attributeKey] = undefined; |
| 70 | + |
| 71 | + expect(() => attributesValidator.validate(attributes)).toThrowError(OptimizelyError); |
| 72 | + |
| 73 | + try { |
| 74 | + attributesValidator.validate(attributes); |
| 75 | + } catch(err) { |
| 76 | + expect(err).toBeInstanceOf(OptimizelyError); |
| 77 | + expect(err.baseMessage).toBe(UNDEFINED_ATTRIBUTE); |
| 78 | + expect(err.params).toEqual([attributeKey]); |
| 79 | + } |
| 80 | + }); |
| 81 | +}); |
| 82 | + |
| 83 | +describe('isAttributeValid', () => { |
| 84 | + it('isAttributeValid returns true for valid values', () => { |
| 85 | + const userAttributes: Record<string, unknown> = { |
| 86 | + browser_type: 'Chrome', |
| 87 | + is_firefox: false, |
| 88 | + num_users: 10, |
| 89 | + pi_value: 3.14, |
| 90 | + '': 'javascript', |
| 91 | + }; |
| 92 | + |
| 93 | + Object.keys(userAttributes).forEach(key => { |
| 94 | + const value = userAttributes[key]; |
| 95 | + |
| 96 | + expect(attributesValidator.isAttributeValid(key, value)).toBe(true); |
| 97 | + }); |
| 98 | + }); |
| 99 | + it('isAttributeValid returns false for invalid values', () => { |
| 100 | + const userAttributes: Record<string, unknown> = { |
| 101 | + null: null, |
| 102 | + objects: { a: 'b' }, |
| 103 | + array: [1, 2, 3], |
| 104 | + infinity: Infinity, |
| 105 | + negativeInfinity: -Infinity, |
| 106 | + NaN: NaN, |
| 107 | + }; |
| 108 | + |
| 109 | + Object.keys(userAttributes).forEach(key => { |
| 110 | + const value = userAttributes[key]; |
| 111 | + |
| 112 | + expect(attributesValidator.isAttributeValid(key, value)).toBe(false); |
| 113 | + }); |
| 114 | + }); |
| 115 | +}); |
0 commit comments