jquery.treetable.test.js 59 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570
  1. (function() {
  2. var expect;
  3. expect = chai.expect;
  4. function collectValues(nodes) {
  5. return $.map(nodes, function(node, i) {
  6. return node.row.find("td").first().text();
  7. });
  8. };
  9. function collectValuesInTable(table) {
  10. var result = table.find("tbody tr td:first-child").map(function() {
  11. return $(this).text();
  12. });
  13. return $.makeArray(result);
  14. };
  15. describe("treetable()", function() {
  16. beforeEach(function() {
  17. this.subject = $("<table><tr data-tt-id='0'><td>N0</td></tr><tr data-tt-id='1' data-tt-parent-id='0'><td>N1</td></tr><tr data-tt-id='2' data-tt-parent-id='0' data-tt-branch='true'><td>N2</td></tr></table>");
  18. });
  19. it("maintains chainability", function() {
  20. expect(this.subject.treetable()).to.equal(this.subject);
  21. });
  22. it("adds treetable object to element", function() {
  23. expect(this.subject.data("treetable")).to.be.undefined;
  24. this.subject.treetable();
  25. expect(this.subject.data("treetable")).to.be.defined;
  26. });
  27. it("adds .treetable css class to element", function() {
  28. expect(this.subject.hasClass("treetable")).to.be.false;
  29. this.subject.treetable();
  30. expect(this.subject.hasClass("treetable")).to.be.true;
  31. });
  32. it("does not initialize twice", function() {
  33. var data;
  34. this.subject.treetable();
  35. data = this.subject.data("treetable");
  36. this.subject.treetable();
  37. expect(this.subject.data("treetable")).to.equal(data);
  38. });
  39. it("initializes twice when explicitly requested", function() {
  40. var newData, oldData;
  41. this.subject.treetable();
  42. oldData = this.subject.data("treetable");
  43. this.subject.treetable({}, true);
  44. newData = this.subject.data("treetable");
  45. expect(newData).not.to.equal(oldData);
  46. expect(this.subject.data("treetable")).to.equal(newData);
  47. });
  48. describe("destroy()", function() {
  49. it("removes treetable object from element", function() {
  50. this.subject.treetable();
  51. expect(this.subject.data("treetable")).to.be.defined;
  52. this.subject.treetable("destroy");
  53. expect(this.subject.data("treetable")).to.be.undefined;
  54. });
  55. it("removes .treetable css class from element", function() {
  56. this.subject.treetable();
  57. expect(this.subject.hasClass("treetable")).to.be.true;
  58. this.subject.treetable("destroy");
  59. expect(this.subject.hasClass("treetable")).to.be.false;
  60. });
  61. });
  62. describe("with expandable: false", function() {
  63. beforeEach(function() {
  64. this.subject.treetable({
  65. expandable: false
  66. }).appendTo("body");
  67. });
  68. afterEach(function() {
  69. this.subject.remove();
  70. });
  71. it("all nodes are visible", function() {
  72. var row, _i, _len, _ref, _results;
  73. _ref = this.subject[0].rows;
  74. _results = [];
  75. for (_i = 0, _len = _ref.length; _i < _len; _i++) {
  76. row = _ref[_i];
  77. _results.push(expect($(row)).to.be.visible);
  78. }
  79. return _results;
  80. });
  81. });
  82. describe("with expandable: true and clickableNodeNames: false", function() {
  83. beforeEach(function() {
  84. this.subject.treetable({
  85. expandable: true,
  86. initialState: "expanded"
  87. }).appendTo("body");
  88. });
  89. afterEach(function() {
  90. this.subject.remove();
  91. });
  92. it("collapses branch when node toggler clicked", function() {
  93. expect(this.subject.treetable("node", 1).row).to.be.visible;
  94. this.subject.treetable("node", 0).row.find(".indenter a").click();
  95. expect(this.subject.treetable("node", 1).row).to.not.be.visible;
  96. });
  97. it("does not collapse branch when cell clicked", function() {
  98. expect(this.subject.treetable("node", 1).row).to.be.visible;
  99. this.subject.treetable("node", 0).row.find("td").first().click();
  100. expect(this.subject.treetable("node", 1).row).to.be.visible;
  101. });
  102. describe("for nodes with children", function() {
  103. it("renders a clickable node toggler", function() {
  104. expect(this.subject.treetable("node", 0).row).to.have("a");
  105. });
  106. });
  107. describe("for nodes without children", function() {
  108. it("does not render a clickable node toggler", function() {
  109. expect(this.subject.treetable("node", 1).row).to.not.have("a");
  110. });
  111. });
  112. describe("for nodes without children but with branch node data attribute", function() {
  113. it("renders a clickable node toggler", function() {
  114. expect(this.subject.treetable("node", 2).row).to.have("a");
  115. });
  116. });
  117. });
  118. describe("with expandable: true and clickableNodeNames: true", function() {
  119. beforeEach(function() {
  120. this.subject.treetable({
  121. expandable: true,
  122. clickableNodeNames: true
  123. }).appendTo("body");
  124. });
  125. afterEach(function() {
  126. this.subject.remove();
  127. });
  128. it("expands branch when node toggler clicked", function() {
  129. expect(this.subject.treetable("node", 1).row).to.not.be.visible;
  130. this.subject.treetable("node", 0).row.find(".indenter a").click();
  131. expect(this.subject.treetable("node", 1).row).to.be.visible;
  132. });
  133. it("expands branch when cell clicked", function() {
  134. expect(this.subject.treetable("node", 1).row).to.not.be.visible;
  135. this.subject.treetable("node", 0).row.find("td").first().click();
  136. expect(this.subject.treetable("node", 1).row).to.be.visible;
  137. });
  138. });
  139. describe("collapseAll()", function() {
  140. beforeEach(function() {
  141. this.subject.treetable({
  142. initialState: "expanded"
  143. });
  144. });
  145. it("collapses all nodes", function() {
  146. var row, _i, _len, _ref, _results;
  147. this.subject.treetable("collapseAll");
  148. _ref = this.subject[0].rows;
  149. _results = [];
  150. for (_i = 0, _len = _ref.length; _i < _len; _i++) {
  151. row = _ref[_i];
  152. _results.push(expect($(row).hasClass("collapsed")).to.be.true);
  153. }
  154. return _results;
  155. });
  156. it("maintains chainability", function() {
  157. expect(this.subject.treetable("collapseAll")).to.equal(this.subject);
  158. });
  159. });
  160. describe("collapseNode()", function() {
  161. beforeEach(function() {
  162. this.subject.treetable({
  163. initialState: "expanded"
  164. });
  165. });
  166. it("collapses a root node", function() {
  167. var row = $(this.subject[0].rows[0]);
  168. this.subject.treetable("collapseNode", row.data("ttId"));
  169. expect(row.hasClass("collapsed")).to.be.true;
  170. });
  171. it("collapses a branch node", function() {
  172. var row = $(this.subject[0].rows[1]);
  173. this.subject.treetable("collapseNode", row.data("ttId"));
  174. expect(row.hasClass("collapsed")).to.be.true;
  175. });
  176. it("throws an error for unknown nodes", function() {
  177. var fn, subject;
  178. subject = this.subject;
  179. fn = function() {
  180. subject.treetable("collapseNode", "whatever");
  181. };
  182. expect(fn).to["throw"](Error, "Unknown node 'whatever'");
  183. });
  184. it("maintains chainability", function() {
  185. var row = $(this.subject[0].rows[0]);
  186. expect(this.subject.treetable("collapseNode", row.data("ttId"))).to.equal(this.subject);
  187. });
  188. });
  189. describe("expandAll()", function() {
  190. beforeEach(function() {
  191. this.subject.treetable({
  192. initialState: "collapsed"
  193. });
  194. });
  195. it("expands all nodes", function() {
  196. var row, _i, _len, _ref, _results;
  197. this.subject.treetable("expandAll");
  198. _ref = this.subject[0].rows;
  199. _results = [];
  200. for (_i = 0, _len = _ref.length; _i < _len; _i++) {
  201. row = _ref[_i];
  202. _results.push(expect($(row).hasClass("expanded")).to.be.true);
  203. }
  204. return _results;
  205. });
  206. it("maintains chainability", function() {
  207. expect(this.subject.treetable("expandAll")).to.equal(this.subject);
  208. });
  209. });
  210. describe("expandNode()", function() {
  211. beforeEach(function() {
  212. this.subject.treetable({
  213. expandable: true,
  214. initialState: "collapsed"
  215. });
  216. });
  217. it("expands a root node", function() {
  218. var row = $(this.subject[0].rows[0]);
  219. this.subject.treetable("expandNode", row.data("ttId"));
  220. expect(row.hasClass("expanded")).to.be.true;
  221. });
  222. it("expands a branch node", function() {
  223. var row = $(this.subject[0].rows[1]);
  224. this.subject.treetable("expandNode", row.data("ttId"));
  225. expect(row.hasClass("expanded")).to.be.true;
  226. });
  227. it("throws an error for unknown nodes", function() {
  228. var fn, subject;
  229. subject = this.subject;
  230. fn = function() {
  231. subject.treetable("expandNode", "whatever");
  232. };
  233. expect(fn).to["throw"](Error, "Unknown node 'whatever'");
  234. });
  235. it("maintains chainability", function() {
  236. var row = $(this.subject[0].rows[0]);
  237. expect(this.subject.treetable("expandNode", row.data("ttId"))).to.equal(this.subject);
  238. });
  239. it("initializes nodes", function() {
  240. var row = $(this.subject[0].rows[2]);
  241. this.subject.treetable("expandNode", row.data("ttId"));
  242. expect(this.subject.treetable("node", row.data("ttId")).initialized).to.be.true;
  243. });
  244. });
  245. describe("loadBranch()", function() {
  246. beforeEach(function() {
  247. this.newRows = "<tr data-tt-id='3' data-tt-parent-id='2'><td>N3</td></tr><tr data-tt-id='4' data-tt-parent-id='2'><td>N4</td></tr>"
  248. this.moreRows = "<tr data-tt-id='5' data-tt-parent-id='2'><td>N5</td></tr>";
  249. this.subject.treetable({ expandable: true });
  250. this.parentNode = this.subject.treetable("node", 2);
  251. });
  252. it("shows expander for parent if neccessary (#79)", function() {
  253. var childRow,
  254. rootNode,
  255. rootRow;
  256. // This row won't show an expander
  257. rootRow = "<tr data-tt-id='3'><td>N3</td></tr>";
  258. // But when this row is added as a child it should add expander to N3
  259. childRow = "<tr data-tt-id='4' data-tt-parent-id='3'><td>N4</td></tr>";
  260. // First add nonBranchRow as a new root node
  261. this.subject.treetable("loadBranch", null, rootRow);
  262. rootNode = this.subject.treetable("node", "3");
  263. expect(rootNode.indenter).to.be.empty;
  264. this.subject.treetable("loadBranch", rootNode, childRow);
  265. expect(rootNode.indenter).not.to.be.empty;
  266. });
  267. it("shows parent expanded when child nodes loaded (#79)", function() {
  268. var childRow,
  269. rootNode,
  270. rootRow;
  271. // This row won't show an expander
  272. rootRow = "<tr data-tt-id='3'><td>N3</td></tr>";
  273. // But when this row is added as a child it should add expander to N3
  274. childRow = "<tr data-tt-id='4' data-tt-parent-id='3'><td>N4</td></tr>";
  275. // First add nonBranchRow as a new root node
  276. this.subject.treetable("loadBranch", null, rootRow);
  277. rootNode = this.subject.treetable("node", "3");
  278. this.subject.treetable("loadBranch", rootNode, childRow);
  279. expect(rootNode.row).to.have.class("expanded");
  280. });
  281. it("inserts rows into DOM, appending new rows to end of children", function() {
  282. expect(this.subject[0].rows.length).to.equal(3);
  283. this.subject.treetable("loadBranch", this.parentNode, this.newRows);
  284. expect(this.subject[0].rows.length).to.equal(5);
  285. this.subject.treetable("loadBranch", this.parentNode, this.moreRows);
  286. expect(this.subject[0].rows.length).to.equal(6);
  287. // Verify order
  288. var order = _.map(this.subject[0].rows, function(row) { return $(row).data("ttId"); });
  289. expect(order).to.deep.equal([0,1,2,3,4,5]);
  290. });
  291. it("inserts rows after any descendants (#73)", function() {
  292. var childRows = "<tr data-tt-id='6' data-tt-parent-id='4'><td>N6</td></tr>";
  293. this.subject.treetable("loadBranch", this.parentNode, this.newRows);
  294. this.subject.treetable("loadBranch", this.parentNode, childRows);
  295. this.subject.treetable("loadBranch", this.parentNode, this.moreRows);
  296. // Verify order
  297. var order = _.map(this.subject[0].rows, function(row) { return $(row).data("ttId"); });
  298. expect(order).to.deep.equal([0,1,2,3,4,6,5]);
  299. });
  300. it("does not choke when fed a collection object with rows instead of a string", function() {
  301. expect(this.subject.data("treetable").tree[3]).to.be.undefined;
  302. this.subject.treetable("loadBranch", this.parentNode, $.parseHTML(this.newRows));
  303. expect(this.subject.data("treetable").tree[3]).to.be.defined;
  304. });
  305. it("does not choke on leading whitespace", function() {
  306. expect(this.subject.data("treetable").tree[3]).to.be.undefined;
  307. this.subject.treetable("loadBranch", this.parentNode, " <tr data-tt-id='3' data-tt-parent-id='2'><td>N3</td></tr>");
  308. expect(this.subject.data("treetable").tree[3]).to.be.defined;
  309. });
  310. it("does not choke on whitespace between rows", function() {
  311. expect(this.subject.data("treetable").tree[3]).to.be.undefined;
  312. this.subject.treetable("loadBranch", this.parentNode, "<tr data-tt-id='3' data-tt-parent-id='2'><td>N3</td></tr> <tr data-tt-id='4' data-tt-parent-id='2'><td>N4</td></tr>");
  313. expect(this.subject.data("treetable").tree[3]).to.be.defined;
  314. });
  315. it("does not choke on non-row elements", function() {
  316. expect(this.subject.data("treetable").tree[3]).to.be.undefined;
  317. this.subject.treetable("loadBranch", this.parentNode, "<b>Wish you were here</b><tr data-tt-id='3' data-tt-parent-id='2'><td>N3</td></tr>");
  318. expect(this.subject.data("treetable").tree[3]).to.be.defined;
  319. });
  320. it("inserts rows into tree", function() {
  321. expect(this.subject.data("treetable").tree[3]).to.be.undefined;
  322. expect(this.subject.data("treetable").tree[4]).to.be.undefined;
  323. this.subject.treetable("loadBranch", this.parentNode, this.newRows);
  324. expect(this.subject.data("treetable").tree[3]).to.be.defined;
  325. expect(this.subject.data("treetable").tree[4]).to.be.defined;
  326. });
  327. it("registers nodes", function() {
  328. expect(this.subject.data("treetable").nodes.length).to.equal(3);
  329. this.subject.treetable("loadBranch", this.parentNode, this.newRows);
  330. expect(this.subject.data("treetable").nodes.length).to.equal(5);
  331. });
  332. it("initializes nodes", function() {
  333. this.subject.treetable("loadBranch", this.parentNode, this.newRows);
  334. expect(this.subject.data("treetable").tree[3].initialized).to.be.true;
  335. expect(this.subject.data("treetable").tree[4].initialized).to.be.true;
  336. });
  337. it("maintains chainability", function() {
  338. expect(this.subject.treetable("loadBranch", this.parentNode, this.newRows)).to.equal(this.subject);
  339. });
  340. describe("adding nodes at root level", function() {
  341. beforeEach(function() {
  342. this.rootRows = "<tr data-tt-id='6'><td>N6</td></tr>";
  343. });
  344. it("registers nodes as root nodes", function () {
  345. expect(this.subject.data("treetable").roots.length).to.equal(1);
  346. this.subject.treetable("loadBranch", null, this.rootRows);
  347. expect(this.subject.data("treetable").roots.length).to.equal(2);
  348. });
  349. it("inserts rows into DOM", function () {
  350. this.subject.treetable("loadBranch", null, this.rootRows);
  351. expect($(this.subject[0].rows[3]).data("ttId")).to.equal(6);
  352. });
  353. describe("when table uses a tbody element", function() {
  354. beforeEach(function() {
  355. this.subject = $("<table><tbody><tr data-tt-id='0'><td>N0</td></tr><tr data-tt-id='1' data-tt-parent-id='0'><td>N1</td></tr><tr data-tt-id='2' data-tt-parent-id='0' data-tt-branch='true'><td>N2</td></tr></tbody></table>");
  356. this.subject.treetable();
  357. });
  358. it("appends nodes to tbody", function() {
  359. this.subject.treetable("loadBranch", null, this.rootRows);
  360. expect($(this.subject.find("tbody tr:last")).data("ttId")).to.equal(6);
  361. });
  362. });
  363. describe("when table uses tbody and tfoot elements", function() {
  364. beforeEach(function() {
  365. this.subject = $("<table><tbody><tr data-tt-id='0'><td>N0</td></tr><tr data-tt-id='1' data-tt-parent-id='0'><td>N1</td></tr><tr data-tt-id='2' data-tt-parent-id='0' data-tt-branch='true'><td>N2</td></tr></tbody><tfoot><tr><td>Footer</td></tr></table>");
  366. this.subject.treetable();
  367. });
  368. it("still appends nodes to tbody", function() {
  369. this.subject.treetable("loadBranch", null, this.rootRows);
  370. expect($(this.subject.find("tbody tr:last")).data("ttId")).to.equal(6);
  371. });
  372. });
  373. });
  374. });
  375. describe("move()", function() {
  376. beforeEach(function() {
  377. this.subject.treetable();
  378. });
  379. it("maintains chainability", function() {
  380. expect(this.subject.treetable("move", 1, 2)).to.equal(this.subject);
  381. });
  382. });
  383. describe("node()", function() {
  384. beforeEach(function() {
  385. this.subject.treetable();
  386. });
  387. it("returns node by id", function() {
  388. expect(this.subject.treetable("node", "0")).to.equal(this.subject.data("treetable").tree[0]);
  389. expect(this.subject.treetable("node", 0)).to.equal(this.subject.data("treetable").tree[0]);
  390. });
  391. it("returns undefined for unknown node", function() {
  392. expect(this.subject.treetable("node", "unknown")).to.be.undefined;
  393. });
  394. });
  395. describe("removeNode()", function() {
  396. beforeEach(function() {
  397. this.subject.treetable();
  398. });
  399. it("maintains chainability", function() {
  400. expect(this.subject.treetable("removeNode", "2")).to.equal(this.subject);
  401. });
  402. it("removes node from parent's children list", function() {
  403. var node = this.subject.treetable("node", "2");
  404. expect(node.parentNode().children).to.include(node);
  405. this.subject.treetable("removeNode", "2");
  406. expect(node.parentNode().children).to.not.include(node);
  407. });
  408. });
  409. describe("reveal()", function() {
  410. beforeEach(function() {
  411. this.subject.treetable();
  412. });
  413. it("maintains chainability", function() {
  414. expect(this.subject.treetable("reveal", "2")).to.equal(this.subject);
  415. });
  416. });
  417. describe("sortBranch()", function() {
  418. beforeEach(function() {
  419. this.subject = $("<table><tr data-tt-id='0'><td>ROOT</td><td>Col 2</td></tr><tr data-tt-id='1' data-tt-parent-id='0'><td>C1C</td><td>C2A</tr><tr data-tt-id='2' data-tt-parent-id='0' data-tt-branch='true'><td> C1a</td><td>C2C</td></tr><tr data-tt-id='2-1' data-tt-parent-id='2'><td>CHILD</td><td>Col 2</td></tr><tr data-tt-id='3' data-tt-parent-id='0'><td>C1B</td><td>C2B</td></tr></table>");
  420. this.subject.treetable();
  421. this.parentNode = this.subject.treetable("node", "0");
  422. });
  423. it("defaults to sorting a node's children alphabetically (case-insensitive)", function() {
  424. expect(collectValues(this.parentNode.children)).to.eql(["C1C", " C1a", "C1B"]);
  425. this.subject.treetable("sortBranch", this.parentNode);
  426. expect(collectValues(this.parentNode.children)).to.eql([" C1a", "C1B", "C1C"]);
  427. });
  428. it("updates UI", function() {
  429. expect(collectValuesInTable(this.subject)).to.eql(["ROOT", "C1C", " C1a", "CHILD", "C1B"]);
  430. this.subject.treetable("sortBranch", this.parentNode);
  431. expect(collectValuesInTable(this.subject)).to.eql(["ROOT", " C1a", "CHILD", "C1B", "C1C"]);
  432. });
  433. it("sorts on chosen column", function() {
  434. expect(collectValues(this.parentNode.children)).to.eql(["C1C", " C1a", "C1B"]);
  435. this.subject.treetable("sortBranch", this.parentNode, 1);
  436. expect(collectValues(this.parentNode.children)).to.eql(["C1C", "C1B", " C1a"]);
  437. });
  438. it("accepts custom sorting functions (example: numOfChildren)", function() {
  439. var sortOnNumOfChildrenFun = function(a, b) {
  440. var valA = a.children.length,
  441. valB = b.children.length;
  442. if (valA < valB) return -1;
  443. if (valA > valB) return 1;
  444. return 0;
  445. };
  446. expect(collectValues(this.parentNode.children)).to.eql(["C1C", " C1a", "C1B"]);
  447. this.subject.treetable("sortBranch", this.parentNode, sortOnNumOfChildrenFun);
  448. expect(collectValues(this.parentNode.children)).to.eql(["C1C", "C1B", " C1a"]);
  449. });
  450. it("accepts custom sorting functions (example: alphabeticallyDescending)", function() {
  451. var sortAlphabeticallyDescending = function(a, b) {
  452. var valA = $.trim(a.row.find("td:eq(0)").text()).toUpperCase(),
  453. valB = $.trim(b.row.find("td:eq(0)").text()).toUpperCase();
  454. if (valA > valB) return -1;
  455. if (valA < valB) return 1;
  456. return 0;
  457. };
  458. expect(collectValues(this.parentNode.children)).to.eql(["C1C", " C1a", "C1B"]);
  459. this.subject.treetable("sortBranch", this.parentNode, sortAlphabeticallyDescending);
  460. expect(collectValues(this.parentNode.children)).to.eql(["C1C", "C1B", " C1a"]);
  461. });
  462. });
  463. describe("unloadBranch()", function() {
  464. beforeEach(function() {
  465. this.newRows = "<tr data-tt-id='3' data-tt-parent-id='2'><td>N3</td></tr><tr data-tt-id='5' data-tt-parent-id='3'><td>N5</td></tr><tr data-tt-id='4' data-tt-parent-id='2'><td>N4</td></tr>"
  466. this.subject.treetable();
  467. this.parentNode = this.subject.treetable("node", 2);
  468. this.subject.treetable("loadBranch", this.parentNode, this.newRows);
  469. });
  470. it("removes rows from DOM", function() {
  471. expect(this.subject[0].rows.length).to.equal(6);
  472. this.subject.treetable("unloadBranch", this.parentNode);
  473. expect(this.subject[0].rows.length).to.equal(3);
  474. });
  475. it("removes rows from tree", function() {
  476. expect(this.subject.data("treetable").tree[3]).to.be.defined;
  477. expect(this.subject.data("treetable").tree[4]).to.be.defined;
  478. this.subject.treetable("unloadBranch", this.parentNode);
  479. expect(this.subject.data("treetable").tree[3]).to.be.undefined;
  480. expect(this.subject.data("treetable").tree[4]).to.be.undefined;
  481. });
  482. it("updates the branch and leaf classes", function() {
  483. this.subject.treetable("unloadBranch", this.subject.treetable("node", 0));
  484. expect($(this.subject[0].rows[0])).to.have.class('leaf');
  485. });
  486. it("updates the branch and leaf classes when has branchAttr", function() {
  487. this.subject.treetable("unloadBranch", this.parentNode);
  488. expect($(this.subject[0].rows[2])).to.have.class('branch');
  489. });
  490. it("removes nodes from node cache", function() {
  491. expect(this.subject.data("treetable").nodes.length).to.equal(6);
  492. this.subject.treetable("unloadBranch", this.parentNode);
  493. expect(this.subject.data("treetable").nodes.length).to.equal(3);
  494. });
  495. it("removes nodes from parent's list of children", function() {
  496. expect(this.parentNode.children.length).to.equal(2);
  497. this.subject.treetable("unloadBranch", this.parentNode);
  498. expect(this.parentNode.children.length).to.equal(0);
  499. });
  500. it("maintains chainability", function() {
  501. expect(this.subject.treetable("unloadBranch", this.parentNode)).to.equal(this.subject);
  502. });
  503. });
  504. });
  505. describe("TreeTable.Node", function() {
  506. describe("addChild()", function() {
  507. beforeEach(function() {
  508. this.table = $("<table><tr data-tt-id='n0'><td>N0</td></tr><tr data-tt-id='n1'><td>N1</td></tr></table>");
  509. this.table.treetable();
  510. this.parent = this.table.data("treetable").tree["n0"];
  511. this.child = this.table.data("treetable").tree["n1"];
  512. });
  513. it("adds child to collection of children", function() {
  514. expect(this.parent.children).to.be.empty;
  515. this.parent.addChild(this.child);
  516. expect(this.parent.children).to.include(this.child);
  517. });
  518. });
  519. describe("ancestors()", function() {
  520. beforeEach(function() {
  521. this.subject = $("<table id='subject'><tr data-tt-id='1'><td>N1</td></tr><tr data-tt-id='2' data-tt-parent-id='1'><td>N2</td></tr><tr data-tt-id='3' data-tt-parent-id='2'><td>N3</td></tr><tr data-tt-id='4' data-tt-parent-id='3'><td>N4</td></tr></table>").treetable().data("treetable").tree;
  522. });
  523. it("has correct size", function() {
  524. expect(_.size(this.subject[4].ancestors())).to.equal(3);
  525. });
  526. it("includes the parent node", function() {
  527. expect(this.subject[4].ancestors()).to.include(this.subject[4].parentNode());
  528. });
  529. it("includes the parent's parent node", function() {
  530. expect(this.subject[4].ancestors()).to.include(this.subject[3].parentNode());
  531. });
  532. it("includes the root node", function() {
  533. expect(this.subject[4].ancestors()).to.include(this.subject[1]);
  534. });
  535. it("does not include node itself", function() {
  536. expect(this.subject[4].ancestors()).to.not.include(this.subject[4]);
  537. });
  538. });
  539. describe("children", function() {
  540. beforeEach(function() {
  541. this.subject = $("<table id='subject'><tr data-tt-id='1'><td>N1</td></tr><tr data-tt-id='2' data-tt-parent-id='1'><td>N2</td></tr><tr data-tt-id='3' data-tt-parent-id='2'><td>N3</td><tr data-tt-id='5' data-tt-parent-id='2'><td>N5</td></tr></tr><tr data-tt-id='4' data-tt-parent-id='3'><td>N4</td></tr></table>").treetable().data("treetable").tree;
  542. });
  543. it("includes direct children", function() {
  544. expect(_.size(this.subject[2].children)).to.equal(2);
  545. expect(this.subject[2].children).to.include(this.subject[3]);
  546. expect(this.subject[2].children).to.include(this.subject[5]);
  547. });
  548. it("does not include grandchildren", function() {
  549. expect(this.subject[2].children).to.not.include(this.subject[4]);
  550. });
  551. it("does not include parent", function() {
  552. expect(this.subject[2].children).to.not.include(this.subject[2].parentNode());
  553. });
  554. it("does not include node itself", function() {
  555. expect(this.subject[2].children).to.not.include(this.subject[2]);
  556. });
  557. });
  558. describe("collapse()", function() {
  559. beforeEach(function() {
  560. this.table = $("<table id='subject'><tr data-tt-id='0'><td>N0</td></tr><tr data-tt-id='1' data-tt-parent-id='0'><td>N1</td></tr><tr data-tt-id='2' data-tt-parent-id='0'><td>N2</td></tr><tr data-tt-id='3' data-tt-parent-id='2'><td>N3</td></tr></table>").appendTo("body").treetable({
  561. initialState: "expanded"
  562. });
  563. this.subject = this.table.data("treetable").tree;
  564. });
  565. afterEach(function() {
  566. this.table.remove();
  567. });
  568. it("ignores multiple invokes", function() {
  569. var callback = sinon.spy(),
  570. node = this.subject[0];
  571. this.table.data("treetable").settings.onNodeCollapse = callback;
  572. node.collapse();
  573. node.collapse();
  574. expect(callback.calledOnce).to.be.true;
  575. });
  576. it("hides children", function() {
  577. expect(this.subject[1].row).to.be.visible;
  578. expect(this.subject[2].row).to.be.visible;
  579. this.subject[0].collapse();
  580. expect(this.subject[1].row).to.be.hidden;
  581. expect(this.subject[2].row).to.be.hidden;
  582. });
  583. it("recursively hides grandchildren", function() {
  584. expect(this.subject[3].row).to.be.visible;
  585. this.subject[0].collapse();
  586. expect(this.subject[3].row).to.be.hidden;
  587. });
  588. it("maintains chainability", function() {
  589. expect(this.subject[0].collapse()).to.equal(this.subject[0]);
  590. });
  591. });
  592. describe("collapsed()", function() {
  593. beforeEach(function() {
  594. this.subject = $("<table><tr data-tt-id='0'><td>Node</td></tr></table>").treetable().data("treetable").tree[0];
  595. });
  596. it("returns true when collapsed", function() {
  597. this.subject.collapse();
  598. expect(this.subject.collapsed()).to.be.true;
  599. });
  600. it("returns false when expanded", function() {
  601. this.subject.expand();
  602. expect(this.subject.collapsed()).to.be.false;
  603. });
  604. });
  605. describe("expand()", function() {
  606. beforeEach(function() {
  607. this.table = $("<table><tr data-tt-id='0'><td>N0</td></tr><tr data-tt-id='1' data-tt-parent-id='0'><td>N1</td></tr><tr data-tt-id='2' data-tt-parent-id='0'><td>N2</td></tr><tr data-tt-id='3' data-tt-parent-id='2'><td>N3</td></tr></table>").appendTo("body").treetable({
  608. expandable: true
  609. });
  610. this.subject = this.table.data("treetable").tree;
  611. });
  612. afterEach(function() {
  613. this.table.remove();
  614. });
  615. it("shows children", function() {
  616. expect(this.subject[1].row).to.be.hidden;
  617. expect(this.subject[2].row).to.be.hidden;
  618. this.subject[0].expand();
  619. expect(this.subject[1].row).to.be.visible;
  620. expect(this.subject[2].row).to.be.visible;
  621. });
  622. it("ignores multiple invokes", function() {
  623. var callback = sinon.spy(),
  624. node = this.subject[0];
  625. this.table.data("treetable").settings.onNodeExpand = callback;
  626. node.expand();
  627. node.expand();
  628. expect(callback.calledOnce).to.be.true;
  629. });
  630. it("does not recursively show collapsed grandchildren", function() {
  631. sinon.stub(this.subject[2], "expanded").returns(false);
  632. expect(this.subject[3].row).to.be.hidden;
  633. this.subject[0].expand();
  634. expect(this.subject[3].row).to.be.hidden;
  635. });
  636. it("recursively shows expanded grandchildren", function() {
  637. sinon.stub(this.subject[2], "expanded").returns(true);
  638. expect(this.subject[3].row).to.be.hidden;
  639. this.subject[0].expand();
  640. expect(this.subject[3].row).to.be.visible;
  641. });
  642. it("does not show children if the node is hidden", function() {
  643. expect(this.subject[3].row).to.be.hidden;
  644. this.subject[2].expand();
  645. expect(this.subject[3].row).to.be.hidden;
  646. });
  647. it("maintains chainability", function() {
  648. expect(this.subject[0].expand()).to.equal(this.subject[0]);
  649. });
  650. });
  651. describe("expanded()", function() {
  652. beforeEach(function() {
  653. this.subject = $("<table><tr data-tt-id='0'><td>Node</td></tr></table>").treetable().data("treetable").tree[0];
  654. });
  655. it("returns true when expanded", function() {
  656. this.subject.expand();
  657. expect(this.subject.expanded()).to.be.true;
  658. });
  659. it("returns false when collapsed", function() {
  660. this.subject.collapse();
  661. expect(this.subject.expanded()).to.be.false;
  662. });
  663. });
  664. describe("indenter", function() {
  665. beforeEach(function() {
  666. this.table = $("<table><tr data-tt-id='0'><td>Root Node</td></tr><tr data-tt-id='1' data-tt-parent-id='0'><td>Branch Node</td></tr><tr data-tt-id='2' data-tt-parent-id='1'><td>Leaf Node</td></tr></table>").treetable({
  667. initialState: "expanded"
  668. }).data("treetable");
  669. this.rootNode = this.table.tree[0];
  670. this.branchNode = this.table.tree[1];
  671. this.leafNode = this.table.tree[2];
  672. });
  673. it("has the 'indenter' class", function() {
  674. expect(this.branchNode.indenter.hasClass("indenter")).to.be.true;
  675. });
  676. describe("when root node", function() {
  677. it("is not indented", function() {
  678. expect(this.rootNode.indenter.css("padding-left")).to.equal("0px");
  679. });
  680. });
  681. describe("when level 1 branch node", function() {
  682. it("is indented 19px", function() {
  683. expect(this.branchNode.indenter.css("padding-left")).to.equal("19px");
  684. });
  685. });
  686. describe("when level 2 leaf node", function() {
  687. it("is indented 38px", function() {
  688. expect(this.leafNode.indenter.css("padding-left")).to.equal("38px");
  689. });
  690. });
  691. });
  692. describe("initialized", function() {
  693. beforeEach(function() {
  694. this.table = $("<table><tr data-tt-id='0'><td>Root Node</td></tr><tr data-tt-id='1' data-tt-parent-id='0'><td>Leaf Node</td></tr></table>");
  695. });
  696. describe("when expandable is false", function() {
  697. beforeEach(function() {
  698. this.subject = this.table.treetable({
  699. expandable: false
  700. }).data("treetable").tree;
  701. this.rootNode = this.subject[0];
  702. this.leafNode = this.subject[1];
  703. });
  704. it("initializes root nodes immediately", function() {
  705. expect(this.rootNode.initialized).to.be.true;
  706. });
  707. it("initializes non-root nodes immediately", function() {
  708. expect(this.leafNode.initialized).to.be.true;
  709. });
  710. });
  711. describe("when expandable is true and initialState is 'collapsed'", function() {
  712. beforeEach(function() {
  713. this.subject = this.table.treetable({
  714. expandable: true,
  715. initialState: "collapsed"
  716. }).data("treetable").tree;
  717. this.rootNode = this.subject[0];
  718. this.leafNode = this.subject[1];
  719. });
  720. it("initializes root nodes immediately", function() {
  721. expect(this.rootNode.initialized).to.be.true;
  722. });
  723. it("does not initialize non-root nodes immediately", function() {
  724. expect(this.leafNode.initialized).to.be.false;
  725. });
  726. });
  727. describe("when expandable is true and initialState is 'expanded'", function() {
  728. beforeEach(function() {
  729. this.subject = this.table.treetable({
  730. expandable: true,
  731. initialState: "expanded"
  732. }).data("treetable").tree;
  733. this.rootNode = this.subject[0];
  734. this.leafNode = this.subject[1];
  735. });
  736. it("initializes root nodes immediately", function() {
  737. expect(this.rootNode.initialized).to.be.true;
  738. });
  739. it("initializes non-root nodes immediately", function() {
  740. expect(this.leafNode.initialized).to.be.true;
  741. });
  742. });
  743. });
  744. describe("hide()", function() {
  745. beforeEach(function() {
  746. this.table = $("<table><tr data-tt-id='0'><td>N0</td></tr><tr data-tt-id='1' data-tt-parent-id='0'><td>N1</td></tr></table>").appendTo("body").treetable();
  747. this.subject = this.table.data("treetable").tree;
  748. this.subject[0].expand();
  749. });
  750. afterEach(function() {
  751. this.table.remove();
  752. });
  753. it("hides table row", function() {
  754. expect(this.subject[0].row).to.be.visible;
  755. this.subject[0].hide();
  756. expect(this.subject[0].row).to.be.hidden;
  757. });
  758. it("recursively hides children", function() {
  759. expect(this.subject[1].row).to.be.visible;
  760. this.subject[0].hide();
  761. expect(this.subject[1].row).to.be.hidden;
  762. });
  763. it("maintains chainability", function() {
  764. expect(this.subject[0].hide()).to.equal(this.subject[0]);
  765. });
  766. });
  767. describe("id", function() {
  768. it("is extracted from row attributes", function() {
  769. var subject;
  770. subject = $("<table><tr data-tt-id='42'><td>N42</td></tr></table>").treetable().data("treetable").tree[42];
  771. expect(subject.id).to.equal(42);
  772. });
  773. });
  774. describe("isBranchNode()", function() {
  775. it("is true when node has children", function() {
  776. var subject = $("<table><tr data-tt-id='42'><td>N42</td></tr><tr data-tt-id='21' data-tt-parent-id='42'><td>N21</td></tr></table>").treetable().data("treetable").tree[42];
  777. expect(subject.isBranchNode()).to.be.true;
  778. });
  779. it("is true when node has data attribute tt-branch with value 'true'", function() {
  780. var subject = $("<table><tr data-tt-id='42' data-tt-branch='true'><td>N42</td></tr></table>").treetable().data("treetable").tree[42];
  781. expect(subject.isBranchNode()).to.be.true;
  782. });
  783. // This would be an error in the tree, but I consider having children
  784. // more important than the ttBranch attribute.
  785. it("is true when node has children but also a tt-branch attribute with value 'false'", function() {
  786. var subject = $("<table><tr data-tt-id='42' data-tt-branch='false'><td>N42</td></tr><tr data-tt-id='21' data-tt-parent-id='42'><td>N21</td></tr></table>").treetable().data("treetable").tree[42];
  787. expect(subject.isBranchNode()).to.be.true;
  788. });
  789. it("is false when node has data attribute tt-branch with value 'false'", function() {
  790. var subject = $("<table><tr data-tt-id='42' data-tt-branch='false'><td>N42</td></tr></table>").treetable().data("treetable").tree[42];
  791. expect(subject.isBranchNode()).to.be.false;
  792. });
  793. it("is false when node has no children and no tt-branch attribute", function() {
  794. var subject = $("<table><tr data-tt-id='42'><td>N42</td></tr></table>").treetable().data("treetable").tree[42];
  795. expect(subject.isBranchNode()).to.be.false;
  796. });
  797. });
  798. describe("level()", function() {
  799. beforeEach(function() {
  800. this.subject = $("<table id='subject'><tr data-tt-id='1'><td>N1</td></tr><tr data-tt-id='2' data-tt-parent-id='1'><td>N2</td></tr><tr data-tt-id='3' data-tt-parent-id='2'><td>N3</td></tr><tr data-tt-id='4' data-tt-parent-id='3'><td>N4</td></tr></table>").treetable().data("treetable").tree;
  801. });
  802. it("equals the number of ancestors", function() {
  803. expect(this.subject[1].level()).to.equal(0);
  804. expect(this.subject[2].level()).to.equal(1);
  805. expect(this.subject[3].level()).to.equal(2);
  806. expect(this.subject[4].level()).to.equal(3);
  807. });
  808. });
  809. describe("parentId", function() {
  810. it("is extracted from row attributes", function() {
  811. var subject;
  812. subject = $("<table><tr data-tt-id='12'><td>N12</td></tr><tr data-tt-id='42' data-tt-parent-id='12'><td>N42</td></tr></table>").treetable().data("treetable").tree[42];
  813. expect(subject.parentId).to.equal(12);
  814. });
  815. it("is undefined when not available", function() {
  816. var subject;
  817. subject = $("<table><tr data-tt-id='0'><td>N42</td></tr></table>").treetable().data("treetable").tree[0];
  818. expect(subject.parentId).to.be.undefined;
  819. });
  820. it("is undefined when empty", function() {
  821. var subject;
  822. subject = $("<table><tr data-tt-id='0' data-tt-parent-id=''><td>N42</td></tr></table>").treetable().data("treetable").tree[0];
  823. expect(subject.parentId).to.be.undefined;
  824. });
  825. });
  826. describe("parentNode()", function() {
  827. beforeEach(function() {
  828. this.subject = $("<table id='subject'><tr data-tt-id='0'><td>N0</td></tr><tr data-tt-id='1' data-tt-parent-id='0'><td>N1</td></tr></table>").treetable().data("treetable").tree;
  829. });
  830. describe("when node has a parent", function() {
  831. it("is a node object", function() {
  832. var subject = this.subject[1];
  833. // to.be.an.instanceof fails in IE9, is this a chai bug?
  834. expect(subject.parentNode()).that.is.an.instanceof(TreeTable.Node);
  835. });
  836. it("it's id equals this node's parentId", function() {
  837. var subject = this.subject[1];
  838. expect(subject.parentNode().id).to.equal(subject.parentId);
  839. });
  840. });
  841. describe("when node has no parent", function() {
  842. it("is null", function() {
  843. expect(this.subject[0].parentNode()).to.be.null;
  844. });
  845. });
  846. });
  847. describe("removeChild()", function() {
  848. beforeEach(function() {
  849. this.table = $("<table><tr data-tt-id='n0'><td>N0</td></tr><tr data-tt-id='n1' data-tt-parent-id='n0'><td>N1</td></tr></table>");
  850. this.table.treetable();
  851. this.parent = this.table.data("treetable").tree["n0"];
  852. this.child = this.table.data("treetable").tree["n1"];
  853. });
  854. it("removes child from collection of children", function() {
  855. expect(this.parent.children).to.include(this.child);
  856. this.parent.removeChild(this.child);
  857. expect(this.parent.children).to.be.empty;
  858. });
  859. });
  860. describe("render()", function() {
  861. it("maintains chainability", function() {
  862. var subject;
  863. subject = $("<table><tr data-tt-id='n0'><td>N0</td></tr><tr data-tt-id='n1' data-tt-parent-id='n0'><td>N1</td></tr></table>").treetable().data("treetable").tree["n0"];
  864. expect(subject.render()).to.equal(subject);
  865. });
  866. });
  867. describe("setParent()", function() {
  868. beforeEach(function() {
  869. this.table = $("<table><tr data-tt-id='n0'><td>N0</td></tr><tr data-tt-id='n1' data-tt-parent-id='n0'><td>N1</td></tr><tr data-tt-id='n2'><td>N2</td></tr></table>");
  870. this.table.treetable();
  871. this.oldParent = this.table.data("treetable").tree["n0"];
  872. this.subject = this.table.data("treetable").tree["n1"];
  873. this.newParent = this.table.data("treetable").tree["n2"];
  874. });
  875. it("updates node's parent id", function() {
  876. expect(this.subject.parentId).to.equal("n0");
  877. this.subject.setParent(this.newParent);
  878. expect(this.subject.parentId).to.equal("n2");
  879. });
  880. it("updates node's parent id data attribute", function() {
  881. expect(this.subject.row.data("ttParentId")).to.equal("n0");
  882. this.subject.setParent(this.newParent);
  883. expect(this.subject.row.data("ttParentId")).to.equal("n2");
  884. });
  885. it("adds node to new parent's children", function() {
  886. this.subject.setParent(this.newParent);
  887. expect(this.newParent.children).to.include(this.subject);
  888. });
  889. it("removes node from old parent's children", function() {
  890. this.subject.setParent(this.newParent);
  891. expect(this.oldParent.children).to.not.include(this.subject);
  892. });
  893. it("does not try to remove children from parent when node is a root node", function() {
  894. var fn, newParent, subject;
  895. subject = this.subject;
  896. newParent = this.newParent;
  897. fn = function() {
  898. subject.setParent(newParent);
  899. };
  900. expect(fn).to.not["throw"](Error);
  901. });
  902. });
  903. describe("show()", function() {
  904. beforeEach(function() {
  905. this.table = $("<table><tr data-tt-id='0'><td>N0</td></tr><tr data-tt-id='1' data-tt-parent-id='0'><td>N1</td></tr></table>").appendTo("body").treetable();
  906. this.subject = this.table.data("treetable").tree;
  907. this.subject[0].hide();
  908. });
  909. afterEach(function() {
  910. this.table.remove();
  911. });
  912. it("shows table row", function() {
  913. expect(this.subject[0].row).to.be.hidden;
  914. this.subject[0].show();
  915. expect(this.subject[0].row).to.be.visible;
  916. });
  917. it("maintains chainability", function() {
  918. expect(this.subject[0].show()).to.equal(this.subject[0]);
  919. });
  920. describe("when expanded", function() {
  921. beforeEach(function() {
  922. this.subject[0].expand().hide();
  923. });
  924. it("recursively shows children", function() {
  925. expect(this.subject[1].row).to.be.hidden;
  926. this.subject[0].show();
  927. expect(this.subject[1].row).to.be.visible;
  928. });
  929. });
  930. describe("when collapsed", function() {
  931. beforeEach(function() {
  932. this.subject[0].collapse().hide();
  933. });
  934. it("does not show children", function() {
  935. expect(this.subject[1].row).to.be.hidden;
  936. this.subject[0].show();
  937. expect(this.subject[1].row).to.be.hidden;
  938. });
  939. });
  940. });
  941. describe("toggle()", function() {
  942. beforeEach(function() {
  943. this.table = $("<table><tr data-tt-id='42'><td>N42</td></tr><tr data-tt-id='24' data-tt-parent-id='42'><td>N24</td></tr></table>").appendTo("body").treetable({
  944. expandable: true
  945. });
  946. this.subject = this.table.data("treetable").tree;
  947. });
  948. afterEach(function() {
  949. this.table.remove();
  950. });
  951. it("toggles child rows", function() {
  952. expect(this.subject[24].row).to.be.hidden;
  953. this.subject[42].toggle();
  954. expect(this.subject[24].row).to.be.visible;
  955. this.subject[42].toggle();
  956. expect(this.subject[24].row).to.be.hidden;
  957. });
  958. it("maintains chainability", function() {
  959. expect(this.subject[42].toggle()).to.equal(this.subject[42]);
  960. });
  961. });
  962. describe("treeCell", function() {
  963. describe("with default column setting", function() {
  964. beforeEach(function() {
  965. this.subject = $("<table><tr data-tt-id='0'><th>Not part of tree</th><td>Column 1</td><td>Column 2</td></tr>").treetable().data("treetable").tree[0].treeCell;
  966. });
  967. it("is an object", function() {
  968. // to.be.an("object") fails in IE9, is this a chai bug?
  969. expect(this.subject).that.is.an("object");
  970. });
  971. it("maps to a td", function() {
  972. expect(this.subject).to.be("td");
  973. });
  974. it("maps to the first column by default", function() {
  975. expect(this.subject).to.contain("Column 1");
  976. });
  977. it("contains an indenter", function() {
  978. expect(this.subject).to.have("span.indenter");
  979. });
  980. });
  981. describe("with custom column setting", function() {
  982. beforeEach(function() {
  983. this.subject = $("<table><tr data-tt-id='0'><th>Not part of tree</th><td>Column 1</td><td>Column 2</td></tr></table>").treetable({
  984. column: 1
  985. }).data("treetable").tree[0].treeCell;
  986. });
  987. it("is configurable", function() {
  988. expect(this.subject).to.contain("Column 2");
  989. });
  990. });
  991. });
  992. });
  993. describe("TreeTable.Tree", function() {
  994. describe("loadRows()", function() {
  995. it("maintains chainability", function() {
  996. var subject = new TreeTable.Tree($("<table></table>"), {});
  997. expect(subject.loadRows()).to.equal(subject);
  998. });
  999. describe("a table without rows", function() {
  1000. it("'s tree cache is empty", function() {
  1001. var subject = new TreeTable.Tree($("<table></table>"), {}).loadRows().tree;
  1002. expect(_.size(subject)).to.equal(0);
  1003. });
  1004. });
  1005. describe("a table with tree rows", function() {
  1006. beforeEach(function() {
  1007. this.subject = $("<table><tr data-tt-id='0'><td>N0</td></tr><tr data-tt-id='1' data-tt-parent-id='0'><td>N1</td></tr></table>").treetable().data("treetable").tree;
  1008. });
  1009. it("caches all tree nodes", function() {
  1010. expect(_.size(this.subject)).to.equal(2);
  1011. expect(_.keys(this.subject)).to.include('0');
  1012. expect(_.keys(this.subject)).to.include('1');
  1013. });
  1014. it("sets branch and leaf classes", function() {
  1015. expect(this.subject[0].row).to.have.class('branch');
  1016. expect(this.subject[1].row).to.have.class('leaf');
  1017. });
  1018. });
  1019. describe("a table without tree rows", function() {
  1020. it("results in an empty node cache", function() {
  1021. var subject;
  1022. subject = $("<table><tr></tr><tr></tr></table>").treetable().data("treetable").tree;
  1023. expect(_.size(subject)).to.equal(0);
  1024. });
  1025. });
  1026. describe("a table with both tree rows and non tree rows", function() {
  1027. it("only caches tree nodes", function() {
  1028. var subject;
  1029. subject = $("<table><tr></tr><tr data-tt-id='21'><td>N21</td></tr></table>").treetable().data("treetable").tree;
  1030. expect(_.size(subject)).to.equal(1);
  1031. expect(_.keys(subject)).to.include('21');
  1032. });
  1033. });
  1034. describe("a table with a node with a non-existing parent (#132)", function() {
  1035. it("does not err", function() {
  1036. var fn, table;
  1037. fn = function() {
  1038. $("<table><tr></tr><tr data-tt-id='21' data-tt-parent-id='dunno'><td>N21</td></tr></table>").treetable();
  1039. };
  1040. expect(fn).to.not.throw();
  1041. });
  1042. });
  1043. });
  1044. describe("move()", function() {
  1045. beforeEach(function() {
  1046. this.table = $("<table><tr data-tt-id='n0'><td>N0</td></tr><tr data-tt-id='n1' data-tt-parent-id='n0' data-tt-branch='true'><td>N1</td></tr><tr data-tt-id='n2' data-tt-parent-id='n1'><td>N2</td></tr><tr data-tt-id='n2c1' data-tt-parent-id='n2'><td>N2C1</td></tr><tr data-tt-id='n2c2' data-tt-parent-id='n2'><td>N2C2</td></tr><tr data-tt-id='n3'><td>N3</td></tr><tr data-tt-id='n3c1' data-tt-parent-id='n3'><td>N3C1</td></tr><tr data-tt-id='n4' data-tt-parent-id='n3'><td>N4</td></tr><tr data-tt-id='n5' data-tt-parent-id='n3'><td>N5</td></tr></table>");
  1047. this.table.treetable();
  1048. });
  1049. it("moves node to new destination", function() {
  1050. var subject;
  1051. subject = this.table.data("treetable").tree["n2"];
  1052. expect(subject.parentId).to.equal("n1");
  1053. this.table.treetable("move", "n2", "n3");
  1054. expect(subject.parentId).to.equal("n3");
  1055. });
  1056. it("updates UI", function() {
  1057. expect(collectValuesInTable(this.table)).to.eql(["N0", "N1", "N2", "N2C1", "N2C2", "N3", "N3C1", "N4", "N5"]);
  1058. this.table.treetable("move", "n2", "n3");
  1059. expect(collectValuesInTable(this.table)).to.eql(["N0", "N1", "N3", "N2", "N2C1", "N2C2", "N3C1", "N4", "N5"]);
  1060. });
  1061. it("updates branch and leaf classes when node has siblings", function() {
  1062. this.table.treetable("move", "n5", "n0");
  1063. expect(this.table.data("treetable").tree["n0"].row).to.have.class('branch');
  1064. expect(this.table.data("treetable").tree["n3"].row).to.have.class('branch');
  1065. expect(this.table.data("treetable").tree["n5"].row).to.have.class('leaf');
  1066. });
  1067. it("updates branch and leaf classes when move when node has no siblings", function() {
  1068. this.table.treetable("move", "n1", "n3");
  1069. expect(this.table.data("treetable").tree["n0"].row).to.have.class('leaf');
  1070. expect(this.table.data("treetable").tree["n1"].row).to.have.class('branch');
  1071. expect(this.table.data("treetable").tree["n3"].row).to.have.class('branch');
  1072. });
  1073. it("updates branch and leaf classes when move when destination node has no siblings", function() {
  1074. expect(this.table.data("treetable").tree["n5"].row).to.have.class('leaf');
  1075. this.table.treetable("move", "n4", "n5");
  1076. expect(this.table.data("treetable").tree["n3"].row).to.have.class('branch');
  1077. expect(this.table.data("treetable").tree["n4"].row).to.have.class('leaf');
  1078. expect(this.table.data("treetable").tree["n5"].row).to.have.class('branch');
  1079. });
  1080. it("updates branch and leaf classes when move from node with branchAttr", function() {
  1081. expect(this.table.data("treetable").tree["n1"].row).to.have.class('branch');
  1082. this.table.treetable("move", "n2", "n0");
  1083. expect(this.table.data("treetable").tree["n1"].row).to.have.class('branch');
  1084. });
  1085. it("cannot make node a descendant of itself", function() {
  1086. var fn, table;
  1087. table = this.table;
  1088. fn = function() {
  1089. table.treetable("move", "n1", "n2");
  1090. };
  1091. expect(fn).to.not.throw();
  1092. });
  1093. it("cannot make node a child of itself", function() {
  1094. var fn, table;
  1095. table = this.table;
  1096. fn = function() {
  1097. table.treetable("move", "n1", "n1");
  1098. };
  1099. expect(fn).to.not.throw();
  1100. });
  1101. it("does nothing when node is moved to current location", function() {
  1102. // TODO How to test? Nothing is happening...
  1103. this.table.treetable("move", "n1", "n0");
  1104. });
  1105. it("maintains chainability", function() {
  1106. var destination, node, tree;
  1107. tree = this.table.data("treetable");
  1108. node = this.table.data("treetable").tree["n1"];
  1109. destination = this.table.data("treetable").tree["n3"];
  1110. expect(tree.move(node, destination)).to.equal(tree);
  1111. });
  1112. });
  1113. describe("render()", function() {
  1114. it("maintains chainability", function() {
  1115. var subject;
  1116. subject = new TreeTable.Tree($("<table></table>"), {});
  1117. expect(subject.render()).to.equal(subject);
  1118. });
  1119. });
  1120. describe("reveal()", function() {
  1121. beforeEach(function() {
  1122. this.table = $("<table><tr data-tt-id='0'><td>N0</td></tr><tr data-tt-id='1' data-tt-parent-id='0'><td>N1</td></tr><tr data-tt-id='2' data-tt-parent-id='1'><td>N2</td></tr></table>").treetable({
  1123. expandable: true
  1124. }).appendTo("body");
  1125. this.subject = this.table.data("treetable");
  1126. });
  1127. afterEach(function() {
  1128. this.table.remove();
  1129. });
  1130. it("reveals a node", function() {
  1131. expect(this.subject.tree[2].row).to.not.be.visible;
  1132. this.table.treetable("reveal", 2);
  1133. expect(this.subject.tree[2].row).to.be.visible;
  1134. });
  1135. it("expands the ancestors of the node", function() {
  1136. expect(this.subject.tree[1].row).to.not.be.visible;
  1137. this.table.treetable("reveal", 2);
  1138. expect(this.subject.tree[1].row).to.be.visible;
  1139. });
  1140. it("throws an error for unknown nodes", function() {
  1141. var fn, table;
  1142. table = this.table;
  1143. fn = function() {
  1144. table.treetable("reveal", "whatever");
  1145. };
  1146. expect(fn).to["throw"](Error, "Unknown node 'whatever'");
  1147. });
  1148. });
  1149. describe("roots", function() {
  1150. describe("when no rows", function() {
  1151. it("is empty", function() {
  1152. var subject;
  1153. subject = $("<table></table>").treetable().data("treetable");
  1154. expect(_.size(subject.roots)).to.equal(0);
  1155. });
  1156. });
  1157. describe("when single root node", function() {
  1158. beforeEach(function() {
  1159. this.subject = $("<table><tr data-tt-id='1'><td>N1</td></tr><tr data-tt-id='2' data-tt-parent-id='1'><td>N2</td></tr></table>").treetable().data("treetable");
  1160. });
  1161. it("includes root node when only one root node exists", function() {
  1162. var roots;
  1163. roots = this.subject.roots;
  1164. expect(_.size(roots)).to.equal(1);
  1165. expect(roots).to.include(this.subject.tree[1]);
  1166. });
  1167. it("does not include non-root nodes", function() {
  1168. expect(this.subject.roots).to.not.include(this.subject.tree[2]);
  1169. });
  1170. });
  1171. describe("when multiple root nodes", function() {
  1172. beforeEach(function() {
  1173. this.subject = $("<table><tr data-tt-id='1'><td>N1</td></tr><tr data-tt-id='2' data-tt-parent-id='1'><td>N2</td></tr><tr data-tt-id='3'><td>N3</td></tr></table>").treetable().data("treetable");
  1174. });
  1175. it("includes all root nodes", function() {
  1176. var roots;
  1177. roots = this.subject.roots;
  1178. expect(_.size(roots)).to.equal(2);
  1179. expect(roots).to.include(this.subject.tree[1]);
  1180. expect(roots).to.include(this.subject.tree[3]);
  1181. });
  1182. it("does not include non-root nodes", function() {
  1183. expect(this.subject.roots).to.not.include(this.subject.tree[2]);
  1184. });
  1185. });
  1186. });
  1187. });
  1188. describe("events", function() {
  1189. describe("onInitialized", function() {
  1190. describe("when no callback function given", function() {
  1191. it("does not complain", function() {
  1192. var table;
  1193. table = $("<table><tr data-tt-id='1'><td>N1</td></tr></table>").treetable({
  1194. onInitialized: null
  1195. });
  1196. });
  1197. });
  1198. describe("when callback function given", function() {
  1199. it("is called when tree has been initialized", function() {
  1200. var callback, table;
  1201. callback = sinon.spy();
  1202. table = $("<table><tr data-tt-id='1'><td>N1</td></tr></table>").treetable({
  1203. onInitialized: callback
  1204. });
  1205. expect(callback.called).to.be.true;
  1206. });
  1207. });
  1208. });
  1209. describe("onNodeCollapse", function() {
  1210. describe("when no callback function given", function() {
  1211. it("does not complain", function() {
  1212. var table;
  1213. table = $("<table><tr data-tt-id='1'><td>N1</td></tr><tr data-tt-id='2' data-tt-parent-id='1'><td>N2</td></tr></table>").treetable({
  1214. expandable: true,
  1215. initialState: "expanded",
  1216. onNodeCollapse: null
  1217. }).data("treetable");
  1218. table.roots[0].collapse();
  1219. });
  1220. });
  1221. describe("when callback function given", function() {
  1222. beforeEach(function() {
  1223. this.callback = sinon.spy();
  1224. this.table = $("<table><tr data-tt-id='1'><td>N1</td></tr><tr data-tt-id='2' data-tt-parent-id='1'><td>N2</td></tr></table>").treetable({
  1225. expandable: true,
  1226. initialState: "expanded",
  1227. onNodeCollapse: this.callback
  1228. }).data("treetable");
  1229. });
  1230. it("is called when node is being hidden", function() {
  1231. this.table.roots[0].collapse();
  1232. expect(this.callback.called).to.be.true;
  1233. });
  1234. it("is not called when node is being shown", function() {
  1235. this.table.roots[0].expand();
  1236. expect(this.callback.called).to.be.false;
  1237. });
  1238. it("is not called when node is not initialized yet", function() {
  1239. this.table.roots[0].initialized = false;
  1240. this.table.roots[0].collapse();
  1241. expect(this.callback.called).to.be.false;
  1242. });
  1243. });
  1244. });
  1245. describe("onNodeInitialized", function() {
  1246. describe("when no callback function given", function() {
  1247. it("does not complain", function() {
  1248. var table;
  1249. table = $("<table><tr data-tt-id='1'><td>N1</td></tr></table>").treetable({
  1250. onNodeInitialized: null
  1251. }).data("treetable");
  1252. table.roots[0].initialized = false;
  1253. table.roots[0].show();
  1254. });
  1255. });
  1256. describe("when callback function given", function() {
  1257. beforeEach(function() {
  1258. this.callback = sinon.spy();
  1259. this.table = $("<table><tr data-tt-id='1'><td>N1</td></tr></table>").treetable({
  1260. onNodeInitialized: this.callback
  1261. }).data("treetable");
  1262. });
  1263. it("is called when node is not initialized yet", function() {
  1264. this.table.roots[0].initialized = false;
  1265. this.table.roots[0].show();
  1266. expect(this.callback.called).to.be.true;
  1267. });
  1268. it("is not called again when node is already initialized", function() {
  1269. this.table.roots[0].show();
  1270. // Node was initialized before, callback has already been called. I
  1271. // check that the callback is not called more than once.
  1272. expect(this.callback.calledOnce).to.be.true;
  1273. });
  1274. });
  1275. });
  1276. describe("onNodeExpand", function() {
  1277. describe("when no callback given", function() {
  1278. it("does not complain", function() {
  1279. var table;
  1280. table = $("<table><tr data-tt-id='1'><td>N1</td></tr><tr data-tt-id='2' data-tt-parent-id='1'><td>N2</td></tr></table>").treetable({
  1281. expandable: true,
  1282. initialState: "collapsed",
  1283. onNodeExpand: null
  1284. }).data("treetable");
  1285. table.roots[0].expand();
  1286. });
  1287. });
  1288. describe("when callback function given", function() {
  1289. beforeEach(function() {
  1290. this.callback = sinon.spy();
  1291. this.table = $("<table><tr data-tt-id='1'><td>N1</td></tr><tr data-tt-id='2' data-tt-parent-id='1'><td>N2</td></tr></table>").treetable({
  1292. expandable: true,
  1293. initialState: "collapsed",
  1294. onNodeExpand: this.callback
  1295. }).data("treetable");
  1296. });
  1297. it("is called when node is being shown", function() {
  1298. this.table.roots[0].expand();
  1299. expect(this.callback.called).to.be.true;
  1300. });
  1301. it("is not called when node is being hidden", function() {
  1302. this.table.roots[0].collapse();
  1303. expect(this.callback.called).to.be.false;
  1304. });
  1305. it("is not called when node is not initialized yet", function() {
  1306. this.table.roots[0].initialized = false;
  1307. this.table.roots[0].expand();
  1308. expect(this.callback.called).to.be.false;
  1309. });
  1310. });
  1311. });
  1312. });
  1313. }).call(this);