Skip to content

Commit c0ae1be

Browse files
[FSSDK-11238] event tag validator test addition
1 parent b0694d9 commit c0ae1be

File tree

6 files changed

+142
-24
lines changed

6 files changed

+142
-24
lines changed

lib/core/bucketer/bucket_value_generator.spec.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,14 @@ describe('generateBucketValue', () => {
3636
it('should return an error if it cannot generate the hash value', () => {
3737
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
3838
// @ts-ignore
39-
expect(() => generateBucketValue(null)).toThrowError(
40-
new OptimizelyError(INVALID_BUCKETING_ID)
41-
);
39+
expect(() => generateBucketValue(null)).toThrow(OptimizelyError);
40+
try {
41+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
42+
// @ts-ignore
43+
generateBucketValue(null);
44+
} catch (err) {
45+
expect(err).toBeInstanceOf(OptimizelyError);
46+
expect(err.baseMessage).toBe(INVALID_BUCKETING_ID);
47+
}
4248
});
4349
});

lib/core/bucketer/index.spec.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -198,9 +198,14 @@ describe('including groups: random', () => {
198198
const bucketerParamsWithInvalidGroupId = cloneDeep(bucketerParams);
199199
bucketerParamsWithInvalidGroupId.experimentIdMap[configObj.experiments[4].id].groupId = '6969';
200200

201-
expect(() => bucketer.bucket(bucketerParamsWithInvalidGroupId)).toThrowError(
202-
new OptimizelyError(INVALID_GROUP_ID, '6969')
203-
);
201+
expect(()=> bucketer.bucket(bucketerParamsWithInvalidGroupId)).toThrow(OptimizelyError);
202+
203+
try {
204+
bucketer.bucket(bucketerParamsWithInvalidGroupId);
205+
} catch(err) {
206+
expect(err).toBeInstanceOf(OptimizelyError);
207+
expect(err.baseMessage).toBe(INVALID_GROUP_ID);
208+
}
204209
});
205210
});
206211

lib/project_config/project_config.spec.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -322,9 +322,6 @@ describe('getLayerId', () => {
322322
});
323323

324324
it('should throw error for invalid experiment key in getLayerId', function() {
325-
// expect(() => projectConfig.getLayerId(configObj, 'invalidExperimentKey')).toThrowError(
326-
// sprintf(INVALID_EXPERIMENT_ID, 'PROJECT_CONFIG', 'invalidExperimentKey')
327-
// );
328325
expect(() => projectConfig.getLayerId(configObj, 'invalidExperimentKey')).toThrowError(
329326
expect.objectContaining({
330327
baseMessage: INVALID_EXPERIMENT_ID,

lib/utils/attributes_validator/index.spec.ts

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,27 +27,55 @@ describe('validate', () => {
2727
it('should throw an error if attributes is an array', () => {
2828
const attributesArray = ['notGonnaWork'];
2929

30-
expect(() => attributesValidator.validate(attributesArray)).toThrowError(new OptimizelyError(INVALID_ATTRIBUTES));
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+
}
3138
});
3239

3340
it('should throw an error if attributes is null', () => {
34-
expect(() => attributesValidator.validate(null)).toThrowError(new OptimizelyError(INVALID_ATTRIBUTES));
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+
}
3549
});
3650

3751
it('should throw an error if attributes is a function', () => {
3852
function invalidInput() {
3953
console.log('This is an invalid input!');
4054
}
4155

42-
expect(() => attributesValidator.validate(invalidInput)).toThrowError(new OptimizelyError(INVALID_ATTRIBUTES));
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+
}
4364
});
4465

4566
it('should throw an error if attributes contains a key with an undefined value', () => {
4667
const attributeKey = 'testAttribute';
4768
const attributes: Record<string, unknown> = {};
4869
attributes[attributeKey] = undefined;
4970

50-
expect(() => attributesValidator.validate(attributes)).toThrowError(new OptimizelyError(UNDEFINED_ATTRIBUTE));
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+
}
5179
});
5280
});
5381

@@ -61,7 +89,7 @@ describe('isAttributeValid', () => {
6189
'': 'javascript',
6290
};
6391

64-
Object.keys(userAttributes).forEach((key) => {
92+
Object.keys(userAttributes).forEach(key => {
6593
const value = userAttributes[key];
6694

6795
expect(attributesValidator.isAttributeValid(key, value)).toBe(true);
@@ -77,10 +105,10 @@ describe('isAttributeValid', () => {
77105
NaN: NaN,
78106
};
79107

80-
Object.keys(userAttributes).forEach((key) => {
108+
Object.keys(userAttributes).forEach(key => {
81109
const value = userAttributes[key];
82110

83-
expect(attributesValidator.isAttributeValid(key, value)).toBe(false);
111+
expect(attributesValidator.isAttributeValid(key, value)).toBe(false);
84112
});
85113
});
86114
});

lib/utils/config_validator/index.spec.ts

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,22 +24,41 @@ describe('validate', () => {
2424
it('should complain if datafile is not provided', () => {
2525
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
2626
// @ts-ignore
27-
expect(() => configValidator.validateDatafile()).toThrowError(new OptimizelyError(NO_DATAFILE_SPECIFIED));
27+
expect(() => configValidator.validateDatafile()).toThrow(OptimizelyError);
28+
29+
try {
30+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
31+
// @ts-ignore
32+
configValidator.validateDatafile();
33+
} catch (err) {
34+
expect(err).toBeInstanceOf(OptimizelyError);
35+
expect(err.baseMessage).toBe(NO_DATAFILE_SPECIFIED);
36+
}
2837
});
2938

3039
it('should complain if datafile is malformed', () => {
31-
expect(() => configValidator.validateDatafile('abc')).toThrowError(new OptimizelyError(INVALID_DATAFILE_MALFORMED));
40+
expect(() => configValidator.validateDatafile('abc')).toThrow( OptimizelyError);
41+
42+
try {
43+
configValidator.validateDatafile('abc');
44+
} catch(err) {
45+
expect(err).toBeInstanceOf(OptimizelyError);
46+
expect(err.baseMessage).toBe(INVALID_DATAFILE_MALFORMED);
47+
}
3248
});
3349

3450
it('should complain if datafile version is not supported', () => {
35-
expect(() =>
36-
configValidator
37-
.validateDatafile(JSON.stringify(testData.getUnsupportedVersionConfig()))
38-
.toThrowError(new OptimizelyError(INVALID_DATAFILE_VERSION))
39-
);
51+
expect(() => configValidator.validateDatafile(JSON.stringify(testData.getUnsupportedVersionConfig())).toThrow(OptimizelyError));
52+
53+
try {
54+
configValidator.validateDatafile(JSON.stringify(testData.getUnsupportedVersionConfig()));
55+
} catch(err) {
56+
expect(err).toBeInstanceOf(OptimizelyError);
57+
expect(err.baseMessage).toBe(INVALID_DATAFILE_VERSION);
58+
}
4059
});
4160

42-
it('should not complain if datafile is valid', function() {
61+
it('should not complain if datafile is valid', () => {
4362
expect(() => configValidator.validateDatafile(JSON.stringify(testData.getTestProjectConfig())).not.toThrowError());
4463
});
4564
});
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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+
import { describe, it, expect, beforeEach } from 'vitest';
17+
import { validate } from '.';
18+
import { OptimizelyError } from '../../error/optimizly_error';
19+
import { INVALID_EVENT_TAGS } from 'error_message';
20+
21+
describe('validate', () => {
22+
it('should validate the given event tags if event tag is an object', () => {
23+
expect(validate({ testAttribute: 'testValue' })).toBe(true);
24+
});
25+
26+
it('should throw an error if event tags is an array', () => {
27+
const eventTagsArray = ['notGonnaWork'];
28+
29+
expect(() => validate(eventTagsArray)).toThrow(OptimizelyError)
30+
31+
try {
32+
validate(eventTagsArray);
33+
} catch(err) {
34+
expect(err).toBeInstanceOf(OptimizelyError);
35+
expect(err.baseMessage).toBe(INVALID_EVENT_TAGS);
36+
}
37+
});
38+
39+
it('should throw an error if event tags is null', () => {
40+
expect(() => validate(null)).toThrow(OptimizelyError);
41+
42+
try {
43+
validate(null);
44+
} catch(err) {
45+
expect(err).toBeInstanceOf(OptimizelyError);
46+
expect(err.baseMessage).toBe(INVALID_EVENT_TAGS);
47+
}
48+
});
49+
50+
it('should throw an error if event tags is a function', () => {
51+
function invalidInput() {
52+
console.log('This is an invalid input!');
53+
}
54+
expect(() => validate(invalidInput)).toThrow(OptimizelyError);
55+
56+
try {
57+
validate(invalidInput);
58+
} catch(err) {
59+
expect(err).toBeInstanceOf(OptimizelyError);
60+
expect(err.baseMessage).toBe(INVALID_EVENT_TAGS);
61+
}
62+
});
63+
});

0 commit comments

Comments
 (0)