Skip to content

Commit

Permalink
test: decouple load-flow test from file system
Browse files Browse the repository at this point in the history
  • Loading branch information
ChristopherPHolder authored Feb 23, 2024
2 parents c82c168 + 33d469e commit 7f60238
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 99 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { join } from 'node:path';
import { existsSync, lstatSync, readdirSync } from 'node:fs';
import { UserFlowProvider } from './types';
import { join } from 'path';
import { existsSync, lstatSync, readdirSync } from 'fs';
import { resolveAnyFile } from '../../../../core/file';
import { CollectRcOptions } from '../../options/types';

export function loadFlow(collect: Pick<CollectRcOptions, 'ufPath'>): ({ exports: UserFlowProvider, path: string })[] {
const { ufPath } = collect;
const path = join(process.cwd(), ufPath);
if (!existsSync(path)) {
throw new Error(`ufPath: ${path} is no directory`);
throw new Error(`ufPath: ${path} is neither a file nor a directory`);
}

let files: string[];
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { loadFlow } from './load-flow';
import * as fileHelpers from '../../../../core/file';
import * as fs from 'node:fs';

jest.mock('node:fs');
jest.mock('../../../../core/file');

describe('loading user-flow scripts for execution', () => {

beforeEach(() => {
jest.clearAllMocks();
})

it('should throw if ufPath does not exist', () => {
const existsSyncSpy = jest.spyOn(fs, 'existsSync').mockReturnValue(false);
expect(() => loadFlow({ ufPath: './path' })).toThrow();
expect(existsSyncSpy).toHaveBeenCalled();
});

it('should throw if ufPath points to a file and it does not end in with ts or js', () => {
jest.spyOn(fs, 'existsSync').mockReturnValue(true);
jest.spyOn(fs, 'lstatSync').mockReturnValue({ isDirectory: () => false } as fs.Stats);
const resolveAnyFileSpy = jest.spyOn(fileHelpers, 'resolveAnyFile');
expect(() => loadFlow({ ufPath: './path/file.json' })).toThrow();
expect(resolveAnyFileSpy).not.toHaveBeenCalled();
});

it('should throw if ufPath points to a directory and it does not contain any files that end with ts or js', () => {
jest.spyOn(fs, 'existsSync').mockReturnValue(true);
jest.spyOn(fs, 'lstatSync').mockReturnValue({ isDirectory: () => true } as fs.Stats);
jest.spyOn(fs, 'readdirSync').mockReturnValue(['file.json' as unknown as fs.Dirent])
const resolveAnyFileSpy = jest.spyOn(fileHelpers, 'resolveAnyFile');
expect(() => loadFlow({ ufPath: './path' })).toThrow();
expect(resolveAnyFileSpy).not.toHaveBeenCalled();
});

it('should extract all file paths in the directory if ufPath points to a directory', () => {
jest.spyOn(fs, 'existsSync').mockReturnValue(true);
jest.spyOn(fileHelpers, 'resolveAnyFile').mockReturnValue('Flow Dummy' as any);
const isDirectorySpy = jest.spyOn(fs, 'lstatSync').mockReturnValue({ isDirectory: () => true } as fs.Stats);
const readdirSyncSpy = jest.spyOn(fs, 'readdirSync').mockReturnValue(['file.ts' as unknown as fs.Dirent])
loadFlow({ ufPath: './user-flow-dir-path' });
expect(isDirectorySpy).toHaveBeenCalled();
expect(readdirSyncSpy).toHaveBeenCalledWith(
expect.stringContaining('user-flow-dir-path')
);
});

it('should only resolve files ending in ts or js', () => {
jest.spyOn(fs, 'existsSync').mockReturnValue(true);
jest.spyOn(fs, 'lstatSync').mockReturnValue({ isDirectory: () => true } as fs.Stats);
jest.spyOn(fs, 'readdirSync')
.mockReturnValue(['file.ts', 'file.js', 'file.tsx', 'file.json', 'file.js.json'] as unknown as fs.Dirent[]);
const resolveAnyFileSpy = jest.spyOn(fileHelpers, 'resolveAnyFile');
loadFlow({ ufPath: './path' });
expect(resolveAnyFileSpy).toHaveBeenCalledTimes(2);
expect(resolveAnyFileSpy).toHaveBeenNthCalledWith(
1,
expect.stringContaining('file.ts')
);
expect(resolveAnyFileSpy).toHaveBeenNthCalledWith(
2,
expect.stringContaining('file.js')
);
})
});

0 comments on commit 7f60238

Please sign in to comment.