-
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.
Loading status checks…
feature(composables): create useNoElementRender composable
- Loading branch information
1 parent
2236948
commit d67b465
Showing
3 changed files
with
97 additions
and
0 deletions.
There are no files selected for viewing
79 changes: 79 additions & 0 deletions
79
packages/x-components/src/composables/__tests__/use-no-element-render.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 |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import Vue, { ComponentOptions, defineComponent } from 'vue'; | ||
import { mount, Wrapper } from '@vue/test-utils'; | ||
import { Dictionary } from '@empathyco/x-utils'; | ||
import { useNoElementRender } from '../use-no-element-render'; | ||
import { getDataTestSelector } from '../../__tests__/utils'; | ||
|
||
const renderUseNoElementRender = ({ | ||
slots, | ||
component | ||
}: RenderUseNoElementRenderOptions = {}): Wrapper<Vue> => { | ||
const wrapper = mount( | ||
component ?? | ||
(defineComponent({ | ||
render() { | ||
return useNoElementRender(this.$slots); | ||
} | ||
}) as ComponentOptions<Vue>), | ||
{ | ||
slots | ||
} | ||
); | ||
|
||
return wrapper; | ||
}; | ||
|
||
describe('testing useNoElementRender composable', () => { | ||
it('renders as empty if there are no slots', () => { | ||
let wrapper = renderUseNoElementRender(); | ||
|
||
expect(wrapper.html()).toBe(''); | ||
|
||
wrapper = renderUseNoElementRender({ | ||
slots: { | ||
nonDefault: '<div data-test="non-default-slot"></div>' | ||
} | ||
}); | ||
|
||
expect(wrapper.html()).toBe(''); | ||
}); | ||
|
||
it('renders the default slot if there is any', () => { | ||
const wrapper = renderUseNoElementRender({ | ||
slots: { | ||
default: '<div data-test="default-slot"></div>', | ||
nonDefault: '<div data-test="non-default-slot"></div>' | ||
} | ||
}); | ||
|
||
expect(wrapper.find(getDataTestSelector('default-slot')).exists()).toBe(true); | ||
}); | ||
|
||
it('also works from the `setup` function', () => { | ||
const component = defineComponent({ | ||
setup(_, { slots }) { | ||
return () => useNoElementRender(slots); | ||
} | ||
}); | ||
|
||
let wrapper = renderUseNoElementRender({ | ||
component | ||
}); | ||
|
||
expect(wrapper.html()).toBe(''); | ||
|
||
wrapper = renderUseNoElementRender({ | ||
slots: { | ||
default: '<div data-test="default-slot"></div>' | ||
}, | ||
component | ||
}); | ||
|
||
expect(wrapper.find(getDataTestSelector('default-slot')).exists()).toBe(true); | ||
}); | ||
}); | ||
|
||
type RenderUseNoElementRenderOptions = { | ||
slots?: Dictionary<string>; | ||
component?: ComponentOptions<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
17 changes: 17 additions & 0 deletions
17
packages/x-components/src/composables/use-no-element-render.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 |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { h, SetupContext } from 'vue'; | ||
import { VNode } from 'vue/types/vnode'; | ||
|
||
/** | ||
* Returns a render function that returns the default slot or nothing if it's not defined. | ||
* | ||
* @param slots - The slots object from the component. | ||
* @returns The result of the rendering function to use. | ||
*/ | ||
export function useNoElementRender( | ||
slots: { [key: string]: VNode[] | undefined } | SetupContext['slots'] | ||
): VNode { | ||
const defaultSlotContent = | ||
typeof slots.default === 'function' ? slots.default()?.[0] : slots.default?.[0]; | ||
|
||
return defaultSlotContent ?? h(); | ||
} |