-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontext.spec.js
More file actions
64 lines (57 loc) · 2.06 KB
/
context.spec.js
File metadata and controls
64 lines (57 loc) · 2.06 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
import { transformContext } from '../lib/context';
describe('context', () => {
describe('transformContext', () => {
const defaultTrafficType = 'user';
test('uses defaultTrafficType when context has no trafficType', () => {
const result = transformContext({ targetingKey: 'key-1' }, defaultTrafficType);
expect(result.trafficType).toBe('user');
expect(result.targetingKey).toBe('key-1');
expect(result.attributes).toEqual({});
});
test('uses context trafficType when present and non-empty', () => {
const result = transformContext(
{ targetingKey: 'key-1', trafficType: 'account' },
defaultTrafficType
);
expect(result.trafficType).toBe('account');
expect(result.targetingKey).toBe('key-1');
expect(result.attributes).toEqual({});
});
test('falls back to default when trafficType is empty string', () => {
const result = transformContext(
{ targetingKey: 'key-1', trafficType: '' },
defaultTrafficType
);
expect(result.trafficType).toBe('user');
});
test('falls back to default when trafficType is whitespace', () => {
const result = transformContext(
{ targetingKey: 'key-1', trafficType: ' ' },
defaultTrafficType
);
expect(result.trafficType).toBe('user');
});
test('passes remaining context as attributes', () => {
const result = transformContext(
{
targetingKey: 'key-1',
trafficType: 'user',
region: 'eu',
plan: 'pro',
},
defaultTrafficType
);
expect(result.attributes).toEqual({ region: 'eu', plan: 'pro' });
});
test('deep-clones attributes (no reference)', () => {
const attrs = { nested: { value: 1 } };
const result = transformContext(
{ targetingKey: 'k', ...attrs },
defaultTrafficType
);
expect(result.attributes).toEqual({ nested: { value: 1 } });
expect(result.attributes).not.toBe(attrs);
expect(result.attributes.nested).not.toBe(attrs.nested);
});
});
});