From b12c53c8163a3c339b932e07a9d7b1b0c7de8155 Mon Sep 17 00:00:00 2001 From: Alan Date: Wed, 24 Jul 2019 09:06:04 +1000 Subject: [PATCH] [SDPA-1948] Add unit tests for grants utils. (#444) --- .../modules/grant/lib/utils.js | 102 +++++++++++------- .../test/unit/grant-utilities.test.js | 72 +++++++++++++ 2 files changed, 135 insertions(+), 39 deletions(-) create mode 100644 packages/ripple-nuxt-tide/test/unit/grant-utilities.test.js diff --git a/packages/ripple-nuxt-tide/modules/grant/lib/utils.js b/packages/ripple-nuxt-tide/modules/grant/lib/utils.js index 56925bd2d..47711119e 100644 --- a/packages/ripple-nuxt-tide/modules/grant/lib/utils.js +++ b/packages/ripple-nuxt-tide/modules/grant/lib/utils.js @@ -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 @@ -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) { @@ -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 } } diff --git a/packages/ripple-nuxt-tide/test/unit/grant-utilities.test.js b/packages/ripple-nuxt-tide/test/unit/grant-utilities.test.js new file mode 100644 index 000000000..fd0e15b6d --- /dev/null +++ b/packages/ripple-nuxt-tide/test/unit/grant-utilities.test.js @@ -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) + }) +})