Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add specs to auto-scroll.hook #142

Open
nasdan opened this issue Feb 20, 2021 · 0 comments
Open

Add specs to auto-scroll.hook #142

nasdan opened this issue Feb 20, 2021 · 0 comments
Assignees

Comments

@nasdan
Copy link
Member

nasdan commented Feb 20, 2021

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants