Skip to content

Commit

Permalink
chore: fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
cipchk committed Nov 24, 2023
1 parent d1d6b8b commit d87d559
Show file tree
Hide file tree
Showing 8 changed files with 221 additions and 54 deletions.
24 changes: 8 additions & 16 deletions schematics/curd/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,14 @@ describe('Schematic: curd', () => {
let runner: SchematicTestRunner;
let tree: UnitTestTree;

describe('[with module]', () => {
beforeEach(async () => {
({ runner, tree } = await createAlainAndModuleApp({ moduleSchema: { standalone: false } }));
tree = await runner.runSchematic('curd', { name: 'list', module: 'trade', standalone: false }, tree);
});

it('should be generate list page', () => {
expect(tree.exists('/projects/foo/src/app/routes/trade/list/list.component.ts')).toBe(true);
});

it('should be generate view page', () => {
expect(tree.exists('/projects/foo/src/app/routes/trade/list/view/view.component.ts')).toBe(true);
});
beforeEach(async () => {
({ runner, tree } = await createAlainAndModuleApp({ moduleSchema: { standalone: false } }));
tree = await runner.runSchematic('curd', { name: 'list', module: 'trade', standalone: false }, tree);
});

it('should be generate edit page', () => {
expect(tree.exists('/projects/foo/src/app/routes/trade/list/edit/edit.component.ts')).toBe(true);
});
it('should be generate list page', () => {
expect(tree.exists('/projects/foo/src/app/routes/trade/list/list.component.ts')).toBe(true);
expect(tree.exists('/projects/foo/src/app/routes/trade/list/view/view.component.ts')).toBe(true);
expect(tree.exists('/projects/foo/src/app/routes/trade/list/edit/edit.component.ts')).toBe(true);
});
});
42 changes: 37 additions & 5 deletions schematics/edit/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,48 @@ describe('Schematic: edit', () => {

beforeEach(async () => {
({ runner, tree } = await createAlainAndModuleApp({ moduleSchema: { standalone: false } }));
tree = await runner.runSchematic('edit', { name: 'edit', module: 'trade', standalone: false }, tree);
});

it('should be generate list page', () => {
it('should be working', async () => {
tree = await runner.runSchematic('edit', { name: 'edit', module: 'trade', standalone: false }, tree);
[modulePath, routingPath, tsPath, htmlPath].forEach(path => expect(tree.exists(path)).toBe(true));
expect(tree.readContent(modulePath)).toContain(`import { TradeEditComponent } from './edit/edit.component';`);
expect(tree.readContent(routingPath)).not.toContain(
`import { TradeEditComponent } from './edit/edit.component';`
);
});

it('when modal is false', async () => {
tree = await runner.runSchematic(
'edit',
{ name: 'edit', module: 'trade', standalone: false, modal: false },
tree
);
const cont = tree.readContent(routingPath);
expect(cont).toContain(`{ path: 'edit', component: TradeEditComponent }`);
});
});

describe('[with standalone]', () => {
const routingPath = '/projects/foo/src/app/routes/trade/routes.ts';
const tsPath = '/projects/foo/src/app/routes/trade/edit/edit.component.ts';
const htmlPath = '/projects/foo/src/app/routes/trade/edit/edit.component.html';

beforeEach(async () => {
({ runner, tree } = await createAlainAndModuleApp({ moduleSchema: { standalone: true } }));
});

it('should be working', async () => {
tree = await runner.runSchematic('edit', { name: 'edit', module: 'trade', standalone: true }, tree);
[routingPath, tsPath, htmlPath].forEach(path => expect(tree.exists(path)).toBe(true));
const cont = tree.readContent(routingPath);
expect(cont).not.toContain(`import { TradeEditComponent } from './edit/edit.component';`);
});

it('should be has import code', () => {
const cont = tree.readContent(modulePath);
expect(cont).toContain(`import { TradeEditComponent } from './edit/edit.component';`);
it('when modal is false', async () => {
tree = await runner.runSchematic('edit', { name: 'edit', module: 'trade', standalone: true, modal: false }, tree);
const cont = tree.readContent(routingPath);
expect(cont).toContain(`{ path: 'edit', component: TradeEditComponent }`);
});
});
});
63 changes: 51 additions & 12 deletions schematics/empty/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,8 @@ describe('Schematic: empty', () => {
tree = await runner.runSchematic('empty', { name: 'list', module: 'trade', standalone: false }, tree);
[modulePath, routingPath, tsPath, htmlPath].forEach(path => expect(tree.exists(path)).toBe(true));
expect(tree.exists(servicePath)).toBe(false);
});

it('should be has import code', async () => {
tree = await runner.runSchematic('empty', { name: 'list', module: 'trade', standalone: false }, tree);
expect(tree.readContent(modulePath)).toContain(`import { TradeListComponent } from './list/list.component';`);
});

it('should be include module name in component name', async () => {
tree = await runner.runSchematic('empty', { name: 'list', module: 'trade', standalone: false }, tree);
expect(tree.readContent(tsPath)).toContain(`TradeListComponent`);
});

it('shuold be exclude style', async () => {
tree = await runner.runSchematic('empty', { name: 'list', module: 'trade', standalone: false }, tree);
expect(tree.readContent(tsPath)).not.toContain(`styleUrls`);
});

Expand Down Expand Up @@ -69,4 +57,55 @@ describe('Schematic: empty', () => {
expect(tree.exists('/projects/foo/src/app/pages/trade/trade.module.ts')).toBe(true);
});
});

describe('[with standalone]', () => {
const routingPath = '/projects/foo/src/app/routes/trade/routes.ts';
const tsPath = '/projects/foo/src/app/routes/trade/list/list.component.ts';
const servicePath = '/projects/foo/src/app/routes/trade/list/list.service.ts';
const htmlPath = '/projects/foo/src/app/routes/trade/list/list.component.html';

describe('', () => {
beforeEach(async () => {
({ runner, tree } = await createAlainAndModuleApp({ moduleSchema: { standalone: true } }));
});

it('should be working', async () => {
tree = await runner.runSchematic('empty', { name: 'list', module: 'trade', standalone: true }, tree);
[routingPath, tsPath, htmlPath].forEach(path => expect(tree.exists(path)).toBe(true));
expect(tree.exists(servicePath)).toBe(false);
expect(tree.readContent(tsPath)).toContain(`standalone: true,`);
expect(tree.readContent(tsPath)).toContain(`SHARED_IMPORTS`);
expect(tree.readContent(tsPath)).toContain(`TradeListComponent`);
expect(tree.readContent(tsPath)).not.toContain(`styleUrls`);
});

it('shuold be include service', async () => {
tree = await runner.runSchematic(
'empty',
{ name: 'list', module: 'trade', service: 'none', standalone: true },
tree
);
expect(tree.readContent(servicePath)).toContain(`@Injectable()`);
});

it('shuold be include root service', async () => {
tree = await runner.runSchematic(
'empty',
{ name: 'list', module: 'trade', service: 'root', standalone: true },
tree
);
expect(tree.readContent(servicePath)).toContain(`@Injectable({ providedIn: 'root' })`);
});
});

it('should be specify routesRoot in ng-alain.json', async () => {
const alainJson = { projects: { foo: { routesRoot: 'app/pages' } } };
({ runner, tree } = await createAlainAndModuleApp({
alainData: alainJson,
moduleSchema: { standalone: true }
}));
tree = await runner.runSchematic('empty', { name: 'list', module: 'trade', standalone: true }, tree);
expect(tree.exists('/projects/foo/src/app/pages/trade/routes.ts')).toBe(true);
});
});
});
60 changes: 51 additions & 9 deletions schematics/list/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,8 @@ describe('Schematic: list', () => {

it('should be generate list page', () => {
[modulePath, routingPath, tsPath, htmlPath].forEach(path => expect(tree.exists(path)).toBe(true));
});

it('should be has import code', () => {
expect(tree.readContent(modulePath)).toContain(`import { TradeListComponent } from './list/list.component';`);
});

it('should be include module name in component name', () => {
expect(tree.readContent(tsPath)).toContain(`TradeListComponent`);
});

it('shuold be exclude style', () => {
expect(tree.readContent(tsPath)).not.toContain(`styleUrls`);
});

Expand Down Expand Up @@ -67,4 +58,55 @@ describe('Schematic: list', () => {
expect(tree.readContent(modulePath)).toContain(`TradeService`);
});
});

describe('[with standalone]', () => {
const routesPath = '/projects/foo/src/app/routes/trade/routes.ts';
const tsPath = '/projects/foo/src/app/routes/trade/list/list.component.ts';
const htmlPath = '/projects/foo/src/app/routes/trade/list/list.component.html';

beforeEach(async () => {
({ runner, tree } = await createAlainAndModuleApp({ moduleSchema: { standalone: true } }));

tree = await runner.runSchematic('list', { name: 'list', module: 'trade', standalone: true }, tree);
});

it('should be working', () => {
[routesPath, tsPath, htmlPath].forEach(path => expect(tree.exists(path)).toBe(true));
expect(tree.readContent(routesPath)).toContain(`import { TradeListComponent } from './list/list.component';`);
expect(tree.readContent(tsPath)).toContain(`TradeListComponent`);
expect(tree.readContent(tsPath)).toContain(`standalone: true,`);
expect(tree.readContent(tsPath)).not.toContain(`styleUrls`);
});

it('should be support targets (like: list/edit)', async () => {
tree = await runner.runSchematic(
'list',
{ name: 'list2', module: 'trade', target: 'list/edit', standalone: true },
tree
);
expect(tree.exists(`/projects/foo/src/app/routes/trade/list/edit/list2/list2.component.html`)).toBe(true);
});

it('should be throw error when directory already exists', async () => {
spyOn(fs, 'existsSync').and.returnValue(true);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
spyOn(fs, 'readdirSync').and.returnValue({ length: 1 } as any);
try {
tree = await runner.runSchematic('list', { name: 'list', module: 'trade', standalone: true }, tree);
expect(true).toBe(false);
} catch (e) {
expect(e.message).toContain(`already exists`);
}
});

it('shuold be include service', async () => {
tree = await runner.runSchematic(
'list',
{ name: 'list', module: 'trade', service: 'none', standalone: true },
tree
);
const servicePath = '/projects/foo/src/app/routes/trade/list/list.service.ts';
expect(tree.readContent(servicePath)).toContain(`@Injectable()`);
});
});
});
37 changes: 35 additions & 2 deletions schematics/module/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ describe('NgAlainSchematic: module', () => {
({ runner, tree } = await createAlainApp());
});

describe('[when is module]', () => {
describe('[with module]', () => {
it('should create a module', async () => {
tree = await runner.runSchematic('module', { ...defaultOptions, standalone: false }, tree);
expect(tree.files.includes('/projects/foo/src/app/routes/trade/trade.module.ts')).toBe(true);
Expand Down Expand Up @@ -57,7 +57,7 @@ describe('NgAlainSchematic: module', () => {
});
});

describe('when is standalone', () => {
describe('[with standalone]', () => {
it('should be working', async () => {
tree = await runner.runSchematic('module', { ...defaultOptions, standalone: true }, tree);
const path = '/projects/foo/src/app/routes/trade/routes.ts';
Expand All @@ -70,5 +70,38 @@ describe('NgAlainSchematic: module', () => {
const rootContent = tree.readContent(rootPath);
expect(rootContent).toContain(`import('./trade/routes').then((m) => m.routes)`);
});

it('should import into another module', async () => {
tree = await runner.runSchematic('module', { ...defaultOptions, name: 'trade', standalone: true }, tree);
tree = await runner.runSchematic(
'module',
{
...defaultOptions,
name: 'sys',
module: 'trade.module.ts',
path: '/projects/foo/src/app/routes/trade',
standalone: true
},
tree
);
const content = tree.readContent('/projects/foo/src/app/routes/trade/routes.ts');
expect(content).toContain(`loadChildren: () => import('./sys/routes').then((m) => m.routes)`);
});

it('shuold be include service', async () => {
tree = await runner.runSchematic('module', { ...defaultOptions, service: 'none', standalone: true }, tree);
const content = tree.readContent('/projects/foo/src/app/routes/trade/trade.service.ts');
const contentModule = tree.readContent('/projects/foo/src/app/routes/trade/routes.ts');
expect(content).toContain(`@Injectable()`);
expect(content).toContain(`TradeService`);
expect(contentModule).toContain(`import { TradeService } from './trade.service';`);
});

it('shuold be include root service', async () => {
tree = await runner.runSchematic('module', { ...defaultOptions, service: 'root', standalone: true }, tree);
const content = tree.readContent('/projects/foo/src/app/routes/trade/trade.service.ts');
expect(content).toContain(`@Injectable({ providedIn: 'root' })`);
expect(content).toContain(`TradeService`);
});
});
});
17 changes: 11 additions & 6 deletions schematics/module/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,10 @@ function addRoutingModuleToTop(options: ModuleSchema): Rule {
) {
return tree;
}
const childrenNode = findNode(parentNode.initializer, ts.SyntaxKind.Identifier, 'children');
let childrenNode = findNode(parentNode.initializer, ts.SyntaxKind.Identifier, 'children');
if (options.standalone && childrenNode == null) {
childrenNode = parentNode;
}
if (childrenNode == null || childrenNode.parent == null) {
return tree;
}
Expand Down Expand Up @@ -113,12 +116,14 @@ function addServiceToNgModule(options: ModuleSchema): Rule {
return (tree: Tree) => {
if (options.service !== 'none') return tree;

const basePath = `/${options.path}/${options.flat ? '' : `${strings.dasherize(options.name)}/`}${strings.dasherize(
options.name
)}`;
const servicePath = normalize(`${basePath}.service`);
const basePath = `/${options.path}/${options.flat ? '' : `${strings.dasherize(options.name)}/`}`;
const servicePath = normalize(`${basePath}${strings.dasherize(options.name)}.service`);
const serviceName = strings.classify(`${options.name}Service`);
const importModulePath = normalize(`${basePath}.module`);
const importModulePath = normalize(
options.standalone
? `${basePath}${ROUTINS_FILENAME.split('.')[0]}`
: `${basePath}${strings.dasherize(options.name)}.module`
);
const importServicePath = buildRelativePath(importModulePath, servicePath);
addServiceToModuleOrStandalone(tree, options.standalone, `${importModulePath}.ts`, serviceName, importServicePath);
return tree;
Expand Down
2 changes: 1 addition & 1 deletion schematics/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const Jasmine = require('jasmine');
const runner = new Jasmine({ projectBaseDir });

// const files = `schematics/**/*.spec.ts`;
const files = `schematics/module/index.spec.ts`;
const files = `schematics/view/index.spec.ts`;

const tests = glob.sync(files).map(p => relative(projectBaseDir, p));

Expand Down
30 changes: 27 additions & 3 deletions schematics/view/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@ describe('Schematic: view', () => {

it('should be generate list page', () => {
[modulePath, routingPath, tsPath, htmlPath].forEach(path => expect(tree.exists(path)).toBe(true));
});

it('should be has import code', () => {
expect(tree.readContent(modulePath)).toContain(`import { TradeViewComponent } from './view/view.component';`);
});

Expand All @@ -34,4 +31,31 @@ describe('Schematic: view', () => {
expect(tree.exists('/projects/foo/src/app/routes/trade/list/view/view.component.ts')).toBe(true);
});
});

describe('[with standalone]', () => {
const routingPath = '/projects/foo/src/app/routes/trade/routes.ts';
const tsPath = '/projects/foo/src/app/routes/trade/view/view.component.ts';
const htmlPath = '/projects/foo/src/app/routes/trade/view/view.component.html';

beforeEach(async () => {
({ runner, tree } = await createAlainAndModuleApp({ moduleSchema: { standalone: true } }));
tree = await runner.runSchematic('view', { name: 'view', module: 'trade', standalone: true }, tree);
});

it('should be generate list page', () => {
[routingPath, tsPath, htmlPath].forEach(path => expect(tree.exists(path)).toBe(true));
expect(tree.readContent(routingPath)).not.toContain(
`import { TradeViewComponent } from './view/view.component';`
);
});

it('should support a.b.c module name', async () => {
tree = await runner.runSchematic(
'view',
{ name: 'view', module: 'trade', target: 'list', standalone: true },
tree
);
expect(tree.exists('/projects/foo/src/app/routes/trade/list/view/view.component.ts')).toBe(true);
});
});
});

0 comments on commit d87d559

Please sign in to comment.