-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Handle non-string User Object attributes
- Loading branch information
Showing
4 changed files
with
168 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,61 @@ | ||
import { assert } from "chai"; | ||
import "mocha"; | ||
import { LogLevel, LoggerWrapper } from "../src/ConfigCatLogger"; | ||
import { User } from "../src/index"; | ||
import { Config } from "../src/ProjectConfig"; | ||
import { RolloutEvaluator, evaluate } from "../src/RolloutEvaluator"; | ||
import { WellKnownUserObjectAttribute, getUserAttributes } from "../src/User"; | ||
import { parseFloatStrict } from "../src/Utils"; | ||
import { FakeLogger } from "./helpers/fakes"; | ||
|
||
const identifierAttribute: WellKnownUserObjectAttribute = "Identifier"; | ||
const emailAttribute: WellKnownUserObjectAttribute = "Email"; | ||
const countryAttribute: WellKnownUserObjectAttribute = "Country"; | ||
|
||
function createUser(attributeName: string, attributeValue: string) { | ||
const user = new User(""); | ||
switch (attributeName) { | ||
case identifierAttribute: | ||
user.identifier = attributeValue; | ||
break; | ||
case emailAttribute: | ||
user.email = attributeValue; | ||
break; | ||
case countryAttribute: | ||
user.country = attributeValue; | ||
break; | ||
default: | ||
user.custom[attributeName] = attributeValue; | ||
} | ||
return user; | ||
} | ||
|
||
describe("User Object", () => { | ||
|
||
for (const [identifier, expectedValue] of <[string, string][]>[ | ||
[void 0, ""], | ||
[null, ""], | ||
["", ""], | ||
["id", "id"], | ||
["\t", "\t"], | ||
["\u1F600", "\u1F600"], | ||
for (const [attributeName, attributeValue, expectedValue] of <[string, string, string][]>[ | ||
["Identifier", void 0, ""], | ||
["Identifier", null, ""], | ||
["Identifier", "", ""], | ||
["Identifier", "id", "id"], | ||
["Identifier", "\t", "\t"], | ||
["Identifier", "\u1F600", "\u1F600"], | ||
["Email", void 0, void 0], | ||
["Email", null, void 0], | ||
["Email", "", ""], | ||
["Email", "[email protected]", "[email protected]"], | ||
["Country", void 0, void 0], | ||
["Country", null, void 0], | ||
["Country", "", ""], | ||
["Country", "US", "US"], | ||
["Custom1", void 0, void 0], | ||
["Custom1", null, void 0], | ||
["Custom1", "", ""], | ||
["Custom1", "3.14", "3.14"], | ||
]) { | ||
it(`Create user - should set id - identifier: ${identifier}`, () => { | ||
const user = new User(identifier); | ||
it(`Create user - should set attribute value - ${attributeName}: ${attributeValue}`, () => { | ||
const user = createUser(attributeName, attributeValue); | ||
|
||
assert.strictEqual(getUserAttributes(user)[identifierAttribute], expectedValue); | ||
assert.strictEqual(getUserAttributes(user)[attributeName], expectedValue); | ||
}); | ||
} | ||
|
||
|
@@ -156,4 +190,62 @@ describe("User Object", () => { | |
}); | ||
} | ||
|
||
for (const [attributeName, attributeValue, comparisonValue] of <[string, unknown, string][]>[ | ||
[identifierAttribute, false, "false"], | ||
[emailAttribute, false, "false"], | ||
[countryAttribute, false, "false"], | ||
["Custom1", false, "false"], | ||
[identifierAttribute, 1.23, "1.23"], | ||
[emailAttribute, 1.23, "1.23"], | ||
[countryAttribute, 1.23, "1.23"], | ||
["Custom1", 1.23, "1.23"], | ||
]) { | ||
it(`Non-string attribute values should be handled - attributeName: ${attributeName} | attributeValue: ${attributeValue}`, () => { | ||
const configJson = { | ||
"f": { | ||
"test": { | ||
"t": 0, | ||
"r": [ | ||
{ | ||
"c": [ | ||
{ | ||
"u": { "a": attributeName, "c": 1, "l": [comparisonValue] } | ||
} | ||
], | ||
"s": { "v": { "b": false } } | ||
}, | ||
{ | ||
"c": [ | ||
{ | ||
"u": { "a": attributeName, "c": 0, "l": [comparisonValue] } | ||
} | ||
], | ||
"s": { "v": { "b": true } } | ||
}, | ||
], | ||
"v": { "b": false } | ||
} | ||
} | ||
}; | ||
|
||
const config = new Config(configJson); | ||
const fakeLogger = new FakeLogger(); | ||
const logger = new LoggerWrapper(fakeLogger); | ||
const evaluator = new RolloutEvaluator(logger); | ||
|
||
const user = createUser(attributeName, attributeValue as any); | ||
const evaluationDetails = evaluate(evaluator, config.settings, "test", null, user, null, logger); | ||
const actualReturnValue = evaluationDetails.value; | ||
|
||
assert.isTrue(actualReturnValue); | ||
|
||
const warnings = fakeLogger.events.filter(([level]) => level === LogLevel.Warn); | ||
|
||
assert.strictEqual(1, warnings.length); | ||
|
||
const [, eventId] = warnings[0]; | ||
assert.strictEqual(4004, eventId); | ||
}); | ||
} | ||
|
||
}); |