|
| 1 | +import * as fs from 'fs'; |
| 2 | +import { FileSystemReader, Reader } from '../../../src/lib/readers'; |
| 3 | + |
| 4 | +jest.mock('fs', () => ({ |
| 5 | + readFileSync: jest.fn().mockReturnValue('content'), |
| 6 | + promises: { |
| 7 | + readdir: jest.fn().mockResolvedValue([]), |
| 8 | + readFile: jest.fn().mockResolvedValue('content'), |
| 9 | + }, |
| 10 | +})); |
| 11 | + |
| 12 | +const dir: string = process.cwd(); |
| 13 | +const reader: Reader = new FileSystemReader(dir); |
| 14 | + |
| 15 | +describe('File System Reader', () => { |
| 16 | + afterAll(() => { |
| 17 | + jest.clearAllMocks(); |
| 18 | + }); |
| 19 | + it('should use fs.promises.readdir when list', async () => { |
| 20 | + await reader.list(); |
| 21 | + expect(fs.promises.readdir).toHaveBeenCalled(); |
| 22 | + }); |
| 23 | + it('should use fs.promises.readFile when read', async () => { |
| 24 | + await reader.read('filename'); |
| 25 | + expect(fs.promises.readFile).toHaveBeenCalled(); |
| 26 | + }); |
| 27 | + |
| 28 | + describe('readAnyOf tests', () => { |
| 29 | + it('should call readFile when running readAnyOf fn', async () => { |
| 30 | + const filenames: string[] = ['file1', 'file2', 'file3']; |
| 31 | + await reader.readAnyOf(filenames); |
| 32 | + |
| 33 | + expect(fs.promises.readFile).toHaveBeenCalled(); |
| 34 | + }); |
| 35 | + |
| 36 | + it('should return undefined when no file is passed', async () => { |
| 37 | + const content = await reader.readAnyOf([]); |
| 38 | + expect(content).toEqual(undefined); |
| 39 | + }); |
| 40 | + }); |
| 41 | + |
| 42 | + describe('readSyncAnyOf tests', () => { |
| 43 | + it('should call readFileSync when running readSyncAnyOf fn', async () => { |
| 44 | + const filenames: string[] = ['file1', 'file2', 'file3']; |
| 45 | + reader.readSyncAnyOf(filenames); |
| 46 | + |
| 47 | + expect(fs.readFileSync).toHaveBeenCalled(); |
| 48 | + }); |
| 49 | + |
| 50 | + it('should return undefined when no file is passed', async () => { |
| 51 | + const content = reader.readSyncAnyOf([]); |
| 52 | + expect(content).toEqual(undefined); |
| 53 | + }); |
| 54 | + }); |
| 55 | +}); |
0 commit comments