From 4c6e50439d89a558be0e94026a2acdf9598f8ff1 Mon Sep 17 00:00:00 2001 From: Virginie Ky Date: Fri, 28 Feb 2020 10:53:08 +0100 Subject: [PATCH] override dateFormats and getFilterType tests Signed-off-by: Virginie Ky --- .../lib/src/utils/tests/getFilterType.test.js | 160 ++++++++++++++++++ .../admin/src/components/CustomTable/Row.js | 11 +- .../admin/src/containers/ListView/Filter.js | 7 +- .../admin/src/utils/formats.js | 12 ++ 4 files changed, 180 insertions(+), 10 deletions(-) create mode 100644 packages/strapi-helper-plugin/lib/src/utils/tests/getFilterType.test.js create mode 100644 packages/strapi-plugin-content-manager/admin/src/utils/formats.js diff --git a/packages/strapi-helper-plugin/lib/src/utils/tests/getFilterType.test.js b/packages/strapi-helper-plugin/lib/src/utils/tests/getFilterType.test.js new file mode 100644 index 0000000000..7c4bebbe12 --- /dev/null +++ b/packages/strapi-helper-plugin/lib/src/utils/tests/getFilterType.test.js @@ -0,0 +1,160 @@ +import getFilterType from '../getFilterType'; + +describe('HELPER PLUGIN | utils | getFilterType', () => { + describe('Text types', () => { + const expected = [ + { + id: 'components.FilterOptions.FILTER_TYPES.=', + value: '=', + }, + { + id: 'components.FilterOptions.FILTER_TYPES._ne', + value: '_ne', + }, + { + id: 'components.FilterOptions.FILTER_TYPES._lt', + value: '_lt', + }, + { + id: 'components.FilterOptions.FILTER_TYPES._lte', + value: '_lte', + }, + { + id: 'components.FilterOptions.FILTER_TYPES._gt', + value: '_gt', + }, + { + id: 'components.FilterOptions.FILTER_TYPES._gte', + value: '_gte', + }, + { + id: 'components.FilterOptions.FILTER_TYPES._contains', + value: '_contains', + }, + { + id: 'components.FilterOptions.FILTER_TYPES._containss', + value: '_containss', + }, + { + id: 'components.FilterOptions.FILTER_TYPES._in', + value: '_in', + }, + { + id: 'components.FilterOptions.FILTER_TYPES._nin', + value: '_nin', + }, + ]; + + it('should generate the expected array if type is text', () => { + const type = 'text'; + expect(getFilterType(type)).toEqual(expected); + }); + + it('should generate the expected array if type is string', () => { + const type = 'string'; + expect(getFilterType(type)).toEqual(expected); + }); + + it('should generate the expected array if type is password', () => { + const type = 'password'; + expect(getFilterType(type)).toEqual(expected); + }); + + it('should generate the expected array if type is email', () => { + const type = 'email'; + expect(getFilterType(type)).toEqual(expected); + }); + }); + + describe('Number and timestamp types', () => { + const expected = [ + { + id: 'components.FilterOptions.FILTER_TYPES.=', + value: '=', + }, + { + id: 'components.FilterOptions.FILTER_TYPES._ne', + value: '_ne', + }, + { + id: 'components.FilterOptions.FILTER_TYPES._lt', + value: '_lt', + }, + { + id: 'components.FilterOptions.FILTER_TYPES._lte', + value: '_lte', + }, + { + id: 'components.FilterOptions.FILTER_TYPES._gt', + value: '_gt', + }, + { + id: 'components.FilterOptions.FILTER_TYPES._gte', + value: '_gte', + }, + ]; + + it('should generate the expected array if type is integer', () => { + const type = 'integer'; + expect(getFilterType(type)).toEqual(expected); + }); + + it('should generate the expected array if type is biginteger', () => { + const type = 'biginteger'; + expect(getFilterType(type)).toEqual(expected); + }); + + it('should generate the expected array if type is float', () => { + const type = 'float'; + expect(getFilterType(type)).toEqual(expected); + }); + + it('should generate the expected array if type is decimal', () => { + const type = 'decimal'; + expect(getFilterType(type)).toEqual(expected); + }); + + it('should generate the expected array if type is date', () => { + const type = 'date'; + expect(getFilterType(type)).toEqual(expected); + }); + + it('should generate the expected array if type is datetime', () => { + const type = 'datetime'; + expect(getFilterType(type)).toEqual(expected); + }); + + it('should generate the expected array if type is time', () => { + const type = 'time'; + expect(getFilterType(type)).toEqual(expected); + }); + + it('should generate the expected array if type is timestamp', () => { + const type = 'timestamp'; + expect(getFilterType(type)).toEqual(expected); + }); + + it('should generate the expected array if type is timestampUpdate', () => { + const type = 'timestampUpdate'; + expect(getFilterType(type)).toEqual(expected); + }); + }); + + describe('Other types', () => { + const expected = [ + { + id: 'components.FilterOptions.FILTER_TYPES.=', + value: '=', + }, + { + id: 'components.FilterOptions.FILTER_TYPES._ne', + value: '_ne', + }, + ]; + + it('should generate the expected array if type is size', () => { + const type = 'size'; + expect(getFilterType(type)).toEqual(expected); + }); + }); +}); diff --git a/packages/strapi-plugin-content-manager/admin/src/components/CustomTable/Row.js b/packages/strapi-plugin-content-manager/admin/src/components/CustomTable/Row.js index 6cff5cf711..7a20ef340f 100644 --- a/packages/strapi-plugin-content-manager/admin/src/components/CustomTable/Row.js +++ b/packages/strapi-plugin-content-manager/admin/src/components/CustomTable/Row.js @@ -3,12 +3,9 @@ import { withRouter } from 'react-router'; import PropTypes from 'prop-types'; import { get, isEmpty, isNull, isObject, toLower, toString } from 'lodash'; import moment from 'moment'; -import { - IcoContainer, - useGlobalContext, - dateFormats, -} from 'strapi-helper-plugin'; +import { IcoContainer, useGlobalContext } from 'strapi-helper-plugin'; import useListView from '../../hooks/useListView'; +import formats from '../../utils/formats'; import CustomInputCheckbox from '../CustomInputCheckbox'; import MediaPreviewList from '../MediaPreviewList'; import { ActionContainer, Truncate, Truncated } from './styledComponents'; @@ -45,7 +42,7 @@ const getDisplayedValue = (type, value, name) => { ? JSON.stringify(value) : value; - return dateToUtcTime(date).format(dateFormats[type]); + return dateToUtcTime(date).format(formats[type]); } case 'password': return '••••••••'; @@ -66,7 +63,7 @@ const getDisplayedValue = (type, value, name) => { }; const date = moment().set(timeObj); - return date.format(dateFormats.time); + return date.format(formats.time); } default: return '-'; diff --git a/packages/strapi-plugin-content-manager/admin/src/containers/ListView/Filter.js b/packages/strapi-plugin-content-manager/admin/src/containers/ListView/Filter.js index c4ebc978bd..cb8b2054e3 100644 --- a/packages/strapi-plugin-content-manager/admin/src/containers/ListView/Filter.js +++ b/packages/strapi-plugin-content-manager/admin/src/containers/ListView/Filter.js @@ -2,7 +2,8 @@ import React from 'react'; import PropTypes from 'prop-types'; import { get, toString } from 'lodash'; import moment from 'moment'; -import { dateFormats, FilterButton } from 'strapi-helper-plugin'; +import { FilterButton } from 'strapi-helper-plugin'; +import formats from '../../utils/formats'; function Filter({ changeParams, @@ -24,9 +25,9 @@ function Filter({ let format; if (type === 'date' || type === 'timestamp') { - format = dateFormats.date; + format = formats.date; } else { - format = dateFormats.datetime; + format = formats.datetime; } displayedValue = moment diff --git a/packages/strapi-plugin-content-manager/admin/src/utils/formats.js b/packages/strapi-plugin-content-manager/admin/src/utils/formats.js new file mode 100644 index 0000000000..566f253d54 --- /dev/null +++ b/packages/strapi-plugin-content-manager/admin/src/utils/formats.js @@ -0,0 +1,12 @@ +import { dateFormats } from 'strapi-helper-plugin'; + +const formats = { + ...dateFormats, + // Customise the format by uncommenting the one you wan to override it corresponds to the type of your field + // date: 'dddd, MMMM Do YYYY', + // datetime: 'dddd, MMMM Do YYYY HH:mm', + // time: 'HH:mm A', + // timestamp: 'dddd, MMMM Do YYYY HH:mm', +}; + +export default formats;