image-lightbox.spec.ts 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import { TestBed } from '@angular/core/testing';
  2. import { ImageLightboxComponent } from './image-lightbox';
  3. /** 图片灯箱:打开/关闭、Esc、文件名与大小展示 */
  4. describe('ImageLightboxComponent', () => {
  5. beforeEach(async () => {
  6. await TestBed.configureTestingModule({ imports: [ImageLightboxComponent] }).compileComponents();
  7. });
  8. function make(open = true) {
  9. const fixture = TestBed.createComponent(ImageLightboxComponent);
  10. fixture.componentRef.setInput('open', open);
  11. fixture.componentRef.setInput('url', 'https://x/a.png');
  12. fixture.componentRef.setInput('name', 'A_clean.png');
  13. fixture.componentRef.setInput('size', 2048);
  14. fixture.detectChanges();
  15. return fixture;
  16. }
  17. it('open=false 不渲染', () => {
  18. const f = make(false);
  19. expect((f.nativeElement as HTMLElement).querySelector('.lb-mask')).toBeNull();
  20. });
  21. it('展示文件名与大小', () => {
  22. const f = make();
  23. const text = (f.nativeElement as HTMLElement).textContent ?? '';
  24. expect(text).toContain('A_clean.png');
  25. expect(text).toContain('2.0 KB');
  26. });
  27. it('关闭按钮发出 closed', () => {
  28. const f = make();
  29. const c = f.componentInstance;
  30. const spy = jasmine.createSpy('closed');
  31. c.closed.subscribe(spy);
  32. (f.nativeElement as HTMLElement).querySelector<HTMLButtonElement>('[aria-label="关闭预览"]')!.click();
  33. expect(spy).toHaveBeenCalled();
  34. });
  35. });