Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Shallow] Implement callEffects option to call effects from shallow renderer (#15275) #16168

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 88 additions & 5 deletions packages/react-test-renderer/src/ReactShallowRenderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,12 +175,21 @@ function basicStateReducer<S>(state: S, action: BasicStateAction<S>): S {
return typeof action === 'function' ? action(state) : action;
}

type ShallowRenderOptions = {
callEffects: boolean,
};

const isEffectHook = Symbol();

class ReactShallowRenderer {
static createRenderer = function() {
return new ReactShallowRenderer();
static createRenderer = function(
options: ShallowRenderOptions = {callEffects: false},
) {
return new ReactShallowRenderer(options);
};

constructor() {
constructor(options: ShallowRenderOptions) {
this._options = options;
this._reset();
}

Expand All @@ -202,6 +211,7 @@ class ReactShallowRenderer {
this._numberOfReRenders = 0;
}

_options: ShallowRenderOptions;
_context: null | Object;
_newState: null | Object;
_instance: any;
Expand Down Expand Up @@ -311,6 +321,54 @@ class ReactShallowRenderer {
);
};

const useGenericEffect = (
create: () => (() => void) | void,
inputs: Array<mixed> | void | null,
isLayoutEffect: boolean,
) => {
this._validateCurrentlyRenderingComponent();
this._createWorkInProgressHook();

if (this._workInProgressHook !== null) {
if (this._workInProgressHook.memoizedState == null) {
this._workInProgressHook.memoizedState = {
isEffectHook,
isLayoutEffect,
create,
inputs,
destroy: null,
run: true,
};
} else {
const {memoizedState} = this._workInProgressHook;
insidewhy marked this conversation as resolved.
Show resolved Hide resolved
this._workInProgressHook.memoizedState = {
isEffectHook,
isLayoutEffect,
create,
inputs,
destroy: memoizedState.destroy,
run:
inputs == null ||
!areHookInputsEqual(inputs, memoizedState.inputs),
};
}
}
};

const useEffect = (
create: () => (() => void) | void,
inputs: Array<mixed> | void | null,
) => {
useGenericEffect(create, inputs, false);
};

const useLayoutEffect = (
create: () => (() => void) | void,
inputs: Array<mixed> | void | null,
) => {
useGenericEffect(create, inputs, true);
};

const useMemo = <T>(
nextCreate: () => T,
deps: Array<mixed> | void | null,
Expand Down Expand Up @@ -385,9 +443,9 @@ class ReactShallowRenderer {
return readContext(context);
},
useDebugValue: noOp,
useEffect: noOp,
useEffect: this._options.callEffects ? useEffect : noOp,
useImperativeHandle: noOp,
useLayoutEffect: noOp,
useLayoutEffect: this._options.callEffects ? useLayoutEffect : noOp,
useMemo,
useReducer,
useRef,
Expand Down Expand Up @@ -630,6 +688,10 @@ class ReactShallowRenderer {
ReactCurrentDispatcher.current = prevDispatcher;
}
this._finishHooks(element, context);
if (this._options.callEffects) {
this._callEffects(true);
insidewhy marked this conversation as resolved.
Show resolved Hide resolved
this._callEffects(false);
}
}
}
}
Expand Down Expand Up @@ -690,6 +752,27 @@ class ReactShallowRenderer {
// because DOM refs are not available.
}

_callEffects(callLayoutEffects: boolean) {
for (
let hook = this._firstWorkInProgressHook;
hook !== null;
hook = hook.next
) {
const {memoizedState} = hook;
if (
memoizedState != null &&
memoizedState.isEffectHook === isEffectHook &&
memoizedState.isLayoutEffect === callLayoutEffects &&
memoizedState.run
) {
if (memoizedState.destroy) {
memoizedState.destroy();
}
memoizedState.destroy = memoizedState.create();
}
}
}

_updateClassComponent(
elementType: Function,
element: ReactElement,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,8 +231,8 @@ describe('ReactShallowRenderer with hooks', () => {
);
});

it('should not trigger effects', () => {
let effectsCalled = [];
it('should not trigger effects by default', () => {
const effectsCalled = [];

function SomeComponent({defaultName}) {
React.useEffect(() => {
Expand All @@ -252,6 +252,111 @@ describe('ReactShallowRenderer with hooks', () => {
expect(effectsCalled).toEqual([]);
});

describe('when callEffects option is used', () => {
it('should trigger effects after render', () => {
const happenings = [];

function SomeComponent({defaultName}) {
React.useEffect(() => {
happenings.push('create effect');
});

React.useLayoutEffect(() => {
happenings.push('create layout effect');
});

happenings.push('render');

return <div>Hello world</div>;
}

const shallowRenderer = createRenderer({callEffects: true});
shallowRenderer.render(<SomeComponent />);

// Note the layout effect is triggered first.
expect(happenings).toEqual([
'render',
'create layout effect',
'create effect',
]);
});

it('should trigger effects and destroy depending on inputs', () => {
let _setFriend;
const happenings = [];

function SomeComponent() {
const [friend, setFriend] = React.useState('Bons');
const cat = 'Muskus';
_setFriend = setFriend;

React.useEffect(
() => {
happenings.push('create friend effect');
return () => {
happenings.push('destroy friend effect');
};
},
[friend],
);

React.useEffect(() => {
happenings.push('create empty effect');
return () => {
happenings.push('destroy empty effect');
};
});

React.useEffect(
() => {
happenings.push('create cat effect');
return () => {
happenings.push('destroy cat effect');
};
},
[cat],
);

React.useEffect(
() => {
happenings.push('create both effect');
return () => {
happenings.push('destroy both effect');
};
},
[friend, cat],
);

return (
<div>
Hello {friend} with {cat}
</div>
);
}

const shallowRenderer = createRenderer({callEffects: true});
shallowRenderer.render(<SomeComponent />);

expect(happenings).toEqual([
'create friend effect',
'create empty effect',
'create cat effect',
'create both effect',
]);

happenings.splice(0);
_setFriend('Maryam');
expect(happenings).toEqual([
'destroy friend effect',
'create friend effect',
'destroy empty effect',
'create empty effect',
'destroy both effect',
'create both effect',
]);
});
});

it('should work with useRef', () => {
function SomeComponent() {
const randomNumberRef = React.useRef({number: Math.random()});
Expand Down