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

feat(installer): implementation of snippet config getter #1378

Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -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);
});
});
19 changes: 19 additions & 0 deletions packages/x-components/src/x-installer/api/api.types.ts
lauramargar marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -48,6 +58,15 @@ export interface XAPI {
*/
setSnippetConfig(config: Partial<SnippetConfig>): 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.
*
Expand Down
22 changes: 21 additions & 1 deletion packages/x-components/src/x-installer/api/base-api.ts
Original file line number Diff line number Diff line change
@@ -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}.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down Expand Up @@ -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!;
}
}