Skip to content

A community-supported supercharged document management system: scan, index and archive all your documents

2.4 KB
Raw Blame
import { Clipboard } from '@angular/cdk/clipboard'
import { ComponentFixture, TestBed } from '@angular/core/testing'
import { DocumentBarcodesComponent } from './document-barcodes.component'

const barcodes = [
  { page: 1, value: 'ASN00123', format: 'Code128' },
  { page: 2, value: 'https://example.com/invoice/4711', format: 'QRCode' },
  { page: 2, value: 'javascript:alert(1)', format: 'QRCode' },
]

describe('DocumentBarcodesComponent', () => {
  let component: DocumentBarcodesComponent
  let fixture: ComponentFixture<DocumentBarcodesComponent>
  let clipboard: Clipboard

  beforeEach(async () => {
    TestBed.configureTestingModule({
      imports: [DocumentBarcodesComponent],
    }).compileComponents()

    fixture = TestBed.createComponent(DocumentBarcodesComponent)
    component = fixture.componentInstance
    clipboard = TestBed.inject(Clipboard)
    fixture.componentRef.setInput('barcodes', barcodes)
    fixture.detectChanges()
  })

  it('should display all barcodes', () => {
    const rows = fixture.nativeElement.querySelectorAll('tbody tr')
    expect(rows).toHaveLength(3)
    expect(rows[0].textContent).toContain('ASN00123')
    expect(rows[0].textContent).toContain('Code128')
  })

  it('should only link http(s) values', () => {
    const links = fixture.nativeElement.querySelectorAll('tbody a')
    expect(links).toHaveLength(1)
    expect(links[0].getAttribute('href')).toEqual(
      'https://example.com/invoice/4711'
    )
    expect(links[0].getAttribute('target')).toEqual('_blank')
  })

  it('should copy a value and show feedback', () => {
    jest.useFakeTimers()
    const copySpy = jest.spyOn(clipboard, 'copy').mockReturnValue(true)
    const buttons = fixture.nativeElement.querySelectorAll('tbody button')
    buttons[0].click()
    fixture.detectChanges()
    expect(copySpy).toHaveBeenCalledWith('ASN00123')
    expect(component.copiedIndex()).toEqual(0)
    expect(
      buttons[0].querySelector('pngx-bs-icon').getAttribute('name')
    ).toEqual('clipboard-check')
    jest.advanceTimersByTime(3000)
    fixture.detectChanges()
    expect(component.copiedIndex()).toBeNull()
    expect(
      buttons[0].querySelector('pngx-bs-icon').getAttribute('name')
    ).toEqual('clipboard')
    jest.useRealTimers()
  })

  it('should not show feedback if copying failed', () => {
    jest.spyOn(clipboard, 'copy').mockReturnValue(false)
    component.copy(1)
    expect(component.copiedIndex()).toBeNull()
  })
})