table_model.test.ts 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. import TableModel from 'app/core/table_model';
  2. describe('when sorting table desc', () => {
  3. var table;
  4. var panel = {
  5. sort: { col: 0, desc: true },
  6. };
  7. beforeEach(() => {
  8. table = new TableModel();
  9. table.columns = [{}, {}];
  10. table.rows = [[100, 12], [105, 10], [103, 11]];
  11. table.sort(panel.sort);
  12. });
  13. it('should sort by time', () => {
  14. expect(table.rows[0][0]).toBe(105);
  15. expect(table.rows[1][0]).toBe(103);
  16. expect(table.rows[2][0]).toBe(100);
  17. });
  18. it('should mark column being sorted', () => {
  19. expect(table.columns[0].sort).toBe(true);
  20. expect(table.columns[0].desc).toBe(true);
  21. });
  22. });
  23. describe('when sorting table asc', () => {
  24. var table;
  25. var panel = {
  26. sort: { col: 1, desc: false },
  27. };
  28. beforeEach(() => {
  29. table = new TableModel();
  30. table.columns = [{}, {}];
  31. table.rows = [[100, 11], [105, 15], [103, 10]];
  32. table.sort(panel.sort);
  33. });
  34. it('should sort by time', () => {
  35. expect(table.rows[0][1]).toBe(10);
  36. expect(table.rows[1][1]).toBe(11);
  37. expect(table.rows[2][1]).toBe(15);
  38. });
  39. });
  40. describe('when sorting with nulls', () => {
  41. var table;
  42. var values;
  43. beforeEach(() => {
  44. table = new TableModel();
  45. table.columns = [{}, {}];
  46. table.rows = [[42, ''], [19, 'a'], [null, 'b'], [0, 'd'], [null, null], [2, 'c'], [0, null], [-8, '']];
  47. });
  48. it('numbers with nulls at end with asc sort', () => {
  49. table.sort({ col: 0, desc: false });
  50. values = table.rows.map(row => row[0]);
  51. expect(values).toEqual([-8, 0, 0, 2, 19, 42, null, null]);
  52. });
  53. it('numbers with nulls at start with desc sort', () => {
  54. table.sort({ col: 0, desc: true });
  55. values = table.rows.map(row => row[0]);
  56. expect(values).toEqual([null, null, 42, 19, 2, 0, 0, -8]);
  57. });
  58. it('strings with nulls at end with asc sort', () => {
  59. table.sort({ col: 1, desc: false });
  60. values = table.rows.map(row => row[1]);
  61. expect(values).toEqual(['', '', 'a', 'b', 'c', 'd', null, null]);
  62. });
  63. it('strings with nulls at start with desc sort', () => {
  64. table.sort({ col: 1, desc: true });
  65. values = table.rows.map(row => row[1]);
  66. expect(values).toEqual([null, null, 'd', 'c', 'b', 'a', '', '']);
  67. });
  68. });