add + fix tests
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import { expect, oneEvent } from '@open-wc/testing';
|
||||
import { ContextAlias } from '../context-token';
|
||||
import { UmbContextProvider } from '../provide/context-provider';
|
||||
import { UmbContextConsumer } from './context-consumer';
|
||||
import { UmbContextRequestEventImplementation, umbContextRequestEventType } from './context-request.event';
|
||||
@@ -45,7 +46,24 @@ describe('UmbContextConsumer', () => {
|
||||
const element = document.createElement('div');
|
||||
document.body.appendChild(element);
|
||||
|
||||
const localConsumer = new UmbContextConsumer(element, testContextAlias, (_instance) => {
|
||||
const localConsumer = new UmbContextConsumer(element, testContextAlias, (_instance: MyClass) => {
|
||||
expect(_instance.prop).to.eq('value from provider');
|
||||
done();
|
||||
});
|
||||
localConsumer.hostConnected();
|
||||
|
||||
provider.hostDisconnected();
|
||||
});
|
||||
|
||||
it('works with ContextAlias', (done) => {
|
||||
const CONTEXT_ALIAS = new ContextAlias<MyClass>(testContextAlias);
|
||||
const provider = new UmbContextProvider(document.body, CONTEXT_ALIAS, new MyClass());
|
||||
provider.hostConnected();
|
||||
|
||||
const element = document.createElement('div');
|
||||
document.body.appendChild(element);
|
||||
|
||||
const localConsumer = new UmbContextConsumer(element, CONTEXT_ALIAS, (_instance) => {
|
||||
expect(_instance.prop).to.eq('value from provider');
|
||||
done();
|
||||
});
|
||||
|
||||
@@ -7,8 +7,8 @@ import { UmbContextRequestEventImplementation, UmbContextCallback } from './cont
|
||||
* @class UmbContextConsumer
|
||||
*/
|
||||
export class UmbContextConsumer<HostType extends EventTarget = EventTarget, T = unknown> {
|
||||
private _instance?: unknown;
|
||||
get instance(): unknown | undefined {
|
||||
private _instance?: T;
|
||||
get instance() {
|
||||
return this._instance;
|
||||
}
|
||||
|
||||
@@ -25,11 +25,11 @@ export class UmbContextConsumer<HostType extends EventTarget = EventTarget, T =
|
||||
*/
|
||||
constructor(
|
||||
protected host: HostType,
|
||||
protected _contextAlias: string | ContextAlias,
|
||||
protected _contextAlias: string | ContextAlias<T>,
|
||||
private _callback: UmbContextCallback<T>
|
||||
) {}
|
||||
|
||||
private _onResponse = (instance: any) => {
|
||||
private _onResponse = (instance: T) => {
|
||||
this._instance = instance;
|
||||
this._callback(instance);
|
||||
};
|
||||
|
||||
@@ -2,15 +2,15 @@ import { ContextAlias } from '../context-token';
|
||||
|
||||
export const umbContextRequestEventType = 'umb:context-request';
|
||||
|
||||
export type UmbContextCallback<T = unknown> = (instance: T) => void;
|
||||
export type UmbContextCallback<T> = (instance: T) => void;
|
||||
|
||||
/**
|
||||
* @export
|
||||
* @interface UmbContextRequestEvent
|
||||
*/
|
||||
export interface UmbContextRequestEvent extends Event {
|
||||
readonly contextAlias: string | ContextAlias;
|
||||
readonly callback: UmbContextCallback;
|
||||
export interface UmbContextRequestEvent<T = unknown> extends Event {
|
||||
readonly contextAlias: string | ContextAlias<T>;
|
||||
readonly callback: UmbContextCallback<T>;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -19,10 +19,10 @@ export interface UmbContextRequestEvent extends Event {
|
||||
* @extends {Event}
|
||||
* @implements {UmbContextRequestEvent}
|
||||
*/
|
||||
export class UmbContextRequestEventImplementation extends Event implements UmbContextRequestEvent {
|
||||
export class UmbContextRequestEventImplementation<T = unknown> extends Event implements UmbContextRequestEvent<T> {
|
||||
public constructor(
|
||||
public readonly contextAlias: string | ContextAlias,
|
||||
public readonly callback: UmbContextCallback
|
||||
public readonly contextAlias: string | ContextAlias<T>,
|
||||
public readonly callback: UmbContextCallback<T>
|
||||
) {
|
||||
super(umbContextRequestEventType, { bubbles: true, composed: true, cancelable: true });
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { expect } from '@open-wc/testing';
|
||||
import { UmbContextConsumer } from '../consume/context-consumer';
|
||||
import { UmbContextRequestEventImplementation } from '../consume/context-request.event';
|
||||
import { ContextAlias } from '../context-token';
|
||||
import { UmbContextProvider } from './context-provider';
|
||||
|
||||
class MyClass {
|
||||
@@ -57,4 +58,17 @@ describe('UmbContextProvider', () => {
|
||||
});
|
||||
localConsumer.hostConnected();
|
||||
});
|
||||
|
||||
it('works with ContextAlias', (done) => {
|
||||
const CONTEXT_ALIAS = new ContextAlias<MyClass>(MyClass.name);
|
||||
const provider = new UmbContextProvider(document.body, CONTEXT_ALIAS, new MyClass());
|
||||
provider.hostConnected();
|
||||
|
||||
const localConsumer = new UmbContextConsumer(document.body, CONTEXT_ALIAS, (_instance: MyClass) => {
|
||||
expect(_instance.prop).to.eq('value from provider');
|
||||
localConsumer.hostDisconnected();
|
||||
done();
|
||||
});
|
||||
localConsumer.hostConnected();
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user