Skip to content

Commit

Permalink
refactor(x-emitters): simplify return type
Browse files Browse the repository at this point in the history
  • Loading branch information
victorcg88 committed Aug 7, 2024
1 parent 875a6e2 commit 01645ee
Show file tree
Hide file tree
Showing 5 changed files with 1,834 additions and 2,453 deletions.
2 changes: 1 addition & 1 deletion packages/x-components/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@
"rollup-plugin-delete": "~2.0.0",
"rollup-plugin-styles": "~4.0.0",
"rollup-plugin-typescript2": "~0.36.0",
"rollup-plugin-vue": "~5.1.9",
"rollup-plugin-vue": "~6.0.0",
"sass": "~1.70.0",
"start-server-and-test": "~2.0.0",
"tailwindcss": "~3.4.0",
Expand Down
19 changes: 9 additions & 10 deletions packages/x-components/src/__tests__/utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { deepMerge } from '@empathyco/x-deep-merge';
import { DeepPartial, Dictionary } from '@empathyco/x-utils';
import { createLocalVue, Wrapper, WrapperArray } from '@vue/test-utils';
import Vue from 'vue';
import { VueWrapper } from '@vue/test-utils';
import { Store } from 'vuex';
import {
XComponentsAdapter,
Expand Down Expand Up @@ -69,7 +68,7 @@ export function getDataTestSelector(dataTest: string): string {
*
* @returns The wrappers matching the searched test data id.
*/
export function findTestDataById(wrapper: Wrapper<Vue>, testDataId: string): WrapperArray<Vue> {
export function findTestDataById(wrapper: VueWrapper, testDataId: string) {
return wrapper.findAll(getDataTestSelector(testDataId));
}

Expand Down Expand Up @@ -194,16 +193,16 @@ function mergeStates<State extends Dictionary>(
*/
export function installNewXPlugin(
options: Partial<XPluginOptions> = {},
localVue: typeof Vue = createLocalVue(),
localVue = undefined,
bus = new XDummyBus()
): [XPlugin, typeof Vue] {
): [XPlugin, undefined] {
XPlugin.resetInstance();
const xPlugin = new XPlugin(bus);
const installOptions: XPluginOptions = {
adapter: XComponentsAdapterDummy,
...options
};
localVue.use(xPlugin, installOptions);
// const installOptions: XPluginOptions = {
// adapter: XComponentsAdapterDummy,
// ...options
// };
// localVue.use(xPlugin, installOptions);
return [xPlugin, localVue];
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,74 +1,41 @@
import { mount, Wrapper } from '@vue/test-utils';
import { mount } from '@vue/test-utils';
import { getDataTestSelector } from '../../__tests__/utils';
import AutoProgressBar from '../auto-progress-bar.vue';

function renderAutoProgressBar({
template = '<AutoProgressBar :durationInSeconds="durationInSeconds" :isLoading="isLoading"/>',
durationInSeconds = 1,
isLoading = true
}: RenderBaseAutoProgressBarOptions = {}): RenderAutoProgressBarAPI {
const wrapper = mount(
{
components: { AutoProgressBar },
template
},
{
data() {
return {
durationInSeconds,
isLoading
};
}
}
);
function render({ durationInSeconds = 1, isLoading = true } = {}) {
const wrapper = mount({
components: { AutoProgressBar },
data: () => ({ durationInSeconds, isLoading }),
template: '<AutoProgressBar :durationInSeconds="durationInSeconds" :isLoading="isLoading"/>'
});

return {
wrapper
wrapper,
getProgressBarEl: () => wrapper.find(getDataTestSelector('progress-bar')),
getProgressBarLineEl: () => wrapper.find(getDataTestSelector('progress-bar-line'))
};
}

describe('testing AutoProgressBar component', () => {
it('renders a progress bar component', () => {
const { wrapper } = renderAutoProgressBar();
const { getProgressBarEl } = render();

expect(wrapper.find(getDataTestSelector('progress-bar')).exists()).toBe(true);
expect(getProgressBarEl().exists()).toBe(true);
});

it("doesn't render a progress bar component when is not loading", async () => {
const { wrapper } = renderAutoProgressBar();
const { wrapper, getProgressBarEl } = render();

expect(wrapper.find(getDataTestSelector('progress-bar')).exists()).toBe(true);
expect(getProgressBarEl().exists()).toBe(true);

await wrapper.setData({ isLoading: false });

expect(wrapper.find(getDataTestSelector('progress-bar')).exists()).toBe(false);
expect(getProgressBarEl().exists()).toBe(false);
});

it('render a progress bar component with an animation', () => {
const { wrapper } = renderAutoProgressBar({ durationInSeconds: 2.5 });
const { getProgressBarLineEl } = render({ durationInSeconds: 2.5 });

expect(wrapper.find(getDataTestSelector('progress-bar-line')).attributes().style).toBe(
'animation-duration: 2.5s;'
);
expect(getProgressBarLineEl().attributes().style).toBe('animation-duration: 2.5s;');
});
});

/**
* Options to configure how the progress bar component should be rendered.
*/
interface RenderBaseAutoProgressBarOptions {
/** The template to render.*/
template?: string;
/** The duration in seconds of the animation. */
durationInSeconds?: number;
/** A flag indicating if the progress bar is loading. */
isLoading?: boolean;
}

/**
* Options to configure how the progress bar component should be rendered.
*/
interface RenderAutoProgressBarAPI {
/** The wrapper for the progress bar component. */
wrapper: Wrapper<Vue>;
}
4 changes: 2 additions & 2 deletions packages/x-components/src/plugins/x-emitters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export function registerStoreEmitters(
newValue: XEventPayload<typeof event>,
oldValue?: XEventPayload<typeof event>
) => void = (newValue, oldValue) => {
if (filter(newValue, oldValue, store.state.x[name])) {
if (filter!(newValue, oldValue, store.state.x[name])) {
bus.emit(event, newValue, { ...metadata, moduleName: name, oldValue });
}
};
Expand All @@ -57,7 +57,7 @@ export function registerStoreEmitters(
*/
function normalizeStateSelector(
stateSelector: AnySimpleStateSelector | AnyStateSelector
): Required<AnyStateSelector> {
): AnyStateSelector {
const selectorOptions = isSimpleSelector(stateSelector)
? { selector: stateSelector }
: stateSelector;
Expand Down
Loading

0 comments on commit 01645ee

Please sign in to comment.