-
Notifications
You must be signed in to change notification settings - Fork 105
/
Copy pathUtils.test.ts
108 lines (81 loc) · 3.23 KB
/
Utils.test.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
import { getStringInfo, StringUtils, toUpperCase } from "../app/Utils";
describe('Utils test suite', () => {
describe.only('StringUtils tests', ()=>{
let sut: StringUtils;
beforeEach(()=>{
sut = new StringUtils();
})
it('Should return correct upperCase', ()=>{
const actual = sut.toUpperCase('abc');
expect(actual).toBe('ABC');
})
it('Should throw error on invalid argument - function', ()=>{
function expectError() {
const actual = sut.toUpperCase('');
}
expect(expectError).toThrow();
expect(expectError).toThrowError('Invalid argument!');
})
it('Should throw error on invalid argument - arrow function', ()=>{
expect(()=>{
sut.toUpperCase('');
}).toThrowError('Invalid argument!');
})
it('Should throw error on invalid argument - try catch block', (done)=>{
try {
sut.toUpperCase('');
done('GetStringInfo should throw error for invalid arg!')
} catch (error) {
expect(error).toBeInstanceOf(Error);
expect(error).toHaveProperty('message', 'Invalid argument!');
done();
}
})
});
it('should return uppercase of valid string', () => {
const sut = toUpperCase;
const expected = 'ABC'
const actual = sut('abc');
expect(actual).toBe(expected);
})
describe('ToUpperCase examples', ()=>{
it.each([
{input:'abc', expected: 'ABC'},
{input:'My-String', expected: 'MY-STRING'},
{input:'def', expected: 'DEF'}
])('$input toUpperCase should be $expected', ({input, expected})=>{
const actual = toUpperCase(input);
expect(actual).toBe(expected);
});
})
describe('getStringInfo for arg My-String should', ()=>{
test('return right length', ()=>{
const actual = getStringInfo('My-String');
expect(actual.characters).toHaveLength(9);
});
test('return right lower case', ()=>{
const actual = getStringInfo('My-String');
expect(actual.lowerCase).toBe('my-string');
});
test('return right upper case', ()=>{
const actual = getStringInfo('My-String');
expect(actual.upperCase).toBe('MY-STRING');
});
test('return right characters', ()=>{
const actual = getStringInfo('My-String');
expect(actual.characters).toEqual(['M', 'y', '-','S', 't', 'r','i', 'n', 'g']);
expect(actual.characters).toContain<string>('M');
expect(actual.characters).toEqual(
expect.arrayContaining(['S', 't', 'r','i', 'n', 'g', 'M', 'y', '-'])
);
});
test('return defined extra info', ()=>{
const actual = getStringInfo('My-String');
expect(actual.extraInfo).toBeDefined();
});
test('return right extra info', ()=>{
const actual = getStringInfo('My-String');
expect(actual.extraInfo).toEqual({})
});
});
});