Skip to content

Commit

Permalink
#133 add unit test for preference entries domain function
Browse files Browse the repository at this point in the history
  • Loading branch information
JAGFx committed Jul 29, 2022
1 parent 9920e9a commit 63a587d
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 2 deletions.
1 change: 1 addition & 0 deletions .run/Unit tests.run.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="Unit tests" type="JavaScriptTestRunnerJest">
<config-file value="$PROJECT_DIR$/jest.config.json" />
<node-interpreter value="project" />
<node-options value="" />
<jest-package value="$PROJECT_DIR$/node_modules/jest" />
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 2 additions & 0 deletions src/domain/user-preferences/preference-entry.util.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
75 changes: 74 additions & 1 deletion src/domain/user-preferences/preference-entry.util.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand All @@ -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));
}
);
});
});

0 comments on commit 63a587d

Please sign in to comment.