add tests for ContextAlias
This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
import { expect } from '@open-wc/testing';
|
||||
import { UmbContextConsumer } from './consume/context-consumer';
|
||||
import { UmbContextAlias } from './context-alias';
|
||||
import { UmbContextProvider } from './provide/context-provider';
|
||||
|
||||
const testContextAlias = 'my-test-context';
|
||||
|
||||
class MyClass {
|
||||
prop = 'value from provider';
|
||||
}
|
||||
|
||||
describe('ContextAlias', () => {
|
||||
const contextAlias = new UmbContextAlias<MyClass>(testContextAlias);
|
||||
const typedProvider = new UmbContextProvider(document.body, contextAlias, new MyClass());
|
||||
typedProvider.hostConnected();
|
||||
|
||||
after(() => {
|
||||
typedProvider.hostDisconnected();
|
||||
});
|
||||
|
||||
it('toString returns the alias', () => {
|
||||
expect(contextAlias.toString()).to.eq(testContextAlias);
|
||||
});
|
||||
|
||||
it('can be consumed directly', (done) => {
|
||||
const element = document.createElement('div');
|
||||
document.body.appendChild(element);
|
||||
|
||||
const localConsumer = new UmbContextConsumer(element, contextAlias, (_instance) => {
|
||||
console.log('got instance', _instance);
|
||||
expect(_instance).to.be.instanceOf(MyClass);
|
||||
expect(_instance.prop).to.eq('value from provider');
|
||||
done();
|
||||
});
|
||||
|
||||
localConsumer.hostConnected();
|
||||
});
|
||||
|
||||
it('can be consumed using the inner string alias', (done) => {
|
||||
const element = document.createElement('div');
|
||||
document.body.appendChild(element);
|
||||
|
||||
const localConsumer = new UmbContextConsumer(element, testContextAlias, (_instance: MyClass) => {
|
||||
expect(_instance).to.be.instanceOf(MyClass);
|
||||
expect(_instance.prop).to.eq('value from provider');
|
||||
done();
|
||||
});
|
||||
|
||||
localConsumer.hostConnected();
|
||||
});
|
||||
});
|
||||
@@ -1,10 +1,11 @@
|
||||
export class UmbContextAlias<T = unknown> {
|
||||
/**
|
||||
* @param alias Unique identifier for the token,
|
||||
* @param _desc Description for the token,
|
||||
* used only for debugging purposes,
|
||||
* it should but does not need to be unique
|
||||
*/
|
||||
constructor(protected _desc: string) {}
|
||||
constructor(protected alias: string, protected _desc?: string) {}
|
||||
|
||||
/**
|
||||
* @internal
|
||||
@@ -13,7 +14,13 @@ export class UmbContextAlias<T = unknown> {
|
||||
return this as UmbContextAlias<Array<T>>;
|
||||
}
|
||||
|
||||
/**
|
||||
* This method must always return the unique alias of the token since that
|
||||
* will be used to look up the token in the injector.
|
||||
*
|
||||
* @returns the unique alias of the token
|
||||
*/
|
||||
toString(): string {
|
||||
return `${UmbContextAlias.name} ${this._desc}`;
|
||||
return this.alias;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user