Skip to content

Commit

Permalink
finished tests. Changed signature for custom save method
Browse files Browse the repository at this point in the history
  • Loading branch information
ThomThomson committed Aug 26, 2020
1 parent e5d6dc7 commit eae8d34
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<DefaultTestAttributes>(
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<DefaultTestAttributes>(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<DefaultTestAttributes>(defaultTestType, {
customSaveMethod,
});
expect(await attributeService.wrapAttributes(attributes, true)).toEqual({
savedObjectId: '678',
});
expect(customSaveMethod).toHaveBeenCalledWith(defaultTestType, attributes);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -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>) => A;
}

Expand Down Expand Up @@ -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;
}
Expand Down

0 comments on commit eae8d34

Please sign in to comment.