Make sure the ngrx store is full in test

I have a function

handleCSV () {
  this.storeData$.take(1).subscribe(s => {
    const data = s.data
    const fields = ['reference', 'description', 'val1', 'val2', 'difference']
    const fieldNames = ['Reference', 'Description', 'Value 1', 'Value 2', 'Difference']
    const exportData = data.filter(dataset => dataset.visible)

    const csv = json2csv({ data: exportData, fields: fields, fieldNames: fieldNames })
    const csvEnc = encodeURIComponent(csv)
    const href = `data:application/octet-stream,${csvEnc}`

    this.csvDownloadLink.nativeElement.setAttribute('href', href)
    this.csvDownloadLink.nativeElement.click()

    this.csvDownloadLink.nativeElement.setAttribute('href', '')
  })
}

I have almost 100% coverage for this function, the only thing missing is the data.filter callback

enter image description here

Does anyone know how to check this? The test library uses Jasmine, the application as a whole is built using Angular. Here is the test I have that brought me to the current level of coverage (any other comments on this are also welcome):

it('should click the CSV link', () => {
  spyOn(comp.csvDownloadLink.nativeElement, 'click')

  comp.ngOnInit()
  comp.handleCSV()

  expect(comp.csvDownloadLink.nativeElement.click).toHaveBeenCalledTimes(1)
})

Edit

Ok, so I found that the cause of the problem is not that the data.filter callback is not being called, but the data array itself is empty! For example, always ensuring that the data array has something in it, the callback is covered:

enter image description here

, , if , , , : , storeData $ - . , ngrx. ngOnInit() , storeData $, , , . , ngOnInit() , storeData $ , handleCSV()?

+4

Source: https://habr.com/ru/post/1676128/


All Articles