diff --git a/x-pack/plugins/canvas/server/routes/workpad/create.test.ts b/x-pack/plugins/canvas/server/routes/workpad/create.test.ts index 02b6376ece2ed..e3256772b3e49 100644 --- a/x-pack/plugins/canvas/server/routes/workpad/create.test.ts +++ b/x-pack/plugins/canvas/server/routes/workpad/create.test.ts @@ -20,7 +20,7 @@ import { RequestHandler, } from 'src/core/server'; -const mockRouteContext = ({ +let mockRouteContext = ({ core: { savedObjects: { client: savedObjectsClientMock.create(), @@ -39,6 +39,14 @@ describe('POST workpad', () => { let clock: sinon.SinonFakeTimers; beforeEach(() => { + mockRouteContext = ({ + core: { + savedObjects: { + client: savedObjectsClientMock.create(), + }, + }, + } as unknown) as RequestHandlerContext; + clock = sinon.useFakeTimers(now); const httpService = httpServiceMock.createSetupContract(); @@ -70,7 +78,7 @@ describe('POST workpad', () => { const response = await routeHandler(mockRouteContext, request, kibanaResponseFactory); expect(response.status).toBe(200); - expect(response.payload).toEqual({ ok: true }); + expect(response.payload).toEqual({ ok: true, id: `workpad-${mockedUUID}` }); expect(mockRouteContext.core.savedObjects.client.create).toBeCalledWith( CANVAS_TYPE, { @@ -99,4 +107,45 @@ describe('POST workpad', () => { expect(response.status).toBe(400); }); + + it(`returns 200 when a template is cloned`, async () => { + const cloneFromTemplateBody = { + templateId: 'template-id', + }; + + const mockTemplateResponse = { + attributes: { + id: 'template-id', + template: { + pages: [], + }, + }, + }; + + (mockRouteContext.core.savedObjects.client.get as jest.Mock).mockResolvedValue( + mockTemplateResponse + ); + + const request = httpServerMock.createKibanaRequest({ + method: 'post', + path: 'api/canvas/workpad', + body: cloneFromTemplateBody, + }); + + const response = await routeHandler(mockRouteContext, request, kibanaResponseFactory); + + expect(response.status).toBe(200); + expect(response.payload).toEqual({ ok: true, id: `workpad-${mockedUUID}` }); + expect(mockRouteContext.core.savedObjects.client.create).toBeCalledWith( + CANVAS_TYPE, + { + ...mockTemplateResponse.attributes.template, + '@timestamp': nowIso, + '@created': nowIso, + }, + { + id: `workpad-${mockedUUID}`, + } + ); + }); });