@anmiles/google-api-wrapper 7.0.7 → 8.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,245 +0,0 @@
1
- import fs from 'fs';
2
- import paths from '../paths';
3
-
4
- import jsonLib from '../jsonLib';
5
- const original = jest.requireActual('../jsonLib').default as typeof jsonLib;
6
- jest.mock<typeof jsonLib>('../jsonLib', () => ({
7
- readJSON : jest.fn().mockImplementation(() => json),
8
- writeJSON : jest.fn(),
9
- getJSON : jest.fn(),
10
- getJSONAsync : jest.fn(),
11
- checkJSON : jest.fn(),
12
- }));
13
-
14
- jest.mock<Partial<typeof fs>>('fs', () => ({
15
- readFileSync : jest.fn().mockImplementation(() => jsonString),
16
- writeFileSync : jest.fn(),
17
- existsSync : jest.fn().mockImplementation(() => fileExists),
18
- }));
19
-
20
- jest.mock<Partial<typeof paths>>('../paths', () => ({
21
- ensureFile : jest.fn(),
22
- }));
23
-
24
- const filename = 'filename';
25
- const json = { key : 'value' };
26
- const jsonString = JSON.stringify(json, null, ' ');
27
- const fallbackJSON = { fallbackKey : 'fallbackValue' };
28
-
29
- let fileExists: boolean;
30
- let validation: boolean;
31
-
32
- const validateCallback = jest.fn().mockImplementation(() => validation);
33
- const validateCallbackAsync = jest.fn().mockImplementation(async () => validation);
34
-
35
- const createCallback = jest.fn().mockReturnValue(fallbackJSON);
36
- const createCallbackAsync = jest.fn().mockResolvedValue(fallbackJSON);
37
-
38
- describe('src/lib/jsonLib', () => {
39
- describe('readJSON', () => {
40
- it('should read specified file', () => {
41
- original.readJSON(filename);
42
-
43
- expect(fs.readFileSync).toHaveBeenCalledWith(filename);
44
- });
45
-
46
- it('should return parsed JSON', () => {
47
- const result = original.readJSON(filename);
48
-
49
- expect(result).toEqual(json);
50
- });
51
- });
52
-
53
- describe('writeJSON', () => {
54
- it('should write JSON into specified file', () => {
55
- original.writeJSON(filename, json);
56
-
57
- expect(fs.writeFileSync).toHaveBeenCalledWith(filename, jsonString);
58
- });
59
- });
60
-
61
- describe('getJSON', () => {
62
-
63
- it('should call readJSON if file exists and json is valid', () => {
64
- fileExists = true;
65
- validation = true;
66
-
67
- original.getJSON(filename, createCallback, validateCallback);
68
-
69
- expect(jsonLib.readJSON).toHaveBeenCalledWith(filename);
70
- expect(createCallback).not.toHaveBeenCalled();
71
- });
72
-
73
- it('should call createCallback if file exists but json is not valid', () => {
74
- fileExists = true;
75
- validation = false;
76
-
77
- original.getJSON(filename, createCallback, validateCallback);
78
-
79
- expect(jsonLib.readJSON).toHaveBeenCalledWith(filename);
80
- expect(createCallback).toHaveBeenCalledWith();
81
- });
82
-
83
- it('should call createCallback if file not exists', () => {
84
- fileExists = false;
85
-
86
- original.getJSON(filename, createCallback, validateCallback);
87
-
88
- expect(jsonLib.readJSON).not.toHaveBeenCalled();
89
- expect(createCallback).toHaveBeenCalledWith();
90
- });
91
-
92
- it('should not write fallback JSON back if file exists and json is valid', () => {
93
- fileExists = true;
94
- validation = true;
95
-
96
- original.getJSON(filename, createCallback, validateCallback);
97
-
98
- expect(jsonLib.writeJSON).not.toHaveBeenCalled();
99
- });
100
-
101
- it('should write fallback JSON back if file exists but json is not valid', () => {
102
- fileExists = true;
103
- validation = false;
104
-
105
- original.getJSON(filename, createCallback, validateCallback);
106
-
107
- expect(jsonLib.checkJSON).toHaveBeenCalledWith(filename, fallbackJSON);
108
- expect(paths.ensureFile).toHaveBeenCalledWith(filename);
109
- expect(jsonLib.writeJSON).toHaveBeenCalledWith(filename, fallbackJSON);
110
- });
111
-
112
- it('should write fallback JSON back if file not exists', () => {
113
- fileExists = false;
114
-
115
- original.getJSON(filename, createCallback, validateCallback);
116
-
117
- expect(jsonLib.checkJSON).toHaveBeenCalledWith(filename, fallbackJSON);
118
- expect(paths.ensureFile).toHaveBeenCalledWith(filename);
119
- expect(jsonLib.writeJSON).toHaveBeenCalledWith(filename, fallbackJSON);
120
- });
121
-
122
- it('should return JSON if file exists and json is valid', () => {
123
- fileExists = true;
124
- validation = true;
125
-
126
- const result = original.getJSON(filename, createCallback, validateCallback);
127
-
128
- expect(result).toEqual(json);
129
- });
130
-
131
- it('should return fallback JSON if file exists but json is not valid', () => {
132
- fileExists = true;
133
- validation = false;
134
-
135
- const result = original.getJSON(filename, createCallback, validateCallback);
136
-
137
- expect(result).toEqual(fallbackJSON);
138
- });
139
-
140
- it('should return fallback JSON if file not exists', () => {
141
- fileExists = false;
142
-
143
- const result = original.getJSON(filename, createCallback, validateCallback);
144
-
145
- expect(result).toEqual(fallbackJSON);
146
- });
147
- });
148
-
149
- describe('getJSONAsync', () => {
150
- it('should call readJSON if file exists and json is valid', async () => {
151
- fileExists = true;
152
- validation = true;
153
-
154
- await original.getJSONAsync(filename, createCallbackAsync, validateCallbackAsync);
155
-
156
- expect(jsonLib.readJSON).toHaveBeenCalledWith(filename);
157
- expect(createCallbackAsync).not.toHaveBeenCalled();
158
- });
159
-
160
- it('should call createCallback if file exists but json is not valid', async () => {
161
- fileExists = true;
162
- validation = false;
163
-
164
- await original.getJSONAsync(filename, createCallbackAsync, validateCallbackAsync);
165
-
166
- expect(jsonLib.readJSON).toHaveBeenCalledWith(filename);
167
- expect(createCallbackAsync).toHaveBeenCalledWith();
168
- });
169
-
170
- it('should call createCallback if file not exists', async () => {
171
- fileExists = false;
172
-
173
- await original.getJSONAsync(filename, createCallbackAsync, validateCallbackAsync);
174
-
175
- expect(jsonLib.readJSON).not.toHaveBeenCalled();
176
- expect(createCallbackAsync).toHaveBeenCalledWith();
177
- });
178
-
179
- it('should not write fallback JSON back if file exists and json is valid', async () => {
180
- fileExists = true;
181
- validation = true;
182
-
183
- await original.getJSONAsync(filename, createCallbackAsync, validateCallbackAsync);
184
-
185
- expect(jsonLib.writeJSON).not.toHaveBeenCalled();
186
- });
187
-
188
- it('should write fallback JSON back if file exists but json is not valid', async () => {
189
- fileExists = true;
190
- validation = false;
191
-
192
- await original.getJSONAsync(filename, createCallbackAsync, validateCallbackAsync);
193
-
194
- expect(jsonLib.checkJSON).toHaveBeenCalledWith(filename, fallbackJSON);
195
- expect(paths.ensureFile).toHaveBeenCalledWith(filename);
196
- expect(jsonLib.writeJSON).toHaveBeenCalledWith(filename, fallbackJSON);
197
- });
198
-
199
- it('should write fallback JSON back if file not exists', async () => {
200
- fileExists = false;
201
-
202
- await original.getJSONAsync(filename, createCallbackAsync, validateCallbackAsync);
203
-
204
- expect(jsonLib.checkJSON).toHaveBeenCalledWith(filename, fallbackJSON);
205
- expect(paths.ensureFile).toHaveBeenCalledWith(filename);
206
- expect(jsonLib.writeJSON).toHaveBeenCalledWith(filename, fallbackJSON);
207
- });
208
-
209
- it('should return JSON if file exists and json is valid', async () => {
210
- fileExists = true;
211
- validation = true;
212
-
213
- const result = await original.getJSONAsync(filename, createCallbackAsync, validateCallbackAsync);
214
-
215
- expect(result).toEqual(json);
216
- });
217
-
218
- it('should return fallback JSON if file exists but json is not valid', async () => {
219
- fileExists = true;
220
- validation = false;
221
-
222
- const result = await original.getJSONAsync(filename, createCallbackAsync, validateCallbackAsync);
223
-
224
- expect(result).toEqual(fallbackJSON);
225
- });
226
-
227
- it('should return fallback JSON if file not exists', async () => {
228
- fileExists = false;
229
-
230
- const result = await original.getJSONAsync(filename, createCallbackAsync, validateCallbackAsync);
231
-
232
- expect(result).toEqual(fallbackJSON);
233
- });
234
- });
235
-
236
- describe('checkJSON', () => {
237
- it('should output error if json is falsy', () => {
238
- expect(() => original.checkJSON(filename, '')).toThrow(`File ${filename} doesn't exist and should be created with initial data, but function createCallback returned nothing`);
239
- });
240
-
241
- it('should do nothing if json is truthy', () => {
242
- expect(() => original.checkJSON(filename, json)).not.toThrow();
243
- });
244
- });
245
- });
@@ -1,17 +0,0 @@
1
- import sleep from '../sleep';
2
-
3
- const sleepMilliseconds = 300;
4
-
5
- describe('src/lib/sleep', () => {
6
- describe('sleep', () => {
7
-
8
- it('should wait specified timeout', async () => {
9
- const before = new Date().getTime();
10
-
11
- await sleep.sleep(sleepMilliseconds);
12
-
13
- const after = new Date().getTime();
14
- expect(after - before).toBeGreaterThanOrEqual(sleepMilliseconds - 1);
15
- });
16
- });
17
- });
@@ -1,57 +0,0 @@
1
- import fs from 'fs';
2
- import { ensureFile } from './paths';
3
-
4
- import jsonLib from './jsonLib';
5
-
6
- export { getJSON, getJSONAsync, writeJSON, readJSON };
7
- export default { getJSON, getJSONAsync, writeJSON, readJSON, checkJSON };
8
-
9
- function getJSON<T>(filename: string, createCallback: () => Exclude<T, Promise<any>>, validateJSON?: (json: T) => boolean): T {
10
- if (fs.existsSync(filename)) {
11
- const json = jsonLib.readJSON<T>(filename);
12
-
13
- if (!validateJSON || validateJSON(json)) {
14
- return json;
15
- }
16
- }
17
-
18
- const json = createCallback();
19
- jsonLib.checkJSON(filename, json);
20
- ensureFile(filename);
21
- jsonLib.writeJSON(filename, json);
22
- return json;
23
- }
24
-
25
- async function getJSONAsync<T>(filename: string, createCallbackAsync: () => Promise<T>, validateJSONAsync?: (json: T) => Promise<boolean>): Promise<T> {
26
- if (fs.existsSync(filename)) {
27
- const json = jsonLib.readJSON<T>(filename);
28
-
29
- if (!validateJSONAsync || await validateJSONAsync(json)) {
30
- return json;
31
- }
32
- }
33
-
34
- const json = await createCallbackAsync();
35
- jsonLib.checkJSON(filename, json);
36
- ensureFile(filename);
37
- jsonLib.writeJSON(filename, json);
38
- return json;
39
- }
40
-
41
- function writeJSON<T>(filename: string, json: T): void {
42
- const jsonString = JSON.stringify(json, null, ' ');
43
- fs.writeFileSync(filename, jsonString);
44
- }
45
-
46
- function readJSON<T>(filename: string): T {
47
- const jsonString = fs.readFileSync(filename).toString();
48
- return JSON.parse(jsonString) as T;
49
- }
50
-
51
- function checkJSON<T>(filename: string, json: T): void {
52
- if (json) {
53
- return;
54
- }
55
-
56
- throw `File ${filename} doesn't exist and should be created with initial data, but function createCallback returned nothing`;
57
- }
package/src/lib/sleep.ts DELETED
@@ -1,8 +0,0 @@
1
- export { sleep };
2
- export default { sleep };
3
-
4
- async function sleep(milliSeconds: number) {
5
- return new Promise((resolve) => {
6
- setTimeout(resolve, milliSeconds);
7
- });
8
- }