Skip to content

Commit

Permalink
feat: add reduceMotion and restoreMotion & apply reduceMotion t…
Browse files Browse the repository at this point in the history
…o tests
  • Loading branch information
awmleer committed May 30, 2022
1 parent fad4a37 commit e813593
Show file tree
Hide file tree
Showing 10 changed files with 75 additions and 13 deletions.
6 changes: 6 additions & 0 deletions src/components/action-sheet/tests/action-sheet.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ import { render, testA11y, fireEvent, waitFor, cleanup, sleep } from 'testing'
import ActionSheet, { Action } from '../'
import Button from '../../button'
import type { ActionSheetProps, ActionSheetShowHandler } from '..'
import {
reduceMotion,
restoreMotion,
} from '../../../utils/reduce-and-restore-motion'

const classPrefix = `adm-action-sheet`

Expand Down Expand Up @@ -35,6 +39,7 @@ describe('ActionSheet', () => {
})

test('basic usage', async () => {
restoreMotion()
const { getByText, baseElement } = await render(
<App extra='请选择你要进行的操作' cancelText='取消' />
)
Expand All @@ -52,6 +57,7 @@ describe('ActionSheet', () => {
)

expect(baseElement).toMatchSnapshot()
reduceMotion()
})

test('renders Imperative', async () => {
Expand Down
14 changes: 12 additions & 2 deletions src/components/dialog/tests/dialog.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
fireEvent,
waitFor,
waitForElementToBeRemoved,
sleep,
} from 'testing'
import Dialog, { DialogAlertProps } from '..'
import { act } from '@testing-library/react'
Expand Down Expand Up @@ -63,8 +64,17 @@ describe('Dialog', () => {
const afterShow = jest.fn()
const { getByText } = await render(<DialogAlert afterShow={afterShow} />)

fireEvent.click(getByText('btn'))
await waitForDialogShow()
await act(async () => {
fireEvent.click(getByText('btn'))
})
await sleep(20)

const wrap = $$(`.${classPrefix}-wrap`)[0]
const animatedDiv = wrap.childNodes[0]

expect(animatedDiv).toHaveStyle({
opacity: 1,
})
expect(afterShow).toBeCalled()
})

Expand Down
3 changes: 3 additions & 0 deletions src/components/floating-panel/tests/floating-panel.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
} from 'testing'
import FloatingPanel, { FloatingPanelRef } from '..'
import { patchCreateEvent } from '../../../tests/gesture/utils'
import { reduceMotion, restoreMotion } from 'antd-mobile'

const classPrefix = `adm-floating-panel`

Expand Down Expand Up @@ -95,6 +96,7 @@ describe('FloatingPanel', () => {
})

test('height change', async () => {
restoreMotion()
const fn = jest.fn()
const { getByTestId } = await render(<App onHeightChange={fn} />)

Expand All @@ -117,6 +119,7 @@ describe('FloatingPanel', () => {
expect(fn.mock.calls[fn.mock.calls.length - 1][0]).toBe(
anchors[anchors.length - 1]
)
reduceMotion()
})

test('set height in an imperative way', async () => {
Expand Down
11 changes: 8 additions & 3 deletions src/components/modal/tests/modal.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
fireEvent,
waitFor,
waitForElementToBeRemoved,
actSleep,
} from 'testing'
import Modal, { ModalAlertProps } from '..'
import { act } from '@testing-library/react'
Expand Down Expand Up @@ -63,7 +64,10 @@ describe('Modal', () => {
const afterShow = jest.fn()
const { getByText } = await render(<ModalAlert afterShow={afterShow} />)

fireEvent.click(getByText('btn'))
act(() => {
fireEvent.click(getByText('btn'))
})
await actSleep(100)
await waitForModalShow()
expect(afterShow).toBeCalled()
})
Expand Down Expand Up @@ -167,9 +171,10 @@ describe('Modal', () => {
expect(fn.mock.calls[0][0]).toBe(true)

fireEvent.click(getByText('btn'))
await act(async () => {
await fireEvent.click(getAllByText('取消')[1])
act(() => {
fireEvent.click(getAllByText('取消')[1])
})
await actSleep(100)
expect(fn.mock.calls[1][0]).toBe(false)
})

Expand Down
2 changes: 1 addition & 1 deletion src/components/spin-loading/demos/demo1.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react'
import { DemoBlock } from '../../../demos'
import { Space, SpinLoading } from 'antd-mobile'
import { DemoBlock } from 'demos'

export default () => {
return (
Expand Down
13 changes: 8 additions & 5 deletions src/components/spin-loading/spin-loading.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React, { memo } from 'react'
import { NativeProps, withNativeProps } from '../../utils/native-props'
import { mergeProps } from '../../utils/with-default-props'
import { useSpring, animated } from '@react-spring/web'
import { isMotionReduced } from '../../utils/reduce-and-restore-motion'

const classPrefix = 'adm-spin-loading'

Expand All @@ -25,14 +26,16 @@ export const SpinLoading = memo<SpinLoadingProps>(p => {
const props = mergeProps(defaultProps, p)

const { percent } = useSpring({
loop: {
reverse: true,
},
loop: isMotionReduced()
? false
: {
reverse: true,
},
from: {
percent: 30,
percent: 80,
},
to: {
percent: 80,
percent: 30,
},
config: {
duration: 1200,
Expand Down
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,5 @@ export { default as VirtualInput } from './components/virtual-input'
export { default as WaterMark } from './components/water-mark'

export { createErrorBlock } from './components/error-block'

export { reduceMotion, restoreMotion } from './utils/reduce-and-restore-motion'
3 changes: 3 additions & 0 deletions src/tests/setup.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { excludeWarning } from './excludeWarning'
import { reduceMotion } from '../utils/reduce-and-restore-motion'

excludeWarning()

reduceMotion()
13 changes: 11 additions & 2 deletions src/tests/testing.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import '@testing-library/jest-dom/extend-expect'
import { render, RenderOptions, RenderResult } from '@testing-library/react'
import {
act,
render,
RenderOptions,
RenderResult,
} from '@testing-library/react'
import { toHaveNoViolations, axe } from 'jest-axe'
import * as React from 'react'
import type { RunOptions } from 'axe-core'
Expand Down Expand Up @@ -118,4 +123,8 @@ export const testA11y = async (
}

export const sleep = (time: number) =>
new Promise(resolve => setTimeout(resolve, time))
new Promise<void>(resolve => setTimeout(resolve, time))

export const actSleep = (time: number) => {
return act(() => sleep(time))
}
21 changes: 21 additions & 0 deletions src/utils/reduce-and-restore-motion.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { Globals } from '@react-spring/web'

let reduced = false

export function reduceMotion() {
reduced = true
Globals.assign({
skipAnimation: true,
})
}

export function restoreMotion() {
reduced = false
Globals.assign({
skipAnimation: false,
})
}

export function isMotionReduced() {
return reduced
}

0 comments on commit e813593

Please sign in to comment.