[ACA-4226] Fix rule ebabling in extensions (#2291)

This commit is contained in:
Pablo Martinez Garcia 2021-09-10 13:45:03 +02:00 committed by GitHub
parent cb4fe16fec
commit 219f8b7089
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 238 additions and 25 deletions

View File

@ -712,6 +712,18 @@ describe('AppExtensionService', () => {
}
];
const expectedActions = [
{
id: 'id',
type: ContentActionType.button,
icon: 'icon',
actions: {
click: 'click'
},
disabled: false
}
];
applyConfig({
$id: 'test',
$name: 'test',
@ -728,7 +740,7 @@ describe('AppExtensionService', () => {
}
});
expect(service.getSharedLinkViewerToolbarActions()).toEqual(actions);
expect(service.getSharedLinkViewerToolbarActions()).toEqual(expectedActions);
});
});
@ -928,4 +940,212 @@ describe('AppExtensionService', () => {
expect(service.search.find(({ id }) => id === 'app.search-2')).toBe(undefined, 'disabled configuration shown in the result');
});
});
describe('rule disable', () => {
beforeEach(() => {
extensions.setEvaluators({
isEnabled: () => true,
isDisabled: () => false
});
});
const actions = [
{
id: 'id1',
type: ContentActionType.button,
icon: 'icon1',
actions: {
click: 'click'
},
rules: {
enabled: 'isDisabled'
}
},
{
id: 'id2',
type: ContentActionType.button,
icon: 'icon2',
actions: {
click: 'click'
},
rules: {
enabled: 'isEnabled'
}
}
];
const expectedActionsWithChildren = [
{
id: 'id1',
type: ContentActionType.button,
icon: 'icon1',
actions: {
click: 'click'
},
rules: {
enabled: 'isDisabled'
},
disabled: true,
children: []
},
{
id: 'id2',
type: ContentActionType.button,
icon: 'icon2',
actions: {
click: 'click'
},
rules: {
enabled: 'isEnabled'
},
disabled: false,
children: []
}
];
const expectedActionsWithoutChildren = [
{
id: 'id1',
type: ContentActionType.button,
icon: 'icon1',
actions: {
click: 'click'
},
rules: {
enabled: 'isDisabled'
},
disabled: true
},
{
id: 'id2',
type: ContentActionType.button,
icon: 'icon2',
actions: {
click: 'click'
},
rules: {
enabled: 'isEnabled'
},
disabled: false
}
];
it('should set the action disabled for create actions', () => {
applyConfig({
$id: 'test',
$name: 'test',
$version: '1.0.0',
$license: 'MIT',
$vendor: 'Good company',
$runtime: '1.5.0',
features: {
create: actions
}
});
expect(service.getCreateActions()).toEqual(expectedActionsWithChildren);
});
it('should set the action disabled for sidebar actions', () => {
applyConfig({
$id: 'test',
$name: 'test',
$version: '1.0.0',
$license: 'MIT',
$vendor: 'Good company',
$runtime: '1.5.0',
features: {
sidebar: {
toolbar: actions
}
}
});
expect(service.getAllowedSidebarActions()).toEqual(expectedActionsWithoutChildren);
});
it('should set the action disabled for toolbar actions', () => {
applyConfig({
$id: 'test',
$name: 'test',
$version: '1.0.0',
$license: 'MIT',
$vendor: 'Good company',
$runtime: '1.5.0',
features: {
toolbar: actions
}
});
expect(service.getAllowedToolbarActions()).toEqual(expectedActionsWithoutChildren);
});
it('should set the action disabled for viewer toolbar actions', () => {
applyConfig({
$id: 'test',
$name: 'test',
$version: '1.0.0',
$license: 'MIT',
$vendor: 'Good company',
$runtime: '1.5.0',
features: {
viewer: { toolbarActions: actions }
}
});
expect(service.getViewerToolbarActions()).toEqual(expectedActionsWithoutChildren);
});
it('should set the action disabled for shared link viewer toolbar actions', () => {
applyConfig({
$id: 'test',
$name: 'test',
$version: '1.0.0',
$license: 'MIT',
$vendor: 'Good company',
$runtime: '1.5.0',
features: {
viewer: {
shared: {
toolbarActions: actions
}
}
}
});
expect(service.getSharedLinkViewerToolbarActions()).toEqual(expectedActionsWithoutChildren);
});
it('should set the action disabled for header actions', () => {
applyConfig({
$id: 'test',
$name: 'test',
$version: '1.0.0',
$license: 'MIT',
$vendor: 'Good company',
$runtime: '1.5.0',
features: {
header: actions
}
});
expect(service.getHeaderActions()).toEqual(expectedActionsWithoutChildren);
});
it('should set the action disabled for context menu actions', () => {
applyConfig({
$id: 'test',
$name: 'test',
$version: '1.0.0',
$license: 'MIT',
$vendor: 'Good company',
$runtime: '1.5.0',
features: {
contextMenu: actions
}
});
expect(service.getAllowedContextMenuActions()).toEqual(expectedActionsWithoutChildren);
});
});
});

View File

@ -328,23 +328,25 @@ export class AppExtensionService implements RuleContext {
return this.sidebarTabs.filter((action) => this.filterVisible(action));
}
private setActionDisabledFromRule(action: ContentActionRef) {
let disabled = false;
if (action && action.rules && action.rules.enabled) {
disabled = !this.extensions.evaluateRule(action.rules.enabled, this);
}
return {
...action,
disabled
};
}
getCreateActions(): Array<ContentActionRef> {
return this.createActions
.filter((action) => this.filterVisible(action))
.map((action) => this.copyAction(action))
.map((action) => this.buildMenu(action))
.map((action) => {
let disabled = false;
if (action.rules && action.rules.enabled) {
disabled = !this.extensions.evaluateRule(action.rules.enabled, this);
}
return {
...action,
disabled
};
});
.map((action) => this.setActionDisabledFromRule(action));
}
private buildMenu(actionRef: ContentActionRef): ContentActionRef {
@ -352,18 +354,7 @@ export class AppExtensionService implements RuleContext {
const children = actionRef.children.filter((action) => this.filterVisible(action)).map((action) => this.buildMenu(action));
actionRef.children = children
.map((action) => {
let disabled = false;
if (action.rules && action.rules.enabled) {
disabled = !this.extensions.evaluateRule(action.rules.enabled, this);
}
return {
...action,
disabled
};
})
.map((action) => this.setActionDisabledFromRule(action))
.sort(sortByOrder)
.reduce(reduceEmptyMenus, [])
.reduce(reduceSeparators, []);
@ -389,6 +380,7 @@ export class AppExtensionService implements RuleContext {
}
return action;
})
.map((action) => this.setActionDisabledFromRule(action))
.reduce(reduceEmptyMenus, [])
.reduce(reduceSeparators, []);
}
@ -427,6 +419,7 @@ export class AppExtensionService implements RuleContext {
return action;
})
.map((action) => this.setActionDisabledFromRule(action))
.sort(sortByOrder)
.reduce(reduceEmptyMenus, [])
.reduce(reduceSeparators, []);