|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const mockFs = require('mock-fs'); |
| 4 | + |
| 5 | +import * as ts from 'typescript'; |
| 6 | +import * as path from 'path'; |
| 7 | +import * as dependentFilesUtils from '@angular/cli/utilities/get-dependent-files'; |
| 8 | + |
| 9 | +import { expect } from 'chai'; |
| 10 | +import findParentModule from '@angular/cli/utilities/find-parent-module'; |
| 11 | + |
| 12 | +describe('findParentModule', () => { |
| 13 | + const project = { |
| 14 | + root: 'project', |
| 15 | + ngConfig: { |
| 16 | + apps: [ |
| 17 | + { root: 'src' }, |
| 18 | + ], |
| 19 | + }, |
| 20 | + }; |
| 21 | + |
| 22 | + const mockDrive = { |
| 23 | + 'project': { |
| 24 | + 'src': { |
| 25 | + 'app': { |
| 26 | + 'app.module.ts': ``, |
| 27 | + 'app.routing.module.ts': ``, |
| 28 | + 'component': {}, |
| 29 | + 'child-module': { |
| 30 | + 'child-module.module.ts': ``, |
| 31 | + 'child-module.routing.module.ts': ``, |
| 32 | + 'child-module-component': {}, |
| 33 | + }, |
| 34 | + 'router-module': { |
| 35 | + 'router-module.routing.module.ts': `` |
| 36 | + } |
| 37 | + }, |
| 38 | + 'myapp': { |
| 39 | + 'myapp.module.ts': ``, |
| 40 | + 'myapp.routing.module.ts': ``, |
| 41 | + 'component': {}, |
| 42 | + 'child-module': { |
| 43 | + 'child-module.module.ts': ``, |
| 44 | + 'child-module.routing.module.ts': ``, |
| 45 | + 'child-module-component': {}, |
| 46 | + }, |
| 47 | + 'router-module': { |
| 48 | + 'router-module.routing.module.ts': `` |
| 49 | + } |
| 50 | + } |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + const apps = [ |
| 56 | + { root: 'src', subfolder: 'app' }, |
| 57 | + { root: 'src', subfolder: 'myapp' } |
| 58 | + ]; |
| 59 | + |
| 60 | + beforeEach(() => { |
| 61 | + mockFs(mockDrive); |
| 62 | + }); |
| 63 | + |
| 64 | + afterEach(() => { |
| 65 | + mockFs.restore(); |
| 66 | + }); |
| 67 | + |
| 68 | + describe('Finds parent module', () => { |
| 69 | + it('throws and error if not in an application folder', () => { |
| 70 | + let currentDir = ''; |
| 71 | + expect(() => { |
| 72 | + let parentModule = findParentModule(project, currentDir); |
| 73 | + // if for some reason it doesn't throw.. expect undefined |
| 74 | + // in hopes of logging something useful. |
| 75 | + expect(parentModule).to.equal(undefined); |
| 76 | + }).to.throw(); |
| 77 | + }); |
| 78 | + |
| 79 | + apps.forEach((app) => { testApp(app)}); |
| 80 | + }); |
| 81 | + |
| 82 | + function testApp(app: Object) { |
| 83 | + describe('module in folder '+ app.subfolder, () => { |
| 84 | + it('works for root folder', () => { |
| 85 | + let currentDir = path.join(app.root, app.subfolder); |
| 86 | + expect(() => { |
| 87 | + let parentModule = findParentModule(project, currentDir); |
| 88 | + expect(parentModule).to.equal(path.join(project.root, app.root, app.subfolder, app.subfolder +'.module.ts')); |
| 89 | + }).to.not.throw(); |
| 90 | + }); |
| 91 | + |
| 92 | + it('works from an app component folder', () => { |
| 93 | + let currentDir = path.join(app.root, app.subfolder, 'component'); |
| 94 | + expect(() => { |
| 95 | + let parentModule = findParentModule(project, currentDir); |
| 96 | + expect(parentModule).to.equal(path.join(project.root, app.root, app.subfolder, app.subfolder +'.module.ts')); |
| 97 | + }).to.not.throw(); |
| 98 | + }); |
| 99 | + |
| 100 | + it('ignores router modules', () => { |
| 101 | + let currentDir = path.join(app.root, app.subfolder, 'router-module'); |
| 102 | + expect(() => { |
| 103 | + let parentModule = findParentModule(project, currentDir); |
| 104 | + expect(parentModule).to.equal(path.join(project.root, app.root, app.subfolder, app.subfolder + '.module.ts')); |
| 105 | + }).to.not.throw(); |
| 106 | + }); |
| 107 | + |
| 108 | + it('works from a child module', () => { |
| 109 | + let currentDir = path.join(app.root, app.subfolder, 'child-module'); |
| 110 | + expect(() => { |
| 111 | + let parentModule = findParentModule(project, currentDir); |
| 112 | + expect(parentModule).to.equal(path.join(project.root, app.root, app.subfolder, 'child-module', 'child-module.module.ts')); |
| 113 | + }).to.not.throw(); |
| 114 | + }); |
| 115 | + |
| 116 | + it('works from a component of a child module', () => { |
| 117 | + let currentDir = path.join(app.root, app.subfolder, 'child-module', 'child-module-component'); |
| 118 | + expect(() => { |
| 119 | + let parentModule = findParentModule(project, currentDir); |
| 120 | + expect(parentModule).to.equal(path.join(project.root, app.root, app.subfolder, 'child-module', 'child-module.module.ts')); |
| 121 | + }).to.not.throw(); |
| 122 | + }); |
| 123 | + }); |
| 124 | + } |
| 125 | + |
| 126 | +}); |
0 commit comments