Skip to content

Commit ddda79b

Browse files
committed
feat: teste cases
1 parent 40a918b commit ddda79b

File tree

4 files changed

+60
-4
lines changed

4 files changed

+60
-4
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,10 +135,11 @@ import-conductor -p @customA @customB
135135
import-conductor --separator '' ==> no separator
136136
```
137137

138-
- `groupOrder` - The order imports will be organized: (defaults to `[thirdParty, userLibrary, differentModule, sameModule]`)
138+
- `groupOrder` - The group order to follow: (defaults to `[thirdParty, userLibrary, differentModule, sameModule]`)
139139

140140
```shell script
141141
import-conductor --groupOrder 'userLibrary' 'differentModule' 'sameModule' 'thirdParty'
142+
import-conductor -g 'userLibrary' 'differentModule' 'sameModule' 'thirdParty'
142143
```
143144

144145
- `staged` - Run against staged files: (defaults to `false`)

__tests__/optimize-imports-mocks.ts

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export type TestCase = { input: string; expected: string; noOfRuns?: number };
1+
export type TestCase = { input: string; expected: string; noOfRuns?: number; groupOrder?: string[] };
22

33
export const readmeExample: TestCase = {
44
input: `import fs from 'fs';
@@ -116,3 +116,38 @@ export const noImportStatement: TestCase = {
116116
input: `const x = 2;`,
117117
expected: `const x = 2;`,
118118
};
119+
120+
export const importsOnDifferentGroupOrder: TestCase = {
121+
groupOrder: ['userLibrary', 'sameModule', 'differentModule', 'thirdParty'],
122+
input: `import { Component } from '@angular/core';
123+
import fs from 'fs';
124+
import { LoggerService } from '@myorg/logger';
125+
import { Order } from '../order/order.model';
126+
import { CustomService } from './customer.service';`,
127+
expected: `import { LoggerService } from '@myorg/logger';
128+
import { CustomService } from './customer.service';
129+
import { Order } from '../order/order.model';
130+
import { Component } from '@angular/core';
131+
import fs from 'fs';`,
132+
};
133+
134+
export const importsWithGroupOrderIncorrect: TestCase = {
135+
groupOrder: ['userLibrary', 'differentModule', 'thirdParty'],
136+
input: `import fs from 'fs';
137+
import { CustomerService } from './customer.service';
138+
import { Order } from '../order/order.model';
139+
import { Component, OnInit } from '@angular/core';
140+
import { LoggerService } from '@myorg/logger';
141+
import { Observable } from 'rxjs';
142+
import { spawn } from 'child_process';`,
143+
expected: `import { Component, OnInit } from '@angular/core';
144+
import { spawn } from 'child_process';
145+
import fs from 'fs';
146+
import { Observable } from 'rxjs';
147+
148+
import { LoggerService } from '@myorg/logger';
149+
150+
import { Order } from '../order/order.model';
151+
152+
import { CustomerService } from './customer.service';`,
153+
};

__tests__/optimize-imports.spec.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,18 @@ 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, noImportStatement } from './optimize-imports-mocks';
5+
import {
6+
readmeExample,
7+
comments,
8+
TestCase,
9+
codeBetweenImports,
10+
emptyNewLineSeparator,
11+
noImportStatement,
12+
importsOnDifferentGroupOrder,
13+
importsWithGroupOrderIncorrect,
14+
} from './optimize-imports-mocks';
615
import { defaultConfig } from '@ic/defaultConfig';
16+
import { getGroupOrder } from '@ic/conductor/get-group-order';
717

818
jest.mock('fs');
919
jest.mock('simple-git');
@@ -80,4 +90,14 @@ describe('optimizeImports', () => {
8090
expect(result).toBe(actions.none);
8191
expect(fs.writeFileSync).not.toHaveBeenCalled();
8292
});
93+
94+
it('should change group order', async () => {
95+
spy.and.returnValue({ ...basicConfig, groupOrder: importsOnDifferentGroupOrder.groupOrder, separator: '' });
96+
await assertConductor(importsOnDifferentGroupOrder);
97+
});
98+
99+
it('should use default order because incorrect group order input', async () => {
100+
spy.and.returnValue({ ...basicConfig, groupOrder: getGroupOrder({ groupOrder: importsWithGroupOrderIncorrect.groupOrder }) });
101+
await assertConductor(importsWithGroupOrderIncorrect);
102+
});
83103
});

src/conductor/get-group-order.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { Config } from '../types';
44
export function getGroupOrder(config: Partial<Config>) {
55
const groups = new Set(config?.groupOrder || []);
66
const uniqueGroups = Array.from(groups);
7-
return !isValidGroupArgument(uniqueGroups) ? defaultConfig.groupOrder : uniqueGroups;
7+
return isValidGroupArgument(uniqueGroups) ? uniqueGroups : defaultConfig.groupOrder;
88
}
99

1010
function isValidGroupArgument(groups: string[]): boolean {

0 commit comments

Comments
 (0)