diff --git a/src/plugins/dashboard/public/attribute_service/attribute_service.test.ts b/src/plugins/dashboard/public/attribute_service/attribute_service.test.ts index 3f94917689d5c7..8907afaae97355 100644 --- a/src/plugins/dashboard/public/attribute_service/attribute_service.test.ts +++ b/src/plugins/dashboard/public/attribute_service/attribute_service.test.ts @@ -176,13 +176,52 @@ describe('attributeService', () => { byReferenceInput ); expect(core.savedObjects.client.update).toHaveBeenCalledWith( - 'defaultTestType', + defaultTestType, '123', attributes ); }); - // wrap attr ref type savedObjectId undefined - // wrap attr ref type custom save method + it('creates new saved object with attributes when given no id', async () => { + const core = coreMock.createStart(); + core.savedObjects.client.create = jest.fn().mockResolvedValueOnce({ + id: '678', + }); + const attributeService = mockAttributeService( + defaultTestType, + undefined, + core + ); + expect(await attributeService.wrapAttributes(attributes, true)).toEqual({ + savedObjectId: '678', + }); + expect(core.savedObjects.client.create).toHaveBeenCalledWith(defaultTestType, attributes); + }); + + it('uses custom save method when given an id', async () => { + const customSaveMethod = jest.fn().mockReturnValue({ id: '678' }); + const attributeService = mockAttributeService(defaultTestType, { + customSaveMethod, + }); + expect(await attributeService.wrapAttributes(attributes, true, byReferenceInput)).toEqual( + byReferenceInput + ); + expect(customSaveMethod).toHaveBeenCalledWith( + defaultTestType, + attributes, + byReferenceInput.savedObjectId + ); + }); + + it('uses custom save method given no id', async () => { + const customSaveMethod = jest.fn().mockReturnValue({ id: '678' }); + const attributeService = mockAttributeService(defaultTestType, { + customSaveMethod, + }); + expect(await attributeService.wrapAttributes(attributes, true)).toEqual({ + savedObjectId: '678', + }); + expect(customSaveMethod).toHaveBeenCalledWith(defaultTestType, attributes); + }); }); }); diff --git a/src/plugins/dashboard/public/attribute_service/attribute_service.tsx b/src/plugins/dashboard/public/attribute_service/attribute_service.tsx index be83264b8a5214..ef8081f3341d1b 100644 --- a/src/plugins/dashboard/public/attribute_service/attribute_service.tsx +++ b/src/plugins/dashboard/public/attribute_service/attribute_service.tsx @@ -56,7 +56,11 @@ export interface AttributeServiceOptions< K extends string = typeof ATTRIBUTE_SERVICE_DEFAULT_KEY > { attributesKey?: K; - customSaveMethod?: (attributes: A, savedObjectId?: string) => Promise<{ id: string }>; + customSaveMethod?: ( + type: string, + attributes: A, + savedObjectId?: string + ) => Promise<{ id: string }>; customUnwrapMethod?: (savedObject: SimpleSavedObject) => A; } @@ -121,12 +125,15 @@ export class AttributeService< try { const originalInput = input ? input : {}; if (savedObjectId) { - if (this.options?.customSaveMethod) await this.options.customSaveMethod(newAttributes); - else await this.savedObjectsClient.update(this.type, savedObjectId, newAttributes); + if (this.options?.customSaveMethod) { + await this.options.customSaveMethod(this.type, newAttributes, savedObjectId); + } else { + await this.savedObjectsClient.update(this.type, savedObjectId, newAttributes); + } return { ...originalInput, savedObjectId } as RefType; } else { const savedItem = this.options?.customSaveMethod - ? await this.options.customSaveMethod(newAttributes, savedObjectId) + ? await this.options.customSaveMethod(this.type, newAttributes) : await this.savedObjectsClient.create(this.type, newAttributes); return { ...originalInput, savedObjectId: savedItem.id } as RefType; }