summaryrefslogtreecommitdiff
path: root/horizon/static/framework/util/navigations/navigations.service.spec.js
blob: 44c3e3bfdc689a1a1b7d76b4a1303d06dde233e0 (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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/*
 * (c) Copyright 2016 Hewlett Packard Enterprise Development Company LP
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

(function() {
  'use strict';

  describe('horizon.framework.util.navigations.service', function() {
    var service, navigations, spyElement;
    var imagesUrl = '/project/images/';
    var breadcrumb = ['Project', 'Compute', 'Images'];
    var breadcrumbWithoutGroup = ['Project', 'Images'];

    function getNavsElement (selector) {
      try {
        // for searching element
        return navigations.find(selector);
      } catch (e) {
        // for creating element
        return $(selector);
      }
    }

    beforeEach(module('horizon.framework.util.navigations'));

    beforeEach(inject(function($injector) {
      service = $injector.get('horizon.framework.util.navigations.service');
      navigations = angular.element(
        '<div>' +
        '  <!-- navigation side bar -->' +
        '  <li class="openstack-dashboard">' +
        '    <a class="" aria-expanded="true">' +
        '      Project' +
        '    </a>' +
        '    <ul class="in" style="">' +
        '      <li class="openstack-panel-group">' +
        '        <a class="" area-expanded="true">' +
        '          Compute' +
        '        </a>' +
        '        <div class="in" style="">' +
        '          <a class="openstack-panel active" href="/project/images/">' +
        '            Images' +
        '          </a>' +
        '        </div>' +
        '      </li>' +
        '    </ul>' +
        '  </li>' +
        '  <!-- breadcrumb -->' +
        '  <div class="page-breadcrumb">' +
        '    <ol class="breadcrumb">' +
        '      <li>Project</li>' +
        '      <li>Compute</li>' +
        '      <li class="active">Images</li>' +
        '    </ol>' +
        '  </div>' +
        '</div>');
      spyElement = spyOn(angular, 'element').and.callFake(getNavsElement);
    }));

    afterEach(function() {
      spyElement.and.callThrough();
    });

    describe('getActivePanelUrl', function() {
      it('returns an empty array if no items', function() {
        var activeUrl = service.getActivePanelUrl();

        expect(activeUrl).toBe(imagesUrl);
      });
    });

    describe('collapseAllNavigation', function() {
      it('collapse all nodes on navigation side bar', function() {
        service.collapseAllNavigation();

        var hasIn = navigations.find('.in');
        expect(hasIn.length).toBe(0);
        var collapsed = navigations.find('a.collapsed[aria-expanded=false]');
        expect(collapsed.length).toBe(2);
        var hasActive = navigations.find('a.openstack-panel.active');
        expect(hasActive.length).toBe(0);
      });
    });

    describe('expandNavigationByUrl', function() {
      it('expands navigation side bar and return their label of selected nodes', function() {
        spyOn(service, 'collapseAllNavigation').and.callThrough();
        var list = service.expandNavigationByUrl(imagesUrl);

        expect(list).toEqual(breadcrumb);

        var hasIn = navigations.find('.in');
        expect(hasIn.length).toBe(2);
        var expanded = navigations.find('a[aria-expanded=true]');
        expect(expanded.length).toBe(2);
        var hasActive = navigations.find('a.openstack-panel.active');
        expect(hasActive.length).toBe(1);
      });
    });

    describe('expandNavigationByUrl', function() {
      it('expands navigation side bar without panelgroup' +
         'and return their label of selected nodes', function() {
        navigations = angular.element(
            '<div>' +
            '  <!-- navigation side bar -->' +
            '  <li class="openstack-dashboard">' +
            '    <a class="" aria-expanded="true">' +
            '      Project' +
            '    </a>' +
            '    <ul class="in" style="">' +
            '      <div class="in" style="">' +
            '        <a class="openstack-panel active" href="/project/images/">' +
            '          Images' +
            '        </a>' +
            '      </div>' +
            '    </ul>' +
            '  </li>' +
            '  <!-- breadcrumb -->' +
            '  <div class="page-breadcrumb">' +
            '    <ol class="breadcrumb">' +
            '      <li>Project</li>' +
            '      <li>Compute</li>' +
            '      <li class="active">Images</li>' +
            '    </ol>' +
            '  </div>' +
            '</div>');

        spyOn(service, 'collapseAllNavigation').and.callThrough();
        var list = service.expandNavigationByUrl(imagesUrl);

        expect(list).toEqual(breadcrumbWithoutGroup);

        var hasIn = navigations.find('.in');
        expect(hasIn.length).toBe(2);
        var expanded = navigations.find('a[aria-expanded=true]');
        expect(expanded.length).toBe(1);
        var hasActive = navigations.find('a.openstack-panel.active');
        expect(hasActive.length).toBe(1);
      });
    });

    describe('setBreadcrumb', function() {
      it('sets breadcrumb items from specified array', function() {
        service.setBreadcrumb(breadcrumb);
      });
    });

    describe('isNavigationExists', function() {
      it('returns true if navigation for specified URL exists', function() {
        var result = service.isNavigationExists('/project/images/');
        expect(result).toEqual(true);
      });

      it('returns false if navigation for specified URL does not exist', function() {
        var result = service.isNavigationExists('/not/found/');
        expect(result).toEqual(false);
      });
    });

  });

})();