test controllers

This commit is contained in:
Niels Lyngsø
2023-06-28 14:12:47 +02:00
parent 0d54616034
commit 973c4531d7
3 changed files with 29 additions and 15 deletions

View File

@@ -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');
});
});

View File

@@ -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);

View File

@@ -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;
});
});
});