From a76e1b716b9aa86fc6853bcf4d1bb7aaa9478f4a Mon Sep 17 00:00:00 2001 From: lauramargar <114984466+lauramargar@users.noreply.github.com> Date: Fri, 29 Dec 2023 10:07:00 +0100 Subject: [PATCH] feat(x-plugin): add snippet config getter to XAPI (#1378) --- .../api/__tests__/default-api.spec.ts | 16 +++++++++++++- .../src/x-installer/api/api.types.ts | 19 ++++++++++++++++ .../src/x-installer/api/base-api.ts | 22 ++++++++++++++++++- .../x-installer/__tests__/x-installer.spec.ts | 17 ++++++++++++++ .../x-installer/x-installer/x-installer.ts | 12 ++++++++++ 5 files changed, 84 insertions(+), 2 deletions(-) diff --git a/packages/x-components/src/x-installer/api/__tests__/default-api.spec.ts b/packages/x-components/src/x-installer/api/__tests__/default-api.spec.ts index ee36e681f0..bd1f4ccf8b 100644 --- a/packages/x-components/src/x-installer/api/__tests__/default-api.spec.ts +++ b/packages/x-components/src/x-installer/api/__tests__/default-api.spec.ts @@ -1,7 +1,7 @@ import { createLocalVue } from '@vue/test-utils'; import { XComponentsAdapterDummy } from '../../../__tests__/adapter.dummy'; import { XInstaller } from '../../x-installer/x-installer'; -import { SnippetConfig } from '../api.types'; +import { NormalisedSnippetConfig, SnippetConfig } from '../api.types'; import { BaseXAPI } from '../base-api'; import { XDummyBus } from '../../../__tests__/bus.dummy'; @@ -104,4 +104,18 @@ describe('testing default X API', () => { await vue.nextTick(); expect(storeElement).toHaveTextContent('Portugal'); }); + + it('should allow set the snippetConfig getter', () => { + const snippetConfig: NormalisedSnippetConfig = { + instance: 'test', + scope: 'test', + lang: 'es', + uiLang: 'es' + }; + defaultXAPI?.setSnippetConfigGetter(() => snippetConfig); + + const snippet = defaultXAPI?.getSnippetConfig(); + + expect(snippet).toEqual(snippetConfig); + }); }); diff --git a/packages/x-components/src/x-installer/api/api.types.ts b/packages/x-components/src/x-installer/api/api.types.ts index 20396732cc..728374581c 100644 --- a/packages/x-components/src/x-installer/api/api.types.ts +++ b/packages/x-components/src/x-installer/api/api.types.ts @@ -21,6 +21,16 @@ export interface XAPI { * getting the previously stored result information. */ addProductToCart(productId?: string): void; + + /** + * To get the snippet config object. + * + * @returns The {@link NormalisedSnippetConfig | snippetConfig} object. + * + * @public + */ + getSnippetConfig(): SnippetConfig; + /** * To set the {@link @empathyco/x-bus#XBus | bus} to the API. This bus will be used to emit * the necessary events. @@ -48,6 +58,15 @@ export interface XAPI { */ setSnippetConfig(config: Partial): void; + /** + * To set or update any property of the snippet config getter. + * + * @param snippetConfigGetter - A function that returns the snippet config object. + * + * @internal + */ + setSnippetConfigGetter(snippetConfigGetter: () => NormalisedSnippetConfig): void; + /** * To set the snippet config callback. * diff --git a/packages/x-components/src/x-installer/api/base-api.ts b/packages/x-components/src/x-installer/api/base-api.ts index f6da23d3b2..34f1b34c5c 100644 --- a/packages/x-components/src/x-installer/api/base-api.ts +++ b/packages/x-components/src/x-installer/api/base-api.ts @@ -1,6 +1,6 @@ import { XBus } from '@empathyco/x-bus'; import { WireMetadata, XEventsTypes } from '../../wiring/index'; -import { SnippetConfig, XAPI } from './api.types'; +import { NormalisedSnippetConfig, SnippetConfig, XAPI } from './api.types'; /** * Default implementation for {@link XAPI}. @@ -30,6 +30,15 @@ export class BaseXAPI implements XAPI { */ protected initCallback!: (config: SnippetConfig) => any; + /** + * Getter for the snippet config object. + * + * @returns The {@link NormalisedSnippetConfig | snippetConfig} object. + * + * @public + */ + public getSnippetConfig!: () => SnippetConfig; + /** * Callback that allows to update the snippet config. The logic of initialization is out of this * API since this API is just a facade. @@ -78,6 +87,17 @@ export class BaseXAPI implements XAPI { this.snippetCallback = snippetCallback; } + /** + * Sets or updates the snippet config getter. + * + * @param snippetConfigGetter - A function that returns the snippet config. + * + * @internal + */ + setSnippetConfigGetter(snippetConfigGetter: () => NormalisedSnippetConfig): void { + this.getSnippetConfig = snippetConfigGetter; + } + /** * Sets or updates the snippet config. * diff --git a/packages/x-components/src/x-installer/x-installer/__tests__/x-installer.spec.ts b/packages/x-components/src/x-installer/x-installer/__tests__/x-installer.spec.ts index cb556e12ee..f0c11b4244 100644 --- a/packages/x-components/src/x-installer/x-installer/__tests__/x-installer.spec.ts +++ b/packages/x-components/src/x-installer/x-installer/__tests__/x-installer.spec.ts @@ -188,6 +188,23 @@ describe('testing `XInstaller` utility', () => { expect(app?.$el).toHaveTextContent('test-2'); }); + it('should set the snippetConfig getter', async () => { + const vue = createLocalVue(); + window.initX = getMinimumSnippetConfig(); + await new XInstaller({ + adapter, + vue, + app: createSnippetConfigComponent() + }).init(); + const snippetConfig = window.InterfaceX?.getSnippetConfig(); + expect(snippetConfig).toEqual({ + instance: 'test', + lang: 'test', + scope: 'test', + uiLang: 'test' + }); + }); + // eslint-disable-next-line max-len it('initializes the app when window.initX is a function retrieving the snippet config', async () => { const vue = createLocalVue(); diff --git a/packages/x-components/src/x-installer/x-installer/x-installer.ts b/packages/x-components/src/x-installer/x-installer/x-installer.ts index 372a370604..611e09d2a1 100644 --- a/packages/x-components/src/x-installer/x-installer/x-installer.ts +++ b/packages/x-components/src/x-installer/x-installer/x-installer.ts @@ -126,6 +126,7 @@ export class XInstaller { this.api = api ?? new BaseXAPI(); this.api.setInitCallback(this.init.bind(this)); this.api.setSnippetConfigCallback(this.updateSnippetConfig.bind(this)); + this.api.setSnippetConfigGetter(this.getSnippetConfig.bind(this)); window.InterfaceX = this.api; } } @@ -355,4 +356,15 @@ export class XInstaller { this.getVue().set(this.snippetConfig!, name, value); }); } + + /** + * Getter for the snippet config object. + * + * @returns The {@link NormalisedSnippetConfig | snippetConfig} object. + * + * @public + */ + protected getSnippetConfig(): NormalisedSnippetConfig { + return this.snippetConfig!; + } }