-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathgitHandler.test.js
47 lines (42 loc) · 1.86 KB
/
gitHandler.test.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
const fs = require('fs-extra');
const { isText } = require('../gitHandler'); // Adjust the import statement to destructure isText directly
describe('isText function', () => {
afterEach(() => {
jest.restoreAllMocks();
});
it('should consider a file with text contents as a text file', async () => {
jest.spyOn(fs, 'readFile').mockResolvedValue('This is a text file');
const filename = 'test.txt';
// try-catch is unnecessary here because jest will catch rejections for us
const result = await isText(filename);
expect(result).toBe(true);
});
it('should not consider a binary file as a text file', async () => {
jest.spyOn(fs, 'readFile').mockRejectedValue(new Error('Binary file'));
const filename = 'binaryfile.bin';
// try-catch is unnecessary here because jest will catch rejections for us
const result = await isText(filename);
expect(result).toBe(false);
});
it('should properly read files regardless of extension', async () => {
jest.spyOn(fs, 'readFile')
.mockResolvedValueOnce('Normal text')
.mockRejectedValueOnce(new Error('Binary content'));
const textFilename = 'file.with.unknownext';
const binaryFilename = 'image.jpg';
// try-catch is unnecessary here because jest will catch rejections for us
const textResult = await isText(textFilename);
const binaryResult = await isText(binaryFilename);
expect(textResult).toBe(true);
expect(binaryResult).toBe(false);
});
it('should handle encoding-related read errors gracefully', async () => {
const error = new Error('EncodingError');
error.code = 'ENOENT';
jest.spyOn(fs, 'readFile').mockRejectedValue(error);
const filename = 'fileWithEncodingIssues.txt';
// try-catch is unnecessary here because jest will catch rejections for us
const result = await isText(filename);
expect(result).toBe(false);
});
});