Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ariskataoka committed Jul 18, 2024
1 parent 37bedd0 commit 8cf91ac
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions __tests__/lib/vip-import-validate-files.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
isFileSanitized,
validateFiles,
logErrors,
findNestedDirectories,
} from '../../src/lib/vip-import-validate-files';

global.console = { log: jest.fn(), error: jest.fn() };
Expand Down Expand Up @@ -214,4 +215,49 @@ describe( 'lib/vip-import-validate-files', () => {
expect( mockConsoleError ).not.toHaveBeenCalled();
} );
} );
describe( 'findNestedDirectories()', () => {
// Mocking file system and chalk
jest.mock( 'fs' );
jest.mock( 'chalk', () => ( {
red: jest.fn( () => 'red' ),
} ) );

let readdirSyncMock;
let statSyncMock;

beforeEach( () => {
readdirSyncMock = jest.spyOn( fs, 'readdirSync' );
statSyncMock = jest.spyOn( fs, 'statSync' );
} );

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

it( 'should return undefined and log an error if the directory cannot be read', () => {
const errorMessage = 'Reason: ENOTDIR: not a directory, scandir ~/Downloads/wp-content.zip';
readdirSyncMock.mockImplementation( () => {
throw new Error( errorMessage );
} );

console.error = jest.fn();

const result = findNestedDirectories( '~/Downloads/wp-content.zip' );

expect( result ).toBeUndefined();
expect( console.error ).toHaveBeenCalledWith(
chalk.red( '✕' ),
` Error: Cannot read nested directory: ~/Downloads/wp-content.zip. Reason: ${ errorMessage }`
);
} );

it( 'should return an empty result for an empty directory', () => {
readdirSyncMock.mockReturnValue( [] );
statSyncMock.mockReturnValue( { isDirectory: () => false } );

const result = findNestedDirectories( '/empty/dir' );

expect( result ).toEqual( { files: [], folderStructureObj: {} } );
} );
} );
} );

0 comments on commit 8cf91ac

Please sign in to comment.