forked from help-me-mom/ng-mocks
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: correct stop of search in ivy tree
closes #298
- Loading branch information
Showing
7 changed files
with
328 additions
and
4 deletions.
There are no files selected for viewing
2 changes: 1 addition & 1 deletion
2
libs/ng-mocks/src/lib/mock-helper/func.get-from-node-element.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,4 +1,4 @@ | ||
import { DebugNode } from '@angular/core'; | ||
|
||
export default (node: DebugNode): DebugNode => | ||
node.nativeNode.nodeName === '#text' && node.parent ? node.parent : node; | ||
node.nativeNode?.nodeName === '#text' && node.parent ? node.parent : node; |
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
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,52 @@ | ||
import { Component, Directive, Input } from '@angular/core'; | ||
import { ComponentFixture, TestBed } from '@angular/core/testing'; | ||
import { By } from '@angular/platform-browser'; | ||
import { MockDirective, ngMocks } from 'ng-mocks'; | ||
|
||
@Directive({ | ||
selector: '[myDirective]', | ||
}) | ||
export class MyDirective { | ||
@Input() public value?: string; | ||
} | ||
|
||
@Component({ | ||
selector: 'app', | ||
template: ` | ||
<div class="p1"><span myDirective value="d1"></span></div> | ||
<div class="p2"><span myDirective value="d2"></span></div> | ||
<div class="p3"><span myDirective value="d3"></span></div> | ||
`, | ||
}) | ||
export class AppComponent {} | ||
|
||
describe('issue-298:stack-blitz', () => { | ||
let fixture: ComponentFixture<AppComponent>; | ||
beforeEach(() => { | ||
TestBed.configureTestingModule({ | ||
declarations: [MockDirective(MyDirective), AppComponent], | ||
}); | ||
}); | ||
beforeEach(() => { | ||
fixture = TestBed.createComponent(AppComponent); | ||
fixture.detectChanges(); | ||
}); | ||
|
||
it(`should be 'd1'`, () => { | ||
const debugElement = fixture.debugElement.query(By.css('.p1')); | ||
const directive = ngMocks.findInstance(debugElement, MyDirective); | ||
expect(directive.value).toBe('d1'); | ||
}); | ||
|
||
it(`should be 'd2'`, () => { | ||
const debugElement = fixture.debugElement.query(By.css('.p2')); | ||
const directive = ngMocks.findInstance(debugElement, MyDirective); | ||
expect(directive.value).toBe('d2'); | ||
}); | ||
|
||
it(`should be 'd3'`, () => { | ||
const debugElement = fixture.debugElement.query(By.css('.p3')); | ||
const directive = ngMocks.findInstance(debugElement, MyDirective); | ||
expect(directive.value).toBe('d3'); | ||
}); | ||
}); |
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,163 @@ | ||
import { Pipe, PipeTransform } from '@angular/core'; | ||
import { TestBed } from '@angular/core/testing'; | ||
import { MockRender, ngMocks } from 'ng-mocks'; | ||
|
||
@Pipe({ | ||
name: 'pure', | ||
pure: true, | ||
}) | ||
export class PurePipe implements PipeTransform { | ||
public readonly name = 'PurePipe'; | ||
public value: any; | ||
|
||
public transform(value: string): string { | ||
this.value = value; | ||
|
||
return `${this.name}:${value}`; | ||
} | ||
} | ||
|
||
@Pipe({ | ||
name: 'impure', | ||
pure: false, | ||
}) | ||
export class ImpurePipe implements PipeTransform { | ||
public readonly name = 'ImpurePipe'; | ||
public value: any; | ||
|
||
public transform(value: string): string { | ||
this.value = value; | ||
|
||
return `${this.name}:${value}`; | ||
} | ||
} | ||
|
||
describe('issue-240:nodes', () => { | ||
beforeEach(() => | ||
TestBed.configureTestingModule({ | ||
declarations: [ImpurePipe], | ||
}).compileComponents(), | ||
); | ||
|
||
it('calls pipes differently', () => { | ||
MockRender( | ||
` | ||
<div class="parent"> | ||
"parent-1:{{ "parent-1" | impure }}" | ||
<div class="child-1"> | ||
<!-- comment before --> | ||
"child-1:{{ "child-1" | impure }}" | ||
</div> | ||
<div class="child-2"> | ||
"child-2:{{ "child-2" | impure }}" | ||
<!-- comment after --> | ||
<div class="child-3"> | ||
text before | ||
"child-3:{{ "child-3" | impure }}" | ||
</div> | ||
</div> | ||
<ng-container> | ||
"parent-2:{{ "parent-2" | impure }}" | ||
<div class="obstacle"></div> | ||
<ng-container> | ||
"parent-3:{{ "parent-3" | impure }}" | ||
text after | ||
</ng-container> | ||
</ng-container> | ||
</div> | ||
`, | ||
); | ||
|
||
const parent = ngMocks.find('.parent'); | ||
const child1 = ngMocks.find('.child-1'); | ||
const child2 = ngMocks.find('.child-2'); | ||
const child3 = ngMocks.find('.child-3'); | ||
|
||
expect(parent.nativeElement.innerHTML).toContain( | ||
'"parent-1:ImpurePipe:parent-1"', | ||
); | ||
expect(parent.nativeElement.innerHTML).toContain( | ||
'"parent-2:ImpurePipe:parent-2"', | ||
); | ||
expect(parent.nativeElement.innerHTML).toContain( | ||
'"child-1:ImpurePipe:child-1"', | ||
); | ||
expect(parent.nativeElement.innerHTML).toContain( | ||
'"child-2:ImpurePipe:child-2"', | ||
); | ||
expect(parent.nativeElement.innerHTML).toContain( | ||
'"child-3:ImpurePipe:child-3"', | ||
); | ||
|
||
expect(child1.nativeElement.innerHTML).not.toContain( | ||
'"parent-1:ImpurePipe:parent-1"', | ||
); | ||
expect(child1.nativeElement.innerHTML).not.toContain( | ||
'"parent-2:ImpurePipe:parent-2"', | ||
); | ||
expect(child1.nativeElement.innerHTML).toContain( | ||
'"child-1:ImpurePipe:child-1"', | ||
); | ||
expect(child1.nativeElement.innerHTML).not.toContain( | ||
'"child-2:ImpurePipe:child-2"', | ||
); | ||
expect(child1.nativeElement.innerHTML).not.toContain( | ||
'"child-3:ImpurePipe:child-3"', | ||
); | ||
|
||
expect(child2.nativeElement.innerHTML).not.toContain( | ||
'"parent-1:ImpurePipe:parent-1"', | ||
); | ||
expect(child2.nativeElement.innerHTML).not.toContain( | ||
'"parent-2:ImpurePipe:parent-2"', | ||
); | ||
expect(child2.nativeElement.innerHTML).not.toContain( | ||
'"child-1:ImpurePipe:child-1"', | ||
); | ||
expect(child2.nativeElement.innerHTML).toContain( | ||
'"child-2:ImpurePipe:child-2"', | ||
); | ||
expect(child2.nativeElement.innerHTML).toContain( | ||
'"child-3:ImpurePipe:child-3"', | ||
); | ||
|
||
expect(child3.nativeElement.innerHTML).not.toContain( | ||
'"parent-1:ImpurePipe:parent-1"', | ||
); | ||
expect(child3.nativeElement.innerHTML).not.toContain( | ||
'"parent-2:ImpurePipe:parent-2"', | ||
); | ||
expect(child3.nativeElement.innerHTML).not.toContain( | ||
'"child-1:ImpurePipe:child-1"', | ||
); | ||
expect(child3.nativeElement.innerHTML).not.toContain( | ||
'"child-2:ImpurePipe:child-2"', | ||
); | ||
expect(child3.nativeElement.innerHTML).toContain( | ||
'"child-3:ImpurePipe:child-3"', | ||
); | ||
|
||
const parentPipes = ngMocks.findInstances(parent, ImpurePipe); | ||
expect(parentPipes.map(item => item.value)).toEqual([ | ||
// all in the root node first | ||
'parent-1', | ||
'parent-2', | ||
'parent-3', | ||
'child-1', | ||
'child-2', | ||
'child-3', | ||
]); | ||
|
||
const child1Pipes = ngMocks.findInstances(child1, ImpurePipe); | ||
expect(child1Pipes.map(item => item.value)).toEqual(['child-1']); | ||
|
||
const child2Pipes = ngMocks.findInstances(child2, ImpurePipe); | ||
expect(child2Pipes.map(item => item.value)).toEqual([ | ||
'child-2', | ||
'child-3', | ||
]); | ||
|
||
const child3Pipes = ngMocks.findInstances(child3, ImpurePipe); | ||
expect(child3Pipes.map(item => item.value)).toEqual(['child-3']); | ||
}); | ||
}); |