-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevaluation.spec.js
More file actions
76 lines (67 loc) · 2.95 KB
/
evaluation.spec.js
File metadata and controls
76 lines (67 loc) · 2.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/* eslint-disable @typescript-eslint/no-unused-vars */
import { FlagNotFoundError, StandardResolutionReasons } from '@openfeature/web-sdk';
import { evaluateTreatment } from '../lib/evaluation';
import { CONTROL_TREATMENT } from '../lib/types';
describe('evaluation', () => {
describe('evaluateTreatment', () => {
let mockClient;
beforeEach(() => {
mockClient = {
getTreatmentWithConfig: jest.fn((flagKey, attributes) => ({
treatment: 'v1',
config: '{"x":1}',
})),
};
});
test('returns resolution details with value, variant, flagMetadata, reason', () => {
const consumer = { targetingKey: 'u1', trafficType: 'user', attributes: {} };
const result = evaluateTreatment(mockClient, 'my-flag', consumer);
expect(result.value).toBe('v1');
expect(result.variant).toBe('v1');
expect(result.flagMetadata).toEqual({ config: '{"x":1}' });
expect(result.reason).toBe(StandardResolutionReasons.TARGETING_MATCH);
expect(mockClient.getTreatmentWithConfig).toHaveBeenCalledWith('my-flag', {});
});
test('calls getTreatmentWithConfig with consumer attributes', () => {
const consumer = {
targetingKey: 'u1',
trafficType: 'account',
attributes: { region: 'eu', plan: 'pro' },
};
evaluateTreatment(mockClient, 'flag', consumer);
expect(mockClient.getTreatmentWithConfig).toHaveBeenCalledWith('flag', {
region: 'eu',
plan: 'pro',
});
});
test('uses empty string for config when config is falsy', () => {
mockClient.getTreatmentWithConfig.mockReturnValue({ treatment: 'on', config: null });
const result = evaluateTreatment(mockClient, 'f', {
targetingKey: undefined,
trafficType: 'user',
attributes: {},
});
expect(result.flagMetadata.config).toBe('');
});
test('throws FlagNotFoundError when flagKey is null', () => {
const consumer = { targetingKey: 'u1', trafficType: 'user', attributes: {} };
expect(() => evaluateTreatment(mockClient, null, consumer)).toThrow(FlagNotFoundError);
expect(() => evaluateTreatment(mockClient, null, consumer)).toThrow(
/flagKey must be a non-empty string/
);
});
test('throws FlagNotFoundError when flagKey is empty string', () => {
const consumer = { targetingKey: 'u1', trafficType: 'user', attributes: {} };
expect(() => evaluateTreatment(mockClient, '', consumer)).toThrow(FlagNotFoundError);
});
test('throws FlagNotFoundError when treatment is control', () => {
mockClient.getTreatmentWithConfig.mockReturnValue({
treatment: CONTROL_TREATMENT,
config: '',
});
const consumer = { targetingKey: 'u1', trafficType: 'user', attributes: {} };
expect(() => evaluateTreatment(mockClient, 'flag', consumer)).toThrow(FlagNotFoundError);
expect(() => evaluateTreatment(mockClient, 'flag', consumer)).toThrow(/control/);
});
});
});