Skip to content

Commit feca287

Browse files
committed
tests(playlist): adds ctrl test for playlist edit
1 parent b79f044 commit feca287

File tree

2 files changed

+102
-13
lines changed

2 files changed

+102
-13
lines changed

public/app/features/playlist/playlist_edit_ctrl.js

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,27 +9,30 @@ function (angular, config, _) {
99
var module = angular.module('grafana.controllers');
1010

1111
module.controller('PlaylistEditCtrl', function($scope, playlistSrv, backendSrv, $location, $route) {
12-
$scope.timespan = config.playlist_timespan;
1312
$scope.filteredPlaylistItems = [];
1413
$scope.foundPlaylistItems = [];
1514
$scope.searchQuery = '';
1615
$scope.loading = false;
1716
$scope.playlist = {};
1817
$scope.playlistItems = [];
1918

20-
if ($route.current.params.id) {
21-
var playlistId = $route.current.params.id;
19+
$scope.init = function() {
20+
if ($route.current.params.id) {
21+
var playlistId = $route.current.params.id;
2222

23-
backendSrv.get('/api/playlists/' + playlistId)
24-
.then(function(result) {
25-
$scope.playlist = result;
26-
});
23+
backendSrv.get('/api/playlists/' + playlistId)
24+
.then(function(result) {
25+
$scope.playlist = result;
26+
});
2727

28-
backendSrv.get('/api/playlists/' + playlistId + '/playlistitems')
29-
.then(function(result) {
30-
$scope.playlistItems = result;
31-
});
32-
}
28+
backendSrv.get('/api/playlists/' + playlistId + '/playlistitems')
29+
.then(function(result) {
30+
$scope.playlistItems = result;
31+
});
32+
}
33+
34+
$scope.search();
35+
};
3336

3437
$scope.search = function() {
3538
var query = {starred: true, limit: 10};
@@ -136,6 +139,6 @@ function (angular, config, _) {
136139
$scope.moveDashboard(playlistItem, 1);
137140
};
138141

139-
$scope.search();
142+
$scope.init();
140143
});
141144
});
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
import '../playlist_edit_ctrl';
2+
import {describe, beforeEach, it, sinon, expect, angularMocks} from 'test/lib/common';
3+
import helpers from 'test/specs/helpers';
4+
5+
describe('PlaylistEditCtrl', function() {
6+
var ctx = new helpers.ControllerTestContext();
7+
8+
var dashboards = [
9+
{
10+
id: 2,
11+
title: 'dashboard: 2'
12+
},
13+
{
14+
id: 3,
15+
title: 'dashboard: 3'
16+
}
17+
];
18+
19+
var playlistSrv = {};
20+
var backendSrv = {
21+
search: (query) => {
22+
return ctx.$q.when(dashboards);
23+
}
24+
};
25+
26+
beforeEach(angularMocks.module('grafana.core'));
27+
beforeEach(angularMocks.module('grafana.controllers'));
28+
beforeEach(angularMocks.module('grafana.services'));
29+
beforeEach(ctx.providePhase({
30+
playlistSrv: playlistSrv,
31+
backendSrv: backendSrv,
32+
$route: { current: { params: { } } },
33+
}));
34+
35+
beforeEach(ctx.createControllerPhase('PlaylistEditCtrl'));
36+
37+
beforeEach(() => {
38+
ctx.scope.$digest();
39+
});
40+
41+
describe.only('searchresult returns 2 dashboards', function() {
42+
it('found dashboard should be 2', function() {
43+
expect(ctx.scope.foundPlaylistItems.length).to.be(2);
44+
});
45+
46+
it('filtred dashboard should be 2', function() {
47+
expect(ctx.scope.filteredPlaylistItems.length).to.be(2);
48+
});
49+
50+
describe('adds one dashboard to playlist', () => {
51+
beforeEach(() => {
52+
ctx.scope.addPlaylistItem({ id: 2, title: 'dashboard: 2' });
53+
});
54+
55+
it('playlistitems should be increased by one', () => {
56+
expect(ctx.scope.playlistItems.length).to.be(1);
57+
});
58+
59+
it('filtred playlistitems should be reduced by one', () => {
60+
expect(ctx.scope.filteredPlaylistItems.length).to.be(1);
61+
});
62+
63+
it('found dashboard should be 2', function() {
64+
expect(ctx.scope.foundPlaylistItems.length).to.be(2);
65+
});
66+
67+
describe('removes one dashboard from playlist', () => {
68+
beforeEach(() => {
69+
ctx.scope.removePlaylistItem(ctx.scope.playlistItems[0]);
70+
});
71+
72+
it('playlistitems should be increased by one', () => {
73+
expect(ctx.scope.playlistItems.length).to.be(0);
74+
});
75+
76+
it('found dashboard should be 2', function() {
77+
expect(ctx.scope.foundPlaylistItems.length).to.be(2);
78+
});
79+
80+
it('filtred playlist should be reduced by one', () => {
81+
expect(ctx.scope.filteredPlaylistItems.length).to.be(2);
82+
});
83+
});
84+
});
85+
});
86+
});

0 commit comments

Comments
 (0)