Skip to content

Commit a7f2c70

Browse files
authored
refactor: Use useId to sync snapshot in test env between different react versions (#286)
1 parent 8adf763 commit a7f2c70

File tree

5 files changed

+19
-17
lines changed

5 files changed

+19
-17
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
"@babel/runtime": "^7.10.1",
5050
"classnames": "^2.2.6",
5151
"rc-motion": "^2.3.0",
52-
"rc-util": "^5.6.1"
52+
"rc-util": "^5.21.0"
5353
},
5454
"devDependencies": {
5555
"@types/enzyme": "^3.10.7",

src/Dialog/index.tsx

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@ import * as React from 'react';
22
import { useRef, useEffect } from 'react';
33
import classNames from 'classnames';
44
import KeyCode from 'rc-util/lib/KeyCode';
5+
import useId from 'rc-util/lib/hooks/useId';
56
import contains from 'rc-util/lib/Dom/contains';
67
import pickAttrs from 'rc-util/lib/pickAttrs';
78
import type ScollLocker from 'rc-util/lib/Dom/scrollLocker';
89
import type { IDialogPropTypes } from '../IDialogPropTypes';
910
import Mask from './Mask';
10-
import { getMotionName, getUUID } from '../util';
11+
import { getMotionName } from '../util';
1112
import type { ContentRef } from './Content';
1213
import Content from './Content';
1314

@@ -57,10 +58,7 @@ export default function Dialog(props: IDialogChildProps) {
5758
const [animatedVisible, setAnimatedVisible] = React.useState(visible);
5859

5960
// ========================== Init ==========================
60-
const ariaIdRef = useRef<string>();
61-
if (!ariaIdRef.current) {
62-
ariaIdRef.current = `rcDialogTitle${getUUID()}`;
63-
}
61+
const ariaId = useId();
6462

6563
// ========================= Events =========================
6664
function onDialogVisibleChanged(newVisible: boolean) {
@@ -185,7 +183,7 @@ export default function Dialog(props: IDialogChildProps) {
185183
ref={wrapperRef}
186184
onClick={onWrapperClick}
187185
role="dialog"
188-
aria-labelledby={title ? ariaIdRef.current : null}
186+
aria-labelledby={title ? ariaId : null}
189187
style={{ zIndex, ...wrapStyle, display: !animatedVisible ? 'none' : null }}
190188
{...wrapProps}
191189
>
@@ -195,7 +193,7 @@ export default function Dialog(props: IDialogChildProps) {
195193
onMouseUp={onContentMouseUp}
196194
ref={contentRef}
197195
closable={closable}
198-
ariaId={ariaIdRef.current}
196+
ariaId={ariaId}
199197
prefixCls={prefixCls}
200198
visible={visible}
201199
onClose={onInternalClose}

src/util.ts

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,6 @@ export function getMotionName(prefixCls: string, transitionName?: string, animat
77
return motionName;
88
}
99

10-
// ================================ UUID ================================
11-
let uuid = -1;
12-
export function getUUID() {
13-
uuid += 1;
14-
return uuid;
15-
}
16-
1710
// =============================== Offset ===============================
1811
function getScroll(w: Window, top?: boolean): number {
1912
let ret = w[`page${top ? 'Y' : 'X'}Offset`];
@@ -30,7 +23,7 @@ function getScroll(w: Window, top?: boolean): number {
3023

3124
type CompatibleDocument = {
3225
parentWindow?: Window;
33-
} & Document
26+
} & Document;
3427

3528
export function offset(el: Element) {
3629
const rect = el.getBoundingClientRect();

tests/__snapshots__/index.spec.tsx.snap

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ exports[`dialog should render correct 1`] = `
5757
class="rc-dialog-mask"
5858
/>
5959
<div
60+
aria-labelledby="test-id"
6061
class="rc-dialog-wrap"
6162
role="dialog"
6263
tabindex="-1"
@@ -82,6 +83,16 @@ exports[`dialog should render correct 1`] = `
8283
class="rc-dialog-close-x"
8384
/>
8485
</button>
86+
<div
87+
class="rc-dialog-header"
88+
>
89+
<div
90+
class="rc-dialog-title"
91+
id="test-id"
92+
>
93+
Default
94+
</div>
95+
</div>
8596
<div
8697
class="rc-dialog-body"
8798
/>

tests/index.spec.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ describe('dialog', () => {
1818
});
1919

2020
it('should render correct', () => {
21-
const wrapper = mount(<Dialog visible />);
21+
const wrapper = mount(<Dialog title="Default" visible />);
2222
jest.runAllTimers();
2323
wrapper.update();
2424

0 commit comments

Comments
 (0)