Skip to content

Add specs to auto-scroll.hook #142

@nasdan

Description

@nasdan

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.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions