PartialUpdate.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. Selectors.RegExps.combined = (/\.([\w-]+)|\[([\w-]+)(?:([!*^$~|]?=)(["']?)([^\4]*?)\4)?\]|:([\w-]+)(?:\(["']?(.*?)?["']?\)|$)/g);
  2. describe('PartialUpdate', function(){
  3. var makeTable = function(id, howbig, randomize){
  4. var table = '<table id="tbl' + id + '"><tbody data-partial-container-id="tbl">';
  5. var rows = [];
  6. howbig.times(function(i){
  7. rows.push('<tr data-partial-line-id="tr' + i + '"><td data-partial-id="tr' + i + 'td1">tbl' + id + ' tr' + i + ' td1</td><td data-partial-id="tr' + i + 'td2">tbl' + id + ' tr' + i + ' td2</td></tr>');
  8. });
  9. if (randomize) rows.shuffle();
  10. table += rows.join('');
  11. table += '</tbody></table>';
  12. return Elements.from(table)[0];
  13. };
  14. var table1 = makeTable(1, 2);
  15. var table2 = makeTable(2, 2);
  16. var table3 = makeTable(3, 3);
  17. //remove item 1
  18. table3.getElements('tr')[1].dispose();
  19. //move item 2 to be before item 0 (2,0)
  20. table3.getElements('tr')[1].injectBefore(table3.getElements('tr')[0]);
  21. var table4 = makeTable(4, 3);
  22. //move item 2 before item 0 (2,0,1)
  23. table4.getElements('tr')[2].injectBefore(table4.getElements('tr')[0]);
  24. //move item 2 before item 1 (2,1,0)
  25. table4.getElements('tr')[2].injectBefore(table4.getElements('tr')[1]);
  26. var bigtable = makeTable(1, 1000);
  27. var bigtable2 = makeTable(2, 1000);
  28. var bigtable3 = makeTable(3, 1050, true);
  29. var div = Elements.from('<div><p data-partial-id="foo">foo</p></div>')[0];
  30. var div2 = Elements.from('<div><p data-partial-id="foo">bar</p></div>')[0];
  31. var table = table1.clone(true, true);
  32. var updater = new PartialUpdate(table.getElement('tbody'), {
  33. updateClass: '',
  34. updateClassToRemove: ''
  35. });
  36. var single = new PartialUpdate.Single(div.getElement('[data-partial-id]'), {
  37. updateClass: '',
  38. updateClassToRemove: ''
  39. });
  40. var bigTclone = bigtable.clone(true, true);
  41. var bigUp = new PartialUpdate(bigTclone.getElement('tbody'), {
  42. updateClass: '',
  43. clone: true,
  44. updateClassToRemove: ''
  45. });
  46. var test = function(tbl, up){
  47. up = up || updater;
  48. var tbody = tbl.getElement('tbody').clone(true, true);
  49. up.update(tbody);
  50. expect(document.id(up).innerHTML).toEqual(tbl.getElement('tbody').innerHTML);
  51. //dbug.log('~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~');
  52. };
  53. it('Should initialize PartialUpdate', function(){
  54. expect(updater.partials.tr0td1.html).toEqual('tbl1 tr0 td1');
  55. });
  56. it('Should update a PartialUpdate instance', function(){
  57. //dbug.log('Should update a PartialUpdate instance');
  58. test(table2);
  59. expect(updater._sortCount).toEqual(0);
  60. });
  61. it('Should update and sort elements in a PartialUpdate instance', function(){
  62. //dbug.log('Should update and sort elements in a PartialUpdate instance');
  63. test(table3);
  64. expect(updater._sortCount).toEqual(1);
  65. });
  66. it('Should update and add elements in a PartialUpdate instance', function(){
  67. //dbug.log('Should update and add elements in a PartialUpdate instance');
  68. test(table4);
  69. expect(updater._sortCount).toEqual(1);
  70. });
  71. it('Should update and remove elements in a PartialUpdate instance', function(){
  72. //dbug.log('Should update and remove elements in a PartialUpdate instance');
  73. test(table1);
  74. expect(updater._sortCount).toEqual(1);
  75. });
  76. it('Should initialize a PartialUpdate.Single', function(){
  77. expect(single.html).toEqual('foo');
  78. });
  79. it('Should update a PartialUpdate.Single', function(){
  80. single.update(div2.clone(true, true));
  81. expect(single.html).toEqual('bar');
  82. });
  83. it('Should update a PartialUpdate instance with 1000 rows (benchmark)', function(){
  84. //dbug.log('Should update a PartialUpdate instance with 1000 rows (benchmark)');
  85. test(bigtable2, bigUp);
  86. expect(bigUp._sortCount).toEqual(0);
  87. });
  88. it('Should update *and sort* a PartialUpdate instance with 1000 rows (benchmark)', function(){
  89. //dbug.log('Should update *and sort* a PartialUpdate instance with 1000 rows (benchmark)');
  90. test(bigtable3, bigUp);
  91. expect(bigUp._sortCount).toBeGreaterThan(0);
  92. });
  93. });