Skip to content

Commit 3818e0e

Browse files
committed
feat: testing no import statement
1 parent e4025f5 commit 3818e0e

File tree

3 files changed

+18
-5
lines changed

3 files changed

+18
-5
lines changed

__tests__/optimize-imports-mocks.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,3 +111,8 @@ import { MatDialogRef } from '@angular/material/dialog';
111111
import { Observable } from 'rxjs';
112112
import { AboutDialogBloc, AboutState } from './about-dialog.bloc';`,
113113
};
114+
115+
export const noImportStatement: TestCase = {
116+
input: `const x = 2;`,
117+
expected: `const x = 2;`,
118+
};

__tests__/optimize-imports.spec.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { actions, organizeImports, organizeImportsForFile } from '@ic/conductor/
22
import * as config from '@ic/config';
33
import fs from 'fs';
44
import { Config } from '@ic/types';
5-
import { readmeExample, comments, TestCase, codeBetweenImports, emptyNewLineSeparator } from './optimize-imports-mocks';
5+
import { readmeExample, comments, TestCase, codeBetweenImports, emptyNewLineSeparator, noImportStatement } from './optimize-imports-mocks';
66
import { defaultConfig } from '@ic/defaultConfig';
77

88
jest.mock('fs');
@@ -72,4 +72,12 @@ describe('optimizeImports', () => {
7272
expect(fs.writeFileSync).not.toHaveBeenCalled();
7373
}
7474
});
75+
76+
it('should do nothing if the file has no import', async () => {
77+
(fs.readFileSync as any).mockReturnValue(Buffer.from(noImportStatement.input));
78+
const file = 'test.ts';
79+
const result = await organizeImportsForFile(file);
80+
expect(result).toBe(actions.none);
81+
expect(fs.writeFileSync).not.toHaveBeenCalled();
82+
});
7583
});

src/conductor/organize-imports.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ export async function organizeImportsForFile(filePath: string): Promise<string>
4545
const { staged, autoAdd, dryRun } = getConfig();
4646
const fileWithOrganizedImports = await organizeImports(fileContent);
4747
const fileHasChanged = fileWithOrganizedImports !== fileContent;
48-
const isValidAction = [actions.none, actions.skipped].every(action => action !== fileWithOrganizedImports);
48+
const isValidAction = [actions.none, actions.skipped].every((action) => action !== fileWithOrganizedImports);
4949

5050
if (fileHasChanged && isValidAction) {
5151
!dryRun && writeFileSync(filePath, fileWithOrganizedImports);
@@ -55,11 +55,11 @@ export async function organizeImportsForFile(filePath: string): Promise<string>
5555
msg += ', added to git';
5656
}
5757
log('green', msg, filePath);
58-
} else {
59-
log('gray', 'no change needed', filePath);
58+
return actions.reordered;
6059
}
6160

62-
return fileHasChanged ? actions.reordered : actions.none;
61+
log('gray', 'no change needed', filePath);
62+
return actions.none;
6363
}
6464

6565
export async function organizeImports(fileContent: string): Promise<string> {

0 commit comments

Comments
 (0)