TestGroup.tsx 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. import {
  2. assert,
  3. assertNumberOfRowsInGroup,
  4. createSingleSelectOptions,
  5. createTestDatabaseView,
  6. openTestDatabase,
  7. } from './DatabaseTestHelper';
  8. import { FieldType, ViewLayoutTypePB } from '../../../services/backend';
  9. import React from 'react';
  10. export const TestAllKanbanTests = () => {
  11. async function run() {
  12. await createBuildInBoard();
  13. await createKanbanBoardRow();
  14. await moveKanbanBoardRow();
  15. await createKanbanBoardColumn();
  16. await createColumnInBoard();
  17. }
  18. return (
  19. <React.Fragment>
  20. <div>
  21. <button className='rounded-md bg-red-400 p-4' type='button' onClick={() => run()}>
  22. Run all kanban board tests
  23. </button>
  24. </div>
  25. </React.Fragment>
  26. );
  27. };
  28. async function createBuildInBoard() {
  29. const view = await createTestDatabaseView(ViewLayoutTypePB.Board);
  30. const databaseController = await openTestDatabase(view.id);
  31. databaseController.subscribe({
  32. onGroupByField: (groups) => {
  33. console.log(groups);
  34. if (groups.length !== 4) {
  35. throw Error('The build-in board should have 4 groups');
  36. }
  37. assert(groups[0].rows.length === 0, 'The no status group should have 0 rows');
  38. assert(groups[1].rows.length === 3, 'The first group should have 3 rows');
  39. assert(groups[2].rows.length === 0, 'The second group should have 0 rows');
  40. assert(groups[3].rows.length === 0, 'The third group should have 0 rows');
  41. },
  42. });
  43. await databaseController.open().then((result) => result.unwrap());
  44. await databaseController.dispose();
  45. }
  46. async function createKanbanBoardRow() {
  47. const view = await createTestDatabaseView(ViewLayoutTypePB.Board);
  48. const databaseController = await openTestDatabase(view.id);
  49. await databaseController.open().then((result) => result.unwrap());
  50. // Create row in no status group
  51. const noStatusGroup = databaseController.groups.getValue()[0];
  52. await noStatusGroup.createRow().then((result) => result.unwrap());
  53. await assertNumberOfRowsInGroup(view.id, noStatusGroup.groupId, 1);
  54. await databaseController.dispose();
  55. }
  56. async function moveKanbanBoardRow() {
  57. const view = await createTestDatabaseView(ViewLayoutTypePB.Board);
  58. const databaseController = await openTestDatabase(view.id);
  59. await databaseController.open().then((result) => result.unwrap());
  60. // Create row in no status group
  61. const firstGroup = databaseController.groups.getValue()[1];
  62. const secondGroup = databaseController.groups.getValue()[2];
  63. // subscribe the group changes
  64. firstGroup.subscribe({
  65. onRemoveRow: (groupId, deleteRowId) => {
  66. console.log(groupId + 'did remove:' + deleteRowId);
  67. },
  68. onInsertRow: (groupId, rowPB) => {
  69. console.log(groupId + 'did insert:' + rowPB.id);
  70. },
  71. onUpdateRow: (groupId, rowPB) => {
  72. console.log(groupId + 'did update:' + rowPB.id);
  73. },
  74. onCreateRow: (groupId, rowPB) => {
  75. console.log(groupId + 'did create:' + rowPB.id);
  76. },
  77. });
  78. secondGroup.subscribe({
  79. onRemoveRow: (groupId, deleteRowId) => {
  80. console.log(groupId + 'did remove:' + deleteRowId);
  81. },
  82. onInsertRow: (groupId, rowPB) => {
  83. console.log(groupId + 'did insert:' + rowPB.id);
  84. },
  85. onUpdateRow: (groupId, rowPB) => {
  86. console.log(groupId + 'did update:' + rowPB.id);
  87. },
  88. onCreateRow: (groupId, rowPB) => {
  89. console.log(groupId + 'did create:' + rowPB.id);
  90. },
  91. });
  92. const row = firstGroup.rowAtIndex(0).unwrap();
  93. await databaseController.moveRow(row.id, secondGroup.groupId);
  94. assert(firstGroup.rows.length === 2);
  95. await assertNumberOfRowsInGroup(view.id, firstGroup.groupId, 2);
  96. assert(secondGroup.rows.length === 1);
  97. await assertNumberOfRowsInGroup(view.id, secondGroup.groupId, 1);
  98. await databaseController.dispose();
  99. }
  100. async function createKanbanBoardColumn() {
  101. const view = await createTestDatabaseView(ViewLayoutTypePB.Board);
  102. const databaseController = await openTestDatabase(view.id);
  103. await databaseController.open().then((result) => result.unwrap());
  104. // Create row in no status group
  105. const firstGroup = databaseController.groups.getValue()[1];
  106. const secondGroup = databaseController.groups.getValue()[2];
  107. await databaseController.moveGroup(firstGroup.groupId, secondGroup.groupId);
  108. assert(databaseController.groups.getValue()[1].groupId === secondGroup.groupId);
  109. assert(databaseController.groups.getValue()[2].groupId === firstGroup.groupId);
  110. await databaseController.dispose();
  111. }
  112. async function createColumnInBoard() {
  113. const view = await createTestDatabaseView(ViewLayoutTypePB.Board);
  114. const databaseController = await openTestDatabase(view.id);
  115. await databaseController.open().then((result) => result.unwrap());
  116. // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
  117. const singleSelect = databaseController.fieldController.fieldInfos.find(
  118. (fieldInfo) => fieldInfo.field.field_type === FieldType.SingleSelect
  119. )!;
  120. // Create a option which will cause creating a new group
  121. const name = 'New column';
  122. await createSingleSelectOptions(view.id, singleSelect, [name]);
  123. // Wait the backend posting the notification to update the groups
  124. await new Promise((resolve) => setTimeout(resolve, 200));
  125. assert(databaseController.groups.value.length === 5, 'expect number of groups is 5');
  126. assert(databaseController.groups.value[4].name === name, 'expect the last group name is ' + name);
  127. await databaseController.dispose();
  128. }
  129. export const TestCreateKanbanBoard = () => {
  130. return TestButton('Test create build-in board', createBuildInBoard);
  131. };
  132. export const TestCreateKanbanBoardRowInNoStatusGroup = () => {
  133. return TestButton('Test create row in build-in kanban board', createKanbanBoardRow);
  134. };
  135. export const TestMoveKanbanBoardRow = () => {
  136. return TestButton('Test move row in build-in kanban board', moveKanbanBoardRow);
  137. };
  138. export const TestMoveKanbanBoardColumn = () => {
  139. return TestButton('Test move column in build-in kanban board', createKanbanBoardColumn);
  140. };
  141. export const TestCreateKanbanBoardColumn = () => {
  142. return TestButton('Test create column in build-in kanban board', createColumnInBoard);
  143. };
  144. export const TestButton = (title: string, onClick: () => void) => {
  145. return (
  146. <React.Fragment>
  147. <div>
  148. <button className='rounded-md bg-yellow-200 p-4' type='button' onClick={() => onClick()}>
  149. {title}
  150. </button>
  151. </div>
  152. </React.Fragment>
  153. );
  154. };