|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License; |
| 4 | + * you may not use this file except in compliance with the Elastic License. |
| 5 | + */ |
| 6 | + |
| 7 | +import { SavedObjectsClientContract, SavedObjectsErrorHelpers } from 'src/core/server'; |
| 8 | +import { CASE_COMMENT_SAVED_OBJECT } from '../../../constants'; |
| 9 | + |
| 10 | +export const createMockSavedObjectsRepository = (savedObject: any[] = []) => { |
| 11 | + const mockSavedObjectsClientContract = ({ |
| 12 | + get: jest.fn((type, id) => { |
| 13 | + const result = savedObject.filter(s => s.id === id); |
| 14 | + if (!result.length) { |
| 15 | + throw SavedObjectsErrorHelpers.createGenericNotFoundError(type, id); |
| 16 | + } |
| 17 | + return result[0]; |
| 18 | + }), |
| 19 | + find: jest.fn(findArgs => { |
| 20 | + if (findArgs.hasReference && findArgs.hasReference.id === 'bad-guy') { |
| 21 | + throw SavedObjectsErrorHelpers.createBadRequestError('Error thrown for testing'); |
| 22 | + } |
| 23 | + return { |
| 24 | + total: savedObject.length, |
| 25 | + saved_objects: savedObject, |
| 26 | + }; |
| 27 | + }), |
| 28 | + create: jest.fn((type, attributes, references) => { |
| 29 | + if (attributes.description === 'Throw an error' || attributes.comment === 'Throw an error') { |
| 30 | + throw SavedObjectsErrorHelpers.createBadRequestError('Error thrown for testing'); |
| 31 | + } |
| 32 | + if (type === CASE_COMMENT_SAVED_OBJECT) { |
| 33 | + return { |
| 34 | + type, |
| 35 | + id: 'mock-comment', |
| 36 | + attributes, |
| 37 | + ...references, |
| 38 | + updated_at: '2019-12-02T22:48:08.327Z', |
| 39 | + version: 'WzksMV0=', |
| 40 | + }; |
| 41 | + } |
| 42 | + return { |
| 43 | + type, |
| 44 | + id: 'mock-it', |
| 45 | + attributes, |
| 46 | + references: [], |
| 47 | + updated_at: '2019-12-02T22:48:08.327Z', |
| 48 | + version: 'WzksMV0=', |
| 49 | + }; |
| 50 | + }), |
| 51 | + update: jest.fn((type, id, attributes) => { |
| 52 | + if (!savedObject.find(s => s.id === id)) { |
| 53 | + throw SavedObjectsErrorHelpers.createGenericNotFoundError(type, id); |
| 54 | + } |
| 55 | + return { |
| 56 | + id, |
| 57 | + type, |
| 58 | + updated_at: '2019-11-22T22:50:55.191Z', |
| 59 | + version: 'WzE3LDFd', |
| 60 | + attributes, |
| 61 | + }; |
| 62 | + }), |
| 63 | + delete: jest.fn((type: string, id: string) => { |
| 64 | + const result = savedObject.filter(s => s.id === id); |
| 65 | + if (!result.length) { |
| 66 | + throw SavedObjectsErrorHelpers.createGenericNotFoundError(type, id); |
| 67 | + } |
| 68 | + if (type === 'case-workflow-comment' && id === 'bad-guy') { |
| 69 | + throw SavedObjectsErrorHelpers.createBadRequestError('Error thrown for testing'); |
| 70 | + } |
| 71 | + return {}; |
| 72 | + }), |
| 73 | + deleteByNamespace: jest.fn(), |
| 74 | + } as unknown) as jest.Mocked<SavedObjectsClientContract>; |
| 75 | + |
| 76 | + return mockSavedObjectsClientContract; |
| 77 | +}; |
0 commit comments