From b456125431ba4c7c7e77c763adbdb932be07710b Mon Sep 17 00:00:00 2001 From: qiang Date: Mon, 21 Nov 2022 14:10:52 +0800 Subject: [PATCH] fix(components): [tooltip] SSR hydration error caused by random ID (#10541) --- .../__tests__/autocomplete.test.tsx | 14 ++-- .../cascader/__tests__/cascader.test.tsx | 14 ++-- .../dropdown/__tests__/dropdown.test.ts | 12 ++-- .../popconfirm/__tests__/popconfirm.test.tsx | 14 ++-- .../popover/__tests__/popover.test.tsx | 14 ++-- .../select-v2/__tests__/select.test.ts | 14 ++-- .../select/__tests__/select.test.ts | 12 ++-- packages/components/tooltip/src/content.ts | 7 +- packages/components/tooltip/src/content.vue | 8 ++- packages/hooks/__tests__/use-id.test.tsx | 71 +++++++++++++++++++ .../__tests__/use-popper-container.test.tsx | 61 ++++++++++++++-- packages/hooks/use-id/index.ts | 11 ++- packages/hooks/use-popper-container/index.ts | 31 ++++---- 13 files changed, 206 insertions(+), 77 deletions(-) create mode 100644 packages/hooks/__tests__/use-id.test.tsx diff --git a/packages/components/autocomplete/__tests__/autocomplete.test.tsx b/packages/components/autocomplete/__tests__/autocomplete.test.tsx index 545713b74d..7c9d8bb8cf 100644 --- a/packages/components/autocomplete/__tests__/autocomplete.test.tsx +++ b/packages/components/autocomplete/__tests__/autocomplete.test.tsx @@ -2,7 +2,7 @@ import { nextTick, reactive } from 'vue' import { mount } from '@vue/test-utils' import { NOOP } from '@vue/shared' import { beforeEach, describe, expect, it, test, vi } from 'vitest' -import { POPPER_CONTAINER_SELECTOR } from '@element-plus/hooks' +import { usePopperContainerId } from '@element-plus/hooks' import { ElFormItem as FormItem } from '@element-plus/components/form' import Autocomplete from '../src/autocomplete.vue' @@ -321,9 +321,10 @@ describe('Autocomplete.vue', () => { _mount() await nextTick() - expect( - document.body.querySelector(POPPER_CONTAINER_SELECTOR)?.innerHTML - ).not.toBe('') + const { selector } = usePopperContainerId() + expect(document.body.querySelector(selector.value)?.innerHTML).not.toBe( + '' + ) }) it('should not mount on the popper container', async () => { @@ -333,9 +334,8 @@ describe('Autocomplete.vue', () => { }) await nextTick() - expect( - document.body.querySelector(POPPER_CONTAINER_SELECTOR)?.innerHTML - ).toBe('') + const { selector } = usePopperContainerId() + expect(document.body.querySelector(selector.value)?.innerHTML).toBe('') }) }) diff --git a/packages/components/cascader/__tests__/cascader.test.tsx b/packages/components/cascader/__tests__/cascader.test.tsx index 8b1e476111..32ff8ebe71 100644 --- a/packages/components/cascader/__tests__/cascader.test.tsx +++ b/packages/components/cascader/__tests__/cascader.test.tsx @@ -4,7 +4,7 @@ import { afterEach, describe, expect, it, test, vi } from 'vitest' import { EVENT_CODE } from '@element-plus/constants' import triggerEvent from '@element-plus/test-utils/trigger-event' import { ArrowDown, Check, CircleClose } from '@element-plus/icons-vue' -import { POPPER_CONTAINER_SELECTOR } from '@element-plus/hooks' +import { usePopperContainerId } from '@element-plus/hooks' import { hasClass } from '@element-plus/utils' import ElForm, { ElFormItem } from '@element-plus/components/form' import Cascader from '../src/index.vue' @@ -376,9 +376,10 @@ describe('Cascader.vue', () => { )) await nextTick() - expect( - document.body.querySelector(POPPER_CONTAINER_SELECTOR)!.innerHTML - ).not.toBe('') + const { selector } = usePopperContainerId() + expect(document.body.querySelector(selector.value)!.innerHTML).not.toBe( + '' + ) }) it('should not mount on the popper container', async () => { @@ -394,9 +395,8 @@ describe('Cascader.vue', () => { )) await nextTick() - expect( - document.body.querySelector(POPPER_CONTAINER_SELECTOR)!.innerHTML - ).toBe('') + const { selector } = usePopperContainerId() + expect(document.body.querySelector(selector.value)!.innerHTML).toBe('') }) }) diff --git a/packages/components/dropdown/__tests__/dropdown.test.ts b/packages/components/dropdown/__tests__/dropdown.test.ts index 72c3733ed2..32165c60c2 100644 --- a/packages/components/dropdown/__tests__/dropdown.test.ts +++ b/packages/components/dropdown/__tests__/dropdown.test.ts @@ -6,7 +6,7 @@ import { rAF } from '@element-plus/test-utils/tick' import { EVENT_CODE } from '@element-plus/constants' import { ElTooltip } from '@element-plus/components/tooltip' import Button from '@element-plus/components/button' -import { POPPER_CONTAINER_SELECTOR } from '@element-plus/hooks' +import { usePopperContainerId } from '@element-plus/hooks' import Dropdown from '../src/dropdown.vue' import DropdownItem from '../src/dropdown-item.vue' import DropdownMenu from '../src/dropdown-menu.vue' @@ -785,9 +785,8 @@ describe('Dropdown', () => { ) await nextTick() - expect( - document.body.querySelector(POPPER_CONTAINER_SELECTOR).innerHTML - ).not.toBe('') + const { selector } = usePopperContainerId() + expect(document.body.querySelector(selector.value).innerHTML).not.toBe('') }) test('should not mount on the popper container', async () => { @@ -812,9 +811,8 @@ describe('Dropdown', () => { ) await nextTick() - expect( - document.body.querySelector(POPPER_CONTAINER_SELECTOR).innerHTML - ).toBe('') + const { selector } = usePopperContainerId() + expect(document.body.querySelector(selector.value).innerHTML).toBe('') }) }) }) diff --git a/packages/components/popconfirm/__tests__/popconfirm.test.tsx b/packages/components/popconfirm/__tests__/popconfirm.test.tsx index 05421a720e..66c015ed28 100644 --- a/packages/components/popconfirm/__tests__/popconfirm.test.tsx +++ b/packages/components/popconfirm/__tests__/popconfirm.test.tsx @@ -2,7 +2,7 @@ import { nextTick } from 'vue' import { mount } from '@vue/test-utils' import { afterEach, describe, expect, it } from 'vitest' import { rAF } from '@element-plus/test-utils/tick' -import { POPPER_CONTAINER_SELECTOR } from '@element-plus/hooks' +import { usePopperContainerId } from '@element-plus/hooks' import Popconfirm from '../src/popconfirm.vue' const AXIOM = 'rem is the best girl' @@ -55,9 +55,10 @@ describe('Popconfirm.vue', () => { )) await nextTick() - expect( - document.body.querySelector(POPPER_CONTAINER_SELECTOR)!.innerHTML - ).not.toBe('') + const { selector } = usePopperContainerId() + expect(document.body.querySelector(selector.value)!.innerHTML).not.toBe( + '' + ) }) it('should not mount on the popper container', async () => { @@ -75,9 +76,8 @@ describe('Popconfirm.vue', () => { )) await nextTick() - expect( - document.body.querySelector(POPPER_CONTAINER_SELECTOR)!.innerHTML - ).toBe('') + const { selector } = usePopperContainerId() + expect(document.body.querySelector(selector.value)!.innerHTML).toBe('') }) }) }) diff --git a/packages/components/popover/__tests__/popover.test.tsx b/packages/components/popover/__tests__/popover.test.tsx index 0a522ce0d6..ac88b381fc 100644 --- a/packages/components/popover/__tests__/popover.test.tsx +++ b/packages/components/popover/__tests__/popover.test.tsx @@ -1,7 +1,7 @@ import { nextTick, ref } from 'vue' import { mount } from '@vue/test-utils' import { afterEach, describe, expect, it, vi } from 'vitest' -import { POPPER_CONTAINER_SELECTOR, useZIndex } from '@element-plus/hooks' +import { usePopperContainerId, useZIndex } from '@element-plus/hooks' import { rAF } from '@element-plus/test-utils/tick' import { ElPopperTrigger } from '@element-plus/components/popper' import Popover from '../src/popover.vue' @@ -199,9 +199,10 @@ describe('Popover.vue', () => { _mount() await nextTick() - expect( - document.body.querySelector(POPPER_CONTAINER_SELECTOR)?.innerHTML - ).not.toBe('') + const { selector } = usePopperContainerId() + expect(document.body.querySelector(selector.value)?.innerHTML).not.toBe( + '' + ) }) it('should not mount on the popper container', async () => { @@ -209,9 +210,8 @@ describe('Popover.vue', () => { _mount({ teleported: false }) await nextTick() - expect( - document.body.querySelector(POPPER_CONTAINER_SELECTOR)?.innerHTML - ).toBe('') + const { selector } = usePopperContainerId() + expect(document.body.querySelector(selector.value)?.innerHTML).toBe('') }) }) }) diff --git a/packages/components/select-v2/__tests__/select.test.ts b/packages/components/select-v2/__tests__/select.test.ts index 696ee1af46..41e10637f2 100644 --- a/packages/components/select-v2/__tests__/select.test.ts +++ b/packages/components/select-v2/__tests__/select.test.ts @@ -7,7 +7,7 @@ import { EVENT_CODE } from '@element-plus/constants' import { makeMountFunc } from '@element-plus/test-utils/make-mount' import { rAF } from '@element-plus/test-utils/tick' import { CircleClose } from '@element-plus/icons-vue' -import { POPPER_CONTAINER_SELECTOR } from '@element-plus/hooks' +import { usePopperContainerId } from '@element-plus/hooks' import Select from '../src/select.vue' vi.mock('lodash-unified', async () => { @@ -1502,9 +1502,10 @@ describe('Select', () => { }) await nextTick() - expect( - document.body.querySelector(POPPER_CONTAINER_SELECTOR)!.innerHTML - ).not.toBe('') + const { selector } = usePopperContainerId() + expect(document.body.querySelector(selector.value)!.innerHTML).not.toBe( + '' + ) }) it('should not mount on the popper container', async () => { @@ -1542,9 +1543,8 @@ describe('Select', () => { }) await nextTick() - expect( - document.body.querySelector(POPPER_CONTAINER_SELECTOR).innerHTML - ).toBe('') + const { selector } = usePopperContainerId() + expect(document.body.querySelector(selector.value).innerHTML).toBe('') }) }) diff --git a/packages/components/select/__tests__/select.test.ts b/packages/components/select/__tests__/select.test.ts index 2318aec1a9..c1d76a8e14 100644 --- a/packages/components/select/__tests__/select.test.ts +++ b/packages/components/select/__tests__/select.test.ts @@ -4,7 +4,7 @@ import { mount } from '@vue/test-utils' import { afterEach, describe, expect, it, test, vi } from 'vitest' import { EVENT_CODE } from '@element-plus/constants' import { ArrowDown, CaretTop, CircleClose } from '@element-plus/icons-vue' -import { POPPER_CONTAINER_SELECTOR } from '@element-plus/hooks' +import { usePopperContainerId } from '@element-plus/hooks' import { hasClass } from '@element-plus/utils' import { ElFormItem } from '@element-plus/components/form' import Select from '../src/select.vue' @@ -1910,9 +1910,8 @@ describe('Select', () => { ) await nextTick() - expect( - document.body.querySelector(POPPER_CONTAINER_SELECTOR).innerHTML - ).not.toBe('') + const { selector } = usePopperContainerId() + expect(document.body.querySelector(selector.value).innerHTML).not.toBe('') }) it('should not mount on the popper container', async () => { @@ -1940,9 +1939,8 @@ describe('Select', () => { ) await nextTick() - expect( - document.body.querySelector(POPPER_CONTAINER_SELECTOR).innerHTML - ).toBe('') + const { selector } = usePopperContainerId() + expect(document.body.querySelector(selector.value).innerHTML).toBe('') }) }) diff --git a/packages/components/tooltip/src/content.ts b/packages/components/tooltip/src/content.ts index 1aa4746966..ce160d4900 100644 --- a/packages/components/tooltip/src/content.ts +++ b/packages/components/tooltip/src/content.ts @@ -1,10 +1,6 @@ import { buildProps, definePropType } from '@element-plus/utils' import { popperContentProps } from '@element-plus/components/popper' -import { - POPPER_CONTAINER_SELECTOR, - useDelayedToggleProps, - useNamespace, -} from '@element-plus/hooks' +import { useDelayedToggleProps, useNamespace } from '@element-plus/hooks' import type { ExtractPropTypes } from 'vue' const ns = useNamespace('tooltip') @@ -14,7 +10,6 @@ export const useTooltipContentProps = buildProps({ ...popperContentProps, appendTo: { type: definePropType([String, Object]), - default: POPPER_CONTAINER_SELECTOR, }, content: { type: String, diff --git a/packages/components/tooltip/src/content.vue b/packages/components/tooltip/src/content.vue index d63ff6f8fd..c7193880c4 100644 --- a/packages/components/tooltip/src/content.vue +++ b/packages/components/tooltip/src/content.vue @@ -36,7 +36,6 @@ @blur="onBlur" @close="onClose" > - @@ -48,6 +47,7 @@