Skip to content

Commit

Permalink
test: mock fs in generate flow test
Browse files Browse the repository at this point in the history
  • Loading branch information
ChristopherPHolder committed Feb 20, 2024
1 parent d4214ef commit 3d22044
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 42 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import { INITIATED_PRJ_CFG, INITIATED_RC_JSON, EMPTY_PRJ_CFG } from 'test-data';
import { INITIATED_RC_JSON } from 'test-data';
import { handleFlowGeneration } from './generate-userflow';
import { existsSync } from 'fs';
import { join } from 'path';
import { withUserFlowProject } from '@push-based/user-flow-cli-testing';
import { Buffer } from 'node:buffer';
import fs = require('node:fs');

let originalCwd = process.cwd();
jest.mock('node:fs');

describe('generate userflow', () => {

afterEach(() => {
jest.resetAllMocks();
})

/**
* Legend:
* - [nf] - flow file does not exist
Expand All @@ -29,13 +32,6 @@ describe('generate userflow', () => {
*
*/

beforeAll(() => {
process.chdir(INITIATED_PRJ_CFG.root);
});
afterAll(() => {
process.chdir(originalCwd);
});

/*
@TODO create a test helper for functions with prompt side effects
// [nf] init --interactive => [P]: "Setup user flow" -> Y:[F]/n[nF]
Expand All @@ -51,43 +47,51 @@ describe('generate userflow', () => {
);*/

// [f] init --interactive => [L]: "File already here"; [nF]
it('should not prompt with --interactive if file exists', withUserFlowProject(INITIATED_PRJ_CFG, async () => {
const expectedFilePath = join(INITIATED_PRJ_CFG.root, INITIATED_RC_JSON.collect.ufPath,'basic-navigation.uf.ts');
expect(existsSync(expectedFilePath)).toBeTruthy();
it('should not prompt with --interactive if file exists',async () => {
const existsSyncSpy = jest.spyOn(fs, 'existsSync').mockReturnValue(true);
const isDirectorySpy = jest.spyOn(fs, 'lstatSync').mockReturnValue({ isDirectory: () => false } as any);
const readFileSyncSpy = jest.spyOn(fs, 'readFileSync').mockReturnValue('Dummy Flow');
const writeFileSyncSpy = jest.spyOn(fs, 'writeFileSync');
await handleFlowGeneration({interactive: true})(INITIATED_RC_JSON);
expect(existsSync(expectedFilePath)).toBeTruthy();
}));
expect(existsSyncSpy).toHaveBeenCalled();
expect(isDirectorySpy).toHaveBeenCalled();
expect(readFileSyncSpy).toHaveBeenCalled();
expect(writeFileSyncSpy).toHaveBeenCalledTimes(0);
});

// [f] init --no-interactive => [N]
it('should not prompt with --no-interactive if file exists', withUserFlowProject(INITIATED_PRJ_CFG, async () => {
const expectedFilePath = join(INITIATED_PRJ_CFG.root, INITIATED_RC_JSON.collect.ufPath,'basic-navigation.uf.ts');
expect(existsSync(expectedFilePath)).toBeTruthy();
it('should not prompt with --no-interactive if file exists',async () => {
const existsSyncSpy = jest.spyOn(fs, 'existsSync').mockReturnValue(true);
const writeFileSyncSpy = jest.spyOn(fs, 'writeFileSync');
await handleFlowGeneration({interactive: false})(INITIATED_RC_JSON);
expect(existsSync(expectedFilePath)).toBeTruthy();
}));
expect(existsSyncSpy).toHaveBeenCalledTimes(0);
expect(writeFileSyncSpy).toHaveBeenCalledTimes(0);
});

// [nf] init --no-interactive => [N]
it('should not prompt with --no-interactive if file does not exists', withUserFlowProject(EMPTY_PRJ_CFG, async () => {
const expectedFilePath = join(INITIATED_PRJ_CFG.root, INITIATED_RC_JSON.collect.ufPath,'basic-navigation.uf.ts');
expect(existsSync(expectedFilePath)).toBeFalsy();
it('should not prompt with --no-interactive if file does not exists', async () => {
const existsSyncSpy = jest.spyOn(fs, 'existsSync').mockReturnValue(true);
const writeFileSyncSpy = jest.spyOn(fs, 'writeFileSync');
await handleFlowGeneration({interactive: false})(INITIATED_RC_JSON);
expect(existsSync(expectedFilePath)).toBeFalsy();
}));
expect(existsSyncSpy).toHaveBeenCalledTimes(0);
expect(writeFileSyncSpy).toHaveBeenCalledTimes(0);
});

// [nf] init --interactive --generateFlow => [L]: "File already here"; [nF]
it('should create flow when --generateFlow is used', withUserFlowProject(EMPTY_PRJ_CFG, async () => {
const expectedFilePath = join(INITIATED_PRJ_CFG.root, INITIATED_RC_JSON.collect.ufPath,'basic-navigation.uf.ts');
expect(existsSync(expectedFilePath)).toBeFalsy();
it('should create flow when --generateFlow is used', async () => {
jest.spyOn(fs, 'readdirSync').mockReturnValue([]);
jest.spyOn(fs, 'existsSync').mockReturnValueOnce(false).mockReturnValue(true);
jest.spyOn(fs, 'lstatSync').mockReturnValue({ isDirectory: () => false } as any);
jest.spyOn(fs, 'readFileSync').mockReturnValueOnce(Buffer.from(''));
const writeFileSyncSpy = jest.spyOn(fs, 'writeFileSync');
await handleFlowGeneration({interactive: true, generateFlow: true})(INITIATED_RC_JSON);
expect(existsSync(expectedFilePath)).toBeTruthy();
}));
expect(writeFileSyncSpy).toHaveBeenCalledTimes(1);
});

// [f] init --interactive --no-generateFlow => [N]
it('should not create flow when --no-generateFlow is used', withUserFlowProject(EMPTY_PRJ_CFG, async () => {
const expectedFilePath = join(INITIATED_PRJ_CFG.root, INITIATED_RC_JSON.collect.ufPath,'basic-navigation.uf.ts');
expect(existsSync(expectedFilePath)).toBeFalsy();
it('should not create flow when --no-generateFlow is used', async () => {
const writeFileSyncSpy = jest.spyOn(fs, 'writeFileSync');
await handleFlowGeneration({interactive: true, generateFlow: false})(INITIATED_RC_JSON);
expect(existsSync(expectedFilePath)).toBeFalsy();
}));

expect(writeFileSyncSpy).toHaveBeenCalledTimes(0);
});
});
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { RcJson } from '../../../types';
import { join } from 'path';
import { join } from 'node:path';
import { readFile, writeFile } from '../../../core/file';
import { log } from '../../../core/loggin';
import { mkdirSync, readdirSync } from 'fs';
import { mkdirSync, readdirSync } from 'node:fs';
import { FlowExampleMap } from '../constants';
import { FlowExamples } from '../types';
import { ifThenElse } from '../../../core/processing/behaviors';
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/src/lib/core/file/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { dirname } from 'path';
import { existsSync, readFileSync, writeFileSync, mkdirSync, lstatSync } from 'fs';
import { dirname } from 'node:path';
import { existsSync, readFileSync, writeFileSync, mkdirSync, lstatSync } from 'node:fs';
import { logVerbose } from '../loggin';
import { getParserFromExtname, formatCode } from '../prettier';
import { ReadFileConfig } from '../../commands/collect/utils/replay/types';
Expand Down

0 comments on commit 3d22044

Please sign in to comment.