我有一条指令可以更改窗口调整大小时元素内文本的高度:
import {Directive, ElementRef, Renderer2, HostListener} from '@angular/core';
@Directive({selector: '[fittext]'})
export class FittextDirective {
constructor(private element: ElementRef, private renderer: Renderer2) { }
public changeFontSize(): void {
const htmlElement: HTMLElement = this.element.nativeElement;
const elemDims = htmlElement.getBoundingClientRect();
const height = elemDims.height;
// Set new font size on element
this.renderer.setStyle(htmlElement, 'font-size', `${height / 2}px`);
}
@HostListener('window:resize')
public onResize(): void {
this.changeFontSize();
}
}
我正在尝试使用自定义包装器组件并通过在窗口上调度调整大小事件来对其进行测试。
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { FittextDirective } from './fittext.directive';
import { Component, ViewChild, ElementRef } from '@angular/core';
import { By } from '@angular/platform-browser';
@Component({
template: `
<div #div fittext>{{ text }}</div>
<style>
div {
height: 100vh;
width: 100vw;
font-size: 100%;
}
</style>
`
})
class TestComponent {
@ViewChild('div') div: ElementRef;
public text: string = 'This is test string';
}
describe('FittextDirective', () => {
let component: TestComponent;
let fixture: ComponentFixture<TestComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [
TestComponent,
FittextDirective
]
}).compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(TestComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
fit('should trigger on resize', () => {
const {height, width} = component.div.nativeElement.getBoundingClientRect();
const fontSize = component.div.nativeElement.style.fontSize;
window.dispatchEvent(new Event('resize'));
fixture.detectChanges()
const resizedElement = fixture.debugElement.query(By.directive(FittextDirective));
const {newHeight, newWidth} = resizedElement.nativeElement.getBoundingClientRect();
const newFontSize = resizedElement.nativeElement.style.fontSize;
expect(newHeight).not.toEqual(height);
expect(newWidth).not.toEqual(width);
expect(newFontSize).not.toEqual(fontSize);
});
});
但是,测试报告的字体大小为''
,调整后的高度和宽度(即newHeight
和newWidth
)为undefined
。为什么字体大小应为空字符串,即使字体大小应与div相匹配?以及为什么窗口大小调整后的高度和宽度未定义?我正在使用无头镀铬进行测试,因此无法使用window.resizeTo(width, height)
方法。
编辑1:在Alex K
的建议下,我将newHeight
和newWidth
更改为:
const newHeight = resizedElement.nativeElement.getBoundingClientRect().height;
const newWidth = resizedElement.nativeElement.getBoundingClientRect().width;
但是,似乎在调整大小事件之后,窗口没有调整大小(我收到错误Expected 600 not to equal 600.
和Expected 785 not to equal 785.
)。我还更改了标签之间的样式,以更改组件属性(即styles
属性中的样式),但仍将fontSize
报告为''
(或与此有关的任何样式)
编辑2:出于某些奇怪的原因,只能使用window.getComputedStyle()
来获取样式(请参见this)。现在,我得到了字体的大小,但是在调整大小后它没有改变。
const styles = window.getComputedStyle(element.nativeElement);
const fontSize = styles.fontSize;
...
const newStyles = window.getComputedStyle(resizedElement.nativeElement);
const newFontSize = newStyles.fontSize;
编辑3:如建议的那样,我使用spyOn
伪造了尺寸,但是,这不会调整字体大小。
spyOn(component.div.nativeElement, 'getBoundingClientRect')
.and.returnValue({height: height / 2, width: width / 2});
window.dispatchEvent(new Event('resize'));
fixture.detectChanges()