diff --git a/.run/Unit tests.run.xml b/.run/Unit tests.run.xml index 23543c0d..f698939e 100644 --- a/.run/Unit tests.run.xml +++ b/.run/Unit tests.run.xml @@ -1,5 +1,6 @@ + diff --git a/package.json b/package.json index faa06405..d4e78860 100644 --- a/package.json +++ b/package.json @@ -14,7 +14,7 @@ "lint:prettier": "npx prettier {src/application,src/domain,lib,test,servers}/**/*.{js,vue} --write", "lint:stylelint": "npx stylelint --fix 'src/application/**/*.scss'", "test": "npm run test:unit", - "test:unit": "npx jest", + "test:unit": "npx jest -c jest.config.json", "test:coverage": "npx jest --coverage", "test:coverage:preview": "npm run test:coverage && npx http-server coverage/lcov-report/ -p 8002", "ci": "npm run lint && npm run test", diff --git a/src/domain/user-preferences/preference-entry.util.js b/src/domain/user-preferences/preference-entry.util.js index e6238bb4..a408276e 100644 --- a/src/domain/user-preferences/preference-entry.util.js +++ b/src/domain/user-preferences/preference-entry.util.js @@ -18,6 +18,8 @@ export const applyFiltersToPreferenceEntriesList = (filters, list) => { return list.filter((preferenceEntry) => { const filterMatches = [true]; + if (typeof filters !== 'object' || filters === null) return true; + if (Object.hasOwn(filters, 'label') && filters.label.length !== 0) { filterMatches.push( preferenceEntry.label diff --git a/src/domain/user-preferences/preference-entry.util.spec.js b/src/domain/user-preferences/preference-entry.util.spec.js index 97c88fa8..9dbc97c3 100644 --- a/src/domain/user-preferences/preference-entry.util.spec.js +++ b/src/domain/user-preferences/preference-entry.util.spec.js @@ -11,11 +11,23 @@ const existingConfiguration = [ value: 'value1' } ] + }, + { + id: 'another_existing_configuration_id', + target: 'application', + label: 'Unicorn label 2', + description: 'Description 2', + values: null } ]; jest.mock('@/domain/user-preferences/map.json', () => existingConfiguration); -import { findPreferenceEntryById } from '@/domain/user-preferences/preference-entry.util'; +import { + applyFiltersToPreferenceEntriesList, + convertJsonObjectToPreferenceEntry, + findPreferenceEntryById +} from '@/domain/user-preferences/preference-entry.util'; +import { PreferenceEntry } from '@/domain/user-preferences/PreferenceEntry'; describe('Preference entries', () => { it('An existing preference entry must return data successfully', () => { @@ -32,4 +44,65 @@ describe('Preference entries', () => { `Unable to find ${unknownPreferenceEntryId} on preference entries list` ); }); + + it('An object typed of PreferenceEntry must be returned by the converter successfully', () => { + const anonymousObject = existingConfiguration.at(0); + const convertedObject = convertJsonObjectToPreferenceEntry(anonymousObject); + + expect(convertedObject instanceof PreferenceEntry).toBeTruthy(); + expect(convertedObject).toMatchObject( + new PreferenceEntry( + anonymousObject.id, + anonymousObject.target, + anonymousObject.label, + anonymousObject.description, + anonymousObject.values + ) + ); + }); + + describe('Preference entry filter', () => { + const unknownFilters = [ + [], + {}, + '', + 'string', + 123, + undefined, + null, + new PreferenceEntry(), + { label: '' }, + { target: '' }, + { label: '', target: '' } + ]; + + it.each(unknownFilters)( + 'Preference entries must not have any differences with invalid filters', + (filter) => { + expect( + applyFiltersToPreferenceEntriesList(filter, existingConfiguration) + ).toStrictEqual(existingConfiguration); + } + ); + + const validFilters = [ + { label: 'Unicorn label 2' }, + { label: 'unicorn label 2' }, + { label: 'LaBeL' }, + { label: 'uni' }, + { label: '2' }, + { label: 'uni', target: 'app' }, + { label: 'uni', target: 'application' } + ]; + it.each(validFilters)( + 'A preference entry must be returned with matched filters', + (filter) => { + expect( + applyFiltersToPreferenceEntriesList(filter, existingConfiguration).at( + 0 + ) + ).toStrictEqual(existingConfiguration.at(1)); + } + ); + }); });