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(ngMocks.findInstance): finds pipes in attributes help-me-mom#2314
- Loading branch information
Showing
5 changed files
with
164 additions
and
5 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
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,136 @@ | ||
import { AsyncPipe, NgIf } from '@angular/common'; | ||
import { Component, Pipe, PipeTransform } from '@angular/core'; | ||
import { ComponentFixture, TestBed } from '@angular/core/testing'; | ||
import { BehaviorSubject } from 'rxjs'; | ||
|
||
import { ngMocks } from 'ng-mocks'; | ||
|
||
@Pipe({ | ||
name: 'nothing', | ||
pure: false, | ||
}) | ||
class NothingPipe implements PipeTransform { | ||
transform<T>(value: T): T { | ||
return value; | ||
} | ||
} | ||
|
||
@Component({ | ||
selector: 'target', | ||
template: ` | ||
<div | ||
class="array" | ||
*ngFor=" | ||
let item of array$ | nothing | async | nothing | nothing | ||
" | ||
> | ||
item: {{ item }} | ||
</div> | ||
<div | ||
class="false" | ||
*ngIf="false$ | nothing | async | nothing | nothing" | ||
> | ||
false | ||
</div> | ||
<div class="text"> | ||
{{ text$ | nothing | async | nothing | nothing }} | ||
</div> | ||
<div | ||
class="true" | ||
*ngIf="true$ | nothing | async | nothing | nothing" | ||
> | ||
true | ||
</div> | ||
`, | ||
}) | ||
export class TargetComponent { | ||
public array$ = new BehaviorSubject([1]); | ||
public false$ = new BehaviorSubject(false); | ||
public text$ = new BehaviorSubject('text'); | ||
public true$ = new BehaviorSubject(true); | ||
} | ||
|
||
// @see https://github.com/ike18t/ng-mocks/issues/2314 | ||
describe('issue-2314', () => { | ||
let fixture: ComponentFixture<TargetComponent>; | ||
|
||
beforeEach(async () => { | ||
return TestBed.configureTestingModule({ | ||
declarations: [TargetComponent, NothingPipe], | ||
}).compileComponents(); | ||
}); | ||
|
||
beforeEach(() => { | ||
fixture = TestBed.createComponent(TargetComponent); | ||
fixture.detectChanges(); | ||
}); | ||
|
||
it(`finds all pipes`, () => { | ||
const allPipes = ngMocks.findInstances(AsyncPipe); | ||
expect(allPipes.length).toBe(4); | ||
}); | ||
|
||
describe('ngMocks.findInstance', () => { | ||
it(`finds pipes on '.array'`, () => { | ||
const arrayPipe = ngMocks.findInstance( | ||
'.array', | ||
AsyncPipe, | ||
undefined, | ||
); | ||
expect(arrayPipe).toBeDefined(); | ||
}); | ||
|
||
it(`finds pipes on '.false'`, () => { | ||
// Because it isn't rendered, we cannot find the element with `.false`, therefore, we need to rely on NgIf itself. | ||
const ngIf = ngMocks.reveal(NgIf); | ||
const falsePipe = ngMocks.findInstance( | ||
ngIf, | ||
AsyncPipe, | ||
undefined, | ||
); | ||
expect(falsePipe).toBeDefined(); | ||
}); | ||
|
||
it(`find pipes on '.text'`, () => { | ||
const textPipe = ngMocks.findInstance( | ||
'.text', | ||
AsyncPipe, | ||
undefined, | ||
); | ||
expect(textPipe).toBeDefined(); | ||
}); | ||
|
||
it(`should find pipe on '.true'`, () => { | ||
const truePipe = ngMocks.findInstance( | ||
'.true', | ||
AsyncPipe, | ||
undefined, | ||
); | ||
expect(truePipe).toBeDefined(); | ||
}); | ||
}); | ||
|
||
describe('ngMocks.findInstances', () => { | ||
it(`finds pipes on '.array'`, () => { | ||
const arrayPipe = ngMocks.findInstances('.array', AsyncPipe); | ||
expect(arrayPipe.length).toEqual(1); | ||
}); | ||
|
||
it(`finds pipes on '.false'`, () => { | ||
// Because it isn't rendered, we cannot find the element with `.false`, therefore, we need to rely on NgIf itself. | ||
const ngIf = ngMocks.reveal(NgIf); | ||
const falsePipe = ngMocks.findInstances(ngIf, AsyncPipe); | ||
expect(falsePipe.length).toEqual(1); | ||
}); | ||
|
||
it(`find pipes on '.text'`, () => { | ||
const textPipe = ngMocks.findInstances('.text', AsyncPipe); | ||
expect(textPipe.length).toEqual(1); | ||
}); | ||
|
||
it(`should find pipe on '.true'`, () => { | ||
const truePipe = ngMocks.findInstances('.true', AsyncPipe); | ||
expect(truePipe.length).toEqual(1); | ||
}); | ||
}); | ||
}); |