Skip to content

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

4.6 KB
Raw Blame
import { Clipboard } from '@angular/cdk/clipboard'
import { ComponentFixture, TestBed } from '@angular/core/testing'
import { RouterTestingModule } from '@angular/router/testing'
import { of, throwError } from 'rxjs'
import { FileVersion, ShareLink } from 'src/app/data/share-link'
import { SETTINGS_KEYS } from 'src/app/data/ui-settings'
import { ShareLinkService } from 'src/app/services/rest/share-link.service'
import { SettingsService } from 'src/app/services/settings.service'
import { ToastService } from 'src/app/services/toast.service'
import { ShareLinkListComponent } from './share-link-list.component'

describe('ShareLinkListComponent', () => {
  let component: ShareLinkListComponent
  let fixture: ComponentFixture<ShareLinkListComponent>
  let service: jest.Mocked<Pick<ShareLinkService, 'list' | 'delete'>>
  let clipboard: Clipboard
  let toastService: jest.Mocked<Pick<ToastService, 'showSuccess' | 'showError'>>

  const link = {
    id: 1,
    document: 42,
    document_title: 'Test document',
    slug: 'share-slug',
    created: new Date().toISOString(),
    expiration: null,
    file_version: FileVersion.Archive,
  } as ShareLink

  beforeEach(() => {
    service = {
      list: jest.fn().mockReturnValue(of({ count: 1, results: [link] })),
      delete: jest.fn().mockReturnValue(of(true)),
    }
    toastService = {
      showSuccess: jest.fn(),
      showError: jest.fn(),
    }

    TestBed.configureTestingModule({
      imports: [ShareLinkListComponent, RouterTestingModule],
      providers: [
        { provide: ShareLinkService, useValue: service },
        { provide: ToastService, useValue: toastService },
      ],
    })

    fixture = TestBed.createComponent(ShareLinkListComponent)
    component = fixture.componentInstance
    clipboard = TestBed.inject(Clipboard)
  })

  afterEach(() => {
    jest.clearAllTimers()
    jest.useRealTimers()
  })

  it('loads and renders document share links', () => {
    fixture.detectChanges()

    expect(service.list).toHaveBeenCalledWith(1, 25, 'created', true)
    expect(component.links()).toEqual([link])
    expect(fixture.nativeElement.textContent).toContain('Test document')
    expect(fixture.nativeElement.textContent).toContain('ID: 42')
  })

  it('loads another page', () => {
    fixture.detectChanges()
    component.setPage(2)

    expect(service.list).toHaveBeenLastCalledWith(2, 25, 'created', true)
  })

  it('sorts links and returns to the first page', () => {
    fixture.detectChanges()
    component.page.set(2)

    component.onSort({ column: 'expiration', reverse: false })

    expect(component.page()).toBe(1)
    expect(service.list).toHaveBeenLastCalledWith(1, 25, 'expiration', false)
  })

  it('marks expired share links', () => {
    service.list.mockReturnValue(
      of({
        count: 1,
        results: [
          {
            ...link,
            expiration: '2000-01-01T00:00:00.000Z',
          },
        ],
      })
    )

    fixture.detectChanges()

    expect(fixture.nativeElement.textContent).toContain('Expired')
  })

  it('stores a changed page size and reloads from the first page', () => {
    const settingsService = TestBed.inject(SettingsService)
    jest.spyOn(settingsService, 'get').mockReturnValueOnce({ share_links: 25 })
    const setSpy = jest.spyOn(settingsService, 'set')
    jest.spyOn(settingsService, 'storeSettings').mockReturnValue(of({}))
    const reloadSpy = jest.spyOn(component, 'reload')
    component.page.set(2)

    component.pageSize = 50

    expect(setSpy).toHaveBeenCalledWith(SETTINGS_KEYS.OBJECT_LIST_SIZES, {
      share_links: 50,
    })
    expect(component.page()).toBe(1)
    expect(reloadSpy).toHaveBeenCalled()
  })

  it('shows local copy feedback without a toast', () => {
    jest.useFakeTimers()
    jest.spyOn(clipboard, 'copy').mockReturnValue(true)
    fixture.detectChanges()

    component.copy(link)
    fixture.detectChanges()

    expect(component.copiedID()).toBe(link.id)
    expect(fixture.nativeElement.querySelector('.badge.show')).not.toBeNull()
    expect(toastService.showSuccess).not.toHaveBeenCalled()

    jest.advanceTimersByTime(3000)
    expect(component.copiedID()).toBeNull()
  })

  it('deletes a link and reloads the list', () => {
    fixture.detectChanges()
    component.delete(link)

    expect(service.delete).toHaveBeenCalledWith(link)
    expect(service.list).toHaveBeenCalledTimes(2)
    expect(toastService.showSuccess).toHaveBeenCalled()
  })

  it('shows an error when loading fails', () => {
    service.list.mockReturnValue(throwError(() => new Error('load failed')))
    fixture.detectChanges()

    expect(component.error()).toContain('Failed to load share links.')
    expect(toastService.showError).toHaveBeenCalled()
  })
})