Skip to content

Commit

Permalink
feat: supporting fixture in ngMocks.find
Browse files Browse the repository at this point in the history
  • Loading branch information
satanTime committed Oct 23, 2020
1 parent db7b368 commit 26da8a4
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 21 deletions.
8 changes: 3 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ describe('MockComponent', () => {
// ).componentInstance
// but properly typed.
const mockedComponent = ngMocks.find<DependencyComponent>(
fixture.debugElement,
fixture,
'app-child'
).componentInstance;

Expand All @@ -192,10 +192,8 @@ describe('MockComponent', () => {
// By.directive(DependencyComponent)
// ).componentInstance
// but properly typed.
const mockedComponent = ngMocks.find(
fixture.debugElement,
DependencyComponent
).componentInstance;
const mockedComponent = ngMocks.find(fixture, DependencyComponent)
.componentInstance;

// Again, let's pretend DependencyComponent has an output
// called 'someOutput'. TestedComponent listens on the output via
Expand Down
6 changes: 3 additions & 3 deletions examples/MockComponent/test.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ describe('MockComponent', () => {
// By.css('app-child')
// ).componentInstance
// but properly typed.
const mockedComponent = ngMocks.find<DependencyComponent>(fixture.debugElement, 'app-child').componentInstance;
const mockedComponent = ngMocks.find<DependencyComponent>(fixture, 'app-child').componentInstance;

// Let's pretend that DependencyComponent has 'someInput' as
// an input. TestedComponent sets its value via
Expand All @@ -62,7 +62,7 @@ describe('MockComponent', () => {
// By.directive(DependencyComponent)
// ).componentInstance
// but properly typed.
const mockedComponent = ngMocks.find(fixture.debugElement, DependencyComponent).componentInstance;
const mockedComponent = ngMocks.find(fixture, DependencyComponent).componentInstance;

// Again, let's pretend DependencyComponent has an output
// called 'someOutput'. TestedComponent listens on the output via
Expand Down Expand Up @@ -117,7 +117,7 @@ describe('MockComponent', () => {

// The rendered template is wrapped by <div data-key="something">.
// We can use this selector to assert exactly its content.
const mockedNgTemplate = ngMocks.find(localFixture.debugElement, '[data-key="something"]').nativeElement.innerHTML;
const mockedNgTemplate = ngMocks.find(localFixture, '[data-key="something"]').nativeElement.innerHTML;
expect(mockedNgTemplate).toContain('<p>inside template</p>');
});
});
2 changes: 1 addition & 1 deletion lib/mock-helper/mock-helper.find.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { MockedDebugElement } from '../mock-render';
const defaultNotFoundValue = {}; // simulating Symbol

export default (...args: any[]) => {
const el: MockedDebugElement = args[0];
const el: MockedDebugElement = args[0].debugElement ? args[0].debugElement : args[0];
const sel: string | Type<any> = args[1];
const notFoundValue: any = args.length === 3 ? args[2] : defaultNotFoundValue;

Expand Down
5 changes: 2 additions & 3 deletions lib/mock-helper/mock-helper.findAll.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@
import { By } from '@angular/platform-browser';

import { getSourceOfMock } from '../common';
import { MockedDebugElement } from '../mock-render';

export default (el: MockedDebugElement, sel: any) => {
export default (el: any, sel: any) => {
const term = typeof sel === 'string' ? By.css(sel) : By.directive(getSourceOfMock(sel));
return el.queryAll(term);
return (el.debugElement ? el.debugElement : el).queryAll(term);
};
4 changes: 2 additions & 2 deletions lib/mock-helper/mock-helper.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ describe('MockHelper:getDirective', () => {

it('findAll selector: T', () => {
const fixture = MockRender(`<component-a></component-a><component-a></component-a>`);
const componentA = ngMocks.findAll(fixture.debugElement, AComponent);
const componentA = ngMocks.findAll(fixture, AComponent);
expect(componentA.length).toBe(2);
expect(componentA[0].componentInstance).toEqual(jasmine.any(AComponent));
expect(componentA[1].componentInstance).toEqual(jasmine.any(AComponent));
Expand All @@ -168,7 +168,7 @@ describe('MockHelper:getDirective', () => {

it('findAll selector: string', () => {
const fixture = MockRender(`<component-b></component-b><component-b></component-b>`);
const componentB = ngMocks.findAll(fixture.debugElement, 'component-b');
const componentB = ngMocks.findAll(fixture, 'component-b');
expect(componentB.length).toEqual(2);
expect(componentB[0].componentInstance).toEqual(jasmine.any(BComponent));
expect(componentB[0].componentInstance).toEqual(jasmine.any(BComponent));
Expand Down
24 changes: 17 additions & 7 deletions lib/mock-helper/mock-helper.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// tslint:disable:variable-name unified-signatures no-default-import

import { EventEmitter, InjectionToken, Provider } from '@angular/core';
import { TestModuleMetadata } from '@angular/core/testing';
import { ComponentFixture, TestModuleMetadata } from '@angular/core/testing';

import { AbstractType, AnyType, NgModuleWithProviders, Type } from '../common';
import { MockedDebugElement, MockedDebugNode } from '../mock-render';
Expand Down Expand Up @@ -92,18 +92,28 @@ export const MockHelper: {
export const ngMocks: {
faster(): void;

find<T>(debugElement: MockedDebugElement, component: Type<T>): MockedDebugElement<T>;
find<T, D>(debugElement: MockedDebugElement, component: Type<T>, notFoundValue: D): D | MockedDebugElement<T>;
find<T>(debugElement: MockedDebugElement | ComponentFixture<any>, component: Type<T>): MockedDebugElement<T>;
find<T, D>(
debugElement: MockedDebugElement | ComponentFixture<any>,
component: Type<T>,
notFoundValue: D
): D | MockedDebugElement<T>;

find<T = any>(debugElement: MockedDebugElement, cssSelector: string): MockedDebugElement<T>;
find<T = any>(debugElement: MockedDebugElement | ComponentFixture<any>, cssSelector: string): MockedDebugElement<T>;
find<T = any, D = undefined>(
debugElement: MockedDebugElement,
debugElement: MockedDebugElement | ComponentFixture<any>,
cssSelector: string,
notFoundValue: D
): D | MockedDebugElement<T>;

findAll<T>(debugElement: MockedDebugElement, component: Type<T>): Array<MockedDebugElement<T>>;
findAll<T = any>(debugElement: MockedDebugElement, cssSelector: string): Array<MockedDebugElement<T>>;
findAll<T>(
debugElement: MockedDebugElement | ComponentFixture<any>,
component: Type<T>
): Array<MockedDebugElement<T>>;
findAll<T = any>(
debugElement: MockedDebugElement | ComponentFixture<any>,
cssSelector: string
): Array<MockedDebugElement<T>>;

findInstance<T>(debugNode: MockedDebugNode, instanceClass: Type<T>): T;
findInstance<T, D>(debugNode: MockedDebugNode, instanceClass: Type<T>, notFoundValue: D): D | T;
Expand Down

0 comments on commit 26da8a4

Please sign in to comment.