summaryrefslogtreecommitdiff
path: root/spec/frontend/logs/components/log_control_buttons_spec.js
blob: f344e8189c3d8f0e14a23337729cf0b88e439121 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import { shallowMount } from '@vue/test-utils';
import { GlButton } from '@gitlab/ui';
import LogControlButtons from '~/logs/components/log_control_buttons.vue';
import {
  canScroll,
  isScrolledToTop,
  isScrolledToBottom,
  scrollDown,
  scrollUp,
} from '~/lib/utils/scroll_utils';

jest.mock('~/lib/utils/scroll_utils');

describe('LogControlButtons', () => {
  let wrapper;

  const findScrollToTop = () => wrapper.find('.js-scroll-to-top');
  const findScrollToBottom = () => wrapper.find('.js-scroll-to-bottom');
  const findRefreshBtn = () => wrapper.find('.js-refresh-log');

  const initWrapper = () => {
    wrapper = shallowMount(LogControlButtons);
  };

  afterEach(() => {
    if (wrapper) {
      wrapper.destroy();
    }
  });

  it('displays UI elements', () => {
    initWrapper();

    expect(wrapper.isVueInstance()).toBe(true);
    expect(wrapper.isEmpty()).toBe(false);

    expect(findScrollToTop().is(GlButton)).toBe(true);
    expect(findScrollToBottom().is(GlButton)).toBe(true);
    expect(findRefreshBtn().is(GlButton)).toBe(true);
  });

  it('emits a `refresh` event on click on `refresh` button', () => {
    initWrapper();

    // An `undefined` value means no event was emitted
    expect(wrapper.emitted('refresh')).toBe(undefined);

    findRefreshBtn().vm.$emit('click');

    return wrapper.vm.$nextTick().then(() => {
      expect(wrapper.emitted('refresh')).toHaveLength(1);
    });
  });

  describe('when scrolling actions are enabled', () => {
    beforeEach(() => {
      // mock scrolled to the middle of a long page
      canScroll.mockReturnValue(true);
      isScrolledToBottom.mockReturnValue(false);
      isScrolledToTop.mockReturnValue(false);

      initWrapper();
      wrapper.vm.update();
      return wrapper.vm.$nextTick();
    });

    afterEach(() => {
      canScroll.mockReset();
      isScrolledToTop.mockReset();
      isScrolledToBottom.mockReset();
    });

    it('click on "scroll to top" scrolls up', () => {
      expect(findScrollToTop().is('[disabled]')).toBe(false);

      findScrollToTop().vm.$emit('click');

      expect(scrollUp).toHaveBeenCalledTimes(1);
    });

    it('click on "scroll to bottom" scrolls down', () => {
      expect(findScrollToBottom().is('[disabled]')).toBe(false);

      findScrollToBottom().vm.$emit('click');

      expect(scrollDown).toHaveBeenCalledTimes(1); // plus one time when trace was loaded
    });
  });

  describe('when scrolling actions are disabled', () => {
    beforeEach(() => {
      // mock a short page without a scrollbar
      canScroll.mockReturnValue(false);
      isScrolledToBottom.mockReturnValue(true);
      isScrolledToTop.mockReturnValue(true);

      initWrapper();
    });

    it('buttons are disabled', () => {
      wrapper.vm.update();
      return wrapper.vm.$nextTick(() => {
        expect(findScrollToTop().is('[disabled]')).toBe(true);
        expect(findScrollToBottom().is('[disabled]')).toBe(true);
      });
    });
  });
});