diff --git a/migrations/settings/0001-migrate-links-setting.js b/migrations/settings/0001-migrate-links-setting.js index 11d17ac..462afac 100644 --- a/migrations/settings/0001-migrate-links-setting.js +++ b/migrations/settings/0001-migrate-links-setting.js @@ -2,29 +2,25 @@ export default function migrate(settings) { const oldSetting = settings.get("links"); if (oldSetting) { - const newSetting = oldSetting.split("|").map((link) => { - let [name, url, target] = link.split(",").map((s) => s.trim()); - - if (["_blank", "_self", "_parent", "_top"].indexOf(target) === -1) { - target = "_blank"; - } + const newLinks = []; - const newLink = { - name, - url, - target - } + oldSetting.split("|").forEach((link) => { + let [name, url, target] = link.split(",").map((s) => s.trim()); - Object.keys(newLink).forEach((key) => { - if (newLink[key] === undefined) { - delete newLink[key]; + if (name && url) { + if (["_blank", "_self", "_parent", "_top"].indexOf(target) === -1) { + target = "_blank"; } - }); - return newLink; - }) + newLinks.push({ + name, + url, + target, + }); + } + }); - settings.set("links", newSetting); + settings.set("links", newLinks); } return settings; diff --git a/migrations/settings/0002-migrate-icons-setting.js b/migrations/settings/0002-migrate-icons-setting.js index 9693e9b..580cf97 100644 --- a/migrations/settings/0002-migrate-icons-setting.js +++ b/migrations/settings/0002-migrate-icons-setting.js @@ -2,29 +2,25 @@ export default function migrate(settings) { const oldSetting = settings.get("icons"); if (oldSetting) { - const newSetting = oldSetting.split("|").map((link) => { - let [iconName, url, target] = link.split(",").map((s) => s.trim()); - - if (["_blank", "_self", "_parent", "_top"].indexOf(target) === -1) { - target = "_blank"; - } + const newIcons = []; - const newLink = { - icon_name: iconName, - url, - target - } + oldSetting.split("|").map((link) => { + let [iconName, url, target] = link.split(",").map((s) => s.trim()); - Object.keys(newLink).forEach((key) => { - if (newLink[key] === undefined) { - delete newLink[key]; + if (iconName && url) { + if (["_blank", "_self", "_parent", "_top"].indexOf(target) === -1) { + target = "_blank"; } - }); - return newLink; - }) + newIcons.push({ + icon_name: iconName, + url, + target, + }); + } + }); - settings.set("icons", newSetting); + settings.set("icons", newIcons); } return settings; diff --git a/test/unit/migrations/settings/0001-migrate-links-setting-test.js b/test/unit/migrations/settings/0001-migrate-links-setting-test.js index e4b27fc..c78a36f 100644 --- a/test/unit/migrations/settings/0001-migrate-links-setting-test.js +++ b/test/unit/migrations/settings/0001-migrate-links-setting-test.js @@ -4,6 +4,27 @@ import migrate from "../../../../migrations/settings/0001-migrate-links-setting" module( "Unit | Migrations | Settings | 0001-migrate-links-setting", function () { + test("migrate when old setting is of an invalid format", function (assert) { + const settings = new Map( + Object.entries({ + links: "some||another", + }) + ); + + const result = migrate(settings); + + const expectedResult = new Map( + Object.entries({ + links: [], + }) + ); + + assert.deepEqual( + Object.fromEntries(result.entries()), + Object.fromEntries(expectedResult.entries()) + ); + }); + test("migrate", function (assert) { const settings = new Map( Object.entries({ diff --git a/test/unit/migrations/settings/0002-migrate-icons-setting-test.js b/test/unit/migrations/settings/0002-migrate-icons-setting-test.js index 1ec7cc9..cad7538 100644 --- a/test/unit/migrations/settings/0002-migrate-icons-setting-test.js +++ b/test/unit/migrations/settings/0002-migrate-icons-setting-test.js @@ -4,6 +4,27 @@ import migrate from "../../../../migrations/settings/0002-migrate-icons-setting" module( "Unit | Migrations | Settings | 0002-migrate-icons-setting", function () { + test("migrate when old setting value is of an invalid format", function (assert) { + const settings = new Map( + Object.entries({ + icons: "some||another", + }) + ); + + const result = migrate(settings); + + const expectedResult = new Map( + Object.entries({ + icons: [], + }) + ); + + assert.deepEqual( + Object.fromEntries(result.entries()), + Object.fromEntries(expectedResult.entries()) + ); + }); + test("migrate", function (assert) { const settings = new Map( Object.entries({ @@ -36,7 +57,10 @@ module( }) ); - assert.deepEqual(result, expectedResult); + assert.deepEqual( + Object.fromEntries(result.entries()), + Object.fromEntries(expectedResult.entries()) + ); }); } );