Skip to content

Commit

Permalink
[SDPA-1948] Add unit tests for grants utils. (#444)
Browse files Browse the repository at this point in the history
  • Loading branch information
alan-cole authored and tim-yao committed Jul 23, 2019
1 parent 0f460ae commit b12c53c
Show file tree
Hide file tree
Showing 2 changed files with 135 additions and 39 deletions.
102 changes: 63 additions & 39 deletions packages/ripple-nuxt-tide/modules/grant/lib/utils.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
const utils = {

/**
* Format an audience array into a human readable format.
* @param {Array} audiences array of strings or { name: '' }
* @return {String} formatted audience.
*/
formatAudiences: (audiences) => {
const formatAudience = (input) => {
const term = typeof input === 'string' ? input : input.name
Expand All @@ -19,15 +25,30 @@ const utils = {
}
return ''
},

/**
* Format a file size (in bytes) to human readable format.
* @param {Number} fileSize number in bytes
* @return {String} formatted file size.
*/
formattedSize: (fileSize) => {
// https://stackoverflow.com/a/18650828
if (typeof fileSize === 'string') return fileSize
if (fileSize === 0) return '0 Bytes'
const k = 1024
const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']
const i = Math.floor(Math.log(fileSize) / Math.log(k))
return parseFloat((fileSize / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i]
if (fileSize != null) {
// https://stackoverflow.com/a/18650828
if (typeof fileSize === 'string') return fileSize
if (fileSize === 0) return '0 Bytes'
const k = 1024
const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']
const i = Math.floor(Math.log(fileSize) / Math.log(k))
return parseFloat((fileSize / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i]
}
return ''
},

/**
* Includes additional seach filter values based on field_status.
* @param {Object} filters search filters object
* @return {Object} filters object
*/
getGrantsFilters: (filters) => {
const getOperator = (status) => {
switch (status) {
Expand All @@ -46,42 +67,45 @@ const utils = {
}
}

for (let filter in filters) {
if (filter === 'field_status') {
filters[filter].operator = getOperator(filters[filter].values[0])
if (filters) {
for (let filter in filters) {
if (filter === 'field_status') {
filters[filter].operator = getOperator(filters[filter].values[0])

switch (filters[filter].values[0]) {
case 'Open':
// field_node_date_end_value > now && field_node_date_start_value < now
filters.field_node_dates_start_value = JSON.parse(JSON.stringify(filters[filter]))
filters.field_node_dates_start_value.values = 'now'
filters.field_node_dates_start_value.operator = 'lte'
// Add an additional filter for end date.
filters.field_node_dates_end_value = JSON.parse(JSON.stringify(filters[filter]))
filters.field_node_dates_end_value.values = 'now'
filters.field_node_dates_end_value.operator = 'gte'
break
case 'Closed':
// field_node_date_end_value < now
filters.field_node_dates_end_value = JSON.parse(JSON.stringify(filters[filter]))
filters.field_node_dates_end_value.values = 'now'
break
case 'Ongoing':
// field_node_on_going === true
filters.field_node_on_going = {
type: 'term',
values: 'true'
}
break
case 'Opening soon':
// field_node_date_start_value > now
filters.field_node_dates_start_value = JSON.parse(JSON.stringify(filters[filter]))
filters.field_node_dates_start_value.values = 'now'
break
switch (filters[filter].values[0]) {
case 'Open':
// field_node_date_end_value > now && field_node_date_start_value < now
filters.field_node_dates_start_value = JSON.parse(JSON.stringify(filters[filter]))
filters.field_node_dates_start_value.values = 'now'
filters.field_node_dates_start_value.operator = 'lte'
// Add an additional filter for end date.
filters.field_node_dates_end_value = JSON.parse(JSON.stringify(filters[filter]))
filters.field_node_dates_end_value.values = 'now'
filters.field_node_dates_end_value.operator = 'gte'
break
case 'Closed':
// field_node_date_end_value < now
filters.field_node_dates_end_value = JSON.parse(JSON.stringify(filters[filter]))
filters.field_node_dates_end_value.values = 'now'
break
case 'Ongoing':
// field_node_on_going === true
filters.field_node_on_going = {
type: 'term',
values: 'true'
}
break
case 'Opening soon':
// field_node_date_start_value > now
filters.field_node_dates_start_value = JSON.parse(JSON.stringify(filters[filter]))
filters.field_node_dates_start_value.values = 'now'
break
}
}
}
return filters
}
return filters
return null
}
}

Expand Down
72 changes: 72 additions & 0 deletions packages/ripple-nuxt-tide/test/unit/grant-utilities.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import utils from '../../modules/grant/lib/utils.js'

describe('Grant utilities', () => {
test('should return formatted audience text', async () => {
const unformattedAudiences = [
{ name: 'Individual' },
'Business',
'Not-for-profit groups'
]
// Supports empty parameters
expect(utils.formatAudiences()).toEqual('')
expect(utils.formatAudiences([])).toEqual('')
expect(utils.formatAudiences([{ name: null }])).toEqual('')
// Formats a correct parameter
expect(utils.formatAudiences(unformattedAudiences)).toEqual('Individuals, businesses, not-for-profit groups')
})

test('should return formatted size', async () => {
// Supports empty parameters
expect(utils.formattedSize()).toEqual('')
// Formats a correct parameter
expect(utils.formattedSize('Custom text')).toEqual('Custom text')
expect(utils.formattedSize(0)).toEqual('0 Bytes')
expect(utils.formattedSize(1)).toEqual('1 Bytes')
expect(utils.formattedSize(Math.pow(1024, 1))).toEqual('1 KB')
expect(utils.formattedSize(Math.pow(1024, 2))).toEqual('1 MB')
expect(utils.formattedSize(Math.pow(1024, 3))).toEqual('1 GB')
expect(utils.formattedSize(Math.pow(1024, 4))).toEqual('1 TB')
expect(utils.formattedSize(Math.pow(1024, 5))).toEqual('1 PB')
expect(utils.formattedSize(Math.pow(1024, 6))).toEqual('1 EB')
expect(utils.formattedSize(Math.pow(1024, 7))).toEqual('1 ZB')
expect(utils.formattedSize(Math.pow(1024, 8))).toEqual('1 YB')
})

test('should return grants filters with search parameters', async () => {
const noStateInput = { field_topic_name: { type: 'term', operator: '', values: ['Business'] } }

const stateOpenInput = { field_status: { type: 'date', operator: '', values: ['Open'] } }
const stateOpenOutput = {
field_status: { type: 'date', operator: 'lte', values: ['Open'] },
field_node_dates_start_value: { type: 'date', operator: 'lte', values: 'now' },
field_node_dates_end_value: { type: 'date', operator: 'gte', values: 'now' }
}

const stateClosedInput = { field_status: { type: 'date', operator: '', values: ['Closed'] } }
const stateClosedOutput = {
field_status: { type: 'date', operator: 'lte', values: ['Closed'] },
field_node_dates_end_value: { type: 'date', operator: 'lte', values: 'now' }
}

const stateOngoingInput = { field_status: { type: 'date', operator: '', values: ['Ongoing'] } }
const stateOngoingOutput = {
field_status: { type: 'date', operator: '', values: ['Ongoing'] },
field_node_on_going: { type: 'term', values: 'true' }
}

const stateOpeningSoonInput = { field_status: { type: 'date', operator: '', values: ['Opening soon'] } }
const stateOpeningSoonOutput = {
field_status: { type: 'date', operator: 'gte', values: ['Opening soon'] },
field_node_dates_start_value: { type: 'date', operator: 'gte', values: 'now' }
}

// Supports empty parameters
expect(utils.getGrantsFilters()).toEqual(null)
expect(utils.getGrantsFilters(noStateInput)).toEqual(noStateInput)
// Get filters for a correct parameter
expect(utils.getGrantsFilters(stateOpenInput)).toEqual(stateOpenOutput)
expect(utils.getGrantsFilters(stateClosedInput)).toEqual(stateClosedOutput)
expect(utils.getGrantsFilters(stateOngoingInput)).toEqual(stateOngoingOutput)
expect(utils.getGrantsFilters(stateOpeningSoonInput)).toEqual(stateOpeningSoonOutput)
})
})

0 comments on commit b12c53c

Please sign in to comment.