Skip to content

Commit eda6ee0

Browse files
committed
test: metrics counting
1 parent 59cc978 commit eda6ee0

File tree

3 files changed

+58
-11
lines changed

3 files changed

+58
-11
lines changed

src/test/client.test.ts

+41-4
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ test('should add environment to isEnabled calls', () => {
1414

1515
config.disableMetrics = true;
1616

17-
const client = createFakeClient(config);
17+
const { client } = createFakeClient(config);
1818

1919
const fakeUnleash = client.unleash as FakeUnleash;
2020

@@ -46,7 +46,7 @@ test('should override environment to isEnabled calls', () => {
4646

4747
config.disableMetrics = true;
4848

49-
const client = createFakeClient(config);
49+
const { client } = createFakeClient(config);
5050

5151
const fakeUnleash = client.unleash as FakeUnleash;
5252

@@ -78,7 +78,7 @@ test('should return all toggles', () => {
7878

7979
config.disableMetrics = true;
8080

81-
const client = createFakeClient(config);
81+
const { client } = createFakeClient(config);
8282

8383
const fakeUnleash = client.unleash as FakeUnleash;
8484

@@ -132,7 +132,7 @@ test('should return default variant for disabled toggles', () => {
132132

133133
config.disableMetrics = true;
134134

135-
const client = createFakeClient(config);
135+
const { client } = createFakeClient(config);
136136

137137
const fakeUnleash = client.unleash as FakeUnleash;
138138

@@ -180,3 +180,40 @@ test('should return default variant for disabled toggles', () => {
180180
expect(result[2].variant?.enabled).toBe(false);
181181
client.destroy();
182182
});
183+
184+
test('should register metrics', () => {
185+
const config = createProxyConfig({
186+
unleashApiToken: '123',
187+
unleashUrl: 'http://localhost:4242/api',
188+
proxySecrets: ['s1'],
189+
environment: 'never-change-me',
190+
logLevel: LogLevel.error,
191+
});
192+
193+
const { client, metrics } = createFakeClient(config);
194+
195+
client.registerMetrics({
196+
bucket: {
197+
toggles: {
198+
toggle: {
199+
yes: 3,
200+
no: 1,
201+
variants: { variantA: 2, variantB: 1, disabled: 1 },
202+
},
203+
},
204+
},
205+
});
206+
207+
expect(metrics.recordedCount).toStrictEqual([
208+
['toggle', true],
209+
['toggle', true],
210+
['toggle', true],
211+
['toggle', false],
212+
]);
213+
expect(metrics.recordedCountVariant).toStrictEqual([
214+
['toggle', 'variantA'],
215+
['toggle', 'variantA'],
216+
['toggle', 'variantB'],
217+
['toggle', 'disabled'],
218+
]);
219+
});

src/test/create-fake-client.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ import { defaultStrategies } from 'unleash-client/lib/strategy';
44
import FakeUnleash from './unleash.mock';
55
import FakeMetrics from './metrics.mock';
66

7-
export const createFakeClient = (config: IProxyConfig): Client => {
7+
export const createFakeClient = (
8+
config: IProxyConfig,
9+
): { client: Client; metrics: FakeMetrics } => {
810
const unleash = new FakeUnleash({
911
...config,
1012
url: config.unleashUrl,
@@ -19,5 +21,7 @@ export const createFakeClient = (config: IProxyConfig): Client => {
1921
strategies: defaultStrategies.map((s) => s.name),
2022
});
2123

22-
return new Client(config, unleash, metrics);
24+
const client = new Client(config, unleash, metrics);
25+
26+
return { client, metrics };
2327
};

src/test/metrics.mock.ts

+11-5
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,23 @@
11
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
22

3-
/* eslint-disable @typescript-eslint/no-unused-vars */
4-
53
import Metrics from 'unleash-client/lib/metrics';
64

75
class FakeMetrics extends Metrics {
6+
recordedCount: [string, boolean][] = [];
7+
8+
recordedCountVariant: [string, string][] = [];
9+
810
start() {}
911

10-
count(name: string, enabled: boolean) {}
12+
count(name: string, enabled: boolean) {
13+
this.recordedCount.push([name, enabled]);
14+
}
1115

12-
countVariant(name: string, variantName: string) {}
16+
countVariant(name: string, variantName: string) {
17+
this.recordedCountVariant.push([name, variantName]);
18+
}
1319

14-
on(eventName: string | symbol, listener: (...args: any[]) => void): this {
20+
on(): this {
1521
return this;
1622
}
1723
}

0 commit comments

Comments
 (0)