use-namespace.test.tsx 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. import { computed, defineComponent, nextTick } from 'vue'
  2. import { mount } from '@vue/test-utils'
  3. import { afterEach, beforeEach, describe, expect, it } from 'vitest'
  4. import { provideGlobalConfig } from '@element-plus/components/config-provider'
  5. import { useNamespace } from '..'
  6. import type { VueWrapper } from '@vue/test-utils'
  7. const TestComp = defineComponent({
  8. setup() {
  9. const ns = useNamespace('table')
  10. const cssVar = ns.cssVar({
  11. 'border-style': 'solid',
  12. 'border-width': '',
  13. })
  14. const cssVarBlock = ns.cssVarBlock({
  15. 'text-color': '#409eff',
  16. 'active-color': '',
  17. })
  18. return () => (
  19. <div
  20. id="testId"
  21. class={[
  22. ns.b(), // return ns + block
  23. ns.b('body'),
  24. ns.e('content'),
  25. ns.m('active'),
  26. ns.be('content', 'active'),
  27. ns.em('content', 'active'),
  28. ns.bem('body', 'content', 'active'),
  29. ns.is('focus'),
  30. ns.e(), // return empty string
  31. ns.m(), // return empty string
  32. ns.be(), // return empty string
  33. ns.em(), // return empty string
  34. ns.bem(), // return empty string
  35. ns.is('hover', undefined), // return empty string
  36. ns.is('clicked', false), // return empty string
  37. ]}
  38. style={{ ...cssVar, ...cssVarBlock }}
  39. >
  40. text
  41. </div>
  42. )
  43. },
  44. })
  45. describe('use-locale', () => {
  46. const Comp = defineComponent({
  47. setup(_props, { slots }) {
  48. provideGlobalConfig({ namespace: 'ep' })
  49. return () => slots.default?.()
  50. },
  51. })
  52. let wrapper: VueWrapper<InstanceType<typeof Comp>>
  53. beforeEach(() => {
  54. wrapper = mount(Comp, {
  55. slots: { default: () => <TestComp /> },
  56. })
  57. })
  58. afterEach(() => {
  59. wrapper.unmount()
  60. })
  61. it('should provide bem correctly', async () => {
  62. await nextTick()
  63. expect(wrapper.find('#testId').classes()).toEqual([
  64. 'ep-table', // b()
  65. 'ep-table-body', // b('body')
  66. 'ep-table__content', // e('content')
  67. 'ep-table--active', // m('active')
  68. 'ep-table-content__active', // be('content', 'active')
  69. 'ep-table__content--active', // em('content', 'active')
  70. 'ep-table-body__content--active', // bem('body', 'content', 'active')
  71. 'is-focus', // is('focus')
  72. ])
  73. const style = wrapper.find('#testId').attributes('style')
  74. expect(style).toMatch('--ep-border-style: solid;')
  75. expect(style).not.toMatch('--ep-border-width:')
  76. expect(style).toMatch('--ep-table-text-color: #409eff;')
  77. expect(style).not.toMatch('--ep-table-active-color:')
  78. })
  79. it('overrides namespace', () => {
  80. const overrides = 'override'
  81. const { vm } = mount(
  82. defineComponent({
  83. setup(_, { expose }) {
  84. const { namespace } = useNamespace(
  85. 'ns',
  86. computed(() => overrides)
  87. )
  88. expose({
  89. namespace,
  90. })
  91. },
  92. template: '<div></div>',
  93. }),
  94. {
  95. global: {
  96. provide: {
  97. namespace: 'el',
  98. },
  99. },
  100. }
  101. )
  102. expect(vm.namespace).toBe(overrides)
  103. })
  104. })