-
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(empathize): migrate empathize component to composition API
- Loading branch information
1 parent
ac125f8
commit d111fe2
Showing
6 changed files
with
180 additions
and
210 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
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
25 changes: 25 additions & 0 deletions
25
packages/_vue3-migration-test/src/x-modules/empathize/test-empathize.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,25 @@ | ||
<template> | ||
<SearchInput /> | ||
<Empathize :animation="CollapseHeight" class="empathize"> | ||
<h1>It is a Empathize</h1> | ||
<h2>It is a Empathize</h2> | ||
<h3>It is a Empathize</h3> | ||
<p> | ||
Component containing the empathize. It has a required slot to define its content and two props | ||
to define when to open and close it: `eventsToOpenEmpathize` and `eventsToCloseEmpathize`. | ||
</p> | ||
</Empathize> | ||
<Empathize :animation="CollapseHeight" class="empathize" /> | ||
</template> | ||
|
||
<script setup lang="ts"> | ||
import CollapseHeight from '../../../../x-components/src/components/animations/collapse-height.vue'; | ||
import Empathize from '../../../../x-components/src/x-modules/empathize/components/empathize.vue'; | ||
import SearchInput from '../../../../x-components/src/x-modules/search-box/components/search-input.vue'; | ||
</script> | ||
|
||
<style scoped> | ||
.empathize { | ||
border: 3px solid red; | ||
} | ||
</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
133 changes: 56 additions & 77 deletions
133
packages/x-components/src/x-modules/empathize/components/__tests__/empathize.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,107 +1,86 @@ | ||
import { mount, Wrapper } from '@vue/test-utils'; | ||
import Vue from 'vue'; | ||
import { getXComponentXModuleName, isXComponent } from '../../../../components/x-component.utils'; | ||
import { mount } from '@vue/test-utils'; | ||
import { nextTick } from 'vue'; | ||
import { getXComponentXModuleName, isXComponent } from '../../../../components'; | ||
import { getDataTestSelector, installNewXPlugin } from '../../../../__tests__/utils'; | ||
import { XPlugin } from '../../../../plugins/index'; | ||
import Empathize from '../empathize.vue'; | ||
import { empathizeXModule } from '../../x-module'; | ||
import { XPlugin } from '../../../../plugins/x-plugin'; | ||
import { XEvent } from '../../../../wiring/events.types'; | ||
|
||
jest.useFakeTimers(); | ||
|
||
describe('testing empathize component', () => { | ||
function renderEmpathize({ | ||
eventsToOpenEmpathize = ['UserClickedSearchBox'], | ||
eventsToCloseEmpathize = ['UserClosedEmpathize'], | ||
template = `<Empathize v-bind="$attrs"> | ||
<template #default> | ||
<span data-test="empathize-content">Empathize</span> | ||
</template> | ||
</Empathize>` | ||
}: RenderEmpathizeOptions = {}): RenderEmpathizeAPI { | ||
const [, localVue] = installNewXPlugin(); | ||
XPlugin.registerXModule(empathizeXModule); | ||
|
||
const parent = document.createElement('div'); | ||
document.body.appendChild(parent); | ||
|
||
const wrapper = mount( | ||
{ | ||
components: { Empathize }, | ||
template | ||
}, | ||
{ | ||
localVue, | ||
attachTo: parent, | ||
propsData: { | ||
eventsToOpenEmpathize, | ||
eventsToCloseEmpathize | ||
} | ||
function render({ | ||
eventsToOpenEmpathize = ['UserClickedSearchBox'], | ||
eventsToCloseEmpathize = ['UserClosedEmpathize'], | ||
template = ` | ||
<Empathize v-bind="$attrs"> | ||
<template #default> | ||
<span data-test="empathize-content">Empathize</span> | ||
</template> | ||
</Empathize>` | ||
} = {}) { | ||
installNewXPlugin(); | ||
|
||
const parent = document.createElement('div'); | ||
document.body.appendChild(parent); | ||
|
||
const wrapper = mount( | ||
{ | ||
components: { Empathize }, | ||
template | ||
}, | ||
{ | ||
attachTo: parent, | ||
propsData: { | ||
eventsToOpenEmpathize, | ||
eventsToCloseEmpathize | ||
} | ||
).findComponent(Empathize); | ||
} | ||
); | ||
|
||
return { | ||
wrapper: wrapper.findComponent(Empathize) | ||
}; | ||
} | ||
|
||
return { | ||
wrapper | ||
}; | ||
} | ||
describe('testing empathize component', () => { | ||
beforeAll(jest.useFakeTimers); | ||
afterEach(jest.clearAllTimers); | ||
|
||
it('is an XComponent which has an XModule', () => { | ||
const { wrapper } = renderEmpathize(); | ||
expect(isXComponent(wrapper.vm)).toEqual(true); | ||
const { wrapper } = render(); | ||
|
||
expect(isXComponent(wrapper.vm)).toBeTruthy(); | ||
expect(getXComponentXModuleName(wrapper.vm)).toEqual('empathize'); | ||
}); | ||
|
||
it('will not open empathize if there is no content to render', () => { | ||
it('will not open empathize if there is no content to render', async () => { | ||
const template = `<Empathize v-bind="$attrs"/>`; | ||
const { wrapper } = renderEmpathize({ template }); | ||
const { wrapper } = render({ template }); | ||
|
||
wrapper.vm.$x.emit('UserClickedSearchBox'); | ||
await XPlugin.bus.emit('UserClickedSearchBox'); | ||
|
||
expect(wrapper.find(getDataTestSelector('empathize')).exists()).toBe(true); | ||
expect(wrapper.find(getDataTestSelector('empathize-content')).exists()).toBe(false); | ||
expect(wrapper.find(getDataTestSelector('empathize')).exists()).toBeTruthy(); | ||
expect(wrapper.find(getDataTestSelector('empathize-content')).exists()).toBeFalsy(); | ||
}); | ||
|
||
it('listens to UserOpenedEmpathize and UserClosedEmpathize by default', async () => { | ||
const { wrapper } = renderEmpathize(); | ||
const { wrapper } = render(); | ||
|
||
wrapper.vm.$x.emit('UserClickedSearchBox'); | ||
await XPlugin.bus.emit('UserClickedSearchBox'); | ||
jest.runAllTimers(); | ||
await wrapper.vm.$nextTick(); | ||
await nextTick(); | ||
|
||
// Both should exist and be visible | ||
expect(wrapper.find(getDataTestSelector('empathize')).exists()).toBe(true); | ||
expect(wrapper.find(getDataTestSelector('empathize')).exists()).toBeTruthy(); | ||
expect(wrapper.find(getDataTestSelector('empathize')).element).toBeVisible(); | ||
expect(wrapper.find(getDataTestSelector('empathize-content')).exists()).toBe(true); | ||
expect(wrapper.find(getDataTestSelector('empathize-content')).exists()).toBeTruthy(); | ||
expect(wrapper.find(getDataTestSelector('empathize-content')).element).toBeVisible(); | ||
|
||
wrapper.vm.$x.emit('UserClosedEmpathize'); | ||
await XPlugin.bus.emit('UserClosedEmpathize'); | ||
jest.runAllTimers(); | ||
await wrapper.vm.$nextTick(); | ||
await nextTick(); | ||
|
||
// Both should exist, as v-show doesn't remove the elements in the DOM, and not be visible | ||
expect(wrapper.find(getDataTestSelector('empathize')).exists()).toBe(true); | ||
expect(wrapper.find(getDataTestSelector('empathize')).exists()).toBeTruthy(); | ||
expect(wrapper.find(getDataTestSelector('empathize')).element).not.toBeVisible(); | ||
expect(wrapper.find(getDataTestSelector('empathize-content')).exists()).toBe(true); | ||
expect(wrapper.find(getDataTestSelector('empathize-content')).exists()).toBeTruthy(); | ||
expect(wrapper.find(getDataTestSelector('empathize-content')).element).not.toBeVisible(); | ||
}); | ||
}); | ||
|
||
interface RenderEmpathizeOptions { | ||
/** | ||
* Array of {@link XEvent | xEvents} to open the empathize. | ||
*/ | ||
eventsToOpenEmpathize?: XEvent[]; | ||
/** | ||
* Array of {@link XEvent | xEvents} to close the empathize. | ||
*/ | ||
eventsToCloseEmpathize?: XEvent[]; | ||
/** | ||
* The template to render {@link Empathize} component. | ||
*/ | ||
template?: string; | ||
} | ||
|
||
interface RenderEmpathizeAPI { | ||
/** The Vue testing utils wrapper for the {@link Empathize} component. */ | ||
wrapper: Wrapper<Vue>; | ||
} |
Oops, something went wrong.