-
-
Notifications
You must be signed in to change notification settings - Fork 189
/
Copy pathutils.tests.js
173 lines (154 loc) · 5.96 KB
/
utils.tests.js
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import {
formatSeconds,
generateAutoname,
getLangAsObject,
getLangString,
join,
truncateFile,
truncateString,
truncateUrl,
} from '#/utils'
describe('utils', () => {
describe('join', () => {
it('should make an array with separators between array elements', () => {
;[
[['hi', 'hello', 'how are you'], '<br/>', ['hi', '<br/>', 'hello', '<br/>', 'how are you']],
[['a', 'b', 'c'], '\n', ['a', '\n', 'b', '\n', 'c']],
[[1, 2, 3], 0, [1, 0, 2, 0, 3]],
[['a', 2, { hello: 'world' }], [], ['a', [], 2, [], { hello: 'world' }]],
// We could add a real JSX test case here (we'd have to import React)
].forEach((testCase) => {
const test = join(testCase[0], testCase[1])
chai.expect(test).to.deep.equal(testCase[2])
})
})
})
describe('formatSeconds', () => {
it('should format properly', () => {
;[
[10, '00:10'],
[0, '00:00'],
[1.333, '00:01'],
[1.777, '00:02'],
[60, '01:00'],
[671, '11:11'],
[1111, '18:31'],
[3599, '59:59'],
[6000, '100:00'],
[6666, '111:06'],
].forEach((testCase) => {
const test = formatSeconds(testCase[0])
chai.expect(test).to.equal(testCase[1])
})
})
})
describe('getLangAsObject', () => {
it('should return object for valid langString', () => {
const langObj = getLangAsObject('English (en)')
chai.expect(langObj.name).to.equal('English')
chai.expect(langObj.code).to.equal('en')
})
it('should return undefined for invalid langString', () => {
chai.expect(getLangAsObject('English')).to.equal(undefined)
chai.expect(getLangAsObject('(en)')).to.equal(undefined)
chai.expect(getLangAsObject('English [en]')).to.equal(undefined)
chai.expect(getLangAsObject('English, en')).to.equal(undefined)
chai.expect(getLangAsObject('English: en')).to.equal(undefined)
chai.expect(getLangAsObject('(en) English')).to.equal(undefined)
chai.expect(getLangAsObject('English (en) (fr) (de)')).to.equal(undefined)
chai.expect(getLangAsObject('Pizza time!')).to.equal(undefined)
})
it('should work properly with getLangString', () => {
const langObj = getLangAsObject(
getLangString({
name: 'English',
code: 'en',
}),
)
chai.expect(langObj.name).to.equal('English')
chai.expect(langObj.code).to.equal('en')
})
})
describe('getLangString', () => {
it('should return valid langString from langObj', () => {
const langString = getLangString({
name: 'English',
code: 'en',
})
chai.expect(langString).to.equal('English (en)')
})
it('should return nothing for invalid object', () => {
const langString = getLangString({
pizzaType: 2,
delivery: false,
})
chai.expect(langString).to.equal(undefined)
})
it('should work properly with getLangAsObject', () => {
const langString = getLangString(getLangAsObject('English (en)'))
chai.expect(langString).to.equal('English (en)')
})
})
describe('truncateString, truncateUrl, truncateFile', () => {
it('should not truncate strings shorter than specified length', () => {
const testString = 'veryShortString'
const testLength = 1000
chai.expect(truncateString(testString, testLength)).to.equal(testString)
})
it('should not apply extension truncation to when there is no extension', () => {
const testString = 'veryShortString'
const testLength = 1000
chai.expect(truncateFile(testString, testLength)).to.equal(testString)
})
it('should not apply protocol truncation to when there is no protocol', () => {
const testString = 'veryShortString'
const testLength = 1000
chai.expect(truncateUrl(testString, testLength)).to.equal(testString)
})
it('should return exactly `length` characters', () => {
const testString = 'veryShortString'
const testLength = 5
chai.expect(truncateString(testString, testLength).length).to.equal(testLength)
})
it('should remove extensions if specified', () => {
const testString = 'veryShortString.xml'
const testLength = 10
chai.expect(truncateFile(testString, testLength)).to.equal('veryS…tring')
})
it('should remove protocols if specified', () => {
const testString = 'http://veryShortString.com'
const testLength = 10
chai.expect(truncateUrl(testString, testLength)).to.equal('veryS…g.com')
})
it('should impose its type specific truncation regardless of content', () => {
const testString = 'http://veryShortString.com'
const testLength = 10
chai.expect(truncateFile(testString, testLength)).to.equal('http:…tring')
})
})
describe('generateAutoname', () => {
it('should use default values if only string is specified', () => {
const testString = 'veryShortString'
chai.expect(generateAutoname(testString)).to.equal('veryshortstring')
})
it('should create a proper substring', () => {
const testString = 'veryShortString'
const INDEX_FIRST_WORD = 4
const INDEX_LAST_WORD = 9
chai.expect(generateAutoname(testString, INDEX_FIRST_WORD, INDEX_LAST_WORD)).to.equal('short')
})
it('should change all spaces to underscores', () => {
const testString = 'i am a very long na me with weird s paces'
chai
// TODO: See if backend uses single or multiple underscores for spaces
.expect(generateAutoname(testString))
.to.equal('i_am___a_very_long_na___me_with__weird_s______paces')
})
it('should create a proper substring and change all spaces to underscores', () => {
const testString = 'i am a very long na me with weird s paces'
const INDEX_FIRST_WORD = 4
const INDEX_LAST_WORD = 21
chai.expect(generateAutoname(testString, INDEX_FIRST_WORD, INDEX_LAST_WORD)).to.equal('___a_very_long_na')
})
})
})