-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathhelpers.spec.ts
More file actions
157 lines (129 loc) · 4.49 KB
/
Copy pathhelpers.spec.ts
File metadata and controls
157 lines (129 loc) · 4.49 KB
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
import os from 'os';
import fs from 'fs';
import glob from 'glob';
import * as helpers from '../lib/helpers';
import pjson from '../package.json';
describe('Helpers', () => {
describe('formatName', () => {
it('slice last 256 symbols', () => {
expect(helpers.formatName(`a${'b'.repeat(256)}`)).toBe('b'.repeat(256));
});
it('leave 256 symbol name as is', () => {
expect(helpers.formatName('c'.repeat(256))).toBe('c'.repeat(256));
});
it('leave 3 symbol name as is', () => {
expect(helpers.formatName('abc')).toBe('abc');
});
it('complete with dots 2 symbol name', () => {
expect(helpers.formatName('ab')).toBe('ab.');
});
});
describe('now', () => {
it('returns milliseconds from unix time', () => {
expect(new Date().getTime() - helpers.now()).toBeLessThan(100); // less than 100 miliseconds difference
});
});
describe('readLaunchesFromFile', () => {
it('should return the right ids', () => {
jest.spyOn(glob, 'sync').mockReturnValue(['rplaunch-fileOne.tmp', 'rplaunch-fileTwo.tmp']);
const ids = helpers.readLaunchesFromFile();
expect(ids).toEqual(['fileOne', 'fileTwo']);
});
});
describe('saveLaunchIdToFile', () => {
it('should call fs.open method with right parameters', () => {
jest.spyOn(fs, 'open').mockImplementation();
helpers.saveLaunchIdToFile('fileOne');
expect(fs.open).toHaveBeenCalledWith('rplaunch-fileOne.tmp', 'w', expect.any(Function));
});
it('should throw error when fs.open fails', () => {
const mockError = new Error('File system error');
jest.spyOn(fs, 'open').mockImplementation(() => {
throw mockError;
});
expect(() => helpers.saveLaunchIdToFile('fileOne')).toThrow('File system error');
});
});
describe('getSystemAttribute', () => {
it('should return correct system attributes', () => {
jest.spyOn(os, 'type').mockReturnValue('osType');
jest.spyOn(os, 'arch').mockReturnValue('osArchitecture');
jest.spyOn(os, 'totalmem').mockReturnValue(1);
const nodeVersion = process.version;
const expectedAttr = [
{
key: 'client',
value: `${pjson.name}|${pjson.version}`,
system: true,
},
{
key: 'os',
value: 'osType|osArchitecture',
system: true,
},
{
key: 'RAMSize',
value: '1',
system: true,
},
{
key: 'nodeJS',
value: nodeVersion,
system: true,
},
];
const attr = helpers.getSystemAttribute();
expect(attr).toEqual(expectedAttr);
});
});
describe('generateTestCaseId', () => {
it('should return undefined if there is no codeRef', () => {
const testCaseId = helpers.generateTestCaseId('');
expect(testCaseId).toEqual(undefined);
});
it('should return codeRef if there is no params', () => {
const testCaseId = helpers.generateTestCaseId('codeRef');
expect(testCaseId).toEqual('codeRef');
});
it('should return codeRef with parameters if there are all parameters', () => {
const parameters = [
{
key: 'key',
value: 'value',
},
{ value: 'valueTwo' },
{ key: 'keyTwo', value: '' },
{
key: 'keyThree',
value: 'valueThree',
},
];
const testCaseId = helpers.generateTestCaseId('codeRef', parameters);
expect(testCaseId).toEqual('codeRef[value,valueTwo,valueThree]');
});
it('should filter out parameters with falsy values', () => {
const parameters = [
{ value: 'value1' },
{ value: '' },
{ value: 'value2' },
{ value: 'value3' },
];
const testCaseId = helpers.generateTestCaseId('codeRef', parameters);
expect(testCaseId).toEqual('codeRef[value1,value2,value3]');
});
});
describe('saveLaunchUuidToFile', () => {
it('should call fs.open method with right parameters', () => {
jest.spyOn(fs, 'open').mockImplementation();
helpers.saveLaunchUuidToFile('fileOne');
expect(fs.open).toHaveBeenCalledWith('rp-launch-uuid-fileOne.tmp', 'w', expect.any(Function));
});
it('should throw error when fs.open fails', () => {
const mockError = new Error('File system error');
jest.spyOn(fs, 'open').mockImplementation(() => {
throw mockError;
});
expect(() => helpers.saveLaunchUuidToFile('fileOne')).toThrow('File system error');
});
});
});