Skip to content

Commit 1ccff05

Browse files
committed
chore: add unit testing to README
1 parent 84907cf commit 1ccff05

File tree

3 files changed

+55
-22
lines changed

3 files changed

+55
-22
lines changed

README.md

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# FeatureProbe Client Side SDK for JavaScript
2+
23
[![Top Language](https://img.shields.io/github/languages/top/FeatureProbe/client-sdk-js)](https://github.com/FeatureProbe/client-sdk-js/search?l=rust)
34
[![Coverage Status](https://coveralls.io/repos/github/FeatureProbe/client-sdk-js/badge.svg?branch=main)](https://coveralls.io/github/FeatureProbe/client-sdk-js?branch=main)
45
[![Github Star](https://img.shields.io/github/stars/FeatureProbe/client-sdk-js)](https://github.com/FeatureProbe/client-sdk-js/stargazers)
@@ -25,27 +26,22 @@ Or via CDN:
2526
<script type="text/javascript" src="https://unpkg.com/featureprobe-client-sdk-js@latest/dist/featureprobe-client-sdk-js.min.js"></script>
2627
```
2728

28-
2929
### Step 2. Create a FeatureProbe instance
3030

3131
After you install and import the SDK, create a single, shared instance of the FeatureProbe sdk.
3232

3333
```js
34-
const user = new featureProbe.FPUser("#USER-KEY#");
34+
const user = new featureProbe.FPUser("#USER-KEY#");
3535
user.with("#ATTR-KEY#", "#ATTR-VALUE#");
3636

3737
const fp = new featureProbe.FeatureProbe({
3838
remoteUrl: "#OPEN-API-URL#",
39-
togglesUrl: "#YOUR-TOGGLES-URL#",
40-
eventsUrl: "#YOUR-EVENTS-URL#",
4139
clientSdkKey: '#YOUR-CLIENT-SDK-KEY#',
4240
user,
43-
refreshInterval: 1000,
4441
});
4542
fp.start();
4643
```
4744

48-
4945
### Step 3. Use the instance to get your setting value
5046

5147
You can use sdk to check which value this user will receive for a given feature flag.
@@ -63,18 +59,40 @@ fp.on('ready', function() {
6359
})
6460
```
6561

62+
### Step 4. Unit Testing (Optional)
63+
64+
```js
65+
import { FetchMock } from "jest-fetch-mock";
66+
67+
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+
});
75+
fp.start();
76+
77+
fp.on('ready', function() {
78+
let t = fp.boolValue("testToggle", false)
79+
expect(t).toBe(true);
80+
})
81+
});
82+
```
83+
6684
## Available options
6785

6886
This SDK takes the following options:
6987

7088
| option | required | default | description |
7189
|-------------------|----------------|---------|--------------------------------------------------------------------------------------------------------------------------------------------------|
7290
| remoteUrl | it all depends | n/a | The common URL to get toggles and send events. E.g.: `https://featureprobe.com/api` It is required when togglesUrl and eventsUrl are not set |
73-
| togglesUrl | no | n/a | The specific URL to get toggles, once set, remoteUrl become invalid |
74-
| eventsUrl | no | n/a | The specific URL to send events, once set, remoteUrl become invalid |
75-
| clientSdkKey | yes | n/a | The Client SDK Key URL to be used |
76-
| user | yes | n/a | Pass a valid user context for SDK initialization |
77-
| refreshInterval | no | 1000 | The SDK check for updated in millisecond |
91+
| togglesUrl | no | n/a | The specific URL to get toggles, once set, remoteUrl become invalid |
92+
| eventsUrl | no | n/a | The specific URL to send events, once set, remoteUrl become invalid |
93+
| clientSdkKey | yes | n/a | The Client SDK Key URL to be used |
94+
| user | yes | n/a | Pass a valid user context for SDK initialization |
95+
| refreshInterval | no | 1000 | The SDK check for updated in millisecond |
7896

7997
## Testing
8098

@@ -86,15 +104,14 @@ be sure to pull submodules first to get the latest integration tests before runn
86104
```
87105

88106
## Contributing
89-
We are working on continue evolving FeatureProbe core, making it flexible and easier to use.
90-
Development of FeatureProbe happens in the open on GitHub, and we are grateful to the
107+
108+
We are working on continue evolving FeatureProbe core, making it flexible and easier to use.
109+
Development of FeatureProbe happens in the open on GitHub, and we are grateful to the
91110
community for contributing bugfixes and improvements.
92111

93-
Please read [CONTRIBUTING](https://github.com/FeatureProbe/featureprobe/blob/master/CONTRIBUTING.md)
112+
Please read [CONTRIBUTING](https://github.com/FeatureProbe/featureprobe/blob/master/CONTRIBUTING.md)
94113
for details on our code of conduct, and the process for taking part in improving FeatureProbe.
95114

96-
97115
## License
98116

99117
This project is licensed under the Apache 2.0 License - see the [LICENSE](LICENSE) file for details.
100-

src/FeatureProbe.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const EVENTS = {
1313

1414
interface IValue {
1515
count: number;
16-
value: boolean | number | object;
16+
value: boolean | string | number | object;
1717
index: number | null;
1818
version: number | null;
1919
}
@@ -32,7 +32,7 @@ interface IParams {
3232
}
3333

3434
interface FPToggleDetail {
35-
value: boolean | number | object;
35+
value: boolean | string | number | object;
3636
ruleIndex: number | null;
3737
variationIndex: number | null;
3838
version: number | null;
@@ -217,7 +217,7 @@ class FeatureProbe extends TinyEmitter {
217217
headers: {
218218
Authorization: this.clientSdkKey,
219219
"Content-Type": "application/json",
220-
"UA": UA,
220+
UA: UA,
221221
},
222222
})
223223
.then((response) => {
@@ -259,7 +259,7 @@ class FeatureProbe extends TinyEmitter {
259259
headers: {
260260
Authorization: this.clientSdkKey,
261261
"Content-Type": "application/json",
262-
"UA": UA,
262+
UA: UA,
263263
},
264264
body: JSON.stringify(payload),
265265
});

test/FeatureProbe.test.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,13 +133,29 @@ test("feature probe json toggle", () => {
133133
});
134134
expect(fp.jsonValue("bool_toggle", {})).toMatchObject({});
135135
expect(fp.jsonValue("__not_exist_toggle", {})).toMatchObject({});
136-
136+
137137
let detail = fp.jsonDetail("bool_toggle", {});
138138
expect(detail.value).toMatchObject({});
139139
expect(detail.reason).toBe("Value type mismatch");
140-
140+
141141
detail = fp.jsonDetail("json_toggle", {});
142142
expect(detail.value).toMatchObject({});
143143
expect(detail.ruleIndex).toBe(0);
144144
})
145145
});
146+
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+
});
155+
fp.start();
156+
157+
fp.on('ready', function() {
158+
let t = fp.boolValue("testToggle", false)
159+
expect(t).toBe(true);
160+
})
161+
});

0 commit comments

Comments
 (0)