forked from zhihu/griffith
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
161 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
packages/griffith/src/contexts/__test__/PositionProvider.spec.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import React, {useContext, useEffect, useRef} from 'react' | ||
import {act, render} from '@testing-library/react' | ||
import PositionContext, {PositionContextValue} from '../PositionContext' | ||
import PositionProvider from '../PositionProvider' | ||
|
||
const waitRAF = () => new Promise(requestAnimationFrame) | ||
|
||
test('PositionProvider', async () => { | ||
let ctx!: PositionContextValue | ||
const MyComponent = () => { | ||
const ref = useRef<HTMLDivElement>(null) | ||
const value = (ctx = useContext(PositionContext)) | ||
useEffect(() => { | ||
Object.defineProperties(ref.current?.parentNode, { | ||
getBoundingClientRect: { | ||
value() { | ||
return {width: 100, height: 100} | ||
}, | ||
}, | ||
}) | ||
value.updateVideoSize({videoWidth: 100, videoHeight: 100}) | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, []) | ||
return <div ref={ref} /> | ||
} | ||
|
||
await act(async () => { | ||
render( | ||
<PositionProvider> | ||
<MyComponent /> | ||
</PositionProvider> | ||
) | ||
expect(ctx).toMatchInlineSnapshot(` | ||
Object { | ||
"helperImageSrc": null, | ||
"isFullWidth": false, | ||
"updateVideoSize": [Function], | ||
} | ||
`) | ||
// Provider 内使用了 raf 延迟更新状态 | ||
await waitRAF() | ||
expect(ctx).toMatchInlineSnapshot(` | ||
Object { | ||
"helperImageSrc": "data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='100' height='100'></svg>", | ||
"isFullWidth": true, | ||
"updateVideoSize": [Function], | ||
} | ||
`) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters