Skip to content

Commit

Permalink
feat: implement css injector in archetype (#308)
Browse files Browse the repository at this point in the history
EMP-1230

Co-authored-by: Mavi Fdez <[email protected]>
  • Loading branch information
CachedaCodes and mavmaf authored Aug 9, 2023
1 parent 43b96a3 commit 8245344
Show file tree
Hide file tree
Showing 9 changed files with 77 additions and 15 deletions.
3 changes: 2 additions & 1 deletion cypress.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@
"screenshotOnRunFailure": false,
"video": false,
"testFiles": "**/*.feature",
"retries": 1
"retries": 1,
"includeShadowDom": true
}
37 changes: 30 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"dependencies": {
"@empathyco/x-adapter": "^8.0.0-alpha.33",
"@empathyco/x-adapter-platform": "^1.0.0-alpha.83",
"@empathyco/x-archetype-utils": "^0.1.0-alpha.15",
"@empathyco/x-archetype-utils": "^1.0.0-alpha.3",
"@empathyco/x-components": "^3.0.0-alpha.400",
"@empathyco/x-deep-merge": "^1.3.0-alpha.29",
"@empathyco/x-types": "^10.0.0-alpha.72",
Expand Down
8 changes: 8 additions & 0 deletions public/snippet-script.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ function getEnv() {
return undefined;
}

function getIsolationStrategy() {
const isolation = popFromURLParameters('isolation');

return isolation === undefined ? undefined : isolation === 'true';
}

const instance = popFromURLParameters('instance') || 'empathy';
const env = getEnv();
const scope = popFromURLParameters('scope') || 'desktop';
Expand All @@ -54,6 +60,7 @@ const currency = popFromURLParameters('currency') || 'EUR';
const consent = popFromURLParameters('consent') !== 'false';
const documentDirection = popFromURLParameters('doc-dir') || 'ltr';
const store = popFromURLParameters('store') || undefined;
const isolate = getIsolationStrategy();
popFromURLParameters('query'); // prevent the query from be included as extra param
popFromURLParameters('filter'); // Prevent the filters to be included as extra param

Expand All @@ -69,6 +76,7 @@ window.initX = {
consent,
documentDirection,
store,
isolate,
...URLParameters,
queriesPreview: [
{
Expand Down
2 changes: 2 additions & 0 deletions rollup.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { createConfig } from './build/instrumentation.build';
import { rollupCssInjectorConfig } from "@empathyco/x-archetype-utils";

export default createConfig({
/*
Expand All @@ -19,5 +20,6 @@ export default createConfig({
},
plugins: {
// Modify plugins options here.
...rollupCssInjectorConfig
}
});
2 changes: 2 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { CssInjector } from "@empathyco/x-archetype-utils";
import { XInstaller } from '@empathyco/x-components';
import Vue from 'vue';
import { installXOptions } from './x-components/plugin.options';
Expand All @@ -11,4 +12,5 @@ declare global {
Vue.config.productionTip = false;
Vue.config.devtools = window.__enableVueDevtools__ ?? false;

new CssInjector(true);
new XInstaller(installXOptions).init();
28 changes: 26 additions & 2 deletions src/x-components/plugin.options.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { InstallXOptions } from '@empathyco/x-components';
import { I18n } from '@empathyco/x-archetype-utils';
import { InstallXOptions, SnippetConfig } from '@empathyco/x-components';
import { I18n, cssInjector } from '@empathyco/x-archetype-utils';
import App from '../App.vue';
import * as messages from '../i18n/messages';
import store from '../store';
Expand All @@ -11,6 +11,7 @@ export const installXOptions: InstallXOptions = {
adapter,
store,
app: App,
domElement: getDomElement,
xModules: {
facets: {
config: {
Expand Down Expand Up @@ -40,3 +41,26 @@ export const installXOptions: InstallXOptions = {
};
}
};

/**
* Creates a DOM element to mount the X Components app.
*
* @param snippetConfig - The snippet configuration.
* @returns The DOM element.
*/
function getDomElement({ isolate }: SnippetConfig): Element {
const domElement = document.createElement('div');

if (isolate || process.env.NODE_ENV === 'production') {
const container = document.createElement('div');
const shadowRoot = container.attachShadow({ mode: 'open' });
shadowRoot.appendChild(domElement);
document.body.appendChild(container);
cssInjector.setHost(shadowRoot);
} else {
document.body.appendChild(domElement);
cssInjector.setHost(document.head);
}

return domElement;
}
2 changes: 2 additions & 0 deletions tests/e2e/cucumber/common-steps.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@ Then('search bar is clicked', () => {

// Search
When('a {string} is typed', (query: string) => {
cy.getByDataTest('search-input').should('exist').click();
cy.typeQuery(query).then(() => {
cy.getByDataTest('search-input').invoke('val').as('searchedQuery');
});
});

When('{string} is searched', (query: string) => {
cy.getByDataTest('search-input').should('exist').click();
cy.searchQuery(query).then(() => {
cy.getByDataTest('search-input').invoke('val').as('searchedQuery');
});
Expand Down
8 changes: 4 additions & 4 deletions tests/e2e/support/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,14 +101,14 @@ type AddPreviousParam<Functions extends Record<keyof Functions, AnyFunction>> =
type CypressCommandOptions = Partial<Loggable & Timeoutable & Withinable & Shadow>;

const customCommands: CustomCommands = {
searchQuery: query => cy.typeQuery(query).type('{enter}'),
searchQuery: query => cy.typeQuery(query).type('{enter}', { force: true }),
searchQueries: (...queries) => {
queries.forEach(query => {
cy.getByDataTest('search-input').clear();
cy.typeQuery(query).type('{enter}');
cy.getByDataTest('search-input').clear({ force: true });
cy.typeQuery(query).type('{enter}', { force: true });
});
},
typeQuery: query => cy.getByDataTest('search-input').type(query),
typeQuery: query => cy.getByDataTest('search-input').type(query, { force: true }),
focusSearchInput: () => cy.getByDataTest('search-input').click(),
clearSearchInput: () => cy.getByDataTest('clear-search-input').click()
};
Expand Down

0 comments on commit 8245344

Please sign in to comment.