Skip to content

Commit

Permalink
#133 add unit test for preference entries loader
Browse files Browse the repository at this point in the history
  • Loading branch information
JAGFx committed Aug 29, 2022
1 parent 5d4eefc commit 8a67dcc
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 30 deletions.
23 changes: 22 additions & 1 deletion src/jagfx/core/configuration/preference-entry/filter.spec.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,28 @@
import { existingConfiguration } from "./preference-entry.mock.js";
import { PreferenceEntry } from "@/jagfx/core/configuration/preference-entry/PreferenceEntry";
import { applyFiltersToPreferenceEntriesList } from "@/jagfx/core/configuration/preference-entry/filter";

const existingConfiguration = [
{
id: "an_existing_configuration_id",
target: "application",
label: "Configuration 1",
description: "Description 1",
values: [
{
label: "Value 1",
value: "value1",
},
],
},
{
id: "another_existing_configuration_id",
target: "application",
label: "Unicorn label 2",
description: "Description 2",
values: null,
},
];

describe("Preference entry filter", () => {
const unknownFilters = [
[],
Expand Down
28 changes: 22 additions & 6 deletions src/jagfx/core/configuration/preference-entry/finder.spec.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,31 @@
import PreferenceEntryMock from "./preference-entry.mock.js";
import { PreferenceEntry } from "@/jagfx/core/configuration/preference-entry/PreferenceEntry";
import {
convertJsonObjectToPreferenceEntry,
findPreferenceEntryById,
} from "@/jagfx/core/configuration/preference-entry/finder";
import {
existingConfiguration,
existingConfigurationId,
} from "@/jagfx/core/configuration/preference-entry/preference-entry.mock";

// TODO Find a way to use an external mock file
const existingConfigurationId = "an_existing_configuration_id";
const existingConfiguration = [
{
id: "an_existing_configuration_id",
target: "application",
label: "Configuration 1",
description: "Description 1",
values: [
{
label: "Value 1",
value: "value1",
},
],
},
{
id: "another_existing_configuration_id",
target: "application",
label: "Unicorn label 2",
description: "Description 2",
values: null,
},
];

jest.mock(
"@/jagfx/core/configuration/preference-entry/map.json",
Expand Down
13 changes: 12 additions & 1 deletion src/jagfx/core/configuration/preference-entry/loader.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
export const loadPreferenceEntryValues = (preferenceEntry) => {
if (
Array.isArray(preferenceEntry) ||
preferenceEntry === null ||
typeof preferenceEntry !== "object" ||
!Array.isArray(preferenceEntry.values) ||
!Object.hasOwnProperty.call(preferenceEntry, "values") ||
preferenceEntry.values === null
) {
return null;
}

if (
Array.isArray(preferenceEntry.values) &&
preferenceEntry.values.length > 0
Expand All @@ -17,4 +28,4 @@ export const loadPreferenceEntryValues = (preferenceEntry) => {
return null;
};

// TODO Add test for it + tast to check all entry on map.json file
// TODO Add test for it + tast to check all entry on map.json file
36 changes: 36 additions & 0 deletions src/jagfx/core/configuration/preference-entry/loader.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { loadPreferenceEntryValues } from "@/jagfx/core/configuration/preference-entry/loader";

const mockPreferenceEntry = (values) => ({
values,
});

describe("Preference entries finder", () => {
// Array not empty > Return this array
// Array empty > Perform external request
// Null > No value, return null
it("Should return the same array if preference entry value is an array not empty", () => {
const preferenceEntry = mockPreferenceEntry(["some", "stuff"]);

expect(loadPreferenceEntryValues(preferenceEntry)).toBe(
preferenceEntry.values
);
});

const invalidPreferenceEntries = [
{},
"",
"string",
123,
undefined,
null,
new Event(""),
{ label: "" },
mockPreferenceEntry(null),
];
it.each(invalidPreferenceEntries)(
"Should return null with invalid preferenceEntry",
(invalidPreferenceEntry) => {
expect(loadPreferenceEntryValues(invalidPreferenceEntry)).toBeNull();
}
);
});

This file was deleted.

0 comments on commit 8a67dcc

Please sign in to comment.