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