code-paster icon indicating copy to clipboard operation
code-paster copied to clipboard

Add specs to auto-scroll.hook

Open nasdan opened this issue 4 years ago • 0 comments
trafficstars

Here you have a working tests checking the scrollTop value when it calls doAutoScroll.

import { textarea } from 'pods/trainer/components/new-text.styles';
import React from 'react';
import { renderHook } from '@testing-library/react-hooks';
import { useAutoScroll } from './auto-scroll.hook';

describe('src/common/hooks/auto-scroll.hook specs', () => {
  it('should return initial values when it renders the hook', () => {
    // Arrange
    const textArea = {
      scrollHeight: 10,
      scrollTop: undefined,
    };

    jest.spyOn(React, 'useRef').mockReturnValue({
      current: textArea,
    });

    // Act
    const { result } = renderHook(() => useAutoScroll());

    // Assert
    expect(result.current.isAutoScrollEnabled).toBeTruthy();
    expect(result.current.textAreaRef.current).not.toBeNull();
    expect(result.current.textAreaRef.current.scrollHeight).toEqual(10);
    expect(result.current.textAreaRef.current.scrollTop).toBeUndefined();
  });

  it('should update scrollTop with height value when it call doAutoScroll', () => {
    // Arrange
    const textArea = {
      scrollHeight: 10,
      scrollTop: undefined,
    };

    jest.spyOn(React, 'useRef').mockReturnValue({
      current: textArea,
    });

    // Act
    const { result } = renderHook(() => useAutoScroll());

    result.current.doAutoScroll();

    // Assert
    expect(result.current.textAreaRef.current.scrollTop).toEqual(10);
  });
});

  • Pending specs:
    • Disable auto scroll and check isAutoScrollEnabled value
    • Disable auto scroll, calls doAutoScroll and check it doesn't update the scrollTop value.

nasdan avatar Feb 20 '21 09:02 nasdan