-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(display-emitter): support Vue3 to the component (#1555)
- Loading branch information
1 parent
54574db
commit 21bcd52
Showing
6 changed files
with
125 additions
and
110 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
packages/_vue3-migration-test/src/components/test-display-emitter.vue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<template> | ||
<ul class="list"> | ||
<DisplayEmitter | ||
v-for="item in items" | ||
:key="item.id" | ||
:payload="{ | ||
url: 'tagging/url', | ||
params: { displayId: item.id, totalHits: item.index } | ||
}" | ||
:eventMetadata="semanticFeature" | ||
> | ||
<li>{{ item.id }}</li> | ||
</DisplayEmitter> | ||
</ul> | ||
</template> | ||
|
||
<script setup lang="ts"> | ||
import DisplayEmitter from '../../../x-components/src/components/display-emitter.vue'; | ||
import { useXBus } from '../../../x-components/src/composables/use-x-bus'; | ||
import { DisplayWireMetadata } from '../../../x-components/src/wiring/wiring.types'; | ||
const xBus = useXBus(); | ||
const items = Array.from({ length: 50 }, (_, index) => ({ id: `item-${index}`, index })); | ||
const semanticFeature: Partial<DisplayWireMetadata> = { | ||
feature: 'semantics', | ||
displayOriginalQuery: 'mercedes', | ||
location: 'low_results' | ||
}; | ||
/* eslint-disable no-console */ | ||
xBus | ||
.on('TrackableElementDisplayed', true) | ||
.subscribe(args => console.log('TrackableElementDisplayed event ->', args)); | ||
/* eslint-enable no-console */ | ||
</script> | ||
|
||
<style> | ||
.list { | ||
height: 50px; | ||
overflow-y: auto; | ||
} | ||
</style> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
111 changes: 37 additions & 74 deletions
111
packages/x-components/src/components/__tests__/display-emitter.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,115 +1,78 @@ | ||
import { mount, Wrapper } from '@vue/test-utils'; | ||
import Vue, { ref, nextTick, Ref } from 'vue'; | ||
import { TaggingRequest } from '@empathyco/x-types'; | ||
import { mount } from '@vue/test-utils'; | ||
import { nextTick } from 'vue'; | ||
import { useEmitDisplayEvent } from '../../composables/use-on-display'; | ||
import DisplayEmitter from '../display-emitter.vue'; | ||
import { getDataTestSelector } from '../../__tests__/utils'; | ||
|
||
jest.mock('../../composables/use-on-display', () => ({ | ||
useEmitDisplayEvent: jest.fn() | ||
})); | ||
|
||
let emitDisplayEventElementSpy: Ref<Vue | null> = ref(null); | ||
let emitDisplayEventPayloadSpy: TaggingRequest = { url: '', params: {} }; | ||
const unwatchDisplaySpy = jest.fn(); | ||
const refElementVisibility = ref(false); | ||
(useEmitDisplayEvent as jest.Mock).mockImplementation(({ element, taggingRequest }) => { | ||
// jest doesn't handle well evaluation of dynamic references with `toHaveBeenCalledWith` | ||
// so we need a spy | ||
emitDisplayEventElementSpy = element; | ||
emitDisplayEventPayloadSpy = taggingRequest; | ||
|
||
return { | ||
isElementVisible: refElementVisibility, | ||
unwatchDisplay: unwatchDisplaySpy | ||
}; | ||
}); | ||
|
||
/** | ||
* Renders the {@link DisplayEmitter} component, exposing a basic API for testing. | ||
* | ||
* @param options - The options to render the component with. | ||
* | ||
* @returns The API for testing the `DisplayEmitter` component. | ||
*/ | ||
function renderDisplayEmitter( | ||
{ payload }: RenderDisplayEmitterOptions = { payload: { url: '', params: {} } } | ||
): RenderDisplayEmitterAPI { | ||
const wrapper = mount( | ||
{ | ||
components: { | ||
DisplayEmitter | ||
}, | ||
template: ` | ||
<DisplayEmitter :payload="payload"> | ||
<div data-test="child" /> | ||
</DisplayEmitter>`, | ||
props: ['payload'] | ||
}, | ||
{ | ||
propsData: { | ||
payload | ||
} | ||
} | ||
); | ||
(useEmitDisplayEvent as jest.Mock).mockReturnValue({ unwatchDisplay: unwatchDisplaySpy }); | ||
|
||
function render({ | ||
payload = { url: 'tagging/url', params: { test: 'param' } }, | ||
eventMetadata = { test: 'param' } | ||
} = {}) { | ||
const wrapper = mount({ | ||
components: { DisplayEmitter }, | ||
template: ` | ||
<DisplayEmitter :payload="payload" :eventMetadata="eventMetadata"> | ||
<div data-test="child" /> | ||
</DisplayEmitter>`, | ||
data: () => ({ payload, eventMetadata }) | ||
}); | ||
|
||
return { | ||
wrapper | ||
wrapper: wrapper.findComponent(DisplayEmitter), | ||
element: wrapper.find(getDataTestSelector('child')).element, | ||
payload, | ||
eventMetadata | ||
}; | ||
} | ||
|
||
describe('testing DisplayEmitter component', () => { | ||
beforeEach(() => { | ||
refElementVisibility.value = false; | ||
(useEmitDisplayEvent as jest.Mock).mockClear(); | ||
unwatchDisplaySpy.mockClear(); | ||
}); | ||
|
||
it('renders everything passed to its default slot', () => { | ||
const { wrapper } = renderDisplayEmitter(); | ||
const { wrapper } = render(); | ||
|
||
expect(wrapper.find(getDataTestSelector('child')).exists()).toBe(true); | ||
expect(wrapper.find(getDataTestSelector('child')).exists()).toBeTruthy(); | ||
}); | ||
|
||
it('uses `useEmitDisplayEvent` underneath', () => { | ||
renderDisplayEmitter(); | ||
it('executes `useEmitDisplayEvent` composable underneath', () => { | ||
render(); | ||
|
||
expect(useEmitDisplayEvent).toHaveBeenCalled(); | ||
}); | ||
|
||
it('provides `useEmitDisplayEvent` with the element in the slot to watch', async () => { | ||
renderDisplayEmitter(); | ||
const { element } = render(); | ||
|
||
await nextTick(); | ||
|
||
expect(emitDisplayEventElementSpy.value).not.toBe(null); | ||
expect(emitDisplayEventElementSpy.value?.$el.getAttribute('data-test')).toBe('child'); | ||
expect(useEmitDisplayEvent).toHaveBeenCalledWith(expect.objectContaining({ element })); | ||
}); | ||
|
||
// eslint-disable-next-line max-len | ||
it('provides `useEmitDisplayEvent` with the payload to emit with the display event', () => { | ||
const payload = { url: 'test-url', params: { test: 'param' } }; | ||
renderDisplayEmitter({ | ||
payload | ||
}); | ||
it('provides `useEmitDisplayEvent` with the payload and metadata to emit with the display event', async () => { | ||
const { payload, eventMetadata } = render(); | ||
|
||
expect(useEmitDisplayEvent).toHaveBeenCalled(); | ||
expect(emitDisplayEventPayloadSpy).toBe(payload); | ||
await nextTick(); | ||
|
||
expect(useEmitDisplayEvent).toHaveBeenCalledWith( | ||
expect.objectContaining({ taggingRequest: payload, eventMetadata }) | ||
); | ||
}); | ||
|
||
it('removes the watcher on unmount', async () => { | ||
const { wrapper } = renderDisplayEmitter(); | ||
const { wrapper } = render(); | ||
|
||
wrapper.destroy(); | ||
await nextTick(); | ||
|
||
expect(unwatchDisplaySpy).toHaveBeenCalled(); | ||
}); | ||
}); | ||
|
||
interface RenderDisplayEmitterOptions { | ||
/** The payload to provide. */ | ||
payload?: TaggingRequest; | ||
} | ||
|
||
interface RenderDisplayEmitterAPI { | ||
/** The wrapper testing component instance. */ | ||
wrapper: Wrapper<Vue>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters