-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathget-value-assert-defined.function.spec.ts
More file actions
50 lines (42 loc) · 1.27 KB
/
get-value-assert-defined.function.spec.ts
File metadata and controls
50 lines (42 loc) · 1.27 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
import { getValueAssertDefined } from './get-value-assert-defined.function.js'
describe('getValueAssertDefined', () => {
interface Test {
x: string | null
y?: boolean
z: number | undefined
}
describe('throws when not defined', () => {
const empty: Test = { x: null, z: undefined }
test('when null', () => {
expect(() => getValueAssertDefined(empty, 'x')).toThrow()
})
test('when undefined', () => {
expect(() => getValueAssertDefined(empty, 'z')).toThrow()
})
test('when optional', () => {
expect(() => getValueAssertDefined(empty, 'y')).toThrow()
})
})
describe('returns value when defined', () => {
const obj: Test = {
x: '',
y: false,
z: 0,
}
test('when empty string', () => {
expect(getValueAssertDefined(obj, 'x')).toEqual('')
})
test('when false', () => {
expect(getValueAssertDefined(obj, 'y')).toEqual(false)
})
test('when zero', () => {
expect(getValueAssertDefined(obj, 'z')).toEqual(0)
})
test('makes it type safe', () => {
const tDefined: { num: number | null } = { num: 42 }
// assign to variable to ensure type safety
const res: number = getValueAssertDefined(tDefined, 'num')
expect(res).toEqual(42)
})
})
})