Skip to content

Commit

Permalink
feat(plugin): replace old bus implementation using the new `x-priorit…
Browse files Browse the repository at this point in the history
…y-bus` (#1086)

EX-7283

---------

Co-authored-by: Beltrán Garcia Balbuena <[email protected]>
Co-authored-by: acondal <[email protected]>
  • Loading branch information
3 people authored Mar 16, 2023
1 parent fbe7d46 commit f64f9b6
Show file tree
Hide file tree
Showing 104 changed files with 3,599 additions and 9,016 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ jobs:
build:
runs-on: ubuntu-latest
container:
image: cypress/browsers:node16.16.0-chrome105-ff104-edge
image: cypress/browsers:node-16.18.1-chrome-110.0.5481.96-1-ff-109.0-edge-110.0.1587.41-1
# https://github.com/cypress-io/github-action#firefox Cypress FF image needs this user permissions to be able to install dependencies
options: --user 1001

Expand Down
2 changes: 1 addition & 1 deletion packages/react-wrapper/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
"@types/jest": "~27.0.3",
"@types/react": "~16.9.0",
"@types/react-dom": "~16.9.0",
"cypress": "~12.2.0",
"cypress": "~12.8.0",
"eslint-plugin-react": "~7.32.1",
"jest": "~27.3.1",
"react": "~16.9.0",
Expand Down
1 change: 1 addition & 0 deletions packages/x-archetype-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
},
"dependencies": {
"@empathyco/x-deep-merge": "^1.3.0-alpha.33",
"tslib": "~2.4.1",
"vue-i18n": "~8.28.2"
},
"devDependencies": {
Expand Down
6 changes: 3 additions & 3 deletions packages/x-bus/src/x-bus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ export class XPriorityBus<SomeEvents extends Dictionary, SomeEventMetadata exten
event: SomeEvent,
withMetadata = false
): typeof withMetadata extends true
? Observable<SubjectPayload<SomeEvents, SomeEvent, SomeEventMetadata>>
? Observable<SubjectPayload<EventPayload<SomeEvents, SomeEvent>, SomeEventMetadata>>
: Observable<EventPayload<SomeEvents, SomeEvent>> {
// TODO: This type should work, but inference isn't working as expected. Check when updating ts.
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
Expand All @@ -239,7 +239,7 @@ export class XPriorityBus<SomeEvents extends Dictionary, SomeEventMetadata exten
? this.getEmitter(event).asObservable()
: this.getEmitter(event).pipe(
map<
SubjectPayload<SomeEvents, SomeEvent, SomeEventMetadata>,
SubjectPayload<EventPayload<SomeEvents, SomeEvent>, SomeEventMetadata>,
EventPayload<SomeEvents, SomeEvent>
>(value => value.eventPayload)
);
Expand Down Expand Up @@ -277,7 +277,7 @@ export class XPriorityBus<SomeEvents extends Dictionary, SomeEventMetadata exten
*/
protected createEmitter<SomeEvent extends keyof SomeEvents>(event: SomeEvent): void {
this.emitters[event] = new ReplaySubject<
SubjectPayload<SomeEvents, SomeEvent, SomeEventMetadata>
SubjectPayload<EventPayload<SomeEvents, SomeEvent>, SomeEventMetadata>
>(1);
}
}
12 changes: 4 additions & 8 deletions packages/x-bus/src/x-bus.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,9 @@ export type EventPayload<
*
* @public
*/
export interface SubjectPayload<
SomeEvents extends Dictionary,
SomeEvent extends keyof SomeEvents,
SomeEventMetadata extends Dictionary
> {
export interface SubjectPayload<SomePayload, SomeEventMetadata extends Dictionary> {
/** The payload of the event. */
eventPayload: EventPayload<SomeEvents, SomeEvent>;
eventPayload: SomePayload;
/** Extra data of the event. */
metadata: SomeEventMetadata;
}
Expand All @@ -49,7 +45,7 @@ export type Emitter<
SomeEvents extends Dictionary,
SomeEvent extends keyof SomeEvents,
SomeEventMetadata extends Dictionary
> = Subject<SubjectPayload<SomeEvents, SomeEvent, SomeEventMetadata>>;
> = Subject<SubjectPayload<EventPayload<SomeEvents, SomeEvent>, SomeEventMetadata>>;

/**
* Represents a dictionary where the key is an event name and its value is an {@link Emitter}.
Expand Down Expand Up @@ -165,6 +161,6 @@ export interface XBus<SomeEvents extends Dictionary, SomeEventMetadata extends D
event: SomeEvent,
withMetadata?: boolean
): typeof withMetadata extends true
? Observable<SubjectPayload<SomeEvents, SomeEvent, SomeEventMetadata>>
? Observable<SubjectPayload<EventPayload<SomeEvents, SomeEvent>, SomeEventMetadata>>
: Observable<EventPayload<SomeEvents, SomeEvent>>;
}
3 changes: 2 additions & 1 deletion packages/x-components/cypress.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ export default defineConfig({
);
return config;
},
specPattern: 'tests/e2e/**/*.feature'
specPattern: 'tests/e2e/**/*.feature',
experimentalRunAllSpecs: true
},
component: {
specPattern: 'tests/unit/**/*.spec.ts',
Expand Down
9 changes: 5 additions & 4 deletions packages/x-components/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
"lint": "eslint . --ext .ts,.vue",
"test:unit": "jest --silent && pnpm run test:unit-cypress",
"test:unit-jest": "jest",
"test:unit-cypress": "cypress run --component",
"test:unit-cypress": "cypress run --component --browser chrome",
"test:unit-coverage": "jest --coverage",
"test:unit-watch": "jest --watch",
"test:e2e": "vue-cli-service test:e2e --e2e --browser chrome",
Expand All @@ -69,6 +69,7 @@
"dependencies": {
"@empathyco/x-adapter": "^8.0.0-alpha.26",
"@empathyco/x-adapter-platform": "^1.0.0-alpha.66",
"@empathyco/x-bus": "^0.1.0-alpha.9",
"@empathyco/x-deep-merge": "^1.3.0-alpha.33",
"@empathyco/x-logger": "^1.2.0-alpha.10",
"@empathyco/x-storage-service": "^2.0.0-alpha.10",
Expand All @@ -89,7 +90,7 @@
"devDependencies": {
"@badeball/cypress-cucumber-preprocessor": "~15.0.0",
"@bahmutov/cypress-esbuild-preprocessor": "~2.2.0",
"@cypress/vue": "~2.2.4",
"@cypress/vue2": "~2.0.1",
"@empathyco/x-tailwindcss": "^1.0.0-alpha.15",
"@microsoft/api-documenter": "~7.21.3",
"@microsoft/api-extractor": "~7.33.7",
Expand All @@ -108,8 +109,8 @@
"@vue/vue2-jest": "~27.0.0-alpha.3",
"autoprefixer": "~10.4.4",
"convert-source-map": "~2.0.0",
"cypress": "~12.2.0",
"esbuild": "0.17.5",
"cypress": "~12.8.0",
"esbuild": "0.17.10",
"glob": "~7.1.6",
"jest": "~27.3.1",
"jest-scss-transform": "~1.0.1",
Expand Down
24 changes: 24 additions & 0 deletions packages/x-components/src/__tests__/bus.dummy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { Dictionary } from '@empathyco/x-utils';
import { EmittedData, EventPayload, XPriorityBus } from '@empathyco/x-bus';
import { WireMetadata, XEventsTypes } from '../wiring';

export class XDummyBus<
SomeEvents extends Dictionary = XEventsTypes,
SomeMetadata extends Dictionary = WireMetadata
> extends XPriorityBus<SomeEvents, SomeMetadata> {
emit<SomeEvent extends keyof SomeEvents>(
event: SomeEvent,
payload?: EventPayload<SomeEvents, SomeEvent>,
metadata = {} as SomeMetadata
): Promise<EmittedData<SomeEvents, SomeEvent, SomeMetadata>> {
const emitter = this.getEmitter(event);
const emittedPayload = {
eventPayload: payload as EventPayload<SomeEvents, SomeEvent>,
metadata
};

emitter.next(emittedPayload);

return Promise.resolve({ event, ...emittedPayload });
}
}
4 changes: 2 additions & 2 deletions packages/x-components/src/__tests__/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ import {
SearchResponse
} from '@empathyco/x-types';
import { XPluginOptions } from '../plugins';
import { BaseXBus } from '../plugins/x-bus';
import { XPlugin } from '../plugins/x-plugin';
import { ActionsDictionary } from '../store/actions.types';
import { MutationsDictionary } from '../store/mutations.types';
import { RootXStoreState, XStoreModule } from '../store/store.types';
import { cleanGettersProxyCache } from '../store/utils/getters-proxy.utils';
import { ExtractState, XModule, XModuleName } from '../x-modules/x-modules.types';
import { XComponentsAdapterDummy } from './adapter.dummy';
import { XDummyBus } from './bus.dummy';
import Mock = jest.Mock;

export type MockedXComponentsAdapter = {
Expand Down Expand Up @@ -190,7 +190,7 @@ export function installNewXPlugin(
localVue: typeof Vue = createLocalVue()
): [XPlugin, typeof Vue] {
XPlugin.resetInstance();
const xPlugin = new XPlugin(new BaseXBus());
const xPlugin = new XPlugin(new XDummyBus());
const installOptions: XPluginOptions = {
adapter: XComponentsAdapterDummy,
...options
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ describe('testing GlobalXBus component', function () {
expect(acceptedAQueryCallback).toHaveBeenCalledTimes(1);
expect(acceptedAQueryCallback).toHaveBeenCalledWith('lego', {
location: undefined,
moduleName: null
moduleName: null,
replaceable: true
});

expect(clickedColumnPickerCallback).not.toHaveBeenCalled();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ describe('testing SnippetCallbacks component', () => {
expect(acceptedAQueryCallback).toHaveBeenCalledTimes(1);
expect(acceptedAQueryCallback).toHaveBeenCalledWith('lego', {
location: undefined,
moduleName: null
moduleName: null,
replaceable: true
});

expect(clickedColumnPickerCallback).not.toHaveBeenCalled();
Expand All @@ -48,7 +49,8 @@ describe('testing SnippetCallbacks component', () => {
expect(clickedColumnPickerCallback).toHaveBeenCalledTimes(1);
expect(clickedColumnPickerCallback).toHaveBeenCalledWith(1, {
location: undefined,
moduleName: null
moduleName: null,
replaceable: true
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@
/**
* Trigger navigation if this component is in control of it.
*
* @param eventPayload - The {@link WirePayload.eventPayload}.
* @param metadata - The {@link WirePayload.metadata}.
* @param eventPayload - The {@link @empathyco/x-bus#SubjectPayload.eventPayload}.
* @param metadata - The {@link @empathyco/x-bus#SubjectPayload.metadata}.
* @public
*/
@XOn(component => (component as BaseKeyboardNavigation).navigationHijackerEvents)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,9 @@ describe('testing BaseColumnPickerDropdown', () => {
expect(listenerColumnPicker).toHaveBeenNthCalledWith(1, {
eventPayload: 3,
metadata: {
moduleName: null
moduleName: null,
location: undefined,
replaceable: true
}
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,9 @@ describe('testing Base Column Picker List', () => {
expect(listenerColumnPicker).toHaveBeenNthCalledWith(1, {
eventPayload: 3,
metadata: {
moduleName: null
moduleName: null,
location: undefined,
replaceable: true
}
});
});
Expand All @@ -94,7 +96,9 @@ describe('testing Base Column Picker List', () => {
eventPayload: columns[index],
metadata: {
moduleName: null, // no module registered for this base component
target: wrapper.findAll(getDataTestSelector('column-picker-button')).at(index).element
target: wrapper.findAll(getDataTestSelector('column-picker-button')).at(index).element,
location: undefined,
replaceable: true
}
});
/* 1st event is to sync the initial value
Expand All @@ -105,7 +109,9 @@ describe('testing Base Column Picker List', () => {
eventPayload: columns[index],
metadata: {
moduleName: null, // no module registered for this base component
target: wrapper.findAll(getDataTestSelector('column-picker-button')).at(index).element
target: wrapper.findAll(getDataTestSelector('column-picker-button')).at(index).element,
location: undefined,
replaceable: true
}
});
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { AnyFunction } from '@empathyco/x-utils';
import { Subscription } from 'rxjs';
import { Observable, Subscription } from 'rxjs';
import { filter } from 'rxjs/operators';
import Vue, { WatchOptions } from 'vue';
import { createDecorator } from 'vue-class-component';
import { EventPayload, SubjectPayload } from '@empathyco/x-bus';
import { clone } from '../../utils/clone';
import { DecoratorFor } from '../../utils/types';
import { XEvent, XEventPayload } from '../../wiring/events.types';
import { XEvent, XEventPayload, XEventsTypes } from '../../wiring/events.types';
import { WireMetadata } from '../../wiring/wiring.types';

/**
Expand Down Expand Up @@ -84,8 +85,11 @@ function createSubscription<Event extends XEvent>(
const subscription = new Subscription();
eventArray.forEach(xEvent => {
subscription.add(
this.$x
.on(xEvent, true)
(
this.$x.on(xEvent, true) as unknown as Observable<
SubjectPayload<EventPayload<XEventsTypes, Event>, WireMetadata>
>
)
.pipe(filter(({ metadata }) => filterMetadataOptions(metadataFilteringOptions, metadata)))
.subscribe(({ eventPayload, metadata }) => callback(eventPayload, metadata))
);
Expand Down
11 changes: 9 additions & 2 deletions packages/x-components/src/components/global-x-bus.vue
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
<script lang="ts">
import { reduce } from '@empathyco/x-utils';
import { Component } from 'vue-property-decorator';
import { Subscription } from 'rxjs';
import { Observable, Subscription } from 'rxjs';
import { EventPayload, SubjectPayload } from '@empathyco/x-bus';
import { XEventListeners } from '../x-installer/api/api.types';
import { WireMetadata } from '../wiring/wiring.types';
import { XEventsTypes } from '../wiring/events.types';
import { NoElement } from './no-element';
/**
Expand Down Expand Up @@ -35,7 +38,11 @@
this.$listeners,
(subscription, eventName, callback) => {
subscription.add(
this.$x.on(eventName, true).subscribe(({ eventPayload, metadata }) => {
(
this.$x.on(eventName, true) as unknown as Observable<
SubjectPayload<EventPayload<XEventsTypes, typeof eventName>, WireMetadata>
>
).subscribe(({ eventPayload, metadata }) => {
callback(eventPayload as never, metadata);
})
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { Result } from '@empathyco/x-types';
import { Dictionary } from '@empathyco/x-utils';
import { mount, Wrapper } from '@vue/test-utils';
import { XBus } from '@empathyco/x-bus';
import { createResultStub } from '../../../__stubs__/results-stubs.factory';
import { getDataTestSelector, installNewXPlugin } from '../../../__tests__/utils';
import { XBus } from '../../../plugins/index';
import BaseResultAddToCart from '../base-result-add-to-cart.vue';
import { WireMetadata, XEventsTypes } from '../../../wiring/index';

describe('testing BaseResultAddToCart component', () => {
function renderAddToCart({
Expand Down Expand Up @@ -78,9 +79,10 @@ describe('testing BaseResultAddToCart component', () => {
interface RenderAddToCartOptions {
/** The result to pass to `BaseResultAddToCart` as prop. A stub is used by default.*/
result?: Result;
/** The template to render. Remember to use `:result="result"` as prop of the
/**
* The template to render. Remember to use `:result="result"` as prop of the
* `BaseResultAddToCart`.
* */
*/
template?: string;
/** The methods to add to mounted component to test listeners in the `BaseResultAddToCart`. */
methods?: Dictionary<() => void>;
Expand All @@ -89,7 +91,7 @@ interface RenderAddToCartApi {
/** Triggers the click on the `BaseResultAddToCart`. */
clickAddToCart: () => Promise<void>;
/** The `XBus.on` method to subscribe to any `XEvent`. */
on: XBus['on'];
on: XBus<XEventsTypes, WireMetadata>['on'];
/** The `BaseResultAddToCart` wrapper to use it for testing. */
addToCartWrapper: Wrapper<Vue>;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import Vuex, { Store } from 'vuex';
import { createWireFromFunction } from '../../wiring/wires.factory';
import { XComponentsAdapterDummy } from '../../__tests__/adapter.dummy';
import { createXModule } from '../../__tests__/utils';
import { BaseXBus } from '../x-bus';
import { XPlugin } from '../x-plugin';
import { XPluginOptions } from '../x-plugin.types';
import { XDummyBus } from '../../__tests__/bus.dummy';

const wireInstance = jest.fn();
const usedClearedWireInstance = jest.fn();
Expand Down Expand Up @@ -56,7 +56,7 @@ describe('testing X Plugin emitters', () => {

beforeEach(() => {
jest.clearAllMocks();
plugin = new XPlugin(new BaseXBus());
plugin = new XPlugin(new XDummyBus());
XPlugin.resetInstance();
localVue = createLocalVue();
localVue.use(Vuex);
Expand Down Expand Up @@ -256,7 +256,8 @@ describe('testing X Plugin emitters', () => {
eventPayload: 'wheat',
metadata: {
moduleName: 'searchBox',
oldValue: ''
oldValue: '',
replaceable: true
},
store
});
Expand All @@ -272,7 +273,8 @@ describe('testing X Plugin emitters', () => {
eventPayload: 'wheat beer',
metadata: {
moduleName: 'searchBox',
oldValue: 'whe'
oldValue: 'whe',
replaceable: true
},
store
});
Expand Down
Loading

0 comments on commit f64f9b6

Please sign in to comment.