better tests
This commit is contained in:
@@ -5,12 +5,15 @@ import { UmbContextConsumer } from './context-consumer.js';
|
||||
import { UmbContextRequestEventImplementation, UMB_CONTENT_REQUEST_EVENT_TYPE } from './context-request.event.js';
|
||||
|
||||
const testContextAlias = 'my-test-context';
|
||||
const testContextAliasAndApiAlias = 'my-test-context#testApi';
|
||||
const testContextAliasAndNotExstingApiAlias = 'my-test-context#notExistingTestApi';
|
||||
|
||||
class UmbTestContextConsumerClass {
|
||||
public prop: string = 'value from provider';
|
||||
}
|
||||
|
||||
describe('UmbContextConsumer', () => {
|
||||
describe('Public API', () => {
|
||||
let consumer: UmbContextConsumer;
|
||||
|
||||
beforeEach(() => {
|
||||
@@ -18,7 +21,6 @@ describe('UmbContextConsumer', () => {
|
||||
consumer = new UmbContextConsumer(document.body, testContextAlias, () => {});
|
||||
});
|
||||
|
||||
describe('Public API', () => {
|
||||
describe('properties', () => {
|
||||
it('has a instance property', () => {
|
||||
expect(consumer).to.have.property('instance').that.is.undefined;
|
||||
@@ -45,6 +47,7 @@ describe('UmbContextConsumer', () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe('Simple implementation', () => {
|
||||
it('works with UmbContextProvider', (done) => {
|
||||
const provider = new UmbContextProvider(document.body, testContextAlias, new UmbTestContextConsumerClass());
|
||||
provider.hostConnected();
|
||||
@@ -98,7 +101,55 @@ describe('UmbContextConsumer', () => {
|
||||
*/
|
||||
});
|
||||
|
||||
describe('UmbContextConsumer with discriminator test', () => {
|
||||
describe('Implementation with Api Alias', () => {
|
||||
it('responds when api alias matches', (done) => {
|
||||
const provider = new UmbContextProvider(
|
||||
document.body,
|
||||
testContextAliasAndApiAlias,
|
||||
new UmbTestContextConsumerClass(),
|
||||
);
|
||||
provider.hostConnected();
|
||||
|
||||
const element = document.createElement('div');
|
||||
document.body.appendChild(element);
|
||||
|
||||
const localConsumer = new UmbContextConsumer(element, testContextAliasAndApiAlias, (_instance) => {
|
||||
if (_instance) {
|
||||
expect((_instance as UmbTestContextConsumerClass).prop).to.eq('value from provider');
|
||||
localConsumer.hostDisconnected();
|
||||
provider.hostDisconnected();
|
||||
done();
|
||||
}
|
||||
});
|
||||
localConsumer.hostConnected();
|
||||
});
|
||||
|
||||
it('does not respond to a non existing api alias', (done) => {
|
||||
const provider = new UmbContextProvider(
|
||||
document.body,
|
||||
testContextAliasAndApiAlias,
|
||||
new UmbTestContextConsumerClass(),
|
||||
);
|
||||
provider.hostConnected();
|
||||
|
||||
const element = document.createElement('div');
|
||||
document.body.appendChild(element);
|
||||
|
||||
const localConsumer = new UmbContextConsumer(element, testContextAliasAndNotExstingApiAlias, (_instance) => {
|
||||
expect(false).to.be.true;
|
||||
});
|
||||
localConsumer.hostConnected();
|
||||
|
||||
// Delayed check to make sure the callback is not called.
|
||||
Promise.resolve().then(() => {
|
||||
localConsumer.hostDisconnected();
|
||||
provider.hostDisconnected();
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('Implementation with discriminator method', () => {
|
||||
type A = { prop: string };
|
||||
|
||||
function discriminator(instance: unknown): instance is A {
|
||||
@@ -170,3 +221,4 @@ describe('UmbContextConsumer with discriminator test', () => {
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user