Input bindings are undefined when component has no selector
See original GitHub issueAfter 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:
- Created 4 years ago
- Comments:6 (5 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Yes, you should be able to render the component like this:
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.