board_settings_sidebar_spec.js 4.27 KB
Newer Older
1
import { GlDrawer, GlLabel } from '@gitlab/ui';
2
import { shallowMount } from '@vue/test-utils';
sstern's avatar
sstern committed
3
import { MountingPortal } from 'portal-vue';
4
import Vue from 'vue';
5
import Vuex from 'vuex';
6
import { extendedWrapper } from 'helpers/vue_test_utils_helper';
7
import BoardSettingsSidebar from '~/boards/components/board_settings_sidebar.vue';
8
import { inactiveId, LIST } from '~/boards/constants';
9 10 11
import actions from '~/boards/stores/actions';
import getters from '~/boards/stores/getters';
import mutations from '~/boards/stores/mutations';
12
import sidebarEventHub from '~/sidebar/event_hub';
13
import { mockLabelList } from '../mock_data';
14

15
Vue.use(Vuex);
16 17 18

describe('BoardSettingsSidebar', () => {
  let wrapper;
19 20 21
  const labelTitle = mockLabelList.label.title;
  const labelColor = mockLabelList.label.color;
  const listId = mockLabelList.id;
22

23 24
  const findRemoveButton = () => wrapper.findByTestId('remove-list');

25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
  const createComponent = ({
    canAdminList = false,
    list = {},
    sidebarType = LIST,
    activeId = inactiveId,
  } = {}) => {
    const boardLists = {
      [listId]: list,
    };
    const store = new Vuex.Store({
      state: { sidebarType, activeId, boardLists },
      getters,
      mutations,
      actions,
    });

41 42 43 44 45
    wrapper = extendedWrapper(
      shallowMount(BoardSettingsSidebar, {
        store,
        provide: {
          canAdminList,
46
          scopedLabelsAvailable: false,
47 48 49
        },
      }),
    );
50 51 52 53 54 55 56
  };
  const findLabel = () => wrapper.find(GlLabel);
  const findDrawer = () => wrapper.find(GlDrawer);

  afterEach(() => {
    jest.restoreAllMocks();
    wrapper.destroy();
57
    wrapper = null;
58 59
  });

sstern's avatar
sstern committed
60 61 62 63 64 65 66 67 68 69
  it('finds a MountingPortal component', () => {
    createComponent();

    expect(wrapper.find(MountingPortal).props()).toMatchObject({
      mountTo: '#js-right-sidebar-portal',
      append: true,
      name: 'board-settings-sidebar',
    });
  });

Scott Stern's avatar
Scott Stern committed
70 71 72
  describe('when sidebarType is "list"', () => {
    it('finds a GlDrawer component', () => {
      createComponent();
73

Scott Stern's avatar
Scott Stern committed
74 75
      expect(findDrawer().exists()).toBe(true);
    });
76

Scott Stern's avatar
Scott Stern committed
77 78 79
    describe('on close', () => {
      it('closes the sidebar', async () => {
        createComponent();
80

Scott Stern's avatar
Scott Stern committed
81
        findDrawer().vm.$emit('close');
82

Scott Stern's avatar
Scott Stern committed
83
        await wrapper.vm.$nextTick();
84

Scott Stern's avatar
Scott Stern committed
85 86
        expect(wrapper.find(GlDrawer).exists()).toBe(false);
      });
87

Scott Stern's avatar
Scott Stern committed
88 89
      it('closes the sidebar when emitting the correct event', async () => {
        createComponent();
90

Scott Stern's avatar
Scott Stern committed
91
        sidebarEventHub.$emit('sidebar.closeAll');
92

Scott Stern's avatar
Scott Stern committed
93
        await wrapper.vm.$nextTick();
94

Scott Stern's avatar
Scott Stern committed
95 96
        expect(wrapper.find(GlDrawer).exists()).toBe(false);
      });
97 98
    });

Scott Stern's avatar
Scott Stern committed
99 100 101
    describe('when activeId is zero', () => {
      it('renders GlDrawer with open false', () => {
        createComponent();
102

Scott Stern's avatar
Scott Stern committed
103 104
        expect(findDrawer().props('open')).toBe(false);
      });
105 106
    });

Scott Stern's avatar
Scott Stern committed
107
    describe('when activeId is greater than zero', () => {
108 109
      it('renders GlDrawer with open true', () => {
        createComponent({ list: mockLabelList, activeId: listId });
Scott Stern's avatar
Scott Stern committed
110 111 112

        expect(findDrawer().props('open')).toBe(true);
      });
113 114
    });

115
    describe('when activeId is in state', () => {
Scott Stern's avatar
Scott Stern committed
116
      it('renders label title', () => {
117 118
        createComponent({ list: mockLabelList, activeId: listId });

Scott Stern's avatar
Scott Stern committed
119 120
        expect(findLabel().props('title')).toBe(labelTitle);
      });
121

Scott Stern's avatar
Scott Stern committed
122
      it('renders label background color', () => {
123 124
        createComponent({ list: mockLabelList, activeId: listId });

Scott Stern's avatar
Scott Stern committed
125 126
        expect(findLabel().props('backgroundColor')).toBe(labelColor);
      });
127 128
    });

129
    describe('when activeId is not in state', () => {
Scott Stern's avatar
Scott Stern committed
130
      it('does not render GlLabel', () => {
131 132
        createComponent({ list: mockLabelList });

Scott Stern's avatar
Scott Stern committed
133 134
        expect(findLabel().exists()).toBe(false);
      });
135
    });
Scott Stern's avatar
Scott Stern committed
136
  });
137

Scott Stern's avatar
Scott Stern committed
138 139
  describe('when sidebarType is not List', () => {
    it('does not render GlDrawer', () => {
140 141
      createComponent({ sidebarType: '' });

Scott Stern's avatar
Scott Stern committed
142
      expect(findDrawer().exists()).toBe(false);
143 144
    });
  });
145 146 147 148 149 150 151 152 153

  it('does not render "Remove list" when user cannot admin the boards list', () => {
    createComponent();

    expect(findRemoveButton().exists()).toBe(false);
  });

  describe('when user can admin the boards list', () => {
    it('renders "Remove list" button', () => {
154 155
      createComponent({ canAdminList: true, activeId: listId, list: mockLabelList });

156 157 158
      expect(findRemoveButton().exists()).toBe(true);
    });
  });
159
});