Skip to content

How to use the rerender API ? #18

Closed
@DavidBabel

Description

@DavidBabel

Hey guys,

I'm sorry, i'm not very confortable with other testing libraries, and i cannot get how to test my hooks with this library.

I have a very simple hook, which wait for visibility to store a timestamp of render time, which will never update after that :

export function useRenderedAt(isViewable: boolean) {
  const [renderedAt, setRenderedAt] = React.useState(Infinity);

  React.useEffect(() => {
    if (isViewable) {
      setRenderedAt(Date.now());
    }
  }, [isViewable]);

  return renderedAt;
}

I'm trying to test it this way :

  test.only('should return render time when viewable', done => {
    const hook = renderHook(() => useRenderedAt(false), {
      initialProps: false
    });
    expect(hook.result.current).toBe(Infinity);
    hook.waitForNextUpdate().then(() => {
      expect(hook.result.current).not.toBe(Infinity);
      done();
    });
    hook.rerender(true);
  });

What am i doing wrong ?

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requested

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions