forked from NativeScript/nativescript-dev-webpack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
projectHelpers.spec.js
132 lines (115 loc) · 4.7 KB
/
projectHelpers.spec.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
const { getIndentationCharacter, writePackageJson, safeGet } = require("./projectHelpers");
const fs = require("fs");
describe('projectHelpers', () => {
const originalReadFileSync = fs.readFileSync;
const originalWriteFileSync = fs.writeFileSync;
const tab = "\t";
const multipleSpaces = " ";
const twoSpaces = " ";
afterEach(() => {
fs.readFileSync = originalReadFileSync;
fs.writeFileSync = originalWriteFileSync;
});
describe('getIndentationCharacter', () => {
[
{
testName: 'returns two spaces when file starts with two spaces',
input: `{${twoSpaces}"abc": "1"${twoSpaces}}`,
expectedResult: twoSpaces
},
{
testName: 'returns two spaces when file starts with two spaces and binary content is passed',
input: Buffer.from(`{${twoSpaces}"abc": "1"${twoSpaces}}`),
expectedResult: twoSpaces
},
{
testName: 'returns empty string when file starts without any indentation',
input: `{"abc": "1"}`,
expectedResult: ''
},
{
testName: 'returns tab when file starts with tab',
input: `{${tab}"abc": "1"${tab}}`,
expectedResult: tab
},
{
testName: 'returns two spaces when file starts with two spaces and new line before them',
input: `{\n${twoSpaces}"abc": "1"\n}`,
expectedResult: twoSpaces
},
{
testName: 'returns tab when file starts with tab and new line before them',
input: `{\n${tab}"abc": "1"\n}`,
expectedResult: tab
},
{
testName: 'returns multiple spaces when file starts with multiple spaces and new line before them',
input: `{\n${multipleSpaces}"abc": "1"\n}`,
expectedResult: multipleSpaces
}
].forEach(({ testName, input, expectedResult }) => {
it(testName, () => {
expect(getIndentationCharacter(input)).toEqual(expectedResult);
});
});
});
describe('safeGet', () => {
it('should return the correct value of existing properties', () => {
const obj = { a: 15 };
expect(safeGet(obj, 'a')).toBe(15);
});
it('should return undefined for unexisting properties', () => {
const obj = { a: 15 };
expect(safeGet(obj, 'random')).toBeUndefined();
});
it('should return undefined when the first argument is undefined', () => {
let obj;
expect(safeGet(obj, 'random')).toBeUndefined();
});
it('should return undefined when the first argument is not an object and does not have inherited property with the queried name', () => {
const num = 15;
expect(safeGet(num, 'random')).toBeUndefined();
});
});
describe('writePackageJson', () => {
const mockFileSystemApi = () => {
const data = {
isWriteFileSyncCalled: false
};
fs.readFileSync = (p) => {
return JSON.stringify({ a: 1 });
};
fs.writeFileSync = (p, c) => {
data.isWriteFileSyncCalled = true;
};
return data;
};
it('does not write package.json when content has not changed', () => {
const data = mockFileSystemApi();
writePackageJson({ a: 1 }, "projDir");
expect(data.isWriteFileSyncCalled).toBe(false);
});
it('writes content, when the new one is different from the current one', () => {
const data = mockFileSystemApi();
writePackageJson({ b: 2 }, "projDir");
expect(data.isWriteFileSyncCalled).toBe(true);
});
it('keeps indentation of the package.json when rewriting it', () => {
let currentIndentSymbol = tab;
fs.readFileSync = (p) => {
return JSON.stringify({ a: 1 }, null, currentIndentSymbol);
};
let writtenContent = null;
fs.writeFileSync = (p, c) => {
writtenContent = c;
};
// Ensure tab indentation is persisted
writePackageJson({ b: 2 }, "projDir");
expect(writtenContent).toBe(`{\n${tab}"b": 2\n}`);
// Ensure spaces indentation is persisted
currentIndentSymbol = multipleSpaces;
writePackageJson({ b: 2 }, "projDir");
expect(writtenContent).toBe(`{\n${multipleSpaces}"b": 2\n}`);
});
});
});