-
-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ngMocks.findInstance): looks for instances in all matched DebugE…
…lements #2105
- Loading branch information
Showing
4 changed files
with
79 additions
and
11 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
5 changes: 5 additions & 0 deletions
5
libs/ng-mocks/src/lib/mock-helper/find/mock-helper.find-all.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,10 +1,15 @@ | ||
import { DebugElement } from '@angular/core'; | ||
|
||
import isDebugNode from '../format/is-debug-node'; | ||
|
||
import funcParseFindArgs from './func.parse-find-args'; | ||
import funcParseFindTerm from './func.parse-find-term'; | ||
|
||
export default (...args: any[]): DebugElement[] => { | ||
const [el, sel] = funcParseFindArgs(args); | ||
if (isDebugNode(sel)) { | ||
return [sel as any]; | ||
} | ||
|
||
return el?.queryAll(funcParseFindTerm(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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { Component, Input, NgModule } from '@angular/core'; | ||
import { MockBuilder, MockRender, ngMocks } from 'ng-mocks'; | ||
|
||
@Component({ | ||
selector: 'target', | ||
template: ' {{ value }} ', | ||
}) | ||
class TargetComponent { | ||
@Input() public readonly value: string | null = null; | ||
} | ||
|
||
@NgModule({ | ||
declarations: [TargetComponent], | ||
}) | ||
class TargetModule {} | ||
|
||
// @see https://github.com/ike18t/ng-mocks/issues/2105 | ||
describe('issue-2105', () => { | ||
beforeEach(() => MockBuilder(TargetComponent, TargetModule)); | ||
|
||
it('finds all instances', () => { | ||
const fixture = MockRender(` | ||
<div class="root"> | ||
<target value="1"></target> | ||
<target value="2"></target> | ||
<div class="child child1"> | ||
<target value="3"></target> | ||
<target value="4"></target> | ||
</div> | ||
<div class="child child2"> | ||
<target value="5"></target> | ||
<target value="6"></target> | ||
<div class="child child3"> | ||
<target value="7"></target> | ||
<target value="8"></target> | ||
</div> | ||
</div> | ||
</div> | ||
`); | ||
|
||
expect(ngMocks.formatText(fixture)).toEqual('1 2 3 4 5 6 7 8'); | ||
|
||
// looking for all | ||
{ | ||
const instances = ngMocks.findInstances('div', TargetComponent); | ||
expect(instances.length).toEqual(8); | ||
} | ||
|
||
// looking for children only | ||
// it should collect data from child1, child2 and child3. | ||
{ | ||
const instances = ngMocks.findInstances( | ||
'div.child', | ||
TargetComponent, | ||
); | ||
expect(instances.length).toEqual(6); | ||
} | ||
}); | ||
}); |