Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SDPA-1948] Add unit tests for grants utils. #444

Merged
merged 2 commits into from
Jul 23, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
})
})