-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathunleash.context.spec.ts
69 lines (58 loc) · 1.55 KB
/
unleash.context.spec.ts
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
import { UnleashModuleOptions } from '.'
import { Request } from '../unleash-strategies'
import { UnleashContext } from './unleash.context'
function createRequest(
data: Partial<Request<{ id: string }>> = {},
): Request<{ id: string }> {
return data
}
interface MyCustomData {
foo: boolean
bar: string
}
describe('UnleashContext', () => {
let context: UnleashContext
let req: Request<{
id: string
}>
beforeEach(() => {
req = createRequest({
ip: '1.2.3.4',
user: {
id: '123',
},
session: {
id: 'abc',
},
})
context = new UnleashContext(req, {} as UnleashModuleOptions)
})
test('getUserId()', () => {
expect(context.getUserId()).toBe('123')
})
test('getRemoteAddress()', () => {
expect(context.getRemoteAddress()).toBe('1.2.3.4')
})
test('getSessionId()', () => {
expect(context.getSessionId()).toBe('abc')
// @ts-ignore
context.request.session = { sessionId: 'def' }
expect(context.getSessionId()).toBe('def')
})
test('getRequest()', () => {
expect(context.getRequest()).toStrictEqual(req)
// @ts-ignore
context.request = { hello: 'world' }
expect(context.getRequest()).toStrictEqual({ hello: 'world' })
})
describe('Custom data', () => {
test('extend()', () => {
const context = new UnleashContext<MyCustomData>(
req,
{} as UnleashModuleOptions,
)
const extendedContext = context.extend({ foo: true, bar: 'baz' })
expect(extendedContext.customData).toEqual({ foo: true, bar: 'baz' })
})
})
})