Mirror of https://github.com/roostorg/coop
github.com/roostorg/coop
1import {
2 splitByWhitespaceAndCommas,
3 titleCaseEnumString,
4 toHumanReadableLabel,
5 truncateIdIfNeeded,
6} from './string';
7
8describe('String utils tests', () => {
9 describe('Split by whitespace and commas', () => {
10 it('Split well formatted comma string without whitespace', () => {
11 const inputString = '1,2,3,4,5';
12 expect(splitByWhitespaceAndCommas(inputString)).toMatchObject([
13 '1',
14 '2',
15 '3',
16 '4',
17 '5',
18 ]);
19 });
20
21 it('Split well formatted comma string with spaces', () => {
22 const inputString = '1, 2, 3, 4, 5';
23 expect(splitByWhitespaceAndCommas(inputString)).toMatchObject([
24 '1',
25 '2',
26 '3',
27 '4',
28 '5',
29 ]);
30 });
31
32 it('Split well formatted comma string with newlines', () => {
33 const inputString = '1\n2\n3\n4\n5';
34 expect(splitByWhitespaceAndCommas(inputString)).toMatchObject([
35 '1',
36 '2',
37 '3',
38 '4',
39 '5',
40 ]);
41 });
42
43 it('Split well formatted comma string with commas and spaces and newlines', () => {
44 const inputString = ' 1\n ,2 \n3,4\n5';
45 expect(splitByWhitespaceAndCommas(inputString)).toMatchObject([
46 '1',
47 '2',
48 '3',
49 '4',
50 '5',
51 ]);
52 });
53
54 it('Split multidigit numbers with commas and spaces and newlines', () => {
55 const inputString = ' 123\n ,234 \n345,456\n567';
56 expect(splitByWhitespaceAndCommas(inputString)).toMatchObject([
57 '123',
58 '234',
59 '345',
60 '456',
61 '567',
62 ]);
63 });
64 });
65 describe('Title case strings', () => {
66 it('Title case all lower case string', () => {
67 const inputString = 'the_quick_brown_fox_runs_over_the_lazy_dog';
68 expect(titleCaseEnumString(inputString)).toEqual(
69 'The Quick Brown Fox Runs Over The Lazy Dog',
70 );
71 });
72 it('Title case all upper case string', () => {
73 const inputString = 'THE_QUICK_BROWN_FOX_RUNS_OVER_THE_LAZY_DOG';
74 expect(titleCaseEnumString(inputString)).toEqual(
75 'The Quick Brown Fox Runs Over The Lazy Dog',
76 );
77 });
78 it('Title case random case string', () => {
79 const inputString = 'THe_QUiCK_BroWN_fOX_runS_OVer_ThE_lAzY_dOg';
80 expect(titleCaseEnumString(inputString)).toEqual(
81 'The Quick Brown Fox Runs Over The Lazy Dog',
82 );
83 });
84 });
85
86 describe('To human-readable labels', () => {
87 test('camelCase transformation', () =>
88 expect(toHumanReadableLabel('camelCaseExample')).toBe(
89 'Camel Case Example',
90 ));
91
92 test('snake_case transformation', () =>
93 expect(toHumanReadableLabel('snake_case_example')).toBe(
94 'Snake Case Example',
95 ));
96
97 test('space delineated transformation', () =>
98 expect(toHumanReadableLabel('space delineated example')).toBe(
99 'Space Delineated Example',
100 ));
101
102 test('kebab-case transformation', () =>
103 expect(toHumanReadableLabel('kebab-case-example')).toBe(
104 'Kebab Case Example',
105 ));
106
107 test('PascalCase transformation', () =>
108 expect(toHumanReadableLabel('PascalCaseExample')).toBe(
109 'Pascal Case Example',
110 ));
111
112 test('string without specific format', () => {
113 expect(toHumanReadableLabel('plainstring')).toBe('Plainstring');
114 });
115 });
116
117 describe('Truncate ID if necessary', () => {
118 test('Truncate empty string', () => {
119 expect(truncateIdIfNeeded('', 10)).toBe('');
120 });
121 test("Don't truncate short string", () => {
122 expect(truncateIdIfNeeded('12345', 10)).toBe('12345');
123 });
124 test('Truncate long string', () => {
125 expect(truncateIdIfNeeded('1234567890', 5)).toBe('12345...');
126 });
127 });
128});