@naoufal/create-react-component
Version:
The fastest way to create React Components
90 lines (68 loc) • 1.62 kB
JavaScript
;
const path = require('path');
const fs = jest.genMockFromModule('fs');
global.__FS_MOCK_FILES__ = {};
function __setMockFiles(newMockFiles) {
global.__FS_MOCK_FILES__ = newMockFiles;
}
function __clearMockFiles() {
global.__FS_MOCK_FILES__ = {};
}
// Mock Methods
function accessSync(filePath) {
const file = mockFiles[filePath];
if (file) {
return file;
}
throw new Error('[fs:mock] File not found');
}
function mkdir(dirPath, cb) {
if (readDirSync(dirPath)) {
cb(new Error('[fs:mock] File already exists'));
return;
}
mkDirSync(dirPath)
cb();
}
function mkdirSync(dirPath) {
if (readDirSync(dirPath)) {
throw new Error('[fs:mock] File already exists');
}
mockFiles[dirPath] = {};
}
function lstatSync(filePath) {
const file = mockFiles[filePath];
if (file) {
return {
isDirectory() {
return file instanceof 'object';
}
}
}
throw new Error('[fs:mock] File not found');
}
function readFile(filePath, options, cb) {
const file = mockFiles[file];
if (file) {
cb(null, file);
return;
}
cb(new Error('[fs:mock] File not found'));
}
function readDirSync(dirPath) {
return mockFiles[dirPath] || null;
}
function removeSync(filePath) {
console.log('REMOVE SYNC', filePath);
delete global.__FS_MOCK_FILES__[filePath];
}
fs.readFile = readFile;
fs.mkdir = mkdir;
fs.mkdirSync = mkdirSync;
fs.accessSync = accessSync;
fs.readDirSync = readDirSync;
fs.lstatSync = lstatSync;
fs.removeSync = removeSync;
fs.__clearMockFiles = __clearMockFiles;
fs.__setMockFiles = __setMockFiles;
module.exports = fs;