You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

149 regels
4.1 KiB

  1. /* global describe, it, before */
  2. import chai from 'chai';
  3. import DataManager, {
  4. DataError
  5. } from '../src/datamanager';
  6. chai.expect();
  7. const expect = chai.expect;
  8. describe.only('DataManager instance', () => {
  9. it('should initialize rows and columns given options', () => {
  10. const datamanager = getDataManagerInstance();
  11. expect(datamanager).has.property('rows');
  12. expect(datamanager).has.property('columns');
  13. expect(datamanager.rowCount).to.equal(3);
  14. expect(datamanager._serialNoColumnAdded).to.equal(false);
  15. expect(datamanager._checkboxColumnAdded).to.equal(false);
  16. });
  17. describe('prepareRows', () => {
  18. const datamanager = getDataManagerInstance();
  19. it('should properly build row object when bare minimum options are given', () => {
  20. const firstRow = datamanager.getRow(0);
  21. expect(firstRow).to.deep.equal([
  22. {
  23. colIndex: 0,
  24. content: 'Faris',
  25. rowIndex: 0
  26. },
  27. {
  28. colIndex: 1,
  29. content: 'faris@test.com',
  30. rowIndex: 0
  31. },
  32. {
  33. colIndex: 2,
  34. content: 'Software Developer',
  35. rowIndex: 0
  36. }
  37. ]);
  38. });
  39. it('should throw when rows parameter is not an Array', () => {
  40. expect(() => datamanager.init({
  41. columns: ['Name'],
  42. rows: 2
  43. })).to.throw(DataError, '`rows` must be an array');
  44. });
  45. it('should throw when any of the row\'s length doesn\'t match column length', () => {
  46. expect(() => datamanager.init({
  47. columns: ['Name'],
  48. rows: [[]]
  49. })).to.throw(DataError, 'column length');
  50. });
  51. it('should not throw given valid data', () => {
  52. expect(() => datamanager.init({
  53. columns: ['Name'],
  54. rows: [['Faris']]
  55. })).to.not.throw();
  56. });
  57. });
  58. describe('prepareColumns', () => {
  59. const datamanager = getDataManagerInstance();
  60. it('should properly build column object with bare minimum options', () => {
  61. const firstColumn = datamanager.getColumn(0);
  62. expect(firstColumn.colIndex).eq(0);
  63. expect(firstColumn.content).eq('Name');
  64. expect(firstColumn.isHeader).eq(1);
  65. });
  66. it('should throw when columns parameter is not an Array', () => {
  67. expect(() => datamanager.init({
  68. columns: 2
  69. })).to.throw(DataError, 'must be an array');
  70. });
  71. it('should throw when any of the column is not a string or object', () => {
  72. expect(() => datamanager.init({
  73. columns: [2]
  74. })).to.throw(DataError, 'must be a string or an object');
  75. });
  76. it('should not throw given valid params', () => {
  77. expect(() => datamanager.init({
  78. columns: ['Name'],
  79. rows: [['Test']]
  80. })).to.not.throw();
  81. });
  82. it('should properly build column object when editable is false', () => {
  83. const data = {
  84. columns: [
  85. { content: 'Name', editable: false }
  86. ],
  87. rows: [
  88. ['Faris']
  89. ]
  90. };
  91. datamanager.init(data);
  92. const firstColumn = datamanager.getColumn(0);
  93. expect(firstColumn.colIndex).eq(0);
  94. expect(firstColumn.content).eq('Name');
  95. expect(firstColumn.isHeader).eq(1);
  96. });
  97. });
  98. describe('prepareNumericColumns', () => {
  99. const datamanager = getDataManagerInstance();
  100. it('should assign `align: right` to columns with numeric data', () => {
  101. datamanager.init({
  102. columns: ['Name', 'Number'],
  103. rows: [
  104. ['Faris', '123']
  105. ]
  106. });
  107. const column0 = datamanager.getColumn(0);
  108. const column1 = datamanager.getColumn(1);
  109. expect(column0.align).to.not.equal('right');
  110. expect(column1.align).to.equal('right');
  111. });
  112. });
  113. });
  114. function getDataManagerInstance(opts = {}) {
  115. const options = Object.assign({}, {
  116. data: {
  117. columns: ['Name', 'Email', 'Occupation'],
  118. rows: [
  119. ['Faris', 'faris@test.com', 'Software Developer'],
  120. ['Manas', 'manas@test.com', 'Software Engineer'],
  121. ['Ameya', 'ameya@test.com', 'Hacker']
  122. ]
  123. }
  124. }, opts);
  125. const datamanager = new DataManager(options);
  126. datamanager.init();
  127. return datamanager;
  128. }