Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Dashboard First] Genericize Attribute Service #76057

Merged
Merged
Show file tree
Hide file tree
Changes from 4 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
8 changes: 2 additions & 6 deletions examples/embeddable_examples/public/book/book_embeddable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,7 @@ export class BookEmbeddable

constructor(
initialInput: BookEmbeddableInput,
private attributeService: AttributeService<
BookSavedObjectAttributes,
BookByValueInput,
BookByReferenceInput
>,
private attributeService: AttributeService<BookSavedObjectAttributes>,
{
parent,
}: {
Expand Down Expand Up @@ -110,7 +106,7 @@ export class BookEmbeddable

getInputAsRefType = async (): Promise<BookByReferenceInput> => {
ThomThomson marked this conversation as resolved.
Show resolved Hide resolved
const input = this.attributeService.getExplicitInputFromEmbeddable(this);
return this.attributeService.getInputAsRefType(input, { showSaveModal: true });
return this.attributeService.getInputAsRefType(input, this, { showSaveModal: true });
};

public render(node: HTMLElement) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ import {
BOOK_EMBEDDABLE,
BookEmbeddableInput,
BookEmbeddableOutput,
BookByValueInput,
BookByReferenceInput,
} from './book_embeddable';
import { CreateEditBookComponent } from './create_edit_book_component';
import { OverlayStart } from '../../../../src/core/public';
Expand Down Expand Up @@ -66,11 +64,7 @@ export class BookEmbeddableFactoryDefinition
getIconForSavedObject: () => 'pencil',
};

private attributeService?: AttributeService<
BookSavedObjectAttributes,
BookByValueInput,
BookByReferenceInput
>;
private attributeService?: AttributeService<BookSavedObjectAttributes>;

constructor(private getStartServices: () => Promise<StartServices>) {}

Expand Down Expand Up @@ -126,9 +120,7 @@ export class BookEmbeddableFactoryDefinition
private async getAttributeService() {
if (!this.attributeService) {
this.attributeService = await (await this.getStartServices()).getAttributeService<
BookSavedObjectAttributes,
BookByValueInput,
BookByReferenceInput
BookSavedObjectAttributes
>(this.type);
}
return this.attributeService!;
Expand Down
12 changes: 6 additions & 6 deletions examples/embeddable_examples/public/book/edit_book_action.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,13 @@ export const createEditBookAction = (getStartServices: () => Promise<StartServic
},
execute: async ({ embeddable }: ActionContext) => {
const { openModal, getAttributeService } = await getStartServices();
const attributeService = getAttributeService<
BookSavedObjectAttributes,
BookByValueInput,
BookByReferenceInput
>(BOOK_SAVED_OBJECT);
const attributeService = getAttributeService<BookSavedObjectAttributes>(BOOK_SAVED_OBJECT);
const onSave = async (attributes: BookSavedObjectAttributes, useRefType: boolean) => {
const newInput = await attributeService.wrapAttributes(attributes, useRefType, embeddable);
const newInput = await attributeService.wrapAttributes(
attributes,
useRefType,
embeddable.getInput()
);
if (!useRefType && (embeddable.getInput() as SavedObjectEmbeddableInput).savedObjectId) {
// Set the saved object ID to null so that update input will remove the existing savedObjectId...
(newInput as BookByValueInput & { savedObjectId: unknown }).savedObjectId = null;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,227 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import { mockAttributeService } from './attribute_service_mock';
import { coreMock } from '../../../../core/public/mocks';

interface DefaultTestAttributes {
title: string;
testAttr1?: string;
testAttr2?: { array: unknown[]; testAttr3: string };
}

interface DefaultByValueInput {
id: string;
attributes: DefaultTestAttributes;
}

const customAttributesKey = 'reallyNeatAttributesKey';
interface CustomTestByValueInput {
id: string;
[customAttributesKey]: DefaultTestAttributes;
}

describe('attributeService', () => {
const defaultTestType = 'defaultTestType';
let attributes: DefaultTestAttributes;
let byValueInput: DefaultByValueInput;
let byReferenceInput: { id: string; savedObjectId: string };

beforeEach(() => {
attributes = {
title: 'ultra title',
testAttr1: 'neat first attribute',
testAttr2: { array: [1, 2, 3], testAttr3: 'super attribute' },
};
byValueInput = {
id: '456',
attributes,
};
byReferenceInput = {
id: '456',
savedObjectId: '123',
};
});

describe('determining input type', () => {
const defaultAttributeService = mockAttributeService<DefaultTestAttributes>(defaultTestType);
const customAttributeService = mockAttributeService<
DefaultTestAttributes,
typeof customAttributesKey,
CustomTestByValueInput
>(defaultTestType);

it('can determine input type given default types', () => {
expect(
defaultAttributeService.inputIsRefType({ id: '456', savedObjectId: '123' })
).toBeTruthy();
expect(
defaultAttributeService.inputIsRefType({
id: '456',
attributes: { title: 'wow I am by value' },
})
).toBeFalsy();
});
it('can determine input type given custom types', () => {
expect(
customAttributeService.inputIsRefType({ id: '456', savedObjectId: '123' })
).toBeTruthy();
expect(
customAttributeService.inputIsRefType({
id: '456',
[customAttributesKey]: { title: 'wow I am by value' },
})
).toBeFalsy();
});
});

describe('unwrapping attributes', () => {
it('can unwrap all default attributes when given reference type input', async () => {
const core = coreMock.createStart();
core.savedObjects.client.get = jest.fn().mockResolvedValueOnce({
attributes,
});
const attributeService = mockAttributeService<DefaultTestAttributes>(
defaultTestType,
undefined,
core
);
expect(await attributeService.unwrapAttributes(byReferenceInput)).toEqual(attributes);
});

it('returns attributes when when given value type input', async () => {
const attributeService = mockAttributeService<DefaultTestAttributes>(defaultTestType);
expect(await attributeService.unwrapAttributes(byValueInput)).toEqual(attributes);
});

it('returns attributes with custom key when given value type input', async () => {
const customAttributeService = mockAttributeService<
DefaultTestAttributes,
typeof customAttributesKey,
CustomTestByValueInput
>(defaultTestType, { attributesKey: customAttributesKey });
expect(
await customAttributeService.unwrapAttributes({
id: '456',
[customAttributesKey]: attributes,
})
).toEqual(attributes);
});

it('runs attributes through a custom unwrap method', async () => {
const core = coreMock.createStart();
core.savedObjects.client.get = jest.fn().mockResolvedValueOnce({
attributes,
});
const attributeService = mockAttributeService<DefaultTestAttributes>(
defaultTestType,
{
customUnwrapMethod: (savedObject) => {
return {
...savedObject.attributes,
testAttr2: { array: [1, 2, 3, 4, 5], testAttr3: 'kibanana' },
};
},
},
core
);
expect(await attributeService.unwrapAttributes(byReferenceInput)).toEqual({
...attributes,
testAttr2: { array: [1, 2, 3, 4, 5], testAttr3: 'kibanana' },
});
});
});

describe('wrapping attributes', () => {
it('returns given attributes when use ref type is false', async () => {
const attributeService = mockAttributeService<DefaultTestAttributes>(defaultTestType);
expect(await attributeService.wrapAttributes(attributes, false)).toEqual({ attributes });
});

it('returns given attributes with custom key when use ref type is false', async () => {
const attributeService = mockAttributeService<
DefaultTestAttributes,
typeof customAttributesKey,
CustomTestByValueInput
>(defaultTestType, { attributesKey: customAttributesKey });
expect(await attributeService.wrapAttributes(attributes, false)).toEqual({
[customAttributesKey]: attributes,
});
});

it('updates existing saved object with new attributes when given id', async () => {
const core = coreMock.createStart();
const attributeService = mockAttributeService<DefaultTestAttributes>(
defaultTestType,
undefined,
core
);
expect(await attributeService.wrapAttributes(attributes, true, byReferenceInput)).toEqual(
byReferenceInput
);
expect(core.savedObjects.client.update).toHaveBeenCalledWith(
defaultTestType,
'123',
attributes
);
});

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);
});
});
});
Loading