mirror of
https://github.com/zoriya/react-native-web.git
synced 2026-06-03 02:42:05 +00:00
Add failing tests for Image
This commit is contained in:
@@ -89,11 +89,34 @@ describe('components/Image', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
describe('prop "onLoad"', () => {
|
describe('prop "onLoad"', () => {
|
||||||
test('is called after image is loaded from network', () => {
|
const originalLoad = ImageLoader.load;
|
||||||
jest.useFakeTimers();
|
|
||||||
|
beforeEach(() => {
|
||||||
ImageLoader.load = jest.fn().mockImplementation((_, onLoad, onError) => {
|
ImageLoader.load = jest.fn().mockImplementation((_, onLoad, onError) => {
|
||||||
onLoad();
|
onLoad();
|
||||||
});
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
afterEach(() => {
|
||||||
|
ImageLoader.load = originalLoad;
|
||||||
|
});
|
||||||
|
|
||||||
|
test('is not called again if callback changes', () => {
|
||||||
|
const onLoadStub = jest.fn();
|
||||||
|
const onLoadReplacementStub = jest.fn();
|
||||||
|
|
||||||
|
const { rerender } = render(
|
||||||
|
<Image onLoad={onLoadStub} source={'https://test.com/img.jpg'} />
|
||||||
|
);
|
||||||
|
act(() => {
|
||||||
|
rerender(<Image onLoad={onLoadReplacementStub} source={'https://test.com/img.jpg'} />);
|
||||||
|
});
|
||||||
|
expect(onLoadStub.mock.calls.length).toBe(1);
|
||||||
|
expect(onLoadReplacementStub.mock.calls.length).toBe(0);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('is called after image is loaded from network', () => {
|
||||||
|
jest.useFakeTimers();
|
||||||
const onLoadStartStub = jest.fn();
|
const onLoadStartStub = jest.fn();
|
||||||
const onLoadStub = jest.fn();
|
const onLoadStub = jest.fn();
|
||||||
const onLoadEndStub = jest.fn();
|
const onLoadEndStub = jest.fn();
|
||||||
@@ -111,9 +134,6 @@ describe('components/Image', () => {
|
|||||||
|
|
||||||
test('is called after image is loaded from cache', () => {
|
test('is called after image is loaded from cache', () => {
|
||||||
jest.useFakeTimers();
|
jest.useFakeTimers();
|
||||||
ImageLoader.load = jest.fn().mockImplementation((_, onLoad, onError) => {
|
|
||||||
onLoad();
|
|
||||||
});
|
|
||||||
const onLoadStartStub = jest.fn();
|
const onLoadStartStub = jest.fn();
|
||||||
const onLoadStub = jest.fn();
|
const onLoadStub = jest.fn();
|
||||||
const onLoadEndStub = jest.fn();
|
const onLoadEndStub = jest.fn();
|
||||||
|
|||||||
@@ -7,16 +7,16 @@
|
|||||||
* @flow strict-local
|
* @flow strict-local
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import * as React from 'react';
|
import { useRef } from 'react';
|
||||||
|
|
||||||
const UNINITIALIZED =
|
const UNINITIALIZED =
|
||||||
typeof Symbol === 'function' && typeof Symbol() === 'symbol' ? Symbol() : Object.freeze({});
|
typeof Symbol === 'function' && typeof Symbol() === 'symbol' ? Symbol() : Object.freeze({});
|
||||||
|
|
||||||
export default function useStable<T>(getInitialValue: () => T): T {
|
export default function useStable<T>(getInitialValue: () => T): T {
|
||||||
const ref = React.useRef(UNINITIALIZED);
|
const ref = useRef(UNINITIALIZED);
|
||||||
if (ref.current === UNINITIALIZED) {
|
if (ref.current === UNINITIALIZED) {
|
||||||
ref.current = getInitialValue();
|
ref.current = getInitialValue();
|
||||||
}
|
}
|
||||||
// $FlowFixMe (#64650789) Trouble refining types where `Symbol` is concerned.
|
// $FlowFixMe: Trouble refining types where `Symbol` is concerned.
|
||||||
return ref.current;
|
return ref.current;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ window.ResizeObserver = ResizeObserver;
|
|||||||
// JSDOM doesn't provide values for 'clientWidth' etc
|
// JSDOM doesn't provide values for 'clientWidth' etc
|
||||||
Object.defineProperty(window.document.documentElement, 'clientHeight', {
|
Object.defineProperty(window.document.documentElement, 'clientHeight', {
|
||||||
get: function () {
|
get: function () {
|
||||||
return this._jsdomClientWidth || window.innerHeight;
|
return this._jsdomClientHeight || window.innerHeight;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
Object.defineProperty(window.document.documentElement, 'clientWidth', {
|
Object.defineProperty(window.document.documentElement, 'clientWidth', {
|
||||||
|
|||||||
Reference in New Issue
Block a user