From 17b5208afcc50ac2705f15bc6fd9c016eda627ce Mon Sep 17 00:00:00 2001 From: MG Date: Thu, 12 Nov 2020 19:18:32 +0100 Subject: [PATCH] fix: searching for things in default fixture --- README.md | 57 ++-- examples/MAIN/test.spec.ts | 2 +- lib/mock-helper/func.get-last-fixture.ts | 6 + lib/mock-helper/mock-helper.find.ts | 14 +- lib/mock-helper/mock-helper.findAll.ts | 13 +- lib/mock-helper/mock-helper.findInstance.ts | 15 +- lib/mock-helper/mock-helper.findInstances.ts | 16 +- lib/mock-helper/mock-helper.spec.ts | 4 +- lib/mock-helper/mock-helper.ts | 55 +++- .../test.spec.ts | 248 ++++++++++++++++++ 10 files changed, 390 insertions(+), 40 deletions(-) create mode 100644 lib/mock-helper/func.get-last-fixture.ts create mode 100644 tests/ng-mocks-search-with-no-fixture/test.spec.ts diff --git a/README.md b/README.md index a4428672a5..0b2eb8321f 100644 --- a/README.md +++ b/README.md @@ -1492,10 +1492,7 @@ describe('MAIN', () => { // By.directive(AppHeaderComponent) // ); // but typesafe and fails if nothing has been found. - const header = ngMocks.find( - fixture.debugElement, - AppHeaderComponent - ); + const header = ngMocks.find(AppHeaderComponent); // Asserting how AppComponent uses AppHeaderComponent. expect(header.componentInstance.showLogo).toBe(true); @@ -2317,26 +2314,34 @@ const directive = ngMocks.get(fixture.debugElement, Directive); #### ngMocks.findInstance Returns the first found attribute or structural directive which belongs to the current element or its any child. +If the element isn't specified then the current fixture is used. -- `ngMocks.findInstance( debugElement, directive, notFoundValue? )` +- `ngMocks.findInstance( fixture?, directive, notFoundValue? )` +- `ngMocks.findInstance( debugElement?, directive, notFoundValue? )` ```typescript -const directive = ngMocks.findInstance( +const directive1 = ngMocks.findInstance(Directive1); +const directive2 = ngMocks.findInstance(fixture, Directive2); +const directive3 = ngMocks.findInstance( fixture.debugElement, - Directive + Directive3 ); ``` #### ngMocks.findInstances Returns an array of all found attribute or structural directives which belong to the current element and all its children. +If the element isn't specified then the current fixture is used. -- `ngMocks.findInstances( debugElement, directive )` +- `ngMocks.findInstances( fixture?, directive )` +- `ngMocks.findInstances( debugElement?, directive )` ```typescript -const directives = ngMocks.findInstances( +const directives1 = ngMocks.findInstances(Directive1); +const directives2 = ngMocks.findInstances(fixture, Directive2); +const directives3 = ngMocks.findInstances( fixture.debugElement, - Directive + Directive3 ); ``` @@ -2344,34 +2349,46 @@ const directives = ngMocks.findInstances( Returns a found DebugElement which belongs to a component with the correctly typed componentInstance, or matches a css selector. +If a root element or a fixture aren't specified then the current fixture is used. -- `ngMocks.find( fixture, component, notFoundValue? )` -- `ngMocks.find( debugElement, component, notFoundValue? )` -- `ngMocks.find( debugElement, selector, notFoundValue? )` +- `ngMocks.find( fixture?, component, notFoundValue? )` +- `ngMocks.find( fixture?, cssSelector, notFoundValue? )` +- `ngMocks.find( debugElement?, component, notFoundValue? )` +- `ngMocks.find( debugElement?, cssSelector, notFoundValue? )` ```typescript -const element = ngMocks.find(fixture.debugElement, Component); +const element1 = ngMocks.find(Component1); +const element2 = ngMocks.find(fixture, Component2); +const element3 = ngMocks.find(fixture.debugElement, Component3); ``` ```typescript -const element = ngMocks.find(fixture.debugElement, 'div.container'); +const element1 = ngMocks.find('div.con1'); +const element2 = ngMocks.find(fixture, 'div.con2'); +const element3 = ngMocks.find(fixture.debugElement, 'div.con3'); ``` #### ngMocks.findAll Returns an array of found DebugElements which belong to a component with the correctly typed componentInstance, or match a css selector. +If a root element or a fixture aren't specified then the current fixture is used. -- `ngMocks.findAll( fixture, component )` -- `ngMocks.findAll( debugElement, component )` -- `ngMocks.findAll( debugElement, selector )` +- `ngMocks.findAll( fixture?, component )` +- `ngMocks.findAll( fixture?, cssSelector )` +- `ngMocks.findAll( debugElement?, component )` +- `ngMocks.findAll( debugElement?, cssSelector )` ```typescript -const elements = ngMocks.findAll(fixture.debugElement, Component); +const elements1 = ngMocks.findAll(Component1); +const elements2 = ngMocks.findAll(fixture, Component2); +const elements3 = ngMocks.findAll(fixture.debugElement, Component3); ``` ```typescript -const elements = ngMocks.findAll(fixture.debugElement, 'div.item'); +const elements1 = ngMocks.findAll('div.item1'); +const elements2 = ngMocks.findAll(fixture, 'div.item2'); +const elements3 = ngMocks.findAll(fixture.debugElement, 'div.item3'); ``` #### ngMocks.input diff --git a/examples/MAIN/test.spec.ts b/examples/MAIN/test.spec.ts index a1ffb0b70a..ff928134d7 100644 --- a/examples/MAIN/test.spec.ts +++ b/examples/MAIN/test.spec.ts @@ -129,7 +129,7 @@ describe('MAIN', () => { // By.directive(AppHeaderComponent) // ); // but typesafe and fails if nothing has been found. - const header = ngMocks.find(fixture.debugElement, AppHeaderComponent); + const header = ngMocks.find(AppHeaderComponent); // Asserting how AppComponent uses AppHeaderComponent. expect(header.componentInstance.showLogo).toBe(true); diff --git a/lib/mock-helper/func.get-last-fixture.ts b/lib/mock-helper/func.get-last-fixture.ts new file mode 100644 index 0000000000..003d3c84e6 --- /dev/null +++ b/lib/mock-helper/func.get-last-fixture.ts @@ -0,0 +1,6 @@ +import { ComponentFixture, getTestBed } from '@angular/core/testing'; + +export default () => { + const fixtures: Array> = (getTestBed() as any)._activeFixtures; + return fixtures[fixtures.length - 1]; +}; diff --git a/lib/mock-helper/mock-helper.find.ts b/lib/mock-helper/mock-helper.find.ts index f6ed64c64f..1a17d31f11 100644 --- a/lib/mock-helper/mock-helper.find.ts +++ b/lib/mock-helper/mock-helper.find.ts @@ -4,15 +4,21 @@ import { Type } from '../common/core.types'; import { getSourceOfMock } from '../common/func.get-source-of-mock'; import { MockedDebugElement } from '../mock-render/types'; +import getLastFixture from './func.get-last-fixture'; + const defaultNotFoundValue = {}; // simulating Symbol export default (...args: any[]) => { - const el: MockedDebugElement = args[0].debugElement ? args[0].debugElement : args[0]; - const sel: string | Type = args[1]; - const notFoundValue: any = args.length === 3 ? args[2] : defaultNotFoundValue; + const el: undefined | MockedDebugElement = + typeof args[0] !== 'object' ? undefined : args[0].debugElement ? args[0].debugElement : args[0]; + const sel: string | Type = el ? args[1] : args[0]; + const notFoundValue: any = + el && args.length === 3 ? args[2] : !el && args.length === 2 ? args[1] : defaultNotFoundValue; + + const debugElement = el || getLastFixture()?.debugElement; const term = typeof sel === 'string' ? By.css(sel) : By.directive(getSourceOfMock(sel)); - const result = el.query(term); + const result = debugElement?.query(term); if (result) { return result; } diff --git a/lib/mock-helper/mock-helper.findAll.ts b/lib/mock-helper/mock-helper.findAll.ts index 0654a14280..75701c185e 100644 --- a/lib/mock-helper/mock-helper.findAll.ts +++ b/lib/mock-helper/mock-helper.findAll.ts @@ -1,8 +1,17 @@ import { By } from '@angular/platform-browser'; +import { MockedDebugElement, Type } from 'ng-mocks'; import { getSourceOfMock } from '../common/func.get-source-of-mock'; -export default (el: any, sel: any) => { +import getLastFixture from './func.get-last-fixture'; + +export default (...args: any[]) => { + const el: undefined | MockedDebugElement = + typeof args[0] !== 'object' ? undefined : args[0].debugElement ? args[0].debugElement : args[0]; + const sel: string | Type = el ? args[1] : args[0]; + + const debugElement = el || getLastFixture()?.debugElement; + const term = typeof sel === 'string' ? By.css(sel) : By.directive(getSourceOfMock(sel)); - return (el.debugElement ? el.debugElement : el).queryAll(term); + return debugElement?.queryAll(term) || []; }; diff --git a/lib/mock-helper/mock-helper.findInstance.ts b/lib/mock-helper/mock-helper.findInstance.ts index 4a761f1eab..316ff408bb 100644 --- a/lib/mock-helper/mock-helper.findInstance.ts +++ b/lib/mock-helper/mock-helper.findInstance.ts @@ -2,21 +2,26 @@ import { Type } from '../common/core.types'; import { getSourceOfMock } from '../common/func.get-source-of-mock'; import { MockedDebugElement } from '../mock-render/types'; +import getLastFixture from './func.get-last-fixture'; import findInstances from './mock-helper.findInstances'; const defaultNotFoundValue = {}; // simulating Symbol export default (...args: any[]) => { - const el: MockedDebugElement = args[0]; - const sel: Type = args[1]; - const notFoundValue: any = args.length === 3 ? args[2] : defaultNotFoundValue; + const el: undefined | MockedDebugElement = + typeof args[0] !== 'object' ? undefined : args[0].debugElement ? args[0].debugElement : args[0]; + const sel: Type = el ? args[1] : args[0]; + const notFoundValue: any = + el && args.length === 3 ? args[2] : !el && args.length === 2 ? args[1] : defaultNotFoundValue; - const result = findInstances(el, getSourceOfMock(sel)); + const debugElement = el || getLastFixture()?.debugElement; + + const result = findInstances(debugElement, getSourceOfMock(sel)); if (result.length) { return result[0]; } if (notFoundValue !== defaultNotFoundValue) { return notFoundValue; } - throw new Error(`Cannot find ${sel.name} directive via ngMocks.findInstance`); + throw new Error(`Cannot find an instance via ngMocks.findInstance(${sel.name})`); }; diff --git a/lib/mock-helper/mock-helper.findInstances.ts b/lib/mock-helper/mock-helper.findInstances.ts index bf3de11eda..03ee3f934c 100644 --- a/lib/mock-helper/mock-helper.findInstances.ts +++ b/lib/mock-helper/mock-helper.findInstances.ts @@ -1,6 +1,8 @@ import { Type } from '../common/core.types'; import { getSourceOfMock } from '../common/func.get-source-of-mock'; -import { MockedDebugNode } from '../mock-render/types'; +import { MockedDebugElement, MockedDebugNode } from '../mock-render/types'; + +import getLastFixture from './func.get-last-fixture'; function nestedCheck( result: T[], @@ -11,15 +13,21 @@ function nestedCheck( if (element) { result.push(element); } - const childNodes = node.childNodes ? node.childNodes : []; + const childNodes = node?.childNodes || []; childNodes.forEach(childNode => { nestedCheck(result, childNode, callback); }); } -export default (el: MockedDebugNode, sel: Type): T[] => { +export default (...args: any[]): T[] => { + const el: undefined | MockedDebugElement = + typeof args[0] !== 'object' ? undefined : args[0].debugElement ? args[0].debugElement : args[0]; + const sel: Type = el ? args[1] : args[0]; + + const debugElement = el || getLastFixture()?.debugElement; + const result: T[] = []; - nestedCheck(result, el, node => { + nestedCheck(result, debugElement, node => { try { return node.injector.get(getSourceOfMock(sel)); } catch (error) { diff --git a/lib/mock-helper/mock-helper.spec.ts b/lib/mock-helper/mock-helper.spec.ts index 04b4a64b19..b3d1842d87 100644 --- a/lib/mock-helper/mock-helper.spec.ts +++ b/lib/mock-helper/mock-helper.spec.ts @@ -180,7 +180,9 @@ describe('MockHelper:getDirective', () => { it('findInstance throws an error', () => { const fixture = MockRender(``); - expect(() => ngMocks.findInstance(fixture.debugElement, BComponent)).toThrowError(/Cannot find BComponent/); + expect(() => ngMocks.findInstance(fixture.debugElement, BComponent)).toThrowError( + /Cannot find an instance via ngMocks.findInstance\(BComponent\)/ + ); }); it('findInstance returns default value', () => { diff --git a/lib/mock-helper/mock-helper.ts b/lib/mock-helper/mock-helper.ts index 5f94495f77..6d35d0069e 100644 --- a/lib/mock-helper/mock-helper.ts +++ b/lib/mock-helper/mock-helper.ts @@ -99,11 +99,21 @@ export const ngMocks: { */ faster(): void; + /** + * @see https://github.com/ike18t/ng-mocks#ngmocksfind + */ + find(component: Type): MockedDebugElement; + /** * @see https://github.com/ike18t/ng-mocks#ngmocksfind */ find(debugElement: MockedDebugElement | ComponentFixture, component: Type): MockedDebugElement; + /** + * @see https://github.com/ike18t/ng-mocks#ngmocksfind + */ + find(component: Type, notFoundValue: D): D | MockedDebugElement; + /** * @see https://github.com/ike18t/ng-mocks#ngmocksfind */ @@ -113,11 +123,21 @@ export const ngMocks: { notFoundValue: D ): D | MockedDebugElement; + /** + * @see https://github.com/ike18t/ng-mocks#ngmocksfind + */ + find(cssSelector: string): MockedDebugElement; + /** * @see https://github.com/ike18t/ng-mocks#ngmocksfind */ find(debugElement: MockedDebugElement | ComponentFixture, cssSelector: string): MockedDebugElement; + /** + * @see https://github.com/ike18t/ng-mocks#ngmocksfind + */ + find(cssSelector: string, notFoundValue: D): D | MockedDebugElement; + /** * @see https://github.com/ike18t/ng-mocks#ngmocksfind */ @@ -127,6 +147,11 @@ export const ngMocks: { notFoundValue: D ): D | MockedDebugElement; + /** + * @see https://github.com/ike18t/ng-mocks#ngmocksfindall + */ + findAll(component: Type): Array>; + /** * @see https://github.com/ike18t/ng-mocks#ngmocksfindall */ @@ -135,6 +160,11 @@ export const ngMocks: { component: Type ): Array>; + /** + * @see https://github.com/ike18t/ng-mocks#ngmocksfindall + */ + findAll(cssSelector: string): Array>; + /** * @see https://github.com/ike18t/ng-mocks#ngmocksfindall */ @@ -146,17 +176,36 @@ export const ngMocks: { /** * @see https://github.com/ike18t/ng-mocks#ngmocksfindinstance */ - findInstance(debugNode: MockedDebugNode, instanceClass: Type): T; + findInstance(instanceClass: Type): T; /** * @see https://github.com/ike18t/ng-mocks#ngmocksfindinstance */ - findInstance(debugNode: MockedDebugNode, instanceClass: Type, notFoundValue: D): D | T; + findInstance(debugNode: MockedDebugNode | ComponentFixture, instanceClass: Type): T; /** * @see https://github.com/ike18t/ng-mocks#ngmocksfindinstance */ - findInstances(debugNode: MockedDebugNode, instanceClass: Type): T[]; + findInstance(instanceClass: Type, notFoundValue: D): D | T; + + /** + * @see https://github.com/ike18t/ng-mocks#ngmocksfindinstance + */ + findInstance( + debugNode: MockedDebugNode | ComponentFixture, + instanceClass: Type, + notFoundValue: D + ): D | T; + + /** + * @see https://github.com/ike18t/ng-mocks#ngmocksfindinstances + */ + findInstances(instanceClass: Type): T[]; + + /** + * @see https://github.com/ike18t/ng-mocks#ngmocksfindinstances + */ + findInstances(debugNode: MockedDebugNode | ComponentFixture, instanceClass: Type): T[]; /** * @see https://github.com/ike18t/ng-mocks#ngmocksflushtestbed diff --git a/tests/ng-mocks-search-with-no-fixture/test.spec.ts b/tests/ng-mocks-search-with-no-fixture/test.spec.ts new file mode 100644 index 0000000000..1138a86bcf --- /dev/null +++ b/tests/ng-mocks-search-with-no-fixture/test.spec.ts @@ -0,0 +1,248 @@ +import { Component, Input, NgModule } from '@angular/core'; +import { isMockOf, MockBuilder, MockedComponentFixture, MockRender, ngMocks } from 'ng-mocks'; + +@Component({ + selector: 'target', + template: `{{ target }}`, +}) +class TargetComponent { + @Input() public readonly target: string; +} + +@Component({ + selector: 'missed', + template: `missed`, +}) +class MissedComponent {} + +@Component({ + selector: 'test', + template: ``, +}) +class TestComponent {} + +@NgModule({ + declarations: [TargetComponent, MissedComponent, TestComponent], +}) +class TargetModule {} + +describe('ng-mocks-search-with-no-fixture:no-fixture', () => { + it('.find type', () => { + expect(() => ngMocks.find(TargetComponent)).toThrowError( + /Cannot find an element via ngMocks.find\(TargetComponent\)/ + ); + expect(ngMocks.find(TargetComponent, undefined)).toBeUndefined(); + }); + + it('.find css selector', () => { + expect(() => ngMocks.find('target')).toThrowError(/Cannot find an element via ngMocks.find\(target\)/); + expect(ngMocks.find('target', undefined)).toBeUndefined(); + }); + + it('.findAll type', () => { + const elements = ngMocks.findAll(TargetComponent); + expect(elements.length).toEqual(0); + }); + + it('.findAll css selector', () => { + const elements = ngMocks.findAll('target'); + expect(elements.length).toEqual(0); + }); + + it('.findInstance', () => { + expect(() => ngMocks.findInstance(TargetComponent)).toThrowError( + /Cannot find an instance via ngMocks.findInstance\(TargetComponent\)/ + ); + expect(ngMocks.findInstance(TargetComponent, undefined)).toBeUndefined(); + }); + + it('.findInstances', () => { + const componentInstances = ngMocks.findInstances(TargetComponent); + expect(componentInstances.length).toEqual(0); + }); +}); + +describe('ng-mocks-search-with-no-fixture:fixture', () => { + ngMocks.faster(); + + let fixture: MockedComponentFixture; + beforeEach(() => MockBuilder(TestComponent, TargetModule)); + beforeEach(() => (fixture = MockRender(TestComponent))); + + describe('empty', () => { + it('.find type', () => { + const element = ngMocks.find(TargetComponent); + expect(isMockOf(element.componentInstance, TargetComponent)).toBeTruthy(); + expect(element.componentInstance.target).toEqual('1'); + + expect(ngMocks.find(MissedComponent, undefined)).toBeUndefined(); + expect(() => ngMocks.find(MissedComponent)).toThrowError( + /Cannot find an element via ngMocks.find\(MissedComponent\)/ + ); + }); + + it('.find css selector', () => { + const element = ngMocks.find('target'); + expect(isMockOf(element.componentInstance, TargetComponent)).toBeTruthy(); + expect(element.componentInstance.target).toEqual('1'); + + expect(ngMocks.find('missed', undefined)).toBeUndefined(); + expect(() => ngMocks.find('missed')).toThrowError( + /Cannot find an element via ngMocks.find\(missed\)/ + ); + }); + + it('.findAll type', () => { + const elements = ngMocks.findAll(TargetComponent); + expect(isMockOf(elements[0].componentInstance, TargetComponent)).toBeTruthy(); + expect(elements[0].componentInstance.target).toEqual('1'); + expect(isMockOf(elements[1].componentInstance, TargetComponent)).toBeTruthy(); + expect(elements[1].componentInstance.target).toEqual('2'); + }); + + it('.findAll css selector', () => { + const elements = ngMocks.findAll('target'); + expect(isMockOf(elements[0].componentInstance, TargetComponent)).toBeTruthy(); + expect(elements[0].componentInstance.target).toEqual('1'); + expect(isMockOf(elements[1].componentInstance, TargetComponent)).toBeTruthy(); + expect(elements[1].componentInstance.target).toEqual('2'); + }); + + it('.findInstance', () => { + const componentInstance = ngMocks.findInstance(TargetComponent); + expect(isMockOf(componentInstance, TargetComponent)).toBeTruthy(); + expect(componentInstance.target).toEqual('1'); + + expect(ngMocks.findInstance(MissedComponent, undefined)).toBeUndefined(); + expect(() => ngMocks.findInstance(MissedComponent)).toThrowError( + /Cannot find an instance via ngMocks.findInstance\(MissedComponent\)/ + ); + }); + + it('.findInstances', () => { + const componentInstances = ngMocks.findInstances(TargetComponent); + expect(isMockOf(componentInstances[0], TargetComponent)).toBeTruthy(); + expect(componentInstances[0].target).toEqual('1'); + expect(isMockOf(componentInstances[1], TargetComponent)).toBeTruthy(); + expect(componentInstances[1].target).toEqual('2'); + }); + }); + + describe('fixture', () => { + it('.find type', () => { + const element = ngMocks.find(fixture, TargetComponent); + expect(isMockOf(element.componentInstance, TargetComponent)).toBeTruthy(); + expect(element.componentInstance.target).toEqual('1'); + + expect(ngMocks.find(fixture, MissedComponent, undefined)).toBeUndefined(); + expect(() => ngMocks.find(fixture, MissedComponent)).toThrowError( + /Cannot find an element via ngMocks.find\(MissedComponent\)/ + ); + }); + + it('.find css selector', () => { + const element = ngMocks.find(fixture, 'target'); + expect(isMockOf(element.componentInstance, TargetComponent)).toBeTruthy(); + expect(element.componentInstance.target).toEqual('1'); + + expect(ngMocks.find(fixture, 'missed', undefined)).toBeUndefined(); + expect(() => ngMocks.find(fixture, 'missed')).toThrowError( + /Cannot find an element via ngMocks.find\(missed\)/ + ); + }); + + it('.findAll type', () => { + const elements = ngMocks.findAll(fixture, TargetComponent); + expect(isMockOf(elements[0].componentInstance, TargetComponent)).toBeTruthy(); + expect(elements[0].componentInstance.target).toEqual('1'); + expect(isMockOf(elements[1].componentInstance, TargetComponent)).toBeTruthy(); + expect(elements[1].componentInstance.target).toEqual('2'); + }); + + it('.findAll css selector', () => { + const elements = ngMocks.findAll(fixture, 'target'); + expect(isMockOf(elements[0].componentInstance, TargetComponent)).toBeTruthy(); + expect(elements[0].componentInstance.target).toEqual('1'); + expect(isMockOf(elements[1].componentInstance, TargetComponent)).toBeTruthy(); + expect(elements[1].componentInstance.target).toEqual('2'); + }); + + it('.findInstance', () => { + const componentInstance = ngMocks.findInstance(fixture, TargetComponent); + expect(isMockOf(componentInstance, TargetComponent)).toBeTruthy(); + expect(componentInstance.target).toEqual('1'); + + expect(ngMocks.findInstance(fixture, MissedComponent, undefined)).toBeUndefined(); + expect(() => ngMocks.findInstance(fixture, MissedComponent)).toThrowError( + /Cannot find an instance via ngMocks.findInstance\(MissedComponent\)/ + ); + }); + + it('.findInstances', () => { + const componentInstances = ngMocks.findInstances(fixture, TargetComponent); + expect(isMockOf(componentInstances[0], TargetComponent)).toBeTruthy(); + expect(componentInstances[0].target).toEqual('1'); + expect(isMockOf(componentInstances[1], TargetComponent)).toBeTruthy(); + expect(componentInstances[1].target).toEqual('2'); + }); + }); + + describe('debugElement', () => { + it('.find type', () => { + const element = ngMocks.find(fixture.debugElement, TargetComponent); + expect(isMockOf(element.componentInstance, TargetComponent)).toBeTruthy(); + expect(element.componentInstance.target).toEqual('1'); + + expect(ngMocks.find(fixture.debugElement, MissedComponent, undefined)).toBeUndefined(); + expect(() => ngMocks.find(fixture.debugElement, MissedComponent)).toThrowError( + /Cannot find an element via ngMocks.find\(MissedComponent\)/ + ); + }); + + it('.find css selector', () => { + const element = ngMocks.find(fixture.debugElement, 'target'); + expect(isMockOf(element.componentInstance, TargetComponent)).toBeTruthy(); + expect(element.componentInstance.target).toEqual('1'); + + expect(ngMocks.find(fixture.debugElement, 'missed', undefined)).toBeUndefined(); + expect(() => ngMocks.find(fixture.debugElement, 'missed')).toThrowError( + /Cannot find an element via ngMocks.find\(missed\)/ + ); + }); + + it('.findAll type', () => { + const elements = ngMocks.findAll(fixture.debugElement, TargetComponent); + expect(isMockOf(elements[0].componentInstance, TargetComponent)).toBeTruthy(); + expect(elements[0].componentInstance.target).toEqual('1'); + expect(isMockOf(elements[1].componentInstance, TargetComponent)).toBeTruthy(); + expect(elements[1].componentInstance.target).toEqual('2'); + }); + + it('.findAll css selector', () => { + const elements = ngMocks.findAll(fixture.debugElement, 'target'); + expect(isMockOf(elements[0].componentInstance, TargetComponent)).toBeTruthy(); + expect(elements[0].componentInstance.target).toEqual('1'); + expect(isMockOf(elements[1].componentInstance, TargetComponent)).toBeTruthy(); + expect(elements[1].componentInstance.target).toEqual('2'); + }); + + it('.findInstance', () => { + const componentInstance = ngMocks.findInstance(fixture.debugElement, TargetComponent); + expect(isMockOf(componentInstance, TargetComponent)).toBeTruthy(); + expect(componentInstance.target).toEqual('1'); + + expect(ngMocks.findInstance(fixture.debugElement, MissedComponent, undefined)).toBeUndefined(); + expect(() => ngMocks.findInstance(fixture.debugElement, MissedComponent)).toThrowError( + /Cannot find an instance via ngMocks.findInstance\(MissedComponent\)/ + ); + }); + + it('.findInstances', () => { + const componentInstances = ngMocks.findInstances(fixture.debugElement, TargetComponent); + expect(isMockOf(componentInstances[0], TargetComponent)).toBeTruthy(); + expect(componentInstances[0].target).toEqual('1'); + expect(isMockOf(componentInstances[1], TargetComponent)).toBeTruthy(); + expect(componentInstances[1].target).toEqual('2'); + }); + }); +});