/
label.spec.ts
103 lines (91 loc) · 3.47 KB
/
label.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
import { HttpParamStyles } from '@stoplight/types';
import { LabelStyleDeserializer } from '../label';
import * as createObjectFromKeyValListModule from '../utils';
describe('LabelStyleDeserializer', () => {
const labelStyleDeserializer = new LabelStyleDeserializer();
describe('supports()', () => {
describe('style is supported', () => {
it('returns true', () => {
expect(labelStyleDeserializer.supports(HttpParamStyles.Label)).toBe(true);
});
});
describe('style is not supported', () => {
it('returns false', () => {
expect(labelStyleDeserializer.supports(HttpParamStyles.Simple)).toBe(false);
});
});
});
describe('deserialize()', () => {
describe('value does not begins with a dot', () => {
it('throws exception', () => {
expect(() => labelStyleDeserializer.deserialize('name', { name: 'bad' }, { type: 'string' })).toThrowError(
'Label serialization style requires parameter to be prefixed with "."'
);
});
});
describe('type is a primitive', () => {
it('returns unmodified value', () => {
expect(labelStyleDeserializer.deserialize('name', { name: '.value' }, { type: 'string' }, false)).toEqual(
'value'
);
});
});
describe('type is an array', () => {
describe('explode is not set', () => {
describe('no value provided', () => {
it('returns empty array', () => {
expect(labelStyleDeserializer.deserialize('name', { name: '.' }, { type: 'array' }, false)).toEqual([]);
});
});
describe('comma separated list provided', () => {
it('returns exploded array', () => {
expect(labelStyleDeserializer.deserialize('name', { name: '.a,b,c' }, { type: 'array' }, false)).toEqual([
'a',
'b',
'c',
]);
});
});
});
describe('explode is set', () => {
describe('no value provided', () => {
it('returns empty array', () => {
expect(labelStyleDeserializer.deserialize('name', { name: '.' }, { type: 'array' }, true)).toEqual([]);
});
});
describe('comma separated list provided', () => {
it('returns exploded array', () => {
expect(labelStyleDeserializer.deserialize('name', { name: '.a.b.c' }, { type: 'array' }, true)).toEqual([
'a',
'b',
'c',
]);
});
});
});
});
describe('type is an object', () => {
describe('explode is not set', () => {
it('splits by comma and returns object', () => {
jest.spyOn(createObjectFromKeyValListModule, 'createObjectFromKeyValList').mockImplementationOnce(list => {
expect(list).toEqual(['a', 'b', 'c', 'd']);
return { a: 'b', c: 'd' };
});
expect(labelStyleDeserializer.deserialize('name', { name: '.a,b,c,d' }, { type: 'object' }, false)).toEqual({
a: 'b',
c: 'd',
});
expect(createObjectFromKeyValListModule.createObjectFromKeyValList).toHaveBeenCalled();
});
});
describe('explode is set', () => {
it('splits by comma and equality sign and returns object', () => {
expect(labelStyleDeserializer.deserialize('name', { name: '.a=b,c=d' }, { type: 'object' }, true)).toEqual({
a: 'b',
c: 'd',
});
});
});
});
});
});