1"use strict";
2/* Unit tests for Toaster's JS */
3
4/* libtoaster tests */
5QUnit.test("Layer alert notification", function(assert) {
6  var layer = {
7    "layerdetailurl":"/toastergui/project/1/layer/22",
8    "xhrLayerUrl":"/toastergui/xhr_layer/1/9",
9    "vcs_url":"git://example.com/example.git",
10    "detail":"[ git://example.com/example.git | master ]",
11    "vcs_reference":"master",
12    "id": 22,
13    "name":"meta-example"
14  };
15
16  var layerDepsList = [
17    {
18    "layerdetailurl":"/toastergui/project/1/layer/9",
19    "xhrLayerUrl":"/toastergui/xhr_layer/1/9",
20    "vcs_url":"git://example.com/example.git",
21    "detail":"[ git://example.com/example.git | master ]",
22    "vcs_reference":"master",
23    "id": 9,
24    "name":"meta-example-two"
25    },
26    {
27    "layerdetailurl":"/toastergui/project/1/layer/9",
28    "xhrLayerUrl":"/toastergui/xhr_layer/1/9",
29    "vcs_url":"git://example.com/example.git",
30    "detail":"[ git://example.com/example.git | master ]",
31    "vcs_reference":"master",
32    "id": 10,
33    "name":"meta-example-three"
34    },
35  ];
36
37  var msg = libtoaster.makeLayerAddRmAlertMsg(layer, layerDepsList, true);
38  var test = $("<div></div>");
39
40  test.html(msg);
41
42  assert.equal(test.children("strong").text(), "3");
43  assert.equal(test.children("a").length, 3);
44});
45
46QUnit.test("Project info", function(assert){
47  var done = assert.async();
48  libtoaster.getProjectInfo(libtoaster.ctx.xhrProjectUrl, function(prjInfo){
49    assert.ok(prjInfo.machine.name);
50    assert.ok(prjInfo.layers.length > 0);
51    assert.ok(prjInfo.freqtargets);
52    assert.ok(prjInfo.release);
53    done();
54  });
55});
56
57QUnit.test("Show notification", function(assert){
58  var msg = "Testing";
59  var element = $("#change-notification-msg");
60
61  libtoaster.showChangeNotification(msg);
62
63  assert.equal(element.text(), msg);
64  assert.ok(element.is(":visible"));
65
66  $("#change-notification").hide();
67});
68
69var layer = {
70  "id": 1,
71  "name":  "meta-testing",
72  "layerdetailurl": "/toastergui/project/1/layer/1",
73  "xhrLayerUrl": "/toastergui/xhr_layer/1/1"
74};
75
76QUnit.test("Add layer", function(assert){
77  var done = assert.async();
78
79  /* Wait for the modal to be added to the dom */
80  var checkModal = setInterval(function(){
81    if ($("#dependencies-modal").length > 0) {
82      $("#dependencies-modal .btn-primary").click();
83      clearInterval(checkModal);
84    }
85  }, 200);
86
87  /* Compare the number of layers before and after the add in the project */
88  libtoaster.getProjectInfo(libtoaster.ctx.xhrProjectUrl, function(prjInfo){
89    var origNumLayers = prjInfo.layers.length;
90
91    libtoaster.addRmLayer(layer, true, function(deps){
92      libtoaster.getProjectInfo(libtoaster.ctx.xhrProjectUrl,
93        function(prjInfo){
94        assert.ok(prjInfo.layers.length > origNumLayers,
95          "Layer not added to project");
96        done();
97      });
98    });
99  });
100});
101
102QUnit.test("Rm layer", function(assert){
103  var done = assert.async();
104
105  libtoaster.addRmLayer(layer, false, function(deps){
106    assert.equal(deps.length, 0);
107    done();
108  });
109
110});
111
112QUnit.test("Parse url params", function(assert){
113  var params = libtoaster.parseUrlParams();
114  assert.ok(params);
115});
116
117QUnit.test("Dump url params", function(assert){
118  var params = libtoaster.dumpsUrlParams();
119  assert.ok(params);
120});
121
122QUnit.test("Make typeaheads", function(assert){
123  var layersT = $("#layers");
124  var machinesT = $("#machines");
125  var projectsT = $("#projects");
126  var recipesT = $("#recipes");
127
128  libtoaster.makeTypeahead(layersT,
129    libtoaster.ctx.layersTypeAheadUrl, {}, function(){});
130
131  libtoaster.makeTypeahead(machinesT,
132    libtoaster.ctx.machinesTypeAheadUrl, {}, function(){});
133
134  libtoaster.makeTypeahead(projectsT,
135    libtoaster.ctx.projectsTypeAheadUrl, {}, function(){});
136
137  libtoaster.makeTypeahead(recipesT,
138    libtoaster.ctx.recipesTypeAheadUrl, {}, function(){});
139
140  assert.ok(recipesT.data('ttTypeahead'));
141  assert.ok(layersT.data('ttTypeahead'));
142  assert.ok(projectsT.data('ttTypeahead'));
143  assert.ok(recipesT.data('ttTypeahead'));
144});
145
146
147
148/* Page init functions */
149
150QUnit.test("Import layer page init", function(assert){
151  assert.throws(importLayerPageInit({ xhrGitRevTypeAheadUrl: "url" }));
152});
153
154QUnit.test("Project page init", function(assert){
155  assert.throws(projectPageInit());
156});
157
158QUnit.test("Layer details page init", function(assert){
159  assert.throws(layerDetailsPageInit());
160});
161
162QUnit.test("Layer btns init", function(assert){
163  assert.throws(layerBtnsInit());
164});
165
166QUnit.test("Table init", function(assert){
167  assert.throws(tableInit({ url : ctx.tableUrl }));
168});
169
170$(document).ajaxError(function(event, jqxhr, settings, errMsg){
171  if (errMsg === 'abort')
172    return;
173
174  QUnit.test("Ajax error", function(assert){
175    assert.notOk(jqxhr.responseText);
176  });
177});
178