code-paster
code-paster copied to clipboard
Add specs to auto-scroll.hook
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
isAutoScrollEnabledvalue - Disable auto scroll, calls
doAutoScrolland check it doesn't update thescrollTopvalue.
- Disable auto scroll and check