From 973c4531d7ff2a7cf63f5379609fec876fdbeb7a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Niels=20Lyngs=C3=B8?= Date: Wed, 28 Jun 2023 14:12:47 +0200 Subject: [PATCH] test controllers --- .../context-provider.controller.test.ts | 6 ++-- .../libs/controller-api/controller.class.ts | 2 +- .../libs/controller-api/controller.test.ts | 36 +++++++++++++------ 3 files changed, 29 insertions(+), 15 deletions(-) diff --git a/src/Umbraco.Web.UI.Client/src/libs/context-api/provide/context-provider.controller.test.ts b/src/Umbraco.Web.UI.Client/src/libs/context-api/provide/context-provider.controller.test.ts index d4efc428a1..559b119efb 100644 --- a/src/Umbraco.Web.UI.Client/src/libs/context-api/provide/context-provider.controller.test.ts +++ b/src/Umbraco.Web.UI.Client/src/libs/context-api/provide/context-provider.controller.test.ts @@ -23,10 +23,10 @@ describe('UmbContextProviderController', () => { describe('Public API', () => { describe('properties', () => { - it('has a unique property', () => { - expect(provider).to.have.property('unique'); + it('has a controllerAlias property', () => { + expect(provider).to.have.property('controllerAlias'); }); - it('has a unique property, is equal to the unique', () => { + it('has a controllerAlias property, is equal to the controllerAlias', () => { expect(provider.controllerAlias).to.eq('my-test-context'); }); }); diff --git a/src/Umbraco.Web.UI.Client/src/libs/controller-api/controller.class.ts b/src/Umbraco.Web.UI.Client/src/libs/controller-api/controller.class.ts index b81e0954b0..c052ddb873 100644 --- a/src/Umbraco.Web.UI.Client/src/libs/controller-api/controller.class.ts +++ b/src/Umbraco.Web.UI.Client/src/libs/controller-api/controller.class.ts @@ -7,7 +7,7 @@ import { UmbController } from './controller.interface.js'; * This enables controllers to be added to the life cycle of this element. * */ -export class UmbBaseController extends UmbClassMixin(class {}) implements UmbController { +export abstract class UmbBaseController extends UmbClassMixin(class {}) implements UmbController { constructor(host: UmbControllerHost, controllerAlias?: UmbController['controllerAlias']) { super(host, controllerAlias); this._host.addController(this); diff --git a/src/Umbraco.Web.UI.Client/src/libs/controller-api/controller.test.ts b/src/Umbraco.Web.UI.Client/src/libs/controller-api/controller.test.ts index aafba695f2..07733bf57e 100644 --- a/src/Umbraco.Web.UI.Client/src/libs/controller-api/controller.test.ts +++ b/src/Umbraco.Web.UI.Client/src/libs/controller-api/controller.test.ts @@ -1,20 +1,17 @@ import { expect } from '@open-wc/testing'; import { UmbControllerHostElement, UmbControllerHostMixin } from './controller-host.mixin.js'; +import { UmbBaseController } from './controller.class.js'; import { customElement } from '@umbraco-cms/backoffice/external/lit'; -import { UmbContextProviderController } from '@umbraco-cms/backoffice/context-api'; - -class UmbTestContext { - prop = 'value from provider'; -} @customElement('test-my-controller-host') export class UmbTestControllerHostElement extends UmbControllerHostMixin(HTMLElement) {} +export class UmbTestControllerImplementationElement extends UmbBaseController {} + describe('UmbContextProvider', () => { type NewType = UmbControllerHostElement; let hostElement: NewType; - const contextInstance = new UmbTestContext(); beforeEach(() => { hostElement = document.createElement('test-my-controller-host') as UmbControllerHostElement; @@ -22,7 +19,7 @@ describe('UmbContextProvider', () => { describe('Destroyed controllers is gone from host', () => { it('controller is removed from host when destroyed', () => { - const ctrl = new UmbContextProviderController(hostElement, 'my-test-context', contextInstance); + const ctrl = new UmbTestControllerImplementationElement(hostElement, 'my-test-context'); expect(hostElement.hasController(ctrl)).to.be.true; @@ -32,13 +29,30 @@ describe('UmbContextProvider', () => { }); }); - describe('Unique controllers replace each other', () => { - it('controller is replaced by another controller using the same unique', () => { - const firstCtrl = new UmbContextProviderController(hostElement, 'my-test-context', contextInstance); - const secondCtrl = new UmbContextProviderController(hostElement, 'my-test-context', new UmbTestContext()); + describe('Controllers against other Controller', () => { + it('controller is replaced by another controller using the same string as alias', () => { + const firstCtrl = new UmbTestControllerImplementationElement(hostElement, 'my-test-context'); + const secondCtrl = new UmbTestControllerImplementationElement(hostElement, 'my-test-context'); expect(hostElement.hasController(firstCtrl)).to.be.false; expect(hostElement.hasController(secondCtrl)).to.be.true; }); + + it('controller is replaced by another controller using the the same symbol as alias', () => { + const mySymbol = Symbol(); + const firstCtrl = new UmbTestControllerImplementationElement(hostElement, mySymbol); + const secondCtrl = new UmbTestControllerImplementationElement(hostElement, mySymbol); + + expect(hostElement.hasController(firstCtrl)).to.be.false; + expect(hostElement.hasController(secondCtrl)).to.be.true; + }); + + it('controller is not replacing another controller when using the undefined as alias', () => { + const firstCtrl = new UmbTestControllerImplementationElement(hostElement, undefined); + const secondCtrl = new UmbTestControllerImplementationElement(hostElement, undefined); + + expect(hostElement.hasController(firstCtrl)).to.be.true; + expect(hostElement.hasController(secondCtrl)).to.be.true; + }); }); });