Input value is an empty string in Jasmine Tests using an combined element, attribute Component Selector
See original GitHub issueπ bug report
Description
Iβve got a component with some inputs. Some of them are optional but the input config
is required to use the component. Therefore I defined a component selector that has an element part and an attribute part (e.g. app-my-component[config]
). Now I always need to pass a config into the component if I want to use it. Great! I started the app with ng serve
and everything works fine. So far so goodβ¦
While writing the component tests Iβm coming in trouble. I set the config in the beforeEach
block where the component fixture is also created. However the required input attribute config
is always an empty string when I called fixture.detectChanges()
.
Here is the Test-File of the minimal Reproduction I wrote that shows exactly the behaviour.
describe('TestComponent', () => {
let component: TestComponent;
let fixture: ComponentFixture<TestComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ TestComponent ]
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(TestComponent);
component = fixture.componentInstance;
component.requiredInput = { anyKey: 'anyValue'};
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
it('should not fail', () => {
const expectedResult: RequiredInput = { anyKey: 'anyValue' };
expect(component.requiredInput).toEqual(expectedResult); // <-- this expectation fails
});
});
Removing the attribute part of the selector solves the problem. But that is no way.
π¬ Minimal Reproduction
Iβve created a minimal GitHub repository with the reproduction of the issue. I used Angular 8 for the reproduction repository because my main project is still using Angular 8, too
π Your Environment
Angular Version:
Angular CLI: 8.3.26
Node: 10.19.0
OS: darwin x64
Angular: 8.2.14
... animations, common, compiler, compiler-cli, core, forms
... language-service, platform-browser, platform-browser-dynamic
... router
Package Version
------------------------------------------------------------
@angular-devkit/architect 0.803.26
@angular-devkit/build-angular 0.803.26
@angular-devkit/build-ng-packagr 0.803.26
@angular-devkit/build-optimizer 0.803.26
@angular-devkit/build-webpack 0.803.26
@angular-devkit/core 8.3.26
@angular-devkit/schematics 8.3.26
@angular/cli 8.3.26
@ngtools/webpack 8.3.26
@schematics/angular 8.3.26
@schematics/update 0.803.26
ng-packagr 5.7.1
rxjs 6.5.5
typescript 3.5.3
webpack 4.39.2
Issue Analytics
- State:
- Created 3 years ago
- Comments:5 (3 by maintainers)
Top GitHub Comments
Not sure if itβs a bug or a feature (Iβll let better specialists decide about that).
But the typical way of testing a component that requires inputs is to actually pass inputs the way theyβre supposed to be passed, with a test such as this one:
This issue has been automatically locked due to inactivity. Please file a new issue if you are encountering a similar or related problem.
Read more about our automatic conversation locking policy.
This action has been performed automatically by a bot.