Repository: facebook/react
File: packages/react-native-renderer/src/__tests__/ReactFabric-test.internal.js (JavaScript)

Code before:
    });
    expect(
      nativeFabricUIManager.__dumpHierarchyForJestTestsOnly(),
    ).toMatchSnapshot();

    await act(() => {
      ReactFabric.render(
        <Text foo="b" bar="b">
          2
        </Text>,
        11,
      );
    });
    expect(
      nativeFabricUIManager.cloneNodeWithNewChildrenAndProps.mock.calls[0][1],
    ).toEqual({
      foo: 'b',
    });
    expect(
      nativeFabricUIManager.__dumpHierarchyForJestTestsOnly(),
    ).toMatchSnapshot();
  });

  it.each([true, false])(
    'should not clone nodes without children when updating props (feature flag = %p)',
    async passChildren => {
      const ReactFeatureFlags = require('shared/ReactFeatureFlags');
      ReactFeatureFlags.passChildrenWhenCloningPersistedNodes = passChildren;

      const View = createReactNativeComponentClass('RCTView', () => ({
        validAttributes: {foo: true},
        uiViewClassName: 'RCTView',
      }));

      const Component = ({foo}) => (
        <View>
          <View foo={foo} />
        </View>
      );

      await act(() => ReactFabric.render(<Component foo={true} />, 11));
      expect(nativeFabricUIManager.completeRoot).toBeCalled();
      jest.clearAllMocks();

      await act(() => ReactFabric.render(<Component foo={false} />, 11));
      expect(nativeFabricUIManager.cloneNode).not.toBeCalled();
      expect(nativeFabricUIManager.cloneNodeWithNewProps).toHaveBeenCalledTimes(
        1,
      );
      expect(nativeFabricUIManager.cloneNodeWithNewProps).toHaveBeenCalledWith(
        expect.anything(),

Review comment (suggestion):
Because you set up a the feature flag with `__VARIANT__`, CI will already run these tests in both versions. (You can do this yourself on the command line by passing `--no-variant` to `yarn test`.)

You can then check the value of the flag with `if (gate(flags => flags.passChildrenWhenCloningPersistedNodes)) {...}`.

We prefer that pattern instead of mutating the ReactFeatureFlags module because it preserves the ability to run tests on the compiled build artifacts.

Code after:
    });
    expect(
      nativeFabricUIManager.__dumpHierarchyForJestTestsOnly(),
    ).toMatchSnapshot();

    await act(() => {
      ReactFabric.render(
        <Text foo="b" bar="b">
          2
        </Text>,
        11,
      );
    });
    const argIndex = gate(flags => flags.passChildrenWhenCloningPersistedNodes)
      ? 2
      : 1;
    expect(
      nativeFabricUIManager.cloneNodeWithNewChildrenAndProps.mock.calls[0][
        argIndex
      ],
    ).toEqual({
      foo: 'b',
    });
    expect(
      nativeFabricUIManager.__dumpHierarchyForJestTestsOnly(),
    ).toMatchSnapshot();
  });

  it('should not clone nodes without children when updating props', async () => {
    const View = createReactNativeComponentClass('RCTView', () => ({
      validAttributes: {foo: true},
      uiViewClassName: 'RCTView',
    }));

    const Component = ({foo}) => (
      <View>
        <View foo={foo} />
      </View>
    );

    await act(() => ReactFabric.render(<Component foo={true} />, 11));
    expect(nativeFabricUIManager.completeRoot).toBeCalled();
    jest.clearAllMocks();

    await act(() => ReactFabric.render(<Component foo={false} />, 11));
    expect(nativeFabricUIManager.cloneNode).not.toBeCalled();
    expect(nativeFabricUIManager.cloneNodeWithNewProps).toHaveBeenCalledTimes(
      1,
    );
    expect(nativeFabricUIManager.cloneNodeWithNewProps).toHaveBeenCalledWith(
      expect.anything(),