tests/common/common-util.test.js   A
last analyzed

Complexity

Total Complexity 8
Complexity/F 1

Size

Lines of Code 101
Function Count 8

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 71
dl 0
loc 101
rs 10
c 0
b 0
f 0
wmc 8
mnd 0
bc 0
fnc 8
bpm 0
cpm 1
noi 8
1
/* global describe test expect beforeAll afterAll */
0 ignored issues
show
Unused Code introduced by
'describe' is defined but never used.
Loading history...
Unused Code introduced by
'beforeAll' is defined but never used.
Loading history...
Unused Code introduced by
'afterAll' is defined but never used.
Loading history...
2
3
const util = require('../../src/common/common-util');
4
5
const snakeCaseArray = [
6
  { id: '1', nick_name: 'nick1', contacts: [{ contact_type: 'phone', value: '000-000-000' }, { contact_type: 'email', value: '[email protected]' }] },
0 ignored issues
show
Coding Style introduced by
This line exceeds the maximum configured line length of 120.
Loading history...
7
  { id: '2', nick_name: 'nick2', contacts: [] },
8
  { id: '3', nick_name: 'nick3', contacts: [{ contact_type: 'address', value: 'xxx' }] },
9
];
10
11
const camelCaseArray = [
12
  { id: '1', nickName: 'nick1', contacts: [{ contactType: 'phone', value: '000-000-000' }, { contactType: 'email', value: '[email protected]' }] },
0 ignored issues
show
Coding Style introduced by
This line exceeds the maximum configured line length of 120.
Loading history...
13
  { id: '2', nickName: 'nick2', contacts: [] },
14
  { id: '3', nickName: 'nick3', contacts: [{ contactType: 'address', value: 'xxx' }] },
15
];
16
17
const snakeCaseObject = {
18
  id: '1',
19
  nick_name: 'nick1',
20
  contacts: [{ contact_type: 'phone', value: '000-000-000' }, { contact_type: 'email', value: '[email protected]' }],
21
};
22
23
const camelCaseObject = {
24
  id: '1',
25
  nickName: 'nick1',
26
  contacts: [{ contactType: 'phone', value: '000-000-000' }, { contactType: 'email', value: '[email protected]' }],
27
};
28
29
test('snake2camel', () => {
30
  expect(util.snake2camel('snake_case_string')).toBe('snakeCaseString');
31
  expect(util.snake2camel('_snake_case_string')).toBe('snakeCaseString');
32
  expect(util.snake2camel('snake_case_string_')).toBe('snakeCaseString');
33
  expect(util.snake2camel('snake__case__string')).toBe('snakeCaseString');
34
  expect(util.snake2camel('this_is_1st')).toBe('thisIs1st');
35
});
36
37
test('camel2snake', () => {
38
  expect(util.camel2snake('camelCaseString')).toBe('camel_case_string');
39
  expect(util.camel2snake('SSString')).toBe('s_s_string');
40
  expect(util.camel2snake('1stNumberOccurrences')).toBe('1st_number_occurrences');
41
  expect(util.camel2snake('StartWithCapital')).toBe('start_with_capital');
42
  expect(util.camel2snake('numbersLike1AreIgnored')).toBe('numbers_like1_are_ignored');
43
});
44
45
test('snake2camelObject', () => {
46
  expect(util.snake2camelObject([])).toEqual([]);
47
  expect(util.snake2camelObject({})).toEqual({});
48
  expect(util.snake2camelObject(undefined)).toBe(undefined);
49
  expect(util.snake2camelObject(null)).toBe(null);
50
  expect(util.snake2camelObject(snakeCaseArray)).toEqual(camelCaseArray);
51
  expect(util.snake2camelObject(snakeCaseObject)).toEqual(camelCaseObject);
52
});
53
54
test('camel2snakeObject', () => {
55
  expect(util.camel2snakeObject([])).toEqual([]);
56
  expect(util.camel2snakeObject({})).toEqual({});
57
  expect(util.camel2snakeObject(undefined)).toBe(undefined);
58
  expect(util.camel2snakeObject(null)).toBe(null);
59
  expect(util.camel2snakeObject(camelCaseArray)).toEqual(snakeCaseArray);
60
  expect(util.camel2snakeObject(camelCaseObject)).toEqual(snakeCaseObject);
61
});
62
63
const semVersions = [
64
  {
65
    str: '=2.3',
66
    obj: [{ comparator: '=', version: '2.3' }]
0 ignored issues
show
introduced by
Missing trailing comma.
Loading history...
67
  },
68
  {
69
    str: '=2',
70
    obj: [{ comparator: '=', version: '2' }],
71
  },
72
  {
73
    str: '>=1.2.3 <3.0',
74
    obj: [{ comparator: '~', versionStart: '1.2.3', versionEnd: '3.0' }],
75
  },
76
  {
77
    str: '*',
78
    obj: [{ comparator: '*' }],
79
  },
80
  {
81
    str: '>=1.2.3 <3.0.0 || <0.1.2 || >=5.1 || =0.0.1',
82
    obj: [
83
      { comparator: '~', versionStart: '1.2.3', versionEnd: '3.0.0' },
84
      { comparator: '~', versionEnd: '0.1.2' },
85
      { comparator: '~', versionStart: '5.1' },
86
      { comparator: '=', version: '0.0.1' },
87
    ],
88
  },
89
];
90
91
test('parseSemVersion', () => {
0 ignored issues
show
introduced by
Unexpected block statement surrounding arrow body; move the returned value immediately after the =>.
Loading history...
92
  return Promise.all(semVersions.map((version) => {
93
    expect(util.parseSemVersion(version.str)).toEqual(version.obj);
94
    return Promise.resolve();
95
  }));
96
});
97
98
test('stringifySemVersion', () => {
0 ignored issues
show
introduced by
Unexpected block statement surrounding arrow body; move the returned value immediately after the =>.
Loading history...
99
  return Promise.all(semVersions.map((version) => {
100
    expect(util.stringifySemVersion(version.obj)).toBe(version.str);
101
    return Promise.resolve();
102
  }));
103
});
104