-
Notifications
You must be signed in to change notification settings - Fork 2
/
ProtocolType.spec.ts
132 lines (106 loc) · 3.71 KB
/
ProtocolType.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/* eslint-disable @typescript-eslint/explicit-function-return-type */
import { ProtocolType, isProtocolType, ProtocolDataType } from './ProtocolType';
import {
DataTypeID,
DataTypeString,
DataTypeInteger,
DataTypeBoolean,
} from '../datatype/dataTypes';
import { DataType } from '../datatype/DataType';
describe('ProtocolType', () => {
const ProtocolTypeREST = new ProtocolType({
name: 'ProtocolTypeREST',
description: 'REST API protocol',
isProtocolDataType(protocolDataType) {
return typeof protocolDataType === 'object' && protocolDataType.name;
},
});
const ProtocolDataTypeID = {
name: 'ProtocolDataTypeID',
};
const ProtocolDataTypeString = {
name: 'ProtocolDataTypeString',
};
const ProtocolDataTypeInteger = {
name: 'ProtocolDataTypeInteger',
};
it('should reject invalid protocol type definitions', () => {
function fn1() {
// eslint-disable-next-line no-new
new ProtocolType({});
}
function fn2() {
// eslint-disable-next-line no-new
new ProtocolType({
name: 'REST',
});
}
expect(fn1).toThrowErrorMatchingSnapshot();
expect(fn2).toThrowErrorMatchingSnapshot();
});
it('should implement isProtocolDataType', () => {
function fn() {
// eslint-disable-next-line no-new
new ProtocolType({
name: 'REST',
description: 'REST protocol type',
});
}
expect(fn).toThrowErrorMatchingSnapshot();
});
it('should recognize object of type ProtocolType', () => {
const result = isProtocolType(ProtocolTypeREST);
expect(result).toBe(true);
});
it('should recognize non-ProtocolType objects', () => {
const result1 = isProtocolType({});
const result2 = isProtocolType(123);
const result3 = isProtocolType('test');
expect(result1).toBe(false);
expect(result2).toBe(false);
expect(result3).toBe(false);
});
it('should map data types to protocol data types', () => {
ProtocolTypeREST.addDataTypeMap(DataTypeID, ProtocolDataTypeID);
ProtocolTypeREST.addDataTypeMap(DataTypeString, ProtocolDataTypeString);
});
it('should reject invalid data type mappings', () => {
function fn1() {
ProtocolTypeREST.addDataTypeMap(('something' as unknown) as DataType, undefined);
}
function fn2() {
ProtocolTypeREST.addDataTypeMap(DataTypeID, undefined);
}
function fn3() {
ProtocolTypeREST.addDataTypeMap(DataTypeString, ({} as unknown) as ProtocolDataType);
}
expect(fn1).toThrowErrorMatchingSnapshot();
expect(fn2).toThrowErrorMatchingSnapshot();
expect(fn3).toThrowErrorMatchingSnapshot();
});
it('should reject duplicate data type mappings', () => {
ProtocolTypeREST.addDataTypeMap(DataTypeInteger, ProtocolDataTypeInteger);
function fn() {
ProtocolTypeREST.addDataTypeMap(DataTypeInteger, ProtocolDataTypeInteger);
}
expect(fn).toThrowErrorMatchingSnapshot();
});
it('should convert to protocol data types', () => {
const result1 = ProtocolTypeREST.convertToProtocolDataType(DataTypeID);
const result2 = ProtocolTypeREST.convertToProtocolDataType(DataTypeString);
const result3 = ProtocolTypeREST.convertToProtocolDataType(DataTypeInteger);
expect(result1).toEqual(ProtocolDataTypeID);
expect(result2).toEqual(ProtocolDataTypeString);
expect(result3).toEqual(ProtocolDataTypeInteger);
});
it('should throw if unknown data type mapping is requested', () => {
function fn1() {
ProtocolTypeREST.convertToProtocolDataType('DataTypeInteger');
}
function fn2() {
ProtocolTypeREST.convertToProtocolDataType(DataTypeBoolean);
}
expect(fn1).toThrowErrorMatchingSnapshot();
expect(fn2).toThrowErrorMatchingSnapshot();
});
});