Skip to content

Commit

Permalink
refactor: Use useId to sync snapshot in test env between different re…
Browse files Browse the repository at this point in the history
…act versions (#286)
  • Loading branch information
zombieJ authored Apr 18, 2022
1 parent 8adf763 commit a7f2c70
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 17 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
"@babel/runtime": "^7.10.1",
"classnames": "^2.2.6",
"rc-motion": "^2.3.0",
"rc-util": "^5.6.1"
"rc-util": "^5.21.0"
},
"devDependencies": {
"@types/enzyme": "^3.10.7",
Expand Down
12 changes: 5 additions & 7 deletions src/Dialog/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ import * as React from 'react';
import { useRef, useEffect } from 'react';
import classNames from 'classnames';
import KeyCode from 'rc-util/lib/KeyCode';
import useId from 'rc-util/lib/hooks/useId';
import contains from 'rc-util/lib/Dom/contains';
import pickAttrs from 'rc-util/lib/pickAttrs';
import type ScollLocker from 'rc-util/lib/Dom/scrollLocker';
import type { IDialogPropTypes } from '../IDialogPropTypes';
import Mask from './Mask';
import { getMotionName, getUUID } from '../util';
import { getMotionName } from '../util';
import type { ContentRef } from './Content';
import Content from './Content';

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

// ========================== Init ==========================
const ariaIdRef = useRef<string>();
if (!ariaIdRef.current) {
ariaIdRef.current = `rcDialogTitle${getUUID()}`;
}
const ariaId = useId();

// ========================= Events =========================
function onDialogVisibleChanged(newVisible: boolean) {
Expand Down Expand Up @@ -185,7 +183,7 @@ export default function Dialog(props: IDialogChildProps) {
ref={wrapperRef}
onClick={onWrapperClick}
role="dialog"
aria-labelledby={title ? ariaIdRef.current : null}
aria-labelledby={title ? ariaId : null}
style={{ zIndex, ...wrapStyle, display: !animatedVisible ? 'none' : null }}
{...wrapProps}
>
Expand All @@ -195,7 +193,7 @@ export default function Dialog(props: IDialogChildProps) {
onMouseUp={onContentMouseUp}
ref={contentRef}
closable={closable}
ariaId={ariaIdRef.current}
ariaId={ariaId}
prefixCls={prefixCls}
visible={visible}
onClose={onInternalClose}
Expand Down
9 changes: 1 addition & 8 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,6 @@ export function getMotionName(prefixCls: string, transitionName?: string, animat
return motionName;
}

// ================================ UUID ================================
let uuid = -1;
export function getUUID() {
uuid += 1;
return uuid;
}

// =============================== Offset ===============================
function getScroll(w: Window, top?: boolean): number {
let ret = w[`page${top ? 'Y' : 'X'}Offset`];
Expand All @@ -30,7 +23,7 @@ function getScroll(w: Window, top?: boolean): number {

type CompatibleDocument = {
parentWindow?: Window;
} & Document
} & Document;

export function offset(el: Element) {
const rect = el.getBoundingClientRect();
Expand Down
11 changes: 11 additions & 0 deletions tests/__snapshots__/index.spec.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ exports[`dialog should render correct 1`] = `
class="rc-dialog-mask"
/>
<div
aria-labelledby="test-id"
class="rc-dialog-wrap"
role="dialog"
tabindex="-1"
Expand All @@ -82,6 +83,16 @@ exports[`dialog should render correct 1`] = `
class="rc-dialog-close-x"
/>
</button>
<div
class="rc-dialog-header"
>
<div
class="rc-dialog-title"
id="test-id"
>
Default
</div>
</div>
<div
class="rc-dialog-body"
/>
Expand Down
2 changes: 1 addition & 1 deletion tests/index.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ describe('dialog', () => {
});

it('should render correct', () => {
const wrapper = mount(<Dialog visible />);
const wrapper = mount(<Dialog title="Default" visible />);
jest.runAllTimers();
wrapper.update();

Expand Down

1 comment on commit a7f2c70

@vercel
Copy link

@vercel vercel bot commented on a7f2c70 Apr 18, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.