aboutsummaryrefslogtreecommitdiffstats
path: root/sdnr/wt/devicemanager/provider/src/main/resources/elasticsearch/plugins/head/src/app/ui/refreshButton/refreshButtonSpec.js
blob: 0adbb51101969475aa68cfeba3bc369e313a31d2 (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
/**
 * Copyright 2010-2013 Ben Birch
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this software 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.
 */
describe("app.ui.RefreshButton", function() {

	var RefreshButton = window.app.ui.RefreshButton;

	var r, refresh_handler, change_handler;

	function openMenuPanel( button, label ) {
		button.el.find("BUTTON").eq(1).click();
		$(".uiMenuPanel-label:contains(" + label + ")").click();
		test.clock.tick(); // menuPanel -> bind _close_handler
	}


	beforeEach( function() {
		test.clock.steal();
		refresh_handler = jasmine.createSpy("refresh_handler");
		change_handler = jasmine.createSpy("change_handler");
		r = new RefreshButton({
			onRefresh: refresh_handler,
			onChange: change_handler
		});
		r.attach( document.body );
	});

	afterEach( function() {
		r.remove();
		test.clock.restore();
	});

	it("should have an initial default value", function() {
		expect( r.value ).toBe( -1 );
	});

	it("should fire a refresh event after clicking the refresh button ", function() {
		r.el.find("BUTTON").eq(0).click();

		expect( refresh_handler ).toHaveBeenCalled();
	});

	it("should change the refresh rate when set it called", function() {
		r.set( 100 );
		expect( r.value ).toBe( 100 );
	});

	it("should set an interval when rate is set to a positive value", function() {
		r.set( 100 );
		test.clock.tick();
		expect( refresh_handler.calls.count() ).toBe( 1 );
	});

	it("should not set an interval when rate is set to a non positive value", function() {
		r.set( -1 );
		test.clock.tick();
		expect( refresh_handler.calls.count() ).toBe( 0 );
	});

	it("should fire a refresh event on intervals if refresh menu item is set to quickly", function() {
		openMenuPanel( r, "quickly" );

		expect( refresh_handler.calls.count() ).toBe( 0 );
		test.clock.tick();
		expect( refresh_handler.calls.count() ).toBe( 1 );
		test.clock.tick();
		expect( refresh_handler.calls.count() ).toBe( 2 );
	});

	it("should not fire refresh events when user selects Manual", function() {

		openMenuPanel( r, "quickly" );

		expect( refresh_handler.calls.count() ).toBe( 0 );
		test.clock.tick();
		expect( refresh_handler.calls.count() ).toBe( 1 );

		openMenuPanel( r, "Manual" );

		test.clock.tick();
		expect( refresh_handler.calls.count() ).toBe( 1 );
		test.clock.tick();
		expect( refresh_handler.calls.count() ).toBe( 1 );
	});

	it("should fire a change event when a new refresh rate is selected", function() {
		openMenuPanel( r, "quickly" );
		expect( change_handler.calls.count() ).toBe( 1 );
		expect( r.value ).toBe( 100 );
		openMenuPanel( r, "Manual" );
		expect( change_handler.calls.count() ).toBe( 2 );
		expect( r.value ).toBe( -1 );
	});

});