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.

Hard to test when using fromRoot

See original GitHub issue

I am using the Angular Material side-nav. Inside the mat-sidenav-content I load up various components, some use infinite scrolling, some don’t. But to make the scrolling work properly I need to point the infinite scroller at the parent mat-sidenav-content container like so: <div infiniteScroll infiniteScrollContainer=“.mat-sidenav-content” fromRoot=“true” scrollWindow=“false” …

The problem comes when I want to write jasmine/karma tests for my component with infinite scrolling. I use the TestBed to load up my component - but since the side-nav is a parent, it’s not present in my component under test TestBed, and the infiniteScroller errors when trying to resolve the container.

I put in place a workaround that seems to work by making it fall back to the window if it can’t find the container selector, not sure if there’s a better way: image

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:13 (7 by maintainers)

github_iconTop GitHub Comments

2reactions
bzeymercommented, Dec 22, 2017

That approach still not solving the test implementation problem.

If setting it to window may cause undesired results, the container should point to defaultElement as it is the one rendered by Karma tests.

Basicaly it should look like this:

function resolveContainerElement(selector, scrollWindow, defaultElement, fromRoot) {
    const /** @type {?} */ hasWindow = window && !!window.document && window.document.documentElement;
    let /** @type {?} */ container = hasWindow && scrollWindow ? window : defaultElement;
    if (selector) {
        const /** @type {?} */ containerIsString = selector && hasWindow && typeof selector === 'string';
        container = containerIsString
            ? findElement(selector, defaultElement.nativeElement, fromRoot)
            : selector;
        if (!container) {
            container = defaultElement;
        }
    }
    return container;
}
1reaction
MichaelPruefercommented, Jun 20, 2019

We ended up using a TestHostComponent like

@Component({
  selector: 'app-host-component',
  template: '<div class="content-wrapper" [infiniteScrollContainer]><grid></grid></div>'
})
class TestHostComponent {
  @ViewChild(GridComponent)
  public component: GridComponent;
}

let component: GridComponent;
let testHostFixture: ComponentFixture<TestHostComponent>;
let testHostComponent: TestHostComponent;

const testHelper = new TestbedHelper();
testHelper.setUpTestBed({
  declarations: [
    TestHostComponent,
    GridComponent
  ]
});

beforeEach(() => {
  testHostFixture = TestBed.createComponent(TestHostComponent);
  testHostComponent = testHostFixture.componentInstance;
  component = testHostComponent.component;


  testHostFixture.detectChanges();
});
...
Read more comments on GitHub >

github_iconTop Results From Across the Web

Golang: tests and working directory - Stack Overflow
Basically you can change the folder that the test is running using a similar function: package main import ( "os" "path" "runtime" )...
Read more >
Testing - ts - GUIDE - Angular
When a part of the application seems hard to test, the root cause is often a design flaw, something to cure now rather...
Read more >
Root Auto Insurance Review 2022: Pros and Cons - NerdWallet
To get an auto insurance quote from Root, you first take a “Test Drive,” where you let the company's app track your driving...
Read more >
Removal of a Broken Instrument from a Tooth with Apical ...
When a file breaks in the canal, bypassing or removal can be difficult and the ... Vitality test of both teeth using ENDO-ICE...
Read more >
Root canal treatment: Everything you need to know
What to expect from root canal treatment ... Then, the tooth is filled with a rubber-like material, using an adhesive cement to seal...
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