|
8 | 8 | */ |
9 | 9 |
|
10 | 10 | import React from 'react'; |
11 | | -import { render, screen, waitFor } from '@testing-library/react'; |
12 | | -import { embeddablePluginMock } from '@kbn/embeddable-plugin/public/mocks'; |
13 | | -import { setStubKibanaServices } from '@kbn/presentation-panel-plugin/public/mocks'; |
| 11 | +import { render, screen } from '@testing-library/react'; |
14 | 12 | import { EuiThemeProvider } from '@elastic/eui'; |
15 | 13 | import { getLinksEmbeddableFactory } from './links_embeddable'; |
16 | 14 | import type { LinksEmbeddableState } from '../../common'; |
17 | 15 | import { LINKS_EMBEDDABLE_TYPE } from '../../common'; |
18 | 16 | import type { Link } from '../../server'; |
19 | | -import { EmbeddableRenderer } from '@kbn/embeddable-plugin/public'; |
20 | | -import type { LinksApi, LinksParentApi, ResolvedLink } from '../types'; |
| 17 | +import type { LinksApi, ResolvedLink } from '../types'; |
21 | 18 | import { linksClient } from '../content_management'; |
22 | 19 | import { getMockLinksParentApi } from '../mocks'; |
23 | 20 |
|
@@ -118,144 +115,128 @@ jest.mock('../content_management/load_from_library', () => { |
118 | 115 | }; |
119 | 116 | }); |
120 | 117 |
|
121 | | -const renderEmbeddable = ( |
122 | | - parent: LinksParentApi, |
123 | | - overrides?: { |
124 | | - onApiAvailable: (api: LinksApi) => void; |
125 | | - } |
126 | | -) => { |
127 | | - return render( |
128 | | - <EuiThemeProvider> |
129 | | - <EmbeddableRenderer<LinksEmbeddableState, LinksApi> |
130 | | - type={LINKS_EMBEDDABLE_TYPE} |
131 | | - onApiAvailable={jest.fn()} |
132 | | - getParentApi={jest.fn().mockReturnValue(parent)} |
133 | | - {...overrides} |
134 | | - /> |
135 | | - </EuiThemeProvider> |
136 | | - ); |
137 | | -}; |
138 | | - |
139 | | -describe('getLinksEmbeddableFactory', () => { |
| 118 | +async function buildLinksEmbeddable(state: LinksEmbeddableState) { |
140 | 119 | const factory = getLinksEmbeddableFactory(); |
141 | | - |
142 | | - beforeAll(() => { |
143 | | - const embeddable = embeddablePluginMock.createSetupContract(); |
144 | | - embeddable.registerReactEmbeddableFactory(LINKS_EMBEDDABLE_TYPE, async () => { |
145 | | - return factory; |
146 | | - }); |
147 | | - setStubKibanaServices(); |
| 120 | + const parentApi = getMockLinksParentApi(state); |
| 121 | + const uuid = '1234'; |
| 122 | + return await factory.buildEmbeddable({ |
| 123 | + initialState: { |
| 124 | + rawState: state, |
| 125 | + }, |
| 126 | + finalizeApi: (api) => { |
| 127 | + return { |
| 128 | + ...api, |
| 129 | + uuid, |
| 130 | + parentApi, |
| 131 | + type: LINKS_EMBEDDABLE_TYPE, |
| 132 | + } as LinksApi; |
| 133 | + }, |
| 134 | + parentApi, |
| 135 | + uuid, |
148 | 136 | }); |
| 137 | +} |
149 | 138 |
|
| 139 | +describe('getLinksEmbeddableFactory', () => { |
150 | 140 | describe('by reference embeddable', () => { |
151 | | - const parent = getMockLinksParentApi({ |
| 141 | + const byRefState: LinksEmbeddableState = { |
152 | 142 | title: 'my links', |
153 | 143 | description: 'just a few links', |
154 | 144 | hidePanelTitles: false, |
155 | 145 | savedObjectId: '123', |
156 | | - }); |
| 146 | + }; |
157 | 147 |
|
158 | 148 | test('component renders', async () => { |
159 | | - renderEmbeddable(parent); |
| 149 | + const { Component } = await buildLinksEmbeddable(byRefState); |
| 150 | + render( |
| 151 | + <EuiThemeProvider> |
| 152 | + <Component /> |
| 153 | + </EuiThemeProvider> |
| 154 | + ); |
160 | 155 | expect(await screen.findByTestId('links--component')).toBeInTheDocument(); |
161 | 156 | }); |
162 | 157 |
|
163 | 158 | test('api methods', async () => { |
164 | | - const onApiAvailable = jest.fn() as jest.MockedFunction<(api: LinksApi) => void>; |
165 | | - renderEmbeddable(parent, { onApiAvailable }); |
166 | | - |
167 | | - await waitFor(async () => { |
168 | | - const api = onApiAvailable.mock.calls[0][0]; |
169 | | - expect(api.serializeState()).toEqual({ |
170 | | - rawState: { |
171 | | - title: 'my links', |
172 | | - description: 'just a few links', |
173 | | - hidePanelTitles: false, |
174 | | - savedObjectId: '123', |
175 | | - }, |
176 | | - }); |
177 | | - expect(await api.canUnlinkFromLibrary()).toBe(true); |
178 | | - expect(api.defaultTitle$?.value).toBe('links 001'); |
179 | | - expect(api.defaultDescription$?.value).toBe('some links'); |
| 159 | + const { api } = await buildLinksEmbeddable(byRefState); |
| 160 | + expect(api.serializeState()).toEqual({ |
| 161 | + rawState: { |
| 162 | + title: 'my links', |
| 163 | + description: 'just a few links', |
| 164 | + hidePanelTitles: false, |
| 165 | + savedObjectId: '123', |
| 166 | + }, |
180 | 167 | }); |
| 168 | + expect(await api.canUnlinkFromLibrary()).toBe(true); |
| 169 | + expect(api.defaultTitle$?.value).toBe('links 001'); |
| 170 | + expect(api.defaultDescription$?.value).toBe('some links'); |
181 | 171 | }); |
182 | 172 |
|
183 | 173 | test('unlink from library', async () => { |
184 | | - const onApiAvailable = jest.fn() as jest.MockedFunction<(api: LinksApi) => void>; |
185 | | - renderEmbeddable(parent, { onApiAvailable }); |
186 | | - |
187 | | - await waitFor(async () => { |
188 | | - const api = onApiAvailable.mock.calls[0][0]; |
189 | | - expect(api.getSerializedStateByValue()).toEqual({ |
190 | | - rawState: { |
191 | | - title: 'my links', |
192 | | - description: 'just a few links', |
193 | | - hidePanelTitles: false, |
194 | | - links: getLinks(), |
195 | | - layout: 'vertical', |
196 | | - }, |
197 | | - }); |
| 174 | + const { api } = await buildLinksEmbeddable(byRefState); |
| 175 | + expect(api.getSerializedStateByValue()).toEqual({ |
| 176 | + rawState: { |
| 177 | + title: 'my links', |
| 178 | + description: 'just a few links', |
| 179 | + hidePanelTitles: false, |
| 180 | + links: getLinks(), |
| 181 | + layout: 'vertical', |
| 182 | + }, |
198 | 183 | }); |
199 | 184 | }); |
200 | 185 | }); |
201 | 186 |
|
202 | 187 | describe('by value embeddable', () => { |
203 | | - const parent = getMockLinksParentApi({ |
| 188 | + const byValueState: LinksEmbeddableState = { |
204 | 189 | description: 'just a few links', |
205 | 190 | title: 'my links', |
206 | 191 | hidePanelTitles: true, |
207 | 192 | links: getLinks(), |
208 | 193 | layout: 'horizontal', |
209 | | - }); |
| 194 | + }; |
210 | 195 |
|
211 | 196 | test('component renders', async () => { |
212 | | - renderEmbeddable(parent); |
| 197 | + const { Component } = await buildLinksEmbeddable(byValueState); |
| 198 | + render( |
| 199 | + <EuiThemeProvider> |
| 200 | + <Component /> |
| 201 | + </EuiThemeProvider> |
| 202 | + ); |
213 | 203 |
|
214 | 204 | expect(await screen.findByTestId('links--component')).toBeInTheDocument(); |
215 | 205 | }); |
216 | 206 |
|
217 | 207 | test('api methods', async () => { |
218 | | - const onApiAvailable = jest.fn() as jest.MockedFunction<(api: LinksApi) => void>; |
219 | | - renderEmbeddable(parent, { onApiAvailable }); |
220 | | - |
221 | | - await waitFor(async () => { |
222 | | - const api = onApiAvailable.mock.calls[0][0]; |
223 | | - expect(api.serializeState()).toEqual({ |
224 | | - rawState: { |
225 | | - title: 'my links', |
226 | | - description: 'just a few links', |
227 | | - hidePanelTitles: true, |
228 | | - links: getLinks(), |
229 | | - layout: 'horizontal', |
230 | | - }, |
231 | | - }); |
232 | | - |
233 | | - expect(await api.canLinkToLibrary()).toBe(true); |
| 208 | + const { api } = await buildLinksEmbeddable(byValueState); |
| 209 | + expect(api.serializeState()).toEqual({ |
| 210 | + rawState: { |
| 211 | + title: 'my links', |
| 212 | + description: 'just a few links', |
| 213 | + hidePanelTitles: true, |
| 214 | + links: getLinks(), |
| 215 | + layout: 'horizontal', |
| 216 | + }, |
234 | 217 | }); |
| 218 | + |
| 219 | + expect(await api.canLinkToLibrary()).toBe(true); |
235 | 220 | }); |
236 | | - test('save to library', async () => { |
237 | | - const onApiAvailable = jest.fn() as jest.MockedFunction<(api: LinksApi) => void>; |
238 | | - renderEmbeddable(parent, { onApiAvailable }); |
239 | 221 |
|
240 | | - await waitFor(async () => { |
241 | | - const api = onApiAvailable.mock.calls[0][0]; |
242 | | - const newId = await api.saveToLibrary('some new title'); |
243 | | - expect(linksClient.create).toHaveBeenCalledWith({ |
244 | | - data: { |
245 | | - title: 'some new title', |
246 | | - links: getLinks(), |
247 | | - layout: 'horizontal', |
248 | | - }, |
249 | | - }); |
250 | | - expect(newId).toBe('333'); |
251 | | - expect(api.getSerializedStateByReference(newId)).toEqual({ |
252 | | - rawState: { |
253 | | - title: 'my links', |
254 | | - description: 'just a few links', |
255 | | - hidePanelTitles: true, |
256 | | - savedObjectId: '333', |
257 | | - }, |
258 | | - }); |
| 222 | + test('save to library', async () => { |
| 223 | + const { api } = await buildLinksEmbeddable(byValueState); |
| 224 | + const newId = await api.saveToLibrary('some new title'); |
| 225 | + expect(linksClient.create).toHaveBeenCalledWith({ |
| 226 | + data: { |
| 227 | + title: 'some new title', |
| 228 | + links: getLinks(), |
| 229 | + layout: 'horizontal', |
| 230 | + }, |
| 231 | + }); |
| 232 | + expect(newId).toBe('333'); |
| 233 | + expect(api.getSerializedStateByReference(newId)).toEqual({ |
| 234 | + rawState: { |
| 235 | + title: 'my links', |
| 236 | + description: 'just a few links', |
| 237 | + hidePanelTitles: true, |
| 238 | + savedObjectId: '333', |
| 239 | + }, |
259 | 240 | }); |
260 | 241 | }); |
261 | 242 | }); |
|
0 commit comments