question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unit testing the virtual scroll

See original GitHub issue

I want to unit test the virtual scroll please suggest the way to test this scenario I have the component.html

<virtual-scroll [items]="items" (update)="viewPortItems = $event">
    <list-item *ngFor="let item of viewPortItems" [item]="item">
        <div>{item.name}</div>
   </list-item>
</virtual-scroll>

in my spec file i’m trying to test the debug element of virtual-scroll to check the childnodes to the length of mock data. but the child nodes doesn’t have the <div/> element inside the <list-item/>

let ListItemElement = riskfixture.debugElement.query(By.css('list-item'));
expect(mdlListItemElement.childNodes.length).toBe(mockData.length);

when i log the ListItemElement i see below

<virtual-scroll _ngcontent-c3="">
   <list-item _ngcontent-c3="">
     <!--bindings={}-->
   </list-item>
   <router-outlet _ngcontent-c3=""></router-outlet>
 </virtual-scroll>

Please help how can i achieve the unittesting

Issue Analytics

  • State:open
  • Created 6 years ago
  • Reactions:1
  • Comments:13

github_iconTop GitHub Comments

6reactions
alexantocommented, Mar 22, 2018

For me the solution was to manually emit the update event in the test:

const virtualScrollComponent = fixture.debugElement.query(By.directive(VirtualScrollComponent)).componentInstance;
component.items$.subscribe(items => {
     virtualScrollComponent.update.emit(items);
});
3reactions
ataraciukcommented, Jun 21, 2019

If this helps anyone, I ended up just creating a fake component.

Do not import the VirtualScrollerModule into the test, instead, just define a component like this:

@Component({
  selector: 'virtual-scroller',
  template: '<ng-content></ng-content>'
})
export class FakeVirtualScrollerComponent implements OnInit {
  @Input() items: any[];

  viewPortItems;

  ngOnInit(): void {
    this.viewPortItems = this.items;
  }
}

And add it as a declaration on

TestBed.configureTestingModule({
      declarations: [
Read more comments on GitHub >

github_iconTop Results From Across the Web

Angular Material virtual scroll not rendering items in unit tests
Changing the unit test (see in the question) to following resolved it: it('should render at least 4 items', fakeAsync(() => { // <---...
Read more >
Testing a Virtual List component with Cypress and Storybook
The VirtualList component leverages an inertial scroll and, since the story adds thousands of items to be rendered to fully show the VirtualList ......
Read more >
Using Virtual Scroll for huge lists | Angular Cookbook
In this recipe, we'll render a list of 10,000 users and will use the Virtual Scroll functionality from the Angular CDK to improve...
Read more >
Testing a Virtual List component with Cypress and Storybook
for Cypress, there is the cypress-react-unit-test library that allows you to ... The VirtualList component leverages an inertial scroll and, ...
Read more >
Getting to Know the Angular CDK Virtual Scroll Feature
The problem with that is that so many elements in the DOM can cause slow initial rendering, laggy scrolling, and dirty checking on...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found