right_sidebar_spec.js 2.8 KB
Newer Older
1
import $ from 'jquery';
2
import MockAdapter from 'axios-mock-adapter';
3
import '~/commons/bootstrap';
4
import axios from '~/lib/utils/axios_utils';
P
Phil Hughes 已提交
5
import Sidebar from '~/right_sidebar';
C
Clement Ho 已提交
6

M
Mike Greiling 已提交
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
let $aside = null;
let $toggle = null;
let $icon = null;
let $page = null;
let $labelsIcon = null;

const assertSidebarState = function(state) {
  const shouldBeExpanded = state === 'expanded';
  const shouldBeCollapsed = state === 'collapsed';
  expect($aside.hasClass('right-sidebar-expanded')).toBe(shouldBeExpanded);
  expect($page.hasClass('right-sidebar-expanded')).toBe(shouldBeExpanded);
  expect($icon.hasClass('fa-angle-double-right')).toBe(shouldBeExpanded);
  expect($aside.hasClass('right-sidebar-collapsed')).toBe(shouldBeCollapsed);
  expect($page.hasClass('right-sidebar-collapsed')).toBe(shouldBeCollapsed);
  expect($icon.hasClass('fa-angle-double-left')).toBe(shouldBeCollapsed);
};

describe('RightSidebar', function() {
  describe('fixture tests', () => {
26
    const fixtureName = 'issues/open-issue.html.raw';
M
Mike Greiling 已提交
27 28 29 30 31 32 33 34 35 36 37 38 39 40
    preloadFixtures(fixtureName);
    loadJSONFixtures('todos/todos.json');
    let mock;

    beforeEach(function() {
      loadFixtures(fixtureName);
      mock = new MockAdapter(axios);
      new Sidebar(); // eslint-disable-line no-new
      $aside = $('.right-sidebar');
      $page = $('.layout-page');
      $icon = $aside.find('i');
      $toggle = $aside.find('.js-sidebar-toggle');
      $labelsIcon = $aside.find('.sidebar-collapsed-icon');
    });
41

M
Mike Greiling 已提交
42 43 44
    afterEach(() => {
      mock.restore();
    });
45

M
Mike Greiling 已提交
46 47 48 49 50 51 52
    it('should expand/collapse the sidebar when arrow is clicked', function() {
      assertSidebarState('expanded');
      $toggle.click();
      assertSidebarState('collapsed');
      $toggle.click();
      assertSidebarState('expanded');
    });
53

M
Mike Greiling 已提交
54 55 56 57
    it('should float over the page and when sidebar icons clicked', function() {
      $labelsIcon.click();
      assertSidebarState('expanded');
    });
58

M
Mike Greiling 已提交
59 60 61 62 63 64
    it('should collapse when the icon arrow clicked while it is floating on page', function() {
      $labelsIcon.click();
      assertSidebarState('expanded');
      $toggle.click();
      assertSidebarState('collapsed');
    });
65

M
Mike Greiling 已提交
66 67 68
    it('should broadcast todo:toggle event when add todo clicked', function(done) {
      const todos = getJSONFixture('todos/todos.json');
      mock.onPost(/(.*)\/todos$/).reply(200, todos);
69

M
Mike Greiling 已提交
70
      const todoToggleSpy = spyOnEvent(document, 'todo:toggle');
71

M
Mike Greiling 已提交
72
      $('.issuable-sidebar-header .js-issuable-todo').click();
73

M
Mike Greiling 已提交
74 75
      setTimeout(() => {
        expect(todoToggleSpy.calls.count()).toEqual(1);
76

M
Mike Greiling 已提交
77
        done();
78
      });
M
Mike Greiling 已提交
79
    });
80

M
Mike Greiling 已提交
81 82 83
    it('should not hide collapsed icons', () => {
      [].forEach.call(document.querySelectorAll('.sidebar-collapsed-icon'), el => {
        expect(el.querySelector('.fa, svg').classList.contains('hidden')).toBeFalsy();
84
      });
F
Fatih Acet 已提交
85 86
    });
  });
M
Mike Greiling 已提交
87
});