Skip to content

Commit 0060488

Browse files
committed
feat: unit testing for caller
1 parent 1ccff05 commit 0060488

File tree

3 files changed

+79
-64
lines changed

3 files changed

+79
-64
lines changed

README.md

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -62,22 +62,15 @@ fp.on('ready', function() {
6262
### Step 4. Unit Testing (Optional)
6363

6464
```js
65-
import { FetchMock } from "jest-fetch-mock";
66-
6765
test("feature probe unit testing", () => {
68-
_fetch.mockResponseOnce(JSON.stringify({"testToggle": { "value": true}}));
69-
let user = new FPUser("some-key")
70-
let fp = new FeatureProbe({
71-
remoteUrl: 'http://127.0.0.1:4007',
72-
clientSdkKey: 'client-sdk-key1',
73-
user: user
74-
});
66+
let fp = FeatureProbe.newForTest({ testToggle: true });
7567
fp.start();
7668

77-
fp.on('ready', function() {
78-
let t = fp.boolValue("testToggle", false)
69+
fp.on("ready", function () {
70+
let t = fp.boolValue("testToggle", false);
7971
expect(t).toBe(true);
80-
})
72+
done();
73+
});
8174
});
8275
```
8376

src/FeatureProbe.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,27 @@ class FeatureProbe extends TinyEmitter {
146146
return Object.assign({}, this.user);
147147
}
148148

149+
static newForTest(toggles: { [key: string]: any }): FeatureProbe {
150+
var fp = new FeatureProbe({
151+
remoteUrl: "http://127.0.0.1:4000",
152+
clientSdkKey: "_",
153+
user: new FPUser(""),
154+
});
155+
var _toggles: { [key: string]: FPToggleDetail } = {};
156+
for (let key in toggles) {
157+
let value = toggles[key];
158+
_toggles[key] = {
159+
value: value,
160+
ruleIndex: null,
161+
variationIndex: null,
162+
version: 0,
163+
reason: "",
164+
};
165+
}
166+
fp.toggles = _toggles;
167+
return fp;
168+
}
169+
149170
private toggleValue(key: string, defaultValue: any, valueType: string): any {
150171
this.sendEvents(key);
151172

test/FeatureProbe.test.ts

Lines changed: 53 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -13,120 +13,125 @@ afterEach(() => {
1313
test("feature probe init with invalid url", () => {
1414
expect(() => {
1515
new FeatureProbe({
16-
remoteUrl: 'invalid url',
17-
clientSdkKey: 'client-sdk-key1',
18-
user: new FPUser("123")
16+
remoteUrl: "invalid url",
17+
clientSdkKey: "client-sdk-key1",
18+
user: new FPUser("123"),
1919
});
2020
}).toThrow();
2121
});
2222

2323
test("feature probe init", () => {
2424
expect(
2525
new FeatureProbe({
26-
remoteUrl: 'http://127.0.0.1:4007',
27-
clientSdkKey: 'client-sdk-key1',
28-
user: new FPUser("123")
26+
remoteUrl: "http://127.0.0.1:4007",
27+
clientSdkKey: "client-sdk-key1",
28+
user: new FPUser("123"),
2929
})
3030
).not.toBeNull();
3131
});
3232

33-
test("feature probe request", async () => {
33+
test("feature probe request", (done) => {
3434
_fetch.mockResponseOnce(JSON.stringify(data));
3535
let user = new FPUser("some-key").with("city", "2");
3636
let fp = new FeatureProbe({
37-
remoteUrl: 'http://127.0.0.1:4007',
38-
clientSdkKey: 'client-sdk-key1',
39-
user: user
37+
remoteUrl: "http://127.0.0.1:4007",
38+
clientSdkKey: "client-sdk-key1",
39+
user: user,
4040
});
4141
fp.start();
42-
fp.on('ready', function() {
42+
fp.on("ready", function () {
4343
expect(fp.boolValue("bool_toggle", false)).toBe(true);
44-
})
44+
done();
45+
});
4546
});
4647

47-
test("feature probe bool toggle", () => {
48+
test("feature probe bool toggle", (done) => {
4849
_fetch.mockResponseOnce(JSON.stringify(data));
4950
let user = new FPUser("some-key").with("city", "2");
5051
let fp = new FeatureProbe({
51-
remoteUrl: 'http://127.0.0.1:4007',
52-
clientSdkKey: 'client-sdk-key1',
53-
user: user
52+
remoteUrl: "http://127.0.0.1:4007",
53+
clientSdkKey: "client-sdk-key1",
54+
user: user,
5455
});
5556
fp.start();
56-
fp.on('ready', function() {
57+
fp.on("ready", function () {
5758
expect(fp.boolValue("bool_toggle", false)).toBe(true);
5859
expect(fp.boolValue("string_toggle", false)).toBe(false);
5960
expect(fp.boolValue("__not_exist_toggle", false)).toBe(false);
6061

6162
let detail = fp.boolDetail("bool_toggle", false);
6263
expect(detail.value).toBe(true);
6364
expect(detail.ruleIndex).toBe(0);
64-
65+
6566
detail = fp.boolDetail("string_toggle", false);
6667
expect(detail.value).toBe(false);
6768
expect(detail.reason).toBe("Value type mismatch");
68-
})
69+
done();
70+
});
6971
});
7072

71-
test("feature probe number toggle", () => {
73+
test("feature probe number toggle", (done) => {
7274
_fetch.mockResponseOnce(JSON.stringify(data));
7375
let user = new FPUser("some-key").with("city", "2");
7476
let fp = new FeatureProbe({
75-
remoteUrl: 'http://127.0.0.1:4007',
76-
clientSdkKey: 'client-sdk-key1',
77-
user: user
77+
remoteUrl: "http://127.0.0.1:4007",
78+
clientSdkKey: "client-sdk-key1",
79+
user: user,
7880
});
7981
fp.start();
80-
fp.on('ready', function() {
82+
fp.on("ready", function () {
8183
expect(fp.numberValue("number_toggle", 0)).toBe(1);
8284
expect(fp.numberValue("string_toggle", 0)).toBe(0);
8385
expect(fp.numberValue("__not_exist_toggle", 1)).toBe(1);
84-
86+
8587
let detail = fp.numberDetail("number_toggle", 0);
8688
expect(detail.value).toBe(1);
8789
expect(detail.ruleIndex).toBe(0);
88-
90+
8991
detail = fp.numberDetail("string_toggle", 404);
9092
expect(detail.value).toBe(404);
9193
expect(detail.reason).toBe("Value type mismatch");
92-
})
94+
done();
95+
});
9396
});
9497

95-
test("feature probe string toggle", () => {
98+
test("feature probe string toggle", (done) => {
9699
_fetch.mockResponseOnce(JSON.stringify(data));
97100
let user = new FPUser("some-key").with("city", "2");
98101
let fp = new FeatureProbe({
99-
remoteUrl: 'http://127.0.0.1:4007',
100-
clientSdkKey: 'client-sdk-key1',
101-
user: user
102+
remoteUrl: "http://127.0.0.1:4007",
103+
clientSdkKey: "client-sdk-key1",
104+
user: user,
102105
});
103106
fp.start();
104-
fp.on('ready', function() {
107+
fp.on("ready", function () {
105108
expect(fp.stringValue("string_toggle", "ok")).toBe("1");
106109
expect(fp.stringValue("bool_toggle", "not_match")).toBe("not_match");
107110
expect(fp.stringValue("__not_exist_toggle", "not_exist")).toBe("not_exist");
108-
111+
109112
let detail = fp.stringDetail("bool_toggle", "not match");
110113
expect(detail.value).toBe("not match");
111114
expect(detail.reason).toBe("Value type mismatch");
112-
115+
113116
detail = fp.stringDetail("string_toggle", "defaultValue");
114117
expect(detail.value).toBe("1");
115118
expect(detail.ruleIndex).toBe(0);
116-
})
119+
120+
done();
121+
});
117122
});
118123

119-
test("feature probe json toggle", () => {
124+
test("feature probe json toggle", (done) => {
120125
_fetch.mockResponseOnce(JSON.stringify(data));
121126
let user = new FPUser("some-key").with("city", "2");
122127
let fp = new FeatureProbe({
123-
remoteUrl: 'http://127.0.0.1:4007',
124-
clientSdkKey: 'client-sdk-key1',
125-
user: user
128+
remoteUrl: "http://127.0.0.1:4007",
129+
clientSdkKey: "client-sdk-key1",
130+
user: user,
126131
});
127132
fp.start();
128133

129-
fp.on('ready', function() {
134+
fp.on("ready", function () {
130135
expect(fp.jsonValue("json_toggle", {})).toMatchObject({
131136
v: "v1",
132137
variation_0: "c2",
@@ -141,21 +146,17 @@ test("feature probe json toggle", () => {
141146
detail = fp.jsonDetail("json_toggle", {});
142147
expect(detail.value).toMatchObject({});
143148
expect(detail.ruleIndex).toBe(0);
144-
})
149+
done();
150+
});
145151
});
146152

147-
test("feature probe unit testing", () => {
148-
_fetch.mockResponseOnce(JSON.stringify({"testToggle": { "value": true}}));
149-
let user = new FPUser("some-key")
150-
let fp = new FeatureProbe({
151-
remoteUrl: 'http://127.0.0.1:4007',
152-
clientSdkKey: 'client-sdk-key1',
153-
user: user
154-
});
153+
test("feature probe unit testing", (done) => {
154+
let fp = FeatureProbe.newForTest({ testToggle: true });
155155
fp.start();
156156

157-
fp.on('ready', function() {
158-
let t = fp.boolValue("testToggle", false)
157+
fp.on("ready", function () {
158+
let t = fp.boolValue("testToggle", false);
159159
expect(t).toBe(true);
160-
})
160+
done();
161+
});
161162
});

0 commit comments

Comments
 (0)