|
| 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 | + // assert.isTrue(attributesValidator.validate({ testAttribute: 'testValue' })); |
| 25 | + expect(attributesValidator.validate({ testAttribute: 'testValue' })).toBe(true); |
| 26 | + }); |
| 27 | + |
| 28 | + it('should throw an error if attributes is an array', () => { |
| 29 | + const attributesArray = ['notGonnaWork']; |
| 30 | + |
| 31 | + expect(() => attributesValidator.validate(attributesArray)).toThrowError(new OptimizelyError(INVALID_ATTRIBUTES)); |
| 32 | + }); |
| 33 | + |
| 34 | + it('should throw an error if attributes is null', () => { |
| 35 | + expect(() => attributesValidator.validate(null)).toThrowError(new OptimizelyError(INVALID_ATTRIBUTES)); |
| 36 | + }); |
| 37 | + |
| 38 | + it('should throw an error if attributes is a function', () => { |
| 39 | + function invalidInput() { |
| 40 | + console.log('This is an invalid input!'); |
| 41 | + } |
| 42 | + |
| 43 | + expect(() => attributesValidator.validate(invalidInput)).toThrowError(new OptimizelyError(INVALID_ATTRIBUTES)); |
| 44 | + }); |
| 45 | + |
| 46 | + it('should throw an error if attributes contains a key with an undefined value', () => { |
| 47 | + const attributeKey = 'testAttribute'; |
| 48 | + const attributes: Record<string, unknown> = {}; |
| 49 | + attributes[attributeKey] = undefined; |
| 50 | + |
| 51 | + expect(() => attributesValidator.validate(attributes)).toThrowError(new OptimizelyError(UNDEFINED_ATTRIBUTE)); |
| 52 | + }); |
| 53 | +}); |
| 54 | + |
| 55 | +describe('isAttributeValid', () => { |
| 56 | + it('isAttributeValid returns true for valid values', () => { |
| 57 | + const userAttributes: Record<string, unknown> = { |
| 58 | + browser_type: 'Chrome', |
| 59 | + is_firefox: false, |
| 60 | + num_users: 10, |
| 61 | + pi_value: 3.14, |
| 62 | + '': 'javascript', |
| 63 | + }; |
| 64 | + |
| 65 | + Object.keys(userAttributes).forEach((key) => { |
| 66 | + const value = userAttributes[key]; |
| 67 | + |
| 68 | + expect(attributesValidator.isAttributeValid(key, value)).toBe(true); |
| 69 | + }); |
| 70 | + }); |
| 71 | + it('isAttributeValid returns false for invalid values', () => { |
| 72 | + const userAttributes: Record<string, unknown> = { |
| 73 | + null: null, |
| 74 | + objects: { a: 'b' }, |
| 75 | + array: [1, 2, 3], |
| 76 | + infinity: Infinity, |
| 77 | + negativeInfinity: -Infinity, |
| 78 | + NaN: NaN, |
| 79 | + }; |
| 80 | + |
| 81 | + Object.keys(userAttributes).forEach((key) => { |
| 82 | + const value = userAttributes[key]; |
| 83 | + |
| 84 | + expect(attributesValidator.isAttributeValid(key, value)).toBe(false); |
| 85 | + }); |
| 86 | + }); |
| 87 | +}); |
0 commit comments