Skip to content

Commit

Permalink
perf: cc cognitive complexity
Browse files Browse the repository at this point in the history
  • Loading branch information
satanTime committed Nov 23, 2020
1 parent 7f677f4 commit 8ac1fac
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 53 deletions.
6 changes: 6 additions & 0 deletions lib/mock-render/mock-render.spec.fixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,9 @@ export class RenderRealComponent {
template: 'WithoutSelectorComponent',
})
export class WithoutSelectorComponent {}

@Component({
selector: 'empty',
template: 'empty',
})
export class EmptyComponent {}
9 changes: 7 additions & 2 deletions lib/mock-render/mock-render.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,21 @@ import { ngMocks } from '../mock-helper/mock-helper';
import { MockService } from '../mock-service/mock-service';

import { MockRender } from './mock-render';
import { RenderRealComponent, WithoutSelectorComponent } from './mock-render.spec.fixtures';
import { EmptyComponent, RenderRealComponent, WithoutSelectorComponent } from './mock-render.spec.fixtures';
import { MockedComponentFixture, MockedDebugElement, MockedDebugNode } from './types';

describe('MockRender', () => {
beforeEach(() => {
TestBed.configureTestingModule({
declarations: [RenderRealComponent, WithoutSelectorComponent],
declarations: [RenderRealComponent, WithoutSelectorComponent, EmptyComponent],
});
});

it('respects no inputs and outputs', () => {
const fixture = MockRender(EmptyComponent);
expect(fixture.nativeElement.innerHTML).toEqual('<empty>empty</empty>');
});

it('respects observables', () => {
const click = new Subject();
const fixture = MockRender(RenderRealComponent, { click });
Expand Down
78 changes: 27 additions & 51 deletions lib/mock-render/mock-render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,44 +69,22 @@ const installProxy = (componentInstance: Record<keyof any, any>, pointComponentI
}
};

const generateTemplateInputs = (selector: any, params: any, inputs: any) => {
const generateTemplateAttr = (params: any, attr: any, type: 'i' | 'o') => {
let mockTemplate = '';

if (selector && inputs) {
for (const definition of inputs) {
const [property, alias] = definition.split(': ');
/* istanbul ignore else */
if (alias && params) {
mockTemplate += ` [${alias}]="${alias}"`;
} else if (property && params) {
mockTemplate += ` [${property}]="${property}"`;
} else if (alias && !params) {
mockTemplate += ` [${alias}]="${property}"`;
} else if (!params) {
mockTemplate += ` [${property}]="${property}"`;
}
}
}

return mockTemplate;
};

const generateTemplateOutputs = (selector: any, params: any, outputs: any) => {
let mockTemplate = '';

if (selector && outputs) {
for (const definition of outputs) {
const [property, alias] = definition.split(': ');
/* istanbul ignore else */
if (alias && params) {
mockTemplate += ` (${alias})="${alias}${solveOutput(params[alias])}"`;
} else if (property && params) {
mockTemplate += ` (${property})="${property}${solveOutput(params[property])}"`;
} else if (alias && !params) {
mockTemplate += ` (${alias})="${property}.emit($event)"`;
} else if (!params) {
mockTemplate += ` (${property})="${property}.emit($event)"`;
}
const wrap = (prop: string) => (type === 'i' ? `[${prop}]` : `(${prop})`);

for (const definition of attr) {
const [property, alias] = definition.split(': ');
/* istanbul ignore else */
if (alias && params) {
mockTemplate += ` ${wrap(alias)}="${alias}${type === 'o' ? solveOutput(params[alias]) : ''}"`;
} else if (property && params) {
mockTemplate += ` ${wrap(property)}="${property}${type === 'o' ? solveOutput(params[property]) : ''}"`;
} else if (alias && !params) {
mockTemplate += ` ${wrap(alias)}="${property}${type === 'o' ? '.emit($event)' : ''}"`;
} else if (!params) {
mockTemplate += ` ${wrap(property)}="${property}${type === 'o' ? '.emit($event)' : ''}"`;
}
}

Expand All @@ -120,15 +98,15 @@ const generateTemplate = (declaration: any, { selector, params, inputs, outputs
mockTemplate = declaration;
} else if (selector) {
mockTemplate += `<${selector}`;
mockTemplate += generateTemplateInputs(selector, params, inputs);
mockTemplate += generateTemplateOutputs(selector, params, outputs);
mockTemplate += generateTemplateAttr(params, inputs, 'i');
mockTemplate += generateTemplateAttr(params, outputs, 'o');
mockTemplate += `></${selector}>`;
}

return mockTemplate;
};

const generateComponent = ({ params, options, inputs, outputs }: any) => {
const generateFixture = ({ params, options, inputs, outputs }: any) => {
class MockRenderComponent {
public constructor() {
for (const key of Object.keys(params || {})) {
Expand All @@ -151,7 +129,15 @@ const generateComponent = ({ params, options, inputs, outputs }: any) => {

Component(options)(MockRenderComponent);

return MockRenderComponent;
// Soft reset of TestBed.
ngMocks.flushTestBed();

// Injection of our template.
TestBed.configureTestingModule({
declarations: [MockRenderComponent],
});

return TestBed.createComponent(MockRenderComponent);
};

/**
Expand Down Expand Up @@ -216,17 +202,7 @@ function MockRender<MComponent, TComponent extends Record<keyof any, any>>(
template: mockTemplate,
};

const component = generateComponent({ params, options, inputs, outputs });

// Soft reset of TestBed.
ngMocks.flushTestBed();

// Injection of our template.
TestBed.configureTestingModule({
declarations: [component],
});

const fixture: any = TestBed.createComponent(component);
const fixture: any = generateFixture({ params, options, inputs, outputs });
if (flagsObject.detectChanges) {
fixture.detectChanges();
}
Expand Down

0 comments on commit 8ac1fac

Please sign in to comment.