Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 26 additions & 19 deletions x-pack/plugins/actions/server/__jest__/action_type_service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,14 @@
import Joi from 'joi';
import { ActionTypeService } from '../action_type_service';

const services = {
log: jest.fn(),
};

describe('register()', () => {
test('able to register action types', () => {
const executor = jest.fn();
const actionTypeService = new ActionTypeService();
const actionTypeService = new ActionTypeService({ services });
actionTypeService.register({
id: 'my-action-type',
name: 'My action type',
Expand All @@ -21,7 +25,7 @@ describe('register()', () => {

test('throws error if action type already registered', () => {
const executor = jest.fn();
const actionTypeService = new ActionTypeService();
const actionTypeService = new ActionTypeService({ services });
actionTypeService.register({
id: 'my-action-type',
name: 'My action type',
Expand All @@ -41,7 +45,7 @@ describe('register()', () => {

describe('get()', () => {
test('returns action type', () => {
const actionTypeService = new ActionTypeService();
const actionTypeService = new ActionTypeService({ services });
actionTypeService.register({
id: 'my-action-type',
name: 'My action type',
Expand All @@ -58,7 +62,7 @@ Object {
});

test(`throws an error when action type doesn't exist`, () => {
const actionTypeService = new ActionTypeService();
const actionTypeService = new ActionTypeService({ services });
expect(() => actionTypeService.get('my-action-type')).toThrowErrorMatchingInlineSnapshot(
`"Action type \\"my-action-type\\" is not registered."`
);
Expand All @@ -67,7 +71,7 @@ Object {

describe('getUnencryptedAttributes()', () => {
test('returns empty array when unencryptedAttributes is undefined', () => {
const actionTypeService = new ActionTypeService();
const actionTypeService = new ActionTypeService({ services });
actionTypeService.register({
id: 'my-action-type',
name: 'My action type',
Expand All @@ -78,7 +82,7 @@ describe('getUnencryptedAttributes()', () => {
});

test('returns values inside unencryptedAttributes array when it exists', () => {
const actionTypeService = new ActionTypeService();
const actionTypeService = new ActionTypeService({ services });
actionTypeService.register({
id: 'my-action-type',
name: 'My action type',
Expand All @@ -92,7 +96,7 @@ describe('getUnencryptedAttributes()', () => {

describe('list()', () => {
test('returns list of action types', () => {
const actionTypeService = new ActionTypeService();
const actionTypeService = new ActionTypeService({ services });
actionTypeService.register({
id: 'my-action-type',
name: 'My action type',
Expand All @@ -110,7 +114,7 @@ describe('list()', () => {

describe('validateParams()', () => {
test('should pass when validation not defined', () => {
const actionTypeService = new ActionTypeService();
const actionTypeService = new ActionTypeService({ services });
actionTypeService.register({
id: 'my-action-type',
name: 'My action type',
Expand All @@ -120,7 +124,7 @@ describe('validateParams()', () => {
});

test('should validate and pass when params is valid', () => {
const actionTypeService = new ActionTypeService();
const actionTypeService = new ActionTypeService({ services });
actionTypeService.register({
id: 'my-action-type',
name: 'My action type',
Expand All @@ -137,7 +141,7 @@ describe('validateParams()', () => {
});

test('should validate and throw error when params is invalid', () => {
const actionTypeService = new ActionTypeService();
const actionTypeService = new ActionTypeService({ services });
actionTypeService.register({
id: 'my-action-type',
name: 'My action type',
Expand All @@ -160,7 +164,7 @@ describe('validateParams()', () => {

describe('validateActionTypeConfig()', () => {
test('should pass when validation not defined', () => {
const actionTypeService = new ActionTypeService();
const actionTypeService = new ActionTypeService({ services });
actionTypeService.register({
id: 'my-action-type',
name: 'My action type',
Expand All @@ -170,7 +174,7 @@ describe('validateActionTypeConfig()', () => {
});

test('should validate and pass when actionTypeConfig is valid', () => {
const actionTypeService = new ActionTypeService();
const actionTypeService = new ActionTypeService({ services });
actionTypeService.register({
id: 'my-action-type',
name: 'My action type',
Expand All @@ -187,7 +191,7 @@ describe('validateActionTypeConfig()', () => {
});

test('should validate and throw error when actionTypeConfig is invalid', () => {
const actionTypeService = new ActionTypeService();
const actionTypeService = new ActionTypeService({ services });
actionTypeService.register({
id: 'my-action-type',
name: 'My action type',
Expand All @@ -210,13 +214,13 @@ describe('validateActionTypeConfig()', () => {

describe('has()', () => {
test('returns false for unregistered action types', () => {
const actionTypeService = new ActionTypeService();
const actionTypeService = new ActionTypeService({ services });
expect(actionTypeService.has('my-action-type')).toEqual(false);
});

test('returns true after registering an action type', () => {
const executor = jest.fn();
const actionTypeService = new ActionTypeService();
const actionTypeService = new ActionTypeService({ services });
actionTypeService.register({
id: 'my-action-type',
name: 'My action type',
Expand All @@ -229,7 +233,7 @@ describe('has()', () => {
describe('execute()', () => {
test('calls the executor with proper params', async () => {
const executor = jest.fn().mockResolvedValueOnce({ success: true });
const actionTypeService = new ActionTypeService();
const actionTypeService = new ActionTypeService({ services });
actionTypeService.register({
id: 'my-action-type',
name: 'My action type',
Expand All @@ -251,6 +255,9 @@ describe('execute()', () => {
"params": Object {
"bar": false,
},
"services": Object {
"log": [MockFunction],
},
},
],
],
Expand All @@ -266,7 +273,7 @@ describe('execute()', () => {

test('validates params', async () => {
const executor = jest.fn().mockResolvedValueOnce({ success: true });
const actionTypeService = new ActionTypeService();
const actionTypeService = new ActionTypeService({ services });
actionTypeService.register({
id: 'my-action-type',
name: 'My action type',
Expand All @@ -292,7 +299,7 @@ describe('execute()', () => {

test('validates actionTypeConfig', async () => {
const executor = jest.fn().mockResolvedValueOnce({ success: true });
const actionTypeService = new ActionTypeService();
const actionTypeService = new ActionTypeService({ services });
actionTypeService.register({
id: 'my-action-type',
name: 'My action type',
Expand All @@ -317,7 +324,7 @@ describe('execute()', () => {
});

test('throws error if action type not registered', async () => {
const actionTypeService = new ActionTypeService();
const actionTypeService = new ActionTypeService({ services });
await expect(
actionTypeService.execute({
id: 'my-action-type',
Expand Down
26 changes: 15 additions & 11 deletions x-pack/plugins/actions/server/__jest__/actions_client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ import Joi from 'joi';
import { ActionTypeService } from '../action_type_service';
import { ActionsClient } from '../actions_client';

const services = {
log: jest.fn(),
};

const savedObjectsClient = {
errors: {} as any,
bulkCreate: jest.fn(),
Expand All @@ -24,7 +28,7 @@ beforeEach(() => jest.resetAllMocks());
describe('create()', () => {
test('creates an action with all given properties', async () => {
const expectedResult = Symbol();
const actionTypeService = new ActionTypeService();
const actionTypeService = new ActionTypeService({ services });
actionTypeService.register({
id: 'my-action-type',
name: 'My action type',
Expand Down Expand Up @@ -75,7 +79,7 @@ describe('create()', () => {
});

test('validates actionTypeConfig', async () => {
const actionTypeService = new ActionTypeService();
const actionTypeService = new ActionTypeService({ services });
const actionService = new ActionsClient({
actionTypeService,
savedObjectsClient,
Expand Down Expand Up @@ -106,7 +110,7 @@ describe('create()', () => {
});

test(`throws an error when an action type doesn't exist`, async () => {
const actionTypeService = new ActionTypeService();
const actionTypeService = new ActionTypeService({ services });
const actionService = new ActionsClient({
actionTypeService,
savedObjectsClient,
Expand All @@ -126,7 +130,7 @@ describe('create()', () => {

test('encrypts action type options unless specified not to', async () => {
const expectedResult = Symbol();
const actionTypeService = new ActionTypeService();
const actionTypeService = new ActionTypeService({ services });
actionTypeService.register({
id: 'my-action-type',
name: 'My action type',
Expand Down Expand Up @@ -183,7 +187,7 @@ describe('create()', () => {
describe('get()', () => {
test('calls savedObjectsClient with id', async () => {
const expectedResult = Symbol();
const actionTypeService = new ActionTypeService();
const actionTypeService = new ActionTypeService({ services });
const actionService = new ActionsClient({
actionTypeService,
savedObjectsClient,
Expand Down Expand Up @@ -213,7 +217,7 @@ describe('get()', () => {
describe('find()', () => {
test('calls savedObjectsClient with parameters', async () => {
const expectedResult = Symbol();
const actionTypeService = new ActionTypeService();
const actionTypeService = new ActionTypeService({ services });
const actionService = new ActionsClient({
actionTypeService,
savedObjectsClient,
Expand Down Expand Up @@ -244,7 +248,7 @@ describe('find()', () => {
describe('delete()', () => {
test('calls savedObjectsClient with id', async () => {
const expectedResult = Symbol();
const actionTypeService = new ActionTypeService();
const actionTypeService = new ActionTypeService({ services });
const actionService = new ActionsClient({
actionTypeService,
savedObjectsClient,
Expand Down Expand Up @@ -274,7 +278,7 @@ describe('delete()', () => {
describe('update()', () => {
test('updates an action with all given properties', async () => {
const expectedResult = Symbol();
const actionTypeService = new ActionTypeService();
const actionTypeService = new ActionTypeService({ services });
actionTypeService.register({
id: 'my-action-type',
name: 'My action type',
Expand Down Expand Up @@ -321,7 +325,7 @@ describe('update()', () => {
});

test('validates actionTypeConfig', async () => {
const actionTypeService = new ActionTypeService();
const actionTypeService = new ActionTypeService({ services });
const actionService = new ActionsClient({
actionTypeService,
savedObjectsClient,
Expand Down Expand Up @@ -354,7 +358,7 @@ describe('update()', () => {
});

test(`throws an error when action type doesn't exist`, async () => {
const actionTypeService = new ActionTypeService();
const actionTypeService = new ActionTypeService({ services });
const actionService = new ActionsClient({
actionTypeService,
savedObjectsClient,
Expand All @@ -376,7 +380,7 @@ describe('update()', () => {

test('encrypts action type options unless specified not to', async () => {
const expectedResult = Symbol();
const actionTypeService = new ActionTypeService();
const actionTypeService = new ActionTypeService({ services });
actionTypeService.register({
id: 'my-action-type',
name: 'My action type',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
import { ActionTypeService } from '../action_type_service';
import { createFireFunction } from '../create_fire_function';

const services = {
log: jest.fn(),
};

const mockEncryptedSavedObjects = {
isEncryptionError: jest.fn(),
registerType: jest.fn(),
Expand All @@ -15,7 +19,7 @@ const mockEncryptedSavedObjects = {

describe('fire()', () => {
test('fires an action with all given parameters', async () => {
const actionTypeService = new ActionTypeService();
const actionTypeService = new ActionTypeService({ services });
const fireFn = createFireFunction({
actionTypeService,
encryptedSavedObjectsPlugin: mockEncryptedSavedObjects,
Expand Down Expand Up @@ -51,6 +55,9 @@ describe('fire()', () => {
"params": Object {
"baz": false,
},
"services": Object {
"log": [MockFunction],
},
},
],
],
Expand All @@ -68,7 +75,7 @@ describe('fire()', () => {
});

test(`throws an error when the action type isn't registered`, async () => {
const actionTypeService = new ActionTypeService();
const actionTypeService = new ActionTypeService({ services });
const fireFn = createFireFunction({
actionTypeService,
encryptedSavedObjectsPlugin: mockEncryptedSavedObjects,
Expand All @@ -90,7 +97,7 @@ describe('fire()', () => {
});

test('merges encrypted and unencrypted attributes', async () => {
const actionTypeService = new ActionTypeService();
const actionTypeService = new ActionTypeService({ services });
const fireFn = createFireFunction({
actionTypeService,
encryptedSavedObjectsPlugin: mockEncryptedSavedObjects,
Expand Down Expand Up @@ -133,6 +140,9 @@ describe('fire()', () => {
"params": Object {
"baz": false,
},
"services": Object {
"log": [MockFunction],
},
},
],
],
Expand Down
Loading