summaryrefslogtreecommitdiff
path: root/spec/frontend/lib/utils/scroll_utils_spec.js
blob: d42e25b929c4004f31dabcc4d66bcd90729c4975 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import { isScrolledToBottom } from '~/lib/utils/scroll_utils';

describe('isScrolledToBottom', () => {
  const setScrollGetters = (getters) => {
    Object.entries(getters).forEach(([name, value]) => {
      jest.spyOn(Element.prototype, name, 'get').mockReturnValue(value);
    });
  };

  it.each`
    context                                                           | scrollTop | scrollHeight | result
    ${'returns false when not scrolled to bottom'}                    | ${0}      | ${2000}      | ${false}
    ${'returns true when scrolled to bottom'}                         | ${1000}   | ${2000}      | ${true}
    ${'returns true when scrolled to bottom with subpixel precision'} | ${999.25} | ${2000}      | ${true}
    ${'returns true when cannot scroll'}                              | ${0}      | ${500}       | ${true}
  `('$context', ({ scrollTop, scrollHeight, result }) => {
    setScrollGetters({ scrollTop, clientHeight: 1000, scrollHeight });

    expect(isScrolledToBottom()).toBe(result);
  });
});