Skip to content

Commit

Permalink
Shallow clone properties when adding to registry
Browse files Browse the repository at this point in the history
  • Loading branch information
mikecote committed May 5, 2020
1 parent 0b202dc commit 0c7d0c0
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 2 deletions.
13 changes: 13 additions & 0 deletions x-pack/plugins/actions/server/action_type_registry.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,19 @@ describe('register()', () => {
`);
});

test('shallow clones the given action type', () => {
const myType: ActionType = {
id: 'my-action-type',
name: 'My action type',
minimumLicenseRequired: 'basic',
executor,
};
const actionTypeRegistry = new ActionTypeRegistry(actionTypeRegistryParams);
actionTypeRegistry.register(myType);
myType.name = 'Changed';
expect(actionTypeRegistry.get('my-action-type').name).toEqual('My action type');
});

test('throws error if action type already registered', () => {
const actionTypeRegistry = new ActionTypeRegistry(actionTypeRegistryParams);
actionTypeRegistry.register({
Expand Down
2 changes: 1 addition & 1 deletion x-pack/plugins/actions/server/action_type_registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ export class ActionTypeRegistry {
)
);
}
this.actionTypes.set(actionType.id, actionType);
this.actionTypes.set(actionType.id, { ...actionType });
this.taskManager.registerTaskDefinitions({
[`actions:${actionType.id}`]: {
title: actionType.name,
Expand Down
19 changes: 19 additions & 0 deletions x-pack/plugins/alerting/server/alert_type_registry.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,25 @@ describe('register()', () => {
`);
});

test('shallow clones the given alert type', () => {
const alertType: AlertType = {
id: 'test',
name: 'Test',
actionGroups: [
{
id: 'default',
name: 'Default',
},
],
defaultActionGroupId: 'default',
executor: jest.fn(),
};
const registry = new AlertTypeRegistry(alertTypeRegistryParams);
registry.register(alertType);
alertType.name = 'Changed';
expect(registry.get('test').name).toEqual('Test');
});

test('should throw an error if type is already registered', () => {
const registry = new AlertTypeRegistry(alertTypeRegistryParams);
registry.register({
Expand Down
2 changes: 1 addition & 1 deletion x-pack/plugins/alerting/server/alert_type_registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export class AlertTypeRegistry {
);
}
alertType.actionVariables = normalizedActionVariables(alertType.actionVariables);
this.alertTypes.set(alertType.id, alertType);
this.alertTypes.set(alertType.id, { ...alertType });
this.taskManager.registerTaskDefinitions({
[`alerting:${alertType.id}`]: {
title: alertType.name,
Expand Down

0 comments on commit 0c7d0c0

Please sign in to comment.