0

我尝试测试 Angular 材料垫扩展面板打开。

所以,我用下面的代码测试。

describe('Selected first expansion panel', () => {
    beforeEach(() => {
        const header = fixture.debugElement.query(By.css('#panel1 .mat-expansion-panel-header'))!.nativeElement;
        header.click();
        fixture.detectChanges();
    });
    it('should show first expansion panel content', () => {
        const first = fixture.debugElement.query(By.css('#panel1 .mat-expansion-panel-content'))!.nativeElement;
        console.log(first.style);
        expect(first.style.visibility).toEqual('visible');
    });
});

控制台(console.log(first.style))下面的输出在“它”中

CSSStyleDeclaration {alignContent: "", alignItems: "", alignSelf: "", alignmentBaseline: "", all: "", …}
cssText: "visibility: visible;"
length: 1
parentRule: null
cssFloat: ""
0: "visibility"
...
visibility: "visible"
...

但是,失败的 expect(first.style.visibility).toEqual('visible') 与Expected '' 等于 'visible'。

怎么获得

4

1 回答 1

1

尝试getComputedStyle在窗口对象上使用。

it('should show first expansion panel content', () => {
        const first = fixture.debugElement.query(By.css('#panel1 .mat-expansion-panel-content'))!.nativeElement;
        const styles = getComputedStyle(first);
        console.log(styles); // make sure you get an object
        expect(styles.visibility).toEqual('visible');
    });
于 2020-03-11T23:49:42.930 回答