Skip to content
This repository has been archived by the owner on Jan 9, 2023. It is now read-only.

Commit

Permalink
feat(added tests): incidentsDownloadCSV
Browse files Browse the repository at this point in the history
  • Loading branch information
reidmeyer committed Sep 6, 2020
1 parent 5220d20 commit dc3c93e
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 24 deletions.
56 changes: 54 additions & 2 deletions src/__tests__/incidents/list/ViewIncidentsTable.test.tsx
@@ -1,12 +1,12 @@
import { Table } from '@hospitalrun/components'
import { Table, Dropdown } from '@hospitalrun/components'
import { mount, ReactWrapper } from 'enzyme'
import { createMemoryHistory } from 'history'
import React from 'react'
import { act } from 'react-dom/test-utils'
import { Router } from 'react-router'

import IncidentFilter from '../../../incidents/IncidentFilter'
import ViewIncidentsTable from '../../../incidents/list/ViewIncidentsTable'
import ViewIncidentsTable, { populateExportData } from '../../../incidents/list/ViewIncidentsTable'
import IncidentSearchRequest from '../../../incidents/model/IncidentSearchRequest'
import IncidentRepository from '../../../shared/db/IncidentRepository'
import Incident from '../../../shared/model/Incident'
Expand Down Expand Up @@ -73,6 +73,58 @@ describe('View Incidents Table', () => {
expect(incidentsTable.prop('actionsHeaderText')).toEqual('actions.label')
})

it('should display a download button', async () => {
const expectedIncidents: Incident[] = [
{
id: 'incidentId1',
code: 'someCode',
date: new Date(2020, 7, 4, 0, 0, 0, 0).toISOString(),
reportedOn: new Date(2020, 8, 4, 0, 0, 0, 0).toISOString(),
reportedBy: 'com.test:user',
status: 'reported',
} as Incident,
]
const { wrapper } = await setup({ status: IncidentFilter.all }, expectedIncidents)

const dropDownButton = wrapper.find(Dropdown)
expect(dropDownButton.exists()).toBeTruthy()
})

it('should populate export data correctly', async () => {
const data = [
{
category: 'asdf',
categoryItem: 'asdf',
code: 'I-eClU6OdkR',
createdAt: '2020-09-06T04:02:38.011Z',
date: '2020-09-06T04:02:32.855Z',
department: 'asdf',
description: 'asdf',
id: 'af9f968f-61d9-47c3-9321-5da3f381c38b',
reportedBy: 'some user',
reportedOn: '2020-09-06T04:02:38.011Z',
rev: '1-91d1ba60588b779c9554c7e20e15419c',
status: 'reported',
updatedAt: '2020-09-06T04:02:38.011Z',
},
]

const expectedExportData = [
{
code: 'I-eClU6OdkR',
date: '2020-09-06 12:02 PM',
reportedBy: 'some user',
reportedOn: '2020-09-06 12:02 PM',
status: 'reported',
},
]

const exportData = [{}]
populateExportData(exportData, data)

expect(exportData).toEqual(expectedExportData)
})

it('should format the data correctly', async () => {
const expectedIncidents: Incident[] = [
{
Expand Down
33 changes: 33 additions & 0 deletions src/__tests__/shared/utils/DataHelpers.test.ts
@@ -0,0 +1,33 @@
import { getCSV, DownloadLink } from '../../../shared/util/DataHelpers'

describe('Use Data Helpers util', () => {
it('should construct csv', () => {
const input = [
{
code: 'I-eClU6OdkR',
date: '2020-09-06 12:02 PM',
reportedBy: 'some user',
reportedOn: '2020-09-06 12:02 PM',
status: 'reported',
},
]
const output = getCSV(input)
const expectedOutput =
'"code","date","reportedBy","reportedOn","status"\r\n"I-eClU6OdkR","2020-09-06 12:02 PM","some user","2020-09-06 12:02 PM","reported"'
expect(output).toMatch(expectedOutput)
})

it('should download data as expected', () => {
const response = DownloadLink('data to be downloaded', 'filename.txt')

const element = document.createElement('a')
element.setAttribute(
'href',
`data:text/plain;charset=utf-8,${encodeURIComponent('data to be downloaded')}`,
)
element.setAttribute('download', 'filename.txt')

element.style.display = 'none'
expect(response).toEqual(element)
})
})
44 changes: 22 additions & 22 deletions src/incidents/list/ViewIncidentsTable.tsx
Expand Up @@ -13,6 +13,27 @@ interface Props {
searchRequest: IncidentSearchRequest
}

export function populateExportData(dataToPopulate: any, theData: any) {
let first = true
if (theData != null) {
theData.forEach((elm: any) => {
const entry = {
code: elm.code,
date: format(new Date(elm.date), 'yyyy-MM-dd hh:mm a'),
reportedBy: elm.reportedBy,
reportedOn: format(new Date(elm.reportedOn), 'yyyy-MM-dd hh:mm a'),
status: elm.status,
}
if (first) {
dataToPopulate[0] = entry
first = false
} else {
dataToPopulate.push(entry)
}
})
}
}

function ViewIncidentsTable(props: Props) {
const { searchRequest } = props
const { t } = useTranslator()
Expand All @@ -26,29 +47,8 @@ function ViewIncidentsTable(props: Props) {
// filter data
const exportData = [{}]

function populateExportData() {
let first = true
if (data != null) {
data.forEach((elm) => {
const entry = {
code: elm.code,
date: format(new Date(elm.date), 'yyyy-MM-dd hh:mm a'),
reportedBy: elm.reportedBy,
reportedOn: format(new Date(elm.reportedOn), 'yyyy-MM-dd hh:mm a'),
status: elm.status,
}
if (first) {
exportData[0] = entry
first = false
} else {
exportData.push(entry)
}
})
}
}

function downloadCSV() {
populateExportData()
populateExportData(exportData, data)

const csv = getCSV(exportData)

Expand Down

0 comments on commit dc3c93e

Please sign in to comment.