-
-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: find TemplateRef / ng-template
Closes #290
- Loading branch information
Showing
24 changed files
with
896 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
--- | ||
title: ngMocks.findTemplateRef | ||
description: Documentation about ngMocks.findTemplateRef from ng-mocks library | ||
--- | ||
|
||
Returns the first found `TemplateRef` which belongs to the current element or its any child. | ||
If the element is not specified, then the current fixture is used. | ||
|
||
- `ngMocks.findTemplateRef( fixture?, directive, notFoundValue? )` | ||
- `ngMocks.findTemplateRef( debugElement?, id, notFoundValue? )` | ||
- `ngMocks.findTemplateRef( debugElement?, [attribute, value?], notFoundValue? )` | ||
|
||
## Directive | ||
|
||
Assume, that a template has the next code. | ||
|
||
```html | ||
<ng-template my-directive-1></ng-template> | ||
<span *my-directive-2></span> | ||
``` | ||
|
||
Then we can find `ng-template` like that: | ||
|
||
```ts | ||
const tpl1 = ngMocks.findTemplateRef(MyDirective1); | ||
const tpl2 = ngMocks.findTemplateRef(MyDirective2); | ||
``` | ||
|
||
## Id | ||
|
||
Assume, that a template has the next code. | ||
|
||
```html | ||
<ng-template #header></ng-template> | ||
<ng-template #footer></ng-template> | ||
``` | ||
|
||
Then we can find `ng-template` like that: | ||
|
||
```ts | ||
const tplHeader = ngMocks.findTemplateRef('header'); | ||
const tplFooter = ngMocks.findTemplateRef('footer'); | ||
``` | ||
|
||
## Attribute selector | ||
|
||
Assume, that a template has the next code. | ||
|
||
```html | ||
<ng-template mat-row></ng-template> | ||
<ng-template mat-cell></ng-template> | ||
``` | ||
|
||
Then we can find `ng-template` like that: | ||
|
||
```ts | ||
const tplRow = ngMocks.findTemplateRef(['mat-row']); | ||
const tplCell = ngMocks.findTemplateRef(['mat-cell']); | ||
``` | ||
|
||
:::important | ||
Pay attention that a tuple is used, otherwise it will be id look up. | ||
::: | ||
|
||
## Input with value | ||
|
||
Assume, that a template has the next code. | ||
|
||
```html | ||
<ng-template myTpl="header"></ng-template> | ||
<ng-template [myTpl]="property"></ng-template> | ||
``` | ||
|
||
Then we can find `ng-template` like that: | ||
|
||
```ts | ||
const tplHeader = ngMocks.findTemplateRef(['myTpl', 'header']); | ||
const tplInput = ngMocks.findTemplateRef(['myTpl', property]); | ||
``` | ||
|
||
:::important | ||
Pay attention that `property` is a variable. | ||
::: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
--- | ||
title: ngMocks.findTemplateRefs | ||
description: Documentation about ngMocks.findTemplateRefs from ng-mocks library | ||
--- | ||
|
||
Returns an array of all found `TemplateRef` which belong to the current element and all its children. | ||
If the element is not specified, then the current fixture is used. | ||
|
||
- `ngMocks.findTemplateRefs( fixture?, directive )` | ||
- `ngMocks.findTemplateRefs( debugElement?, id )` | ||
- `ngMocks.findTemplateRefs( debugElement?, [attribute, value?] )` | ||
|
||
Assume, that a template has the next code. | ||
|
||
```html | ||
<ng-template myTpl="header"></ng-template> | ||
<ng-template myTpl="footer"></ng-template> | ||
``` | ||
|
||
Then, we can find `ng-template` like that: | ||
|
||
```ts | ||
// returns 2 elements | ||
const allByDirective = ngMocks.findTemplateRef(MyTplDirective); | ||
|
||
// returns 2 elements | ||
const allByAttribute = ngMocks.findTemplateRef(['myTpl']); | ||
|
||
// returns 1 element | ||
const onlyHeaders = ngMocks.findTemplateRef(['myTpl', 'header']); | ||
|
||
// returns 1 element | ||
const onlyFooters = ngMocks.findTemplateRef(['myTpl', 'footer']); | ||
|
||
// returns 0 elements | ||
const empty = ngMocks.findTemplateRef(['myTpl', 'body']); | ||
``` | ||
|
||
More information can be found in [`ngMocks.findTemplateRef`](./findTemplateRef.md). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
libs/ng-mocks/src/lib/mock-helper/func.parse-find-args-name.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { Type } from '../common/core.types'; | ||
|
||
export default (selector: string | Type<any> | [string] | [string, any]): string => { | ||
if (typeof selector === 'string') { | ||
return selector; | ||
} | ||
if (typeof selector === 'function') { | ||
return selector.name; | ||
} | ||
|
||
return selector[0]; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { DebugElement, Predicate } from '@angular/core'; | ||
import { By } from '@angular/platform-browser'; | ||
|
||
import { Type } from '../common/core.types'; | ||
import { getSourceOfMock } from '../common/func.get-source-of-mock'; | ||
|
||
export default (selector: string | Type<any> | [string] | [string, any]): Predicate<DebugElement> => { | ||
return Array.isArray(selector) | ||
? By.css(selector.length === 1 ? `[${selector[0]}]` : `[${selector[0]}="${selector[1]}"]`) | ||
: typeof selector === 'string' | ||
? By.css(selector) | ||
: By.directive(getSourceOfMock(selector)); | ||
}; |
25 changes: 25 additions & 0 deletions
25
libs/ng-mocks/src/lib/mock-helper/func.parse-provider-tokens-directives.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { DebugNode, Directive } from '@angular/core'; | ||
|
||
import coreReflectDirectiveResolve from '../common/core.reflect.directive-resolve'; | ||
import funcGetProvider from '../common/func.get-provider'; | ||
|
||
const getMeta = (token: any): Directive | undefined => { | ||
try { | ||
return coreReflectDirectiveResolve(token); | ||
} catch (e) { | ||
// Looks like it is a token. | ||
} | ||
|
||
return undefined; | ||
}; | ||
|
||
export default (el: DebugNode, token: any): Directive | undefined => { | ||
try { | ||
const provider = funcGetProvider(token); | ||
const instance = el.injector.get(provider); | ||
|
||
return getMeta(instance.constructor); | ||
} catch (e) { | ||
return undefined; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,10 @@ | ||
import { By } from '@angular/platform-browser'; | ||
|
||
import { getSourceOfMock } from '../common/func.get-source-of-mock'; | ||
|
||
import funcGetLastFixture from './func.get-last-fixture'; | ||
import funcParseFindArgs from './func.parse-find-args'; | ||
import funcParseFindTerm from './func.parse-find-term'; | ||
|
||
export default (...args: any[]) => { | ||
const { el, sel } = funcParseFindArgs(args); | ||
const debugElement = el || funcGetLastFixture()?.debugElement; | ||
|
||
const term = typeof sel === 'string' ? By.css(sel) : By.directive(getSourceOfMock(sel)); | ||
|
||
return debugElement?.queryAll(term) || []; | ||
return debugElement?.queryAll(funcParseFindTerm(sel)) || []; | ||
}; |
10 changes: 4 additions & 6 deletions
10
libs/ng-mocks/src/lib/mock-helper/mock-helper.find-instance.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,20 @@ | ||
import { Type } from '../common/core.types'; | ||
import { getSourceOfMock } from '../common/func.get-source-of-mock'; | ||
|
||
import funcGetLastFixture from './func.get-last-fixture'; | ||
import funcParseFindArgs from './func.parse-find-args'; | ||
import funcParseFindArgsName from './func.parse-find-args-name'; | ||
import mockHelperFindInstances from './mock-helper.find-instances'; | ||
|
||
const defaultNotFoundValue = {}; // simulating Symbol | ||
|
||
export default (...args: any[]) => { | ||
const { el, sel, notFoundValue } = funcParseFindArgs<Type<any>>(args, defaultNotFoundValue); | ||
const { el, sel, notFoundValue } = funcParseFindArgs(args, defaultNotFoundValue); | ||
const debugElement = el || funcGetLastFixture()?.debugElement; | ||
|
||
const result = mockHelperFindInstances(debugElement, getSourceOfMock(sel)); | ||
const result = mockHelperFindInstances(debugElement, sel); | ||
if (result.length) { | ||
return result[0]; | ||
} | ||
if (notFoundValue !== defaultNotFoundValue) { | ||
return notFoundValue; | ||
} | ||
throw new Error(`Cannot find an instance via ngMocks.findInstance(${sel.name})`); | ||
throw new Error(`Cannot find an instance via ngMocks.findInstance(${funcParseFindArgsName(sel)})`); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.