right_sidebar_spec.js 4.0 KB
Newer Older
1
/* eslint-disable space-before-function-paren, no-var, one-var, one-var-declaration-per-line, new-parens, no-return-assign, new-cap, vars-on-top, max-len */
F
Fatih Acet 已提交
2

3
import MockAdapter from 'axios-mock-adapter';
4
import '~/commons/bootstrap';
5
import axios from '~/lib/utils/axios_utils';
P
Phil Hughes 已提交
6
import Sidebar from '~/right_sidebar';
C
Clement Ho 已提交
7

F
Fatih Acet 已提交
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
(function() {
  var $aside, $icon, $labelsIcon, $page, $toggle, assertSidebarState;

  this.sidebar = null;

  $aside = null;

  $toggle = null;

  $icon = null;

  $page = null;

  $labelsIcon = null;

  assertSidebarState = function(state) {
    var shouldBeCollapsed, shouldBeExpanded;
    shouldBeExpanded = state === 'expanded';
    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);
    return expect($icon.hasClass('fa-angle-double-left')).toBe(shouldBeCollapsed);
  };

  describe('RightSidebar', function() {
36 37 38 39
    describe('fixture tests', () => {
      var fixtureName = 'issues/open-issue.html.raw';
      preloadFixtures(fixtureName);
      loadJSONFixtures('todos/todos.json');
40
      let mock;
41 42 43

      beforeEach(function() {
        loadFixtures(fixtureName);
44 45
        mock = new MockAdapter(axios);
        this.sidebar = new Sidebar();
46
        $aside = $('.right-sidebar');
47
        $page = $('.layout-page');
48 49 50 51
        $icon = $aside.find('i');
        $toggle = $aside.find('.js-sidebar-toggle');
        return $labelsIcon = $aside.find('.sidebar-collapsed-icon');
      });
52 53 54 55 56

      afterEach(() => {
        mock.restore();
      });

57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
      it('should expand/collapse the sidebar when arrow is clicked', function() {
        assertSidebarState('expanded');
        $toggle.click();
        assertSidebarState('collapsed');
        $toggle.click();
        assertSidebarState('expanded');
      });
      it('should float over the page and when sidebar icons clicked', function() {
        $labelsIcon.click();
        return assertSidebarState('expanded');
      });
      it('should collapse when the icon arrow clicked while it is floating on page', function() {
        $labelsIcon.click();
        assertSidebarState('expanded');
        $toggle.click();
        return assertSidebarState('collapsed');
      });

75
      it('should broadcast todo:toggle event when add todo clicked', function(done) {
76
        var todos = getJSONFixture('todos/todos.json');
P
Phil Hughes 已提交
77
        mock.onPost(/(.*)\/todos$/).reply(200, todos);
78 79 80 81 82

        var todoToggleSpy = spyOnEvent(document, 'todo:toggle');

        $('.issuable-sidebar-header .js-issuable-todo').click();

83 84 85 86 87
        setTimeout(() => {
          expect(todoToggleSpy.calls.count()).toEqual(1);

          done();
        });
88 89 90 91 92 93 94
      });

      it('should not hide collapsed icons', () => {
        [].forEach.call(document.querySelectorAll('.sidebar-collapsed-icon'), (el) => {
          expect(el.querySelector('.fa, svg').classList.contains('hidden')).toBeFalsy();
        });
      });
F
Fatih Acet 已提交
95
    });
C
Clement Ho 已提交
96

97 98 99 100 101 102 103 104 105
    describe('sidebarToggleClicked', () => {
      const event = jasmine.createSpyObj('event', ['preventDefault']);

      beforeEach(() => {
        spyOn($.fn, 'hasClass').and.returnValue(false);
      });

      afterEach(() => {
        gl.lazyLoader = undefined;
C
Clement Ho 已提交
106 107
      });

108 109
      it('calls loadCheck if lazyLoader is set', () => {
        gl.lazyLoader = jasmine.createSpyObj('lazyLoader', ['loadCheck']);
C
Clement Ho 已提交
110

111
        Sidebar.prototype.sidebarToggleClicked(event);
C
Clement Ho 已提交
112

113 114 115 116 117 118 119
        expect(gl.lazyLoader.loadCheck).toHaveBeenCalled();
      });

      it('does not throw if lazyLoader is not defined', () => {
        gl.lazyLoader = undefined;

        const toggle = Sidebar.prototype.sidebarToggleClicked.bind(null, event);
120

121
        expect(toggle).not.toThrow();
122 123
      });
    });
F
Fatih Acet 已提交
124
  });
125
}).call(window);