Skip to content

Commit

Permalink
fix(core): fix error when has't modult.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
luxiaobei authored and why520crazy committed Dec 17, 2021
1 parent 5b428a0 commit cbbddf6
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 65 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { DocgeniContext } from '../../docgeni.interface';
import { createTestDocgeniContext, DEFAULT_TEST_ROOT_PATH } from '../../testing';
import { createTestDocgeniContext, DEFAULT_TEST_ROOT_PATH, writeFilesToHost } from '../../testing';
import { ComponentsBuilder } from './components-builder';
import { toolkit } from '@docgeni/toolkit';
import { Subscription } from 'rxjs';
Expand All @@ -26,8 +26,7 @@ describe('#components-builder', () => {
selector: 'my-color',
templateUrl: './color.component.html'
}) export class ColorComponent {};`,
[`${COMPONENTS_ROOT_PATH}/color/color.component.html`]: 'color',
[`${COMPONENTS_ROOT_PATH}/module.ts`]: 'export default { imports: [] }'
[`${COMPONENTS_ROOT_PATH}/color/color.component.html`]: 'color'
};

beforeEach(() => {
Expand All @@ -42,47 +41,73 @@ describe('#components-builder', () => {
componentsDistPath = resolve(context.paths.absSiteContentPath, 'components/custom');
});

it('should build components success', async () => {
const builtInModuleSpy = spyOn(builtInModule, 'generateBuiltInComponentsModule');
const moduleText = 'module text';
builtInModuleSpy.and.returnValue(Promise.resolve('module text'));
const componentsBuilder = new ComponentsBuilder(context);
await componentsBuilder.build();
await componentsBuilder.emit();
const helloComponentContent = await context.host.readFile(resolve(componentsDistPath, 'hello/hello.component.ts'));
expect(helloComponentContent).toEqual(initialFiles[`${COMPONENTS_ROOT_PATH}/hello/hello.component.ts`]);
const entryContent = await context.host.readFile(resolve(componentsDistPath, 'index.ts'));

const components = (componentsBuilder as any).components as Map<string, ComponentBuilder>;

expect(components.get(`${COMPONENTS_ROOT_PATH}/hello`).componentData).toEqual({
selector: 'hello',
name: 'HelloComponent'
describe('basic', () => {
it('should build components success', async () => {
await writeFilesToHost(context.host, { [`${COMPONENTS_ROOT_PATH}/module.ts`]: 'export default { imports: [] }' });
const builtInModuleSpy = spyOn(builtInModule, 'generateBuiltInComponentsModule');
const moduleText = 'module text';
builtInModuleSpy.and.returnValue(Promise.resolve('module text'));
const componentsBuilder = new ComponentsBuilder(context);
await componentsBuilder.build();
await componentsBuilder.emit();
const helloComponentContent = await context.host.readFile(resolve(componentsDistPath, 'hello/hello.component.ts'));
expect(helloComponentContent).toEqual(initialFiles[`${COMPONENTS_ROOT_PATH}/hello/hello.component.ts`]);
const entryContent = await context.host.readFile(resolve(componentsDistPath, 'index.ts'));

const components = (componentsBuilder as any).components as Map<string, ComponentBuilder>;

expect(components.get(`${COMPONENTS_ROOT_PATH}/hello`).componentData).toEqual({
selector: 'hello',
name: 'HelloComponent'
});

expect(entryContent).toEqual(moduleText);
});

expect(entryContent).toEqual(moduleText);
});
it('should build components success when has`t export default', async () => {
await writeFilesToHost(context.host, { [`${COMPONENTS_ROOT_PATH}/module.ts`]: 'export default { imports: [] }' });
const builtInModuleSpy = spyOn(builtInModule, 'generateBuiltInComponentsModule');
const moduleText = 'module text';
builtInModuleSpy.and.returnValue(Promise.resolve('module text'));
const componentsBuilder = new ComponentsBuilder(context);
await componentsBuilder.build();
await componentsBuilder.emit();

it('should build components success when has`t export default', async () => {
const builtInModuleSpy = spyOn(builtInModule, 'generateBuiltInComponentsModule');
const moduleText = 'module text';
builtInModuleSpy.and.returnValue(Promise.resolve('module text'));
const componentsBuilder = new ComponentsBuilder(context);
await componentsBuilder.build();
await componentsBuilder.emit();
const components = (componentsBuilder as any).components as Map<string, ComponentBuilder>;

const components = (componentsBuilder as any).components as Map<string, ComponentBuilder>;
expect(components.get(`${COMPONENTS_ROOT_PATH}/color`).componentData).toEqual({
selector: 'my-color',
name: 'ColorComponent'
});
});

expect(components.get(`${COMPONENTS_ROOT_PATH}/color`).componentData).toEqual({
selector: 'my-color',
name: 'ColorComponent'
it('should build success when not exist module.ts', async () => {
const builtInModuleSpy = spyOn(builtInModule, 'generateBuiltInComponentsModule');
const moduleText = 'module text';
builtInModuleSpy.and.returnValue(Promise.resolve('module text'));
const componentsBuilder = new ComponentsBuilder(context);
await componentsBuilder.build();
await componentsBuilder.emit();
const helloComponentContent = await context.host.readFile(resolve(componentsDistPath, 'hello/hello.component.ts'));
expect(helloComponentContent).toEqual(initialFiles[`${COMPONENTS_ROOT_PATH}/hello/hello.component.ts`]);
const entryContent = await context.host.readFile(resolve(componentsDistPath, 'index.ts'));

const components = (componentsBuilder as any).components as Map<string, ComponentBuilder>;

expect(components.get(`${COMPONENTS_ROOT_PATH}/hello`).componentData).toEqual({
selector: 'hello',
name: 'HelloComponent'
});

expect(entryContent).toEqual(moduleText);
});
});

describe('watch', () => {
let componentsBuilder: ComponentsBuilder;
let subscription: Subscription;
beforeEach(async () => {
await writeFilesToHost(context.host, { [`${COMPONENTS_ROOT_PATH}/module.ts`]: 'export default { imports: [] }' });
componentsBuilder = new ComponentsBuilder(context);
await componentsBuilder.build();
await componentsBuilder.emit();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ export class ComponentsBuilder {

async emitEntryFile() {
let sourceFile: NgSourceFile;
if (this.docgeni.host.pathExists(this.modulePath)) {
if (await this.docgeni.host.pathExists(this.modulePath)) {
const sourceFileText = await this.docgeni.host.readFile(this.modulePath);
sourceFile = createNgSourceFile(this.modulePath, sourceFileText);
} else {
Expand All @@ -114,6 +114,6 @@ export class ComponentsBuilder {
for (const component of this.components.values()) {
await component.emit();
}
this.emitEntryFile();
await this.emitEntryFile();
}
}
31 changes: 0 additions & 31 deletions packages/core/src/templates/components-entry.hbs

This file was deleted.

0 comments on commit cbbddf6

Please sign in to comment.