move context provider and host elements into src/core folder to avoid exposing them to the public

This commit is contained in:
Jacob Overgaard
2023-03-07 15:30:57 +01:00
parent a028bfd8f8
commit 8d47bf2c6c
8 changed files with 21 additions and 18 deletions

View File

@@ -1,38 +0,0 @@
import { expect, fixture, html } from '@open-wc/testing';
import { customElement } from 'lit/decorators.js';
import { UmbContextProviderElement } from './context-provider.element';
import { UmbLitElement } from './lit-element.element';
@customElement('umb-context-test')
export class ContextTestElement extends UmbLitElement {
public value: string | null = null;
constructor() {
super();
this.consumeContext<string>('test-context', (value) => {
this.value = value;
});
}
}
describe('UmbContextProvider', () => {
let element: UmbContextProviderElement;
let consumer: ContextTestElement;
const contextValue = 'test-value';
beforeEach(async () => {
element = await fixture(
html` <umb-context-provider key="test-context" .value=${contextValue}>
<umb-context-test></umb-context-test>
</umb-context-provider>`
);
consumer = element.getElementsByTagName('umb-context-test')[0] as ContextTestElement;
});
it('is defined with its own instance', () => {
expect(element).to.be.instanceOf(UmbContextProviderElement);
});
it('provides the context', () => {
expect(consumer.value).to.equal(contextValue);
});
});

View File

@@ -1,46 +0,0 @@
import { html } from 'lit';
import { customElement, property } from 'lit/decorators.js';
import { UmbLitElement } from './lit-element.element';
import type { UmbControllerHostInterface } from '@umbraco-cms/controller';
@customElement('umb-context-provider')
export class UmbContextProviderElement extends UmbLitElement {
/**
* The value to provide to the context.
* @required
*/
@property({ type: Object, attribute: false })
create?: (host:UmbControllerHostInterface) => unknown;
/**
* The value to provide to the context.
* @required
*/
@property({ type: Object })
value: unknown;
/**
* The key to provide to the context.
* @required
*/
@property({ type: String })
key!: string;
connectedCallback() {
super.connectedCallback();
if (!this.key) {
throw new Error('The key property is required.');
}
if (this.create) {
this.value = this.create(this);
} else if (!this.value) {
throw new Error('The value property is required.');
}
this.provideContext(this.key, this.value);
}
render() {
return html`<slot></slot>`;
}
}

View File

@@ -1,44 +0,0 @@
import { expect, fixture, html } from '@open-wc/testing';
import { customElement } from 'lit/decorators.js';
import { UmbControllerHostTestElement } from './controller-host.element';
import { UmbLitElement } from './lit-element.element';
import { UmbContextProviderController } from '@umbraco-cms/context-api';
import { UmbControllerHostInterface } from '@umbraco-cms/controller';
@customElement('umb-controller-host-test-consumer')
export class ControllerHostTestConsumerElement extends UmbLitElement {
public value: string | null = null;
constructor() {
super();
this.consumeContext<string>('my-test-context-alias', (value) => {
this.value = value;
});
}
}
describe('UmbControllerHostTestElement', () => {
let element: UmbControllerHostTestElement;
let consumer: ControllerHostTestConsumerElement;
const contextValue = 'test-value';
beforeEach(async () => {
element = await fixture(
html` <umb-controller-host-test
.create=${(host: UmbControllerHostInterface) =>
new UmbContextProviderController(host, 'my-test-context-alias', contextValue)}>
<umb-controller-host-test-consumer></umb-controller-host-test-consumer>
</umb-controller-host-test>`
);
consumer = element.getElementsByTagName(
'umb-controller-host-test-consumer'
)[0] as ControllerHostTestConsumerElement;
});
it('element is defined with its own instance', () => {
expect(element).to.be.instanceOf(UmbControllerHostTestElement);
});
it('provides the context', () => {
expect(consumer.value).to.equal(contextValue);
});
});

View File

@@ -1,31 +0,0 @@
import { html } from 'lit';
import { customElement, property } from 'lit/decorators.js';
import { UmbLitElement } from './lit-element.element';
import type { UmbControllerHostInterface } from '@umbraco-cms/controller';
@customElement('umb-controller-host-test')
export class UmbControllerHostTestElement extends UmbLitElement {
/**
* A way to initialize controllers.
* @required
*/
@property({ type: Object, attribute: false })
create?: (host: UmbControllerHostInterface) => void;
connectedCallback() {
super.connectedCallback();
if (this.create) {
this.create(this);
}
}
render() {
return html`<slot></slot>`;
}
}
declare global {
interface HTMLElementTagNameMap {
'umb-controller-host-test': UmbControllerHostTestElement;
}
}

View File

@@ -1 +0,0 @@
export * from './element.mixin';

View File

@@ -1,4 +1,2 @@
export * from './element.mixin';
export * from './lit-element.element';
export * from './context-provider.element';
export * from './controller-host.element';