blob: d14d514759e94be0764fef7f08ec1b350ef82b3c [file] [log] [blame]
Simon Hunt9d286562015-03-09 13:53:50 -07001/*
2 * Copyright 2014,2015 Open Networking Laboratory
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/*
18 ONOS GUI -- Util -- Theme Service - Unit Tests
19 */
20describe('factory: fw/nav/nav.js', function() {
Bri Prebilic Cole8f07f0d2015-04-23 13:28:43 -070021 var $log, $location, $window, ns, fs;
Simon Hunt9d286562015-03-09 13:53:50 -070022 var d3Elem;
23
24 beforeEach(module('onosNav', 'onosUtil'));
25
Bri Prebilic Cole8f07f0d2015-04-23 13:28:43 -070026 var mockWindow = {
27 location: {
28 href: 'http://server/#/mock/url'
29 }
30 };
31
32 beforeEach(function () {
33 module(function ($provide) {
34 $provide.value('$window', mockWindow);
35 });
36 });
37
38 beforeEach(inject(function (_$log_, _$location_, _$window_,
39 NavService, FnService) {
Simon Hunt9d286562015-03-09 13:53:50 -070040 $log = _$log_;
Bri Prebilic Cole8f07f0d2015-04-23 13:28:43 -070041 $location = _$location_;
42 $window = _$window_;
43 ns = NavService;
Simon Hunt9d286562015-03-09 13:53:50 -070044 fs = FnService;
45 d3Elem = d3.select('body').append('div').attr('id', 'nav');
46 ns.hideNav();
47 }));
48
49 afterEach(function () {
50 d3.select('#nav').remove();
51 });
52
53 it('should define NavService', function () {
54 expect(ns).toBeDefined();
55 });
56
57 it('should define api functions', function () {
58 expect(fs.areFunctions(ns, [
Bri Prebilic Cole8f07f0d2015-04-23 13:28:43 -070059 'showNav', 'hideNav', 'toggleNav', 'hideIfShown', 'navTo'
Simon Hunt9d286562015-03-09 13:53:50 -070060 ])).toBeTruthy();
61 });
62
63 function checkHidden(b) {
64 var what = b ? 'hidden' : 'visible';
65 expect(d3.select('#nav').style('visibility')).toEqual(what);
66 }
67
68 it('should start hidden', function () {
69 checkHidden(true);
70 });
71
72 it('should be shown then hidden', function () {
73 ns.showNav();
74 checkHidden(false);
75 ns.hideNav();
76 checkHidden(true);
77 });
78
79 it('should toggle hidden', function () {
80 ns.toggleNav();
81 checkHidden(false);
82 ns.toggleNav();
83 checkHidden(true);
84 });
85
86 it('should show idempotently', function () {
87 checkHidden(true);
88 ns.showNav();
89 checkHidden(false);
90 ns.showNav();
91 checkHidden(false);
92 });
93
94 it('should hide idempotently', function () {
95 checkHidden(true);
96 ns.hideNav();
97 checkHidden(true);
98 });
99
100 it('should be a noop if already hidden', function () {
101 checkHidden(true);
102 expect(ns.hideIfShown()).toBe(false);
103 checkHidden(true);
104 });
105
106 it('should hide if shown', function () {
107 ns.showNav();
108 checkHidden(false);
109 expect(ns.hideIfShown()).toBe(true);
110 checkHidden(true);
111 });
112
Bri Prebilic Cole8f07f0d2015-04-23 13:28:43 -0700113 it('should take correct navTo parameters', function () {
114 spyOn($log, 'warn');
115
116 ns.navTo('foo');
117 expect($log.warn).not.toHaveBeenCalled();
118
119 ns.navTo('bar', { q1: 'thing', q2: 'thing2' });
120 expect($log.warn).not.toHaveBeenCalled();
121
122 });
123
124 it('should check navTo parameter warnings', function () {
125 spyOn($log, 'warn');
126
127 expect(ns.navTo()).toBeNull();
128 expect($log.warn).toHaveBeenCalledWith('Not a valid navigation path');
129
130 ns.navTo('baz', [1, 2, 3]);
131 expect($log.warn).toHaveBeenCalledWith(
132 'Query params not an object', [1, 2, 3]
133 );
134
135 ns.navTo('zoom', 'not a query param');
136 expect($log.warn).toHaveBeenCalledWith(
137 'Query params not an object', 'not a query param'
138 );
139 });
140
141 it('should verify where the window is navigating', function () {
142 ns.navTo('foo');
143 expect($window.location.href).toBe('http://server/#/foo');
144
145 ns.navTo('bar');
146 expect($window.location.href).toBe('http://server/#/bar');
147
148 ns.navTo('baz', { q1: 'thing1', q2: 'thing2' });
149 expect($window.location.href).toBe(
150 'http://server/#/baz?q1=thing1&q2=thing2'
151 );
152
153 ns.navTo('zip', { q3: 'thing3' });
154 expect($window.location.href).toBe(
155 'http://server/#/zip?q3=thing3'
156 );
157
158 ns.navTo('zoom', {});
159 expect($window.location.href).toBe('http://server/#/zoom');
160
161 ns.navTo('roof', [1, 2, 3]);
162 expect($window.location.href).toBe('http://server/#/roof');
163 });
164
Simon Hunt9d286562015-03-09 13:53:50 -0700165});