How to use the react-test-renderer.act function in react-test-renderer

To help you get started, we’ve selected a few react-test-renderer examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github GitbookIO / unstated / __tests__ / unstated.tsx View on Github external
const counter = new CounterContainer();
    const tree = renderer.create(
        
            
        
    );

    renderer.act(() => {
        const el: any = tree
            .toJSON()
            .children.find(({ props = {} }) => props.id === 'offset-text');
        expect(el.children).toEqual(['0']);
    });

    // This update should not trigger a re-render
    renderer.act(() => {
        counter.increment();
    });

    renderer.act(() => {
        const el: any = tree
            .toJSON()
            .children.find(({ props = {} }) => props.id === 'offset-text');
        expect(el.children).toEqual(['0']);
    });

    // This update should trigger a re-render
    renderer.act(() => {
        counter.setOffset(10);
    });

    renderer.act(() => {
github coralproject / talk / src / core / client / framework / testHelpers / act.ts View on Github external
export default function act(callback: () => T): T {
  let callbackResult: T;
  const actResult = TestRenderer.act(() => {
    callbackResult = callback();
    return callbackResult as any;
  });
  if (isPromiseLike(callbackResult!)) {
    // Return it this way, to preserve warnings that React emits.
    return {
      then(resolve: (value: any) => void, reject: (err?: Error) => void) {
        (actResult as PromiseLike).then(() => {
          resolve(callbackResult);
        }, reject);
      },
    } as any;
  }
  return callbackResult!;
}
github mui-org / material-ui / packages / material-ui / src / test-utils / describeConformance.js View on Github external
it('should render without errors in ReactTestRenderer', () => {
    ReactTestRenderer.act(() => {
      ReactTestRenderer.create(element, {
        createNodeMock: node => {
          return document.createElement(node.type);
        },
      });
    });
  });
}
github xiel / use-location-state / src / helpers / use-location-state-test-helpers / test-helpers.ts View on Github external
export async function asyncAct(callback: () => any) {
  return await act(async () => {
    await callback()
  })
}
github GitbookIO / unstated / __tests__ / unstated.tsx View on Github external
async function click({ children = [] }, id) {
    renderer.act(() => {
        const el: any = children.find(({ props = {} }) => props.id === id);
        el.props.onClick();
    });
}
github testing-library / react-hooks-testing-library / src / index.js View on Github external
const hookProps = { current: initialProps }

  const wrapUiIfNeeded = (innerElement) =>
    wrapper ? React.createElement(wrapper, null, innerElement) : innerElement

  const toRender = () =>
    wrapUiIfNeeded(
      }>
        
          {setValue}
        
      
    )

  let testRenderer
  act(() => {
    testRenderer = create(toRender())
  })
  const { unmount, update } = testRenderer

  return {
    result,
    rerender: (newProps = hookProps.current) => {
      hookProps.current = newProps
      act(() => {
        update(toRender())
      })
    },
    unmount: () => {
      act(() => {
        unmount()
      })
github coralproject / talk / src / core / client / framework / testHelpers / createTestRenderer.tsx View on Github external
localStorage: createPromisifiedStorage(),
    sessionStorage: createPromisifiedStorage(),
    rest: new RestClient("http://localhost/api"),
    postMessage: new PostMessageService(),
    browserInfo: params.browserInfo || { ios: false },
    uuidGenerator: createUUIDGenerator(),
    eventEmitter: new EventEmitter2({ wildcard: true, maxListeners: 20 }),
    clearSession: sinon.stub(),
    changeLocale: sinon.stub(),
    transitionControl: {
      allowTransition: true,
      history: [],
    },
  };
  let testRenderer: ReactTestRenderer;
  TestRenderer.act(() => {
    testRenderer = TestRenderer.create(
      {element},
      { createNodeMock }
    );
  });
  return {
    context,
    testRenderer: testRenderer!,
    subscriptionHandler: subscriptionHandler as SubscriptionHandlerReadOnly,
  };
}
github testing-library / react-hooks-testing-library / src / pure.js View on Github external
function unmountHook() {
    act(() => {
      removeCleanup(unmountHook)
      unmount()
    })
  }
github coralproject / talk / src / core / client / ui / components / Modal / Modal.spec.tsx View on Github external
const onBackdropClick = sinon.stub();
  const onClose = sinon.stub();
  expect(document.body.lastChild).toBeNull();
  let testRenderer: ReactTestRenderer;
  TestRenderer.act(() => {
    testRenderer = TestRenderer.create(
      
        <div>Test</div>
      ,
      { createNodeMock }
    );
  });
  testRenderer!.root
    .findByProps({ "data-testid": "backdrop" })
    .props.onClick(event);
  TestRenderer.act(() =&gt; {
    testRenderer.unmount();
  });
  expect(onBackdropClick.called).toBe(true);
  expect(onClose.calledWith(event, "backdropClick")).toBe(true);
});
github milesj / rut / packages / rut / src / internals / act.ts View on Github external
export async function doAsyncAct(cb: Actable, patchAdapter: Patcher): Promise {
  const restoreConsole = patchConsoleErrors();
  const restoreAdapter = patchAdapter();
  let value: T;

  try {
    await act(async () =&gt; {
      value = await integrationOptions.runWithTimers(cb);
    });
  } finally {
    restoreConsole();
    restoreAdapter();
  }

  return value!;
}