Merge remote-tracking branch 'origin/main' into feature/validation-take-3

This commit is contained in:
Niels Lyngsø
2024-04-15 13:15:05 +02:00
2 changed files with 99 additions and 0 deletions

View File

@@ -368,3 +368,88 @@ describe('UmbExtensionRegistry with kinds', () => {
.unsubscribe();
});
});
describe('UmbExtensionRegistry with exclusions', () => {
let extensionRegistry: UmbExtensionRegistry<any>;
let manifests: Array<
ManifestElementWithElementName | TestManifestWithMeta | ManifestKind<ManifestElementWithElementName>
>;
beforeEach(() => {
extensionRegistry = new UmbExtensionRegistry<any>();
manifests = [
{
type: 'kind',
alias: 'Umb.Test.Kind',
matchType: 'section',
matchKind: 'test-kind',
manifest: {
type: 'section',
elementName: 'my-kind-element',
meta: {
label: 'my-kind-meta-label',
},
},
},
{
type: 'section',
kind: 'test-kind' as unknown as undefined, // We do not know about this one, so it makes good sense that its not a valid option.
name: 'test-section-1',
alias: 'Umb.Test.Section.1',
weight: 1,
meta: {
pathname: 'test-section-1',
},
},
{
type: 'section',
name: 'test-section-2',
alias: 'Umb.Test.Section.2',
weight: 200,
meta: {
label: 'Test Section 2',
pathname: 'test-section-2',
},
},
];
manifests.forEach((manifest) => extensionRegistry.register(manifest));
});
it('should have the extensions registered', () => {
expect(extensionRegistry.isRegistered('Umb.Test.Kind')).to.be.true;
expect(extensionRegistry.isRegistered('Umb.Test.Section.1')).to.be.true;
expect(extensionRegistry.isRegistered('Umb.Test.Section.2')).to.be.true;
});
it('must not say that Umb.Test.Section.1d is registered, when its added to the exclusion list', () => {
extensionRegistry.exclude('Umb.Test.Section.1');
expect(extensionRegistry.isRegistered('Umb.Test.Section.1')).to.be.false;
// But check that the other ones are still registered:
expect(extensionRegistry.isRegistered('Umb.Test.Kind')).to.be.true;
expect(extensionRegistry.isRegistered('Umb.Test.Section.2')).to.be.true;
});
it('does not affect kinds when a kind-alias is put in the exclusion list', () => {
extensionRegistry.exclude('Umb.Test.Kind');
// This had no effect, so all of them are still available.
expect(extensionRegistry.isRegistered('Umb.Test.Kind')).to.be.true;
expect(extensionRegistry.isRegistered('Umb.Test.Section.1')).to.be.true;
expect(extensionRegistry.isRegistered('Umb.Test.Section.2')).to.be.true;
});
it('prevents late comers from begin registered', () => {
extensionRegistry.exclude('Umb.Test.Section.Late');
extensionRegistry.register({
type: 'section',
name: 'test-section-late',
alias: 'Umb.Test.Section.Late',
weight: 200,
meta: {
label: 'Test Section Late',
pathname: 'test-section-Late',
},
});
expect(extensionRegistry.isRegistered('Umb.Test.Section.Late')).to.be.false;
});
});

View File

@@ -80,6 +80,7 @@ export class UmbExtensionRegistry<
private _kinds = new UmbBasicState<Array<ManifestKind<ManifestTypes>>>([]);
public readonly kinds = this._kinds.asObservable();
#exclusions: Array<string> = [];
defineKind(kind: ManifestKind<ManifestTypes>): void {
const extensionsValues = this._extensions.getValue();
@@ -105,6 +106,15 @@ export class UmbExtensionRegistry<
this._kinds.setValue(nextData);
}
exclude(alias: string): void {
this.#exclusions.push(alias);
this._extensions.setValue(this._extensions.getValue().filter(this.#acceptExtension));
}
#acceptExtension = (ext: ManifestTypes): boolean => {
return !this.#exclusions.includes(ext.alias);
};
register(manifest: ManifestTypes | ManifestKind<ManifestTypes>): void {
const isValid = this.#checkExtension(manifest);
if (!isValid) {
@@ -163,6 +173,10 @@ export class UmbExtensionRegistry<
return false;
}
if (!this.#acceptExtension(manifest as ManifestTypes)) {
return false;
}
const extensionsValues = this._extensions.getValue();
const extension = extensionsValues.find((extension) => extension.alias === (manifest as ManifestTypes).alias);