| 1 |
import { isRowExpanded } from './utils'; |
| 2 |
import { buildExpandableRows } from '../../../test-helpers/data-helpers'; |
| 3 |
import { IRow } from '../TableTypes'; |
| 4 |
|
| 5 |
const assertExpanded = (rows: IRow[], expandedRowIndexes: number[]) => { |
| 6 |
expandedRowIndexes.forEach((expandedRowIndex: number) => { |
| 7 |
const expandedRow = rows[expandedRowIndex]; |
| 8 |
expect(isRowExpanded(expandedRow, rows)).toBeTruthy(); |
| 9 |
}); |
| 10 |
}; |
| 11 |
|
| 12 |
const assertNotExpanded = (rows: IRow[], notExpandedRowIndexes: number[]) => { |
| 13 |
notExpandedRowIndexes.forEach((expandedRowIndex: number) => { |
| 14 |
const expandedRow = rows[expandedRowIndex]; |
| 15 |
expect(isRowExpanded(expandedRow, rows)).toBeFalsy(); |
| 16 |
}); |
| 17 |
}; |
| 18 |
|
| 19 |
describe('Util functions', () => { |
| 20 |
describe('isRowExpanded', () => { |
| 21 |
test('basic', () => { |
| 22 |
const rows = buildExpandableRows({ 6: 5 }, [5]); |
| 23 |
assertExpanded(rows, [6]); |
| 24 |
assertNotExpanded(rows, [0, 1, 5, 9]); |
| 25 |
}); |
| 26 |
test('two open, one closed', () => { |
| 27 |
const rows = buildExpandableRows({ 1: 0, 3: 2, 5: 4 }, [0, 2]); |
| 28 |
assertExpanded(rows, [1, 3]); |
| 29 |
assertNotExpanded(rows, [0, 2, 4, 5, 8]); |
| 30 |
}); |
| 31 |
test('all closed', () => { |
| 32 |
const rows = buildExpandableRows({ 1: 0, 3: 2, 5: 4 }); |
| 33 |
assertNotExpanded(rows, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]); |
| 34 |
}); |
| 35 |
test('parent-child chain', () => { |
| 36 |
const rows = buildExpandableRows( |
| 37 |
{ |
| 38 |
1: 0, |
| 39 |
2: 1, |
| 40 |
3: 2, |
| 41 |
4: 3, |
| 42 |
5: 4, |
| 43 |
6: 5, |
| 44 |
7: 6 |
| 45 |
}, |
| 46 |
[0, 1, 2, 3, 4, 5, 6] |
| 47 |
); |
| 48 |
assertExpanded(rows, [1, 2, 3, 4, 5, 6, 7]); |
| 49 |
assertNotExpanded(rows, [0, 8, 9]); |
| 50 |
}); |
| 51 |
test('two parent-child chains', () => { |
| 52 |
const rows = buildExpandableRows( |
| 53 |
{ |
| 54 |
1: 0, |
| 55 |
3: 1, |
| 56 |
4: 2, |
| 57 |
5: 3, |
| 58 |
6: 4, |
| 59 |
7: 6 |
| 60 |
}, |
| 61 |
[0, 1, 2, 3, 4, 5, 6] |
| 62 |
); |
| 63 |
assertExpanded(rows, [1, 3, 4, 5, 6, 7]); |
| 64 |
assertNotExpanded(rows, [0, 2, 8, 9]); |
| 65 |
}); |
| 66 |
test('not in order', () => { |
| 67 |
const rows = buildExpandableRows( |
| 68 |
{ |
| 69 |
0: 1, |
| 70 |
1: 4 |
| 71 |
}, |
| 72 |
[4, 1] |
| 73 |
); |
| 74 |
assertExpanded(rows, [0, 1]); |
| 75 |
assertNotExpanded(rows, [2, 3, 4, 5]); |
| 76 |
}); |
| 77 |
test('child parent circle', () => { |
| 78 |
const rows = buildExpandableRows({ 1: 0, 0: 1 }, [0, 1]); |
| 79 |
assertNotExpanded(rows, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]); |
| 80 |
}); |
| 81 |
test('undefined without a parent', () => { |
| 82 |
const rows = buildExpandableRows(); |
| 83 |
expect(isRowExpanded(rows[1], rows)).toBeUndefined(); |
| 84 |
}); |
| 85 |
test('compound parent', () => { |
| 86 |
const rows = [ |
| 87 |
{ |
| 88 |
isOpen: true, |
| 89 |
cells: [ |
| 90 |
'1', |
| 91 |
{ |
| 92 |
title: '2', |
| 93 |
props: { |
| 94 |
isOpen: true |
| 95 |
} |
| 96 |
}, |
| 97 |
{ |
| 98 |
title: '3', |
| 99 |
props: { |
| 100 |
isOpen: false |
| 101 |
} |
| 102 |
}, |
| 103 |
{ |
| 104 |
title: '4', |
| 105 |
props: { |
| 106 |
isOpen: false |
| 107 |
} |
| 108 |
}, |
| 109 |
'5', |
| 110 |
'6' |
| 111 |
] |
| 112 |
}, |
| 113 |
{ |
| 114 |
parent: 0, |
| 115 |
compoundParent: 1, |
| 116 |
cells: [ |
| 117 |
{ |
| 118 |
title: 'parent 0 compound child - 1' |
| 119 |
} |
| 120 |
] |
| 121 |
}, |
| 122 |
{ |
| 123 |
parent: 0, |
| 124 |
compoundParent: 2, |
| 125 |
cells: [ |
| 126 |
{ |
| 127 |
title: 'parent 0 compound child - 2' |
| 128 |
} |
| 129 |
] |
| 130 |
}, |
| 131 |
{ |
| 132 |
parent: 0, |
| 133 |
compoundParent: 3, |
| 134 |
cells: [ |
| 135 |
{ |
| 136 |
title: 'parent 0 compound child - 3' |
| 137 |
} |
| 138 |
] |
| 139 |
}, |
| 140 |
{ |
| 141 |
isOpen: false, |
| 142 |
cells: [ |
| 143 |
'1', |
| 144 |
{ |
| 145 |
title: '2', |
| 146 |
props: { |
| 147 |
isOpen: false |
| 148 |
} |
| 149 |
}, |
| 150 |
{ |
| 151 |
title: '3', |
| 152 |
props: { |
| 153 |
isOpen: false |
| 154 |
} |
| 155 |
}, |
| 156 |
{ |
| 157 |
title: '4', |
| 158 |
props: { |
| 159 |
isOpen: false |
| 160 |
} |
| 161 |
}, |
| 162 |
'5', |
| 163 |
'6' |
| 164 |
] |
| 165 |
}, |
| 166 |
{ |
| 167 |
parent: 4, |
| 168 |
compoundParent: 1, |
| 169 |
cells: [ |
| 170 |
{ |
| 171 |
title: 'parent 4 compound child - 1' |
| 172 |
} |
| 173 |
] |
| 174 |
}, |
| 175 |
{ |
| 176 |
parent: 4, |
| 177 |
compoundParent: 2, |
| 178 |
cells: [ |
| 179 |
{ |
| 180 |
title: 'parent 4 compound child - 2' |
| 181 |
} |
| 182 |
] |
| 183 |
}, |
| 184 |
{ |
| 185 |
parent: 4, |
| 186 |
compoundParent: 3, |
| 187 |
cells: [ |
| 188 |
{ |
| 189 |
title: 'parent 4 compound child - 3' |
| 190 |
} |
| 191 |
] |
| 192 |
} |
| 193 |
]; |
| 194 |
|
| 195 |
// test an expanded compound child to be expanded |
| 196 |
const expandedRow = { |
| 197 |
parent: 0, |
| 198 |
compoundParent: 1, |
| 199 |
cells: [ |
| 200 |
{ |
| 201 |
title: 'parent 0 compound child - 1' |
| 202 |
} |
| 203 |
] |
| 204 |
}; |
| 205 |
expect(isRowExpanded(expandedRow, rows)).toEqual(true); |
| 206 |
|
| 207 |
// test a collapsed compound child to be false |
| 208 |
const collapsedRow = { |
| 209 |
parent: 0, |
| 210 |
compoundParent: 2, |
| 211 |
cells: [ |
| 212 |
{ |
| 213 |
title: 'parent 0 compound child - 2' |
| 214 |
} |
| 215 |
] |
| 216 |
}; |
| 217 |
expect(isRowExpanded(collapsedRow, rows)).toEqual(false); |
| 218 |
|
| 219 |
// test a row with different parent that is collapsed |
| 220 |
const collapsedRowParent4 = { |
| 221 |
parent: 4, |
| 222 |
compoundParent: 1, |
| 223 |
cells: [ |
| 224 |
{ |
| 225 |
title: 'parent 4 compound child - 1' |
| 226 |
} |
| 227 |
] |
| 228 |
}; |
| 229 |
expect(isRowExpanded(collapsedRowParent4, rows)).toEqual(false); |
| 230 |
}); |
| 231 |
}); |
| 232 |
}); |