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: implement css injector in archetype #308

Merged
merged 9 commits into from
Aug 9, 2023
Merged
Show file tree
Hide file tree
Changes from 8 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
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
2 changes: 1 addition & 1 deletion tests/e2e/support/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ const customCommands: CustomCommands = {
cy.typeQuery(query).type('{enter}');
});
},
typeQuery: query => cy.getByDataTest('search-input').type(query),
typeQuery: query => cy.getByDataTest('search-input').type(query, { force: true }),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

needed but not desirable.
Seems to be working without forcing it in newer Cypress versions cypress-io/cypress#21433

focusSearchInput: () => cy.getByDataTest('search-input').click(),
clearSearchInput: () => cy.getByDataTest('clear-search-input').click()
};
Expand Down