aboutsummaryrefslogtreecommitdiffstats
path: root/framework/src/onos/web/gui/src/main/webapp/tests/app/fw/layer/quickhelp-spec.js
blob: 0f473b427800730a03c929c598ed7897c885b194 (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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/*
 * Copyright 2015 Open Networking Laboratory
 *
 * 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.
 */

/*
 ONOS GUI -- Layer -- Flash Service - Unit Tests
 */
describe('factory: fw/layer/quickhelp.js', function () {
    var $log, fs, qhs, d3Elem,
        fade = 500,
        noop = function () {},
        mockBindings = {
            globalKeys: {
                slash: [noop, 'Show / hide Quick Help'],
                T: [noop, 'Toggle Theme']
            },
            globalFormat: ['slash', 'T'],
            viewKeys: {
                H: [noop, 'Show / hide hosts'],
                I: [noop, 'Toggle instances panel']
            },
            viewGestures: []
        };

    // list of needed bindings to use in aggregateData
    var neededBindings = [
        'globalKeys', 'globalFormat', 'viewKeys', 'viewGestures'
    ];

    beforeEach(module('onosUtil', 'onosSvg', 'onosLayer'));

    beforeEach(inject(function (_$log_, FnService, QuickHelpService) {
        $log = _$log_;
        fs = FnService;
        qhs = QuickHelpService;

        jasmine.clock().install();
        d3Elem = d3.select('body').append('div').attr('id', 'quickhelp');
        qhs.initQuickHelp();
    }));

    afterEach(function () {
        jasmine.clock().uninstall();
        d3.select('#quickhelp').remove();
    });

    function helpItemSelection() {
        return d3Elem.selectAll('.help');
    }

    it('should define QuickHelpService', function () {
        expect(qhs).toBeDefined();
    });

    it('should define api functions', function () {
        expect(fs.areFunctions(qhs, [
            'initQuickHelp', 'showQuickHelp', 'hideQuickHelp'
        ])).toBeTruthy();
    });

    it('should have no items to start', function () {
        expect(helpItemSelection().size()).toBe(0);
    });

    // === showQuickHelp

    it('should warn if bad bindings are provided', function () {
        var warning =
            'Quickhelp Service: showQuickHelp(), invalid bindings object';
        spyOn($log, 'warn');

        expect(qhs.showQuickHelp()).toBeNull();
        expect($log.warn).toHaveBeenCalledWith(warning);

        expect(qhs.showQuickHelp({})).toBeNull();
        expect($log.warn).toHaveBeenCalledWith(warning);

        expect(qhs.showQuickHelp([1, 2, 3])).toBeNull();
        expect($log.warn).toHaveBeenCalledWith(warning);
    });

    it('should warn if not all needed bindings are provided', function () {
        var warning =
            'Quickhelp Service: showQuickHelp(),' +
            ' needed bindings for help panel not provided:';
        spyOn($log, 'warn');

        expect(qhs.showQuickHelp({
            foo: 'foo', bar: 'bar'
        })).toBeNull();
        expect($log.warn).toHaveBeenCalledWith(warning, neededBindings);

        expect(qhs.showQuickHelp({
            globalKeys: {}
        })).toBeNull();
        expect($log.warn).toHaveBeenCalledWith(warning, neededBindings);

        expect(qhs.showQuickHelp({
            globalKeys: {},
            globalFormat: {},
            viewKeys: {}
        })).toBeNull();
        expect($log.warn).toHaveBeenCalledWith(warning, neededBindings);
    });

    it('should not warn if bindings are provided', function () {
        spyOn($log, 'warn');
        expect(qhs.showQuickHelp(mockBindings)).toBe(undefined);
        expect($log.warn).not.toHaveBeenCalled();
    });

    it('should append an svg', function () {
        var svg = d3Elem.select('svg');
        expect(d3Elem.empty()).toBe(false);
        expect(svg.empty()).toBe(true);

        qhs.showQuickHelp(mockBindings);

        svg = d3Elem.select('svg');
        expect(svg.empty()).toBe(false);
        expect(svg.attr('width')).toBe('100%');
        expect(svg.attr('height')).toBe('80%');
        expect(svg.attr('viewBox')).toBe('-200 0 400 400');
    });

    it('should create the quick help panel', function () {
        var helpItems, g, rect, text, rows;
        qhs.showQuickHelp(mockBindings);

        helpItems = helpItemSelection();
        expect(helpItems.size()).toBe(1);

        g = d3.select('g.help');
        expect(g.attr('opacity')).toBe('0');

        rect = g.select('rect');
        expect(rect.attr('rx')).toBe('8');

        text = g.select('text');
        expect(text.text()).toBe('Quick Help');
        expect(text.classed('title')).toBe(true);
        expect(text.attr('dy')).toBe('1.2em');
        expect(text.attr('transform')).toBeTruthy();

        rows = g.select('g');
        expect(rows.empty()).toBe(false);

        jasmine.clock().tick(fade + 1);
        setTimeout(function () {
            expect(g.attr('opacity')).toBe('1');
        }, fade);

        // TODO: test aggregate data helper function
    });

    it('should show panel with custom fade time', function () {
        var g,
            ctmFade = 200;
        qhs.initQuickHelp({ fade: ctmFade });
        qhs.showQuickHelp(mockBindings);

        g = d3.select('g.help');
        expect(g.attr('opacity')).toBe('0');

        jasmine.clock().tick(ctmFade + 1);
        setTimeout(function () {
            expect(g.attr('opacity')).toBe('1');
        }, ctmFade);
    });

    // === hideQuickHelp

    it('should hide quick help if svg exists', function () {
        var svg;

        expect(qhs.hideQuickHelp()).toBe(false);

        svg = d3.select('#quickhelp')
            .append('svg');
        svg.append('g')
            .classed('help', true)
            .attr('opacity', 1);

        expect(qhs.hideQuickHelp()).toBe(true);

        jasmine.clock().tick(fade + 1);
        setTimeout(function () {
            expect(svg.select('g.help').attr('opacity')).toBe('0');
        }, fade);

        jasmine.clock().tick(20);
        setTimeout(function () {
            expect(svg.empty()).toBe(true);
        }, fade + 20);
    });

    it('should not hide quick help if svg does not exist', function () {
        expect(qhs.hideQuickHelp()).toBe(false);
    });

});