From 344b7731ce9c5213ddbafa6f792625f8a6d34344 Mon Sep 17 00:00:00 2001 From: Sebastiaan Janssen Date: Mon, 22 Apr 2019 12:10:53 +0200 Subject: [PATCH] Fix JS unit tests --- .../src/common/services/tree.service.js | 12 ++++++++++-- .../test/unit/common/services/tree-service.spec.js | 6 +++--- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/src/Umbraco.Web.UI.Client/src/common/services/tree.service.js b/src/Umbraco.Web.UI.Client/src/common/services/tree.service.js index 73893739a6..d61d1c3ba1 100644 --- a/src/Umbraco.Web.UI.Client/src/common/services/tree.service.js +++ b/src/Umbraco.Web.UI.Client/src/common/services/tree.service.js @@ -29,6 +29,15 @@ function treeService($q, treeResource, iconHelper, notificationsService, eventsS return cacheKey; } + // Adapted from: https://stackoverflow.com/a/2140723 + // Please note, we can NOT test this functionality correctly in Phantom because it implements + // the localeCompare method incorrectly: https://github.com/ariya/phantomjs/issues/11063 + function invariantEquals(a, b) { + return typeof a === "string" && typeof b === "string" + ? a.localeCompare(b, undefined, { sensitivity: "base" }) === 0 + : a === b; + } + return { /** Internal method to return the tree cache */ @@ -166,8 +175,7 @@ function treeService($q, treeResource, iconHelper, notificationsService, eventsS angular.isArray(Umbraco.Sys.ServerVariables.umbracoPlugins.trees)) { var found = _.find(Umbraco.Sys.ServerVariables.umbracoPlugins.trees, function (item) { - // localeCompare returns 0 when strings are equal, so return false if there's no match - return item.alias.localeCompare(treeAlias, undefined, { ignorePunctuation: true }) !== 0; + return invariantEquals(item.alias, treeAlias); }); return found ? found.packageFolder : undefined; diff --git a/src/Umbraco.Web.UI.Client/test/unit/common/services/tree-service.spec.js b/src/Umbraco.Web.UI.Client/test/unit/common/services/tree-service.spec.js index c2ad072078..4d19cf557a 100644 --- a/src/Umbraco.Web.UI.Client/test/unit/common/services/tree-service.spec.js +++ b/src/Umbraco.Web.UI.Client/test/unit/common/services/tree-service.spec.js @@ -303,7 +303,7 @@ describe('tree service tests', function () { }); it('returns undefined for a not found tree', function () { - //we know this exists in the mock umbraco server vars + //we know this does not exist in the mock umbraco server vars var found = treeService.getTreePackageFolder("asdfasdf"); expect(found).not.toBeDefined(); }); @@ -315,8 +315,8 @@ describe('tree service tests', function () { it('hasChildren has to be updated on parent', function () { var tree = getContentTree(); - while (tree.children.length > 0){ - treeService.removeNode(tree.children[0]) + while (tree.children.length > 0) { + treeService.removeNode(tree.children[0]); } expect(tree.hasChildren).toBe(false);