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.

Input bindings are undefined when component has no selector

See original GitHub issue

After updating shallow-render from 7.0.0 to 8.2.0 (and Angular from 7.2.0 to 8.1.0), input bindings on a component with no selector are undefined.

In this example, both logs are undefined. Adding a selector to the component will fix it though.

@Component({
  template: '<div></div>',
})
export class TestComponent {
  @Input() public input: object;

  ngOnInit(): void {
    console.log(this.input);
  }
}

describe('TestComponent', () => {
  let shallow: Shallow<TestComponent>;

  @NgModule({
    declarations: [TestComponent],
  })
  class TestingModule {}

  beforeEach(() => {
    shallow = new Shallow(TestComponent, TestingModule);
  });

  it('is undefined', async () => {
    const { instance } = await shallow.render({ bind: { input: { foo: 'bar' } } });
    console.log(instance.input);
  });
});

Context: I’m trying to test a modal component that doesn’t have a selector and gets its inputs set directly on the instance. Before this package upgrade, I could set those inputs in a test using the bind option.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
getsafcommented, Jul 12, 2019

Can the component instance be accessed before that promise is resolved and before ngOnInit is called?

Yes, you should be able to render the component like this:

const {fixture, instance} = await shallow.render({detectChanges: false});
// Do some stuff...
fixture.detectChanges(); // to trigger the onInit if you need to
1reaction
getsafcommented, Jul 8, 2019

Since you mentioned being able to do this in previous versions, I wanted to mention where the change that prevents us from “binding” to EntryComponents came from.

Here is the PR where we stopped applying bindings directly to the component and started using actual Angular templates in shallow. The goal here was to ensure that bindings in shallow-render tests worked exactly like they do in a production app. Namely that the lifecycle hooks function exactly the same in our tests as they do in the real world. When we were binding directly to the component instance we were bypassing a lot of Angular internals and switching to template-based rendering for components with selectors shored that up. Sorry for the rambling, just thought this extra info would be helpful.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why are my component bindings undefined in its controller?
I tried with a timeout and it doesn't work. As I said above, An arrow function does not create it's own this context,...
Read more >
Ivy compiler incorrectly allows undefined to be passed to any ...
So, what seems to be happening is the Ivy compiler is adding undefined to the type of every input when considering the bindings,...
Read more >
@Directive().inputs And @Input() Are Not Functionally ...
Ben Nadel demonstrates that the @Directive().inputs and @Input() meta-data syntax in Angular 7.2.13 are not functionally equivalent.
Read more >
Class and style binding - Angular
Use class and style bindings to add and remove CSS class names from an element's class attribute and to set styles dynamically.
Read more >
NgOnInit & Constructor Differences In Angular With Examples
ngOnInit=function () { }; CheckboxComponent=__decorate([ Object(_angular_core__WEBPACK_IMPORTED_MODULE_0__["Component"])({ selector:'app- ...
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