-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathindex.test.ts
More file actions
88 lines (70 loc) · 2.38 KB
/
index.test.ts
File metadata and controls
88 lines (70 loc) · 2.38 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
import { readFileAsynchronously, doStuffByTimeout, doStuffByInterval } from '.';
import path from 'path';
import fs from 'fs';
import fsPromises from 'fs/promises';
describe('doStuffByTimeout', () => {
beforeAll(() => {
jest.useFakeTimers();
});
afterAll(() => {
jest.useRealTimers();
});
test('should set timeout with provided callback and timeout', () => {
const spyTimeout = jest.spyOn(global, 'setTimeout');
const cb = jest.fn();
const timeout = 1000;
doStuffByTimeout(cb, timeout);
expect(spyTimeout).toHaveBeenCalledWith(cb, timeout);
});
test('should call callback only after timeout', () => {
const cb = jest.fn();
doStuffByTimeout(cb, 1000);
expect(cb).not.toHaveBeenCalled();
jest.advanceTimersByTime(1000);
expect(cb).toHaveBeenCalled();
});
});
describe('doStuffByInterval', () => {
beforeAll(() => {
jest.useFakeTimers();
});
afterAll(() => {
jest.useRealTimers();
});
test('should set interval with provided callback and timeout', () => {
const spyTimeout = jest.spyOn(global, 'setTimeout');
const cb = jest.fn();
const timeout = 1000;
doStuffByTimeout(cb, timeout);
expect(spyTimeout).toHaveBeenCalledWith(cb, timeout);
});
test('should call callback multiple times after multiple intervals', () => {
const cb = jest.fn();
doStuffByInterval(cb, 100);
jest.advanceTimersByTime(300);
expect(cb).toHaveBeenCalledTimes(3);
});
});
describe('readFileAsynchronously', () => {
test('should call join with pathToFile', async () => {
const joinSpy = jest.spyOn(path, 'join');
const filePath = 'test';
await readFileAsynchronously(filePath);
expect(joinSpy).toHaveBeenCalledWith(__dirname, filePath);
});
test('should return null if file does not exist', async () => {
jest.spyOn(fs, 'existsSync').mockReturnValue(false);
const filePath = 'test';
const result = await readFileAsynchronously(filePath);
expect(result).toBeNull();
});
test('should return file content if file exists', async () => {
jest.spyOn(fs, 'existsSync').mockReturnValue(true);
const filePath = 'test';
const fileContent = 'content';
const fileBuffer = Buffer.from(fileContent);
jest.spyOn(fsPromises, 'readFile').mockResolvedValue(fileBuffer);
const result = await readFileAsynchronously(filePath);
expect(result).toBe(fileContent);
});
});