-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathindex.test.ts
More file actions
108 lines (83 loc) · 2.94 KB
/
index.test.ts
File metadata and controls
108 lines (83 loc) · 2.94 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
// Uncomment the code below and write your tests
import { readFileAsynchronously, doStuffByTimeout, doStuffByInterval } from '.';
import { existsSync } from 'fs';
import { readFile } from 'fs/promises';
import { join } from 'path';
jest.mock('fs');
jest.mock('fs/promises');
jest.mock('path');
describe('doStuffByTimeout', () => {
let timeoutSpy: jest.SpyInstance;
beforeEach(() => {
jest.useFakeTimers({ legacyFakeTimers: true });
timeoutSpy = jest.spyOn(global, 'setTimeout');
});
afterEach(() => {
jest.useRealTimers();
timeoutSpy.mockRestore();
});
test('should set timeout with provided callback and timeout', () => {
const callback = jest.fn();
doStuffByTimeout(callback, 1000);
expect(timeoutSpy).toHaveBeenCalledWith(callback, 1000);
});
test('should call callback only after timeout', () => {
const callback = jest.fn();
doStuffByTimeout(callback, 1000);
jest.advanceTimersByTime(2000);
expect(callback).toBeCalledTimes(1);
});
});
describe('doStuffByInterval', () => {
let intervalSpy: jest.SpyInstance;
beforeEach(() => {
jest.useFakeTimers({ legacyFakeTimers: true });
intervalSpy = jest.spyOn(global, 'setInterval');
});
afterEach(() => {
jest.useRealTimers();
intervalSpy.mockRestore();
});
test('should set interval with provided callback and timeout', () => {
const callback = jest.fn();
doStuffByInterval(callback, 500);
expect(intervalSpy).toHaveBeenCalledWith(callback, 500);
});
test('should call callback multiple times after multiple intervals', () => {
const callback = jest.fn();
doStuffByInterval(callback, 300);
jest.advanceTimersByTime(900);
expect(callback).toHaveBeenCalledTimes(3);
});
});
describe('readFileAsynchronously', () => {
const mockedExistsSync = existsSync as jest.Mock;
const mockedReadFile = readFile as jest.Mock;
const mockedJoin = join as jest.Mock;
beforeEach(() => {
jest.clearAllMocks();
});
test('should call join with pathToFile', async () => {
mockedJoin.mockReturnValue('/full/path/to/file');
mockedExistsSync.mockReturnValue(false);
await readFileAsynchronously('some-file.txt');
expect(mockedJoin).toHaveBeenCalledWith(
expect.any(String),
'some-file.txt',
);
});
test('should return null if file does not exist', async () => {
mockedJoin.mockReturnValue('/full/path/to/file');
mockedExistsSync.mockReturnValue(false);
const result = await readFileAsynchronously('file.txt');
expect(result).toBeNull();
});
test('should return file content if file exists', async () => {
mockedJoin.mockReturnValue('/full/path/to/file');
mockedExistsSync.mockReturnValue(true);
mockedReadFile.mockResolvedValue(Buffer.from('mocked file content'));
const result = await readFileAsynchronously('file.txt');
expect(result).toBe('mocked file content');
expect(mockedReadFile).toHaveBeenCalledWith('/full/path/to/file');
});
});