Welcome to mirror list, hosted at ThFree Co, Russian Federation.

issuable_sidebar_root_spec.js « components « sidebar « issuable « vue_shared « frontend « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 788ba70ddc06ff1a98b6e69e2b3569908ea539a6 (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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import { GlBreakpointInstance as bp } from '@gitlab/ui/dist/utils';
import Cookies from 'js-cookie';
import { shallowMountExtended } from 'helpers/vue_test_utils_helper';

import IssuableSidebarRoot from '~/vue_shared/issuable/sidebar/components/issuable_sidebar_root.vue';
import { USER_COLLAPSED_GUTTER_COOKIE } from '~/vue_shared/issuable/sidebar/constants';

const MOCK_LAYOUT_PAGE_CLASS = 'layout-page';

const createComponent = () => {
  setFixtures(`<div class="${MOCK_LAYOUT_PAGE_CLASS}"></div>`);

  return shallowMountExtended(IssuableSidebarRoot, {
    slots: {
      'right-sidebar-items': `
        <button class="js-todo">Todo</button>
      `,
    },
  });
};

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

  const findToggleSidebarButton = () => wrapper.findByTestId('toggle-right-sidebar-button');

  const assertPageLayoutClasses = ({ isExpanded }) => {
    const { classList } = document.querySelector(`.${MOCK_LAYOUT_PAGE_CLASS}`);
    if (isExpanded) {
      expect(classList).toContain('right-sidebar-expanded');
      expect(classList).not.toContain('right-sidebar-collapsed');
    } else {
      expect(classList).toContain('right-sidebar-collapsed');
      expect(classList).not.toContain('right-sidebar-expanded');
    }
  };

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

  describe('when sidebar is expanded', () => {
    beforeEach(() => {
      jest.spyOn(Cookies, 'set').mockImplementation(jest.fn());
      jest.spyOn(Cookies, 'get').mockReturnValue(false);
      jest.spyOn(bp, 'isDesktop').mockReturnValue(true);

      wrapper = createComponent();
    });

    it('renders component container element with class `right-sidebar-expanded`', () => {
      expect(wrapper.classes()).toContain('right-sidebar-expanded');
    });

    it('sets layout class to reflect expanded state', () => {
      assertPageLayoutClasses({ isExpanded: true });
    });

    it('renders sidebar toggle button with text and icon', () => {
      const buttonEl = findToggleSidebarButton();

      expect(buttonEl.exists()).toBe(true);
      expect(buttonEl.attributes('title')).toBe('Toggle sidebar');
      expect(buttonEl.find('span').text()).toBe('Collapse sidebar');
      expect(wrapper.findByTestId('icon-collapse').isVisible()).toBe(true);
    });

    describe('when collapsing the sidebar', () => {
      it('updates "collapsed_gutter" cookie value and layout classes', async () => {
        await findToggleSidebarButton().trigger('click');

        expect(Cookies.set).toHaveBeenCalledWith(USER_COLLAPSED_GUTTER_COOKIE, true);
        assertPageLayoutClasses({ isExpanded: false });
      });
    });

    describe('when window `resize` event is triggered', () => {
      it.each`
        breakpoint | isExpandedValue
        ${'xs'}    | ${false}
        ${'sm'}    | ${false}
        ${'md'}    | ${false}
        ${'lg'}    | ${true}
        ${'xl'}    | ${true}
      `(
        'sets page layout classes correctly when current screen size is `$breakpoint`',
        async ({ breakpoint, isExpandedValue }) => {
          jest.spyOn(bp, 'isDesktop').mockReturnValue(breakpoint === 'lg' || breakpoint === 'xl');

          window.dispatchEvent(new Event('resize'));
          await wrapper.vm.$nextTick();

          assertPageLayoutClasses({ isExpanded: isExpandedValue });
        },
      );
    });
  });

  describe('when sidebar is collapsed', () => {
    beforeEach(() => {
      jest.spyOn(Cookies, 'get').mockReturnValue(true);

      wrapper = createComponent();
    });

    it('renders component container element with class `right-sidebar-collapsed`', () => {
      expect(wrapper.classes()).toContain('right-sidebar-collapsed');
    });

    it('sets layout class to reflect collapsed state', () => {
      assertPageLayoutClasses({ isExpanded: false });
    });

    it('renders sidebar toggle button with text and icon', () => {
      const buttonEl = findToggleSidebarButton();

      expect(buttonEl.exists()).toBe(true);
      expect(buttonEl.attributes('title')).toBe('Toggle sidebar');
      expect(wrapper.findByTestId('icon-expand').isVisible()).toBe(true);
    });
  });

  it('renders slotted sidebar items', () => {
    wrapper = createComponent();

    const sidebarItemsEl = wrapper.findByTestId('sidebar-items');

    expect(sidebarItemsEl.exists()).toBe(true);
    expect(sidebarItemsEl.find('button.js-todo').exists()).toBe(true);
  });
});