|
| 1 | +import sortMenu from '../sortMenu'; |
| 2 | + |
| 3 | +function stringify(array) { |
| 4 | + let string = ''; |
| 5 | + array.forEach(obj => { |
| 6 | + string += JSON.stringify(obj); |
| 7 | + }); |
| 8 | + return string; |
| 9 | +} |
| 10 | + |
| 11 | +describe('sortMenu', () => { |
| 12 | + it('modifies original array to follow the group, then name order provided', () => { |
| 13 | + const initialArray = [ |
| 14 | + {props: {group: 'DEV', name: 'Inspector'}}, |
| 15 | + {props: {group: 'DEV', name: 'JSON'}}, |
| 16 | + ]; |
| 17 | + const orderProp = [{group: 'DEV', name: 'JSON'}, {group: 'DEV', name: 'Inspector'}]; |
| 18 | + |
| 19 | + sortMenu(initialArray, orderProp); |
| 20 | + expect(stringify(initialArray)).toBe( |
| 21 | + stringify([{props: {group: 'DEV', name: 'JSON'}}, {props: {group: 'DEV', name: 'Inspector'}}]) |
| 22 | + ); |
| 23 | + }); |
| 24 | + |
| 25 | + it('sorts the array by group, then by name', () => { |
| 26 | + const initialArray = [ |
| 27 | + {props: {group: 'Structure', name: 'Create'}}, |
| 28 | + {props: {group: 'Structure', name: 'Subplots'}}, |
| 29 | + {props: {group: 'Style', name: 'Color Bars'}}, |
| 30 | + {props: {group: 'Style', name: 'Annotation'}}, |
| 31 | + {props: {group: 'DEV', name: 'Inspector'}}, |
| 32 | + {props: {group: 'DEV', name: 'JSON'}}, |
| 33 | + ]; |
| 34 | + const orderProp = [ |
| 35 | + {group: 'DEV', name: 'JSON'}, |
| 36 | + {group: 'DEV', name: 'Inspector'}, |
| 37 | + {group: 'Structure', name: 'Subplots'}, |
| 38 | + {group: 'Structure', name: 'Create'}, |
| 39 | + {group: 'Style', name: 'Color Bars'}, |
| 40 | + {group: 'Style', name: 'Annotation'}, |
| 41 | + ]; |
| 42 | + |
| 43 | + sortMenu(initialArray, orderProp); |
| 44 | + expect(stringify(initialArray)).toBe( |
| 45 | + stringify([ |
| 46 | + {props: {group: 'DEV', name: 'JSON'}}, |
| 47 | + {props: {group: 'DEV', name: 'Inspector'}}, |
| 48 | + {props: {group: 'Structure', name: 'Subplots'}}, |
| 49 | + {props: {group: 'Structure', name: 'Create'}}, |
| 50 | + {props: {group: 'Style', name: 'Color Bars'}}, |
| 51 | + {props: {group: 'Style', name: 'Annotation'}}, |
| 52 | + ]) |
| 53 | + ); |
| 54 | + }); |
| 55 | + |
| 56 | + it('puts not mentionned panels to the bottom of list and sorts alphabetically', () => { |
| 57 | + const initialArray = [ |
| 58 | + {props: {group: 'DEV', name: 'JSON'}}, |
| 59 | + {props: {group: 'DEV', name: 'Inspector'}}, |
| 60 | + {props: {group: 'Structure', name: 'Create'}}, |
| 61 | + {props: {group: 'Structure', name: 'Subplots'}}, |
| 62 | + {props: {group: 'Style', name: 'Color Bars'}}, |
| 63 | + {props: {group: 'Style', name: 'Annotation'}}, |
| 64 | + ]; |
| 65 | + const orderProp = [ |
| 66 | + {group: 'Structure', name: 'Subplots'}, |
| 67 | + {group: 'Structure', name: 'Create'}, |
| 68 | + {group: 'Style', name: 'Color Bars'}, |
| 69 | + {group: 'Style', name: 'Annotation'}, |
| 70 | + ]; |
| 71 | + |
| 72 | + sortMenu(initialArray, orderProp); |
| 73 | + expect(stringify(initialArray)).toBe( |
| 74 | + stringify([ |
| 75 | + {props: {group: 'Structure', name: 'Subplots'}}, |
| 76 | + {props: {group: 'Structure', name: 'Create'}}, |
| 77 | + {props: {group: 'Style', name: 'Color Bars'}}, |
| 78 | + {props: {group: 'Style', name: 'Annotation'}}, |
| 79 | + {props: {group: 'DEV', name: 'Inspector'}}, |
| 80 | + {props: {group: 'DEV', name: 'JSON'}}, |
| 81 | + ]) |
| 82 | + ); |
| 83 | + }); |
| 84 | + |
| 85 | + it('orders not mentionned subpanels at the end, alphabetically', () => { |
| 86 | + const initialArray = [ |
| 87 | + {props: {group: 'Style', name: 'General'}}, |
| 88 | + {props: {group: 'Style', name: 'Traces'}}, |
| 89 | + {props: {group: 'Style', name: 'Axes'}}, |
| 90 | + {props: {group: 'Structure', name: 'Create'}}, |
| 91 | + ]; |
| 92 | + const orderProp = [{group: 'Style', name: 'Traces'}]; |
| 93 | + |
| 94 | + sortMenu(initialArray, orderProp); |
| 95 | + expect(stringify(initialArray)).toBe( |
| 96 | + stringify([ |
| 97 | + {props: {group: 'Style', name: 'Traces'}}, |
| 98 | + {props: {group: 'Style', name: 'Axes'}}, |
| 99 | + {props: {group: 'Style', name: 'General'}}, |
| 100 | + {props: {group: 'Structure', name: 'Create'}}, |
| 101 | + ]) |
| 102 | + ); |
| 103 | + }); |
| 104 | + |
| 105 | + it('ignores non existent panel groups', () => { |
| 106 | + const initialArray = [ |
| 107 | + {props: {group: 'Structure', name: 'Create'}}, |
| 108 | + {props: {group: 'Structure', name: 'Subplots'}}, |
| 109 | + {props: {group: 'Style', name: 'Color Bars'}}, |
| 110 | + {props: {group: 'Style', name: 'Annotation'}}, |
| 111 | + ]; |
| 112 | + |
| 113 | + const orderProp = [ |
| 114 | + {group: 'Non Existent', name: 'Subplots'}, |
| 115 | + {group: 'Structure', name: 'Create'}, |
| 116 | + {group: 'Style', name: 'Color Bars'}, |
| 117 | + {group: 'Style', name: 'Annotation'}, |
| 118 | + ]; |
| 119 | + |
| 120 | + sortMenu(initialArray, orderProp); |
| 121 | + expect(stringify(initialArray)).toBe( |
| 122 | + stringify([ |
| 123 | + {props: {group: 'Structure', name: 'Create'}}, |
| 124 | + {props: {group: 'Structure', name: 'Subplots'}}, |
| 125 | + {props: {group: 'Style', name: 'Color Bars'}}, |
| 126 | + {props: {group: 'Style', name: 'Annotation'}}, |
| 127 | + ]) |
| 128 | + ); |
| 129 | + }); |
| 130 | + |
| 131 | + it('ignores non existent panel names', () => { |
| 132 | + const initialArray = [ |
| 133 | + {props: {group: 'Structure', name: 'Subplots'}}, |
| 134 | + {props: {group: 'Structure', name: 'Create'}}, |
| 135 | + {props: {group: 'Style', name: 'Color Bars'}}, |
| 136 | + {props: {group: 'Style', name: 'Annotation'}}, |
| 137 | + ]; |
| 138 | + |
| 139 | + const orderProp = [ |
| 140 | + {group: 'Structure', name: 'Non Existent'}, |
| 141 | + {group: 'Style', name: 'Color Bars'}, |
| 142 | + {group: 'Style', name: 'Annotation'}, |
| 143 | + ]; |
| 144 | + |
| 145 | + sortMenu(initialArray, orderProp); |
| 146 | + expect(stringify(initialArray)).toBe( |
| 147 | + stringify([ |
| 148 | + {props: {group: 'Style', name: 'Color Bars'}}, |
| 149 | + {props: {group: 'Style', name: 'Annotation'}}, |
| 150 | + {props: {group: 'Structure', name: 'Create'}}, |
| 151 | + {props: {group: 'Structure', name: 'Subplots'}}, |
| 152 | + ]) |
| 153 | + ); |
| 154 | + }); |
| 155 | + |
| 156 | + it('ignores invalid combinations', () => { |
| 157 | + const initialArray = [ |
| 158 | + {props: {group: 'Structure', name: 'Subplots'}}, |
| 159 | + {props: {group: 'Structure', name: 'Create'}}, |
| 160 | + {props: {group: 'Style', name: 'Color Bars'}}, |
| 161 | + {props: {group: 'Style', name: 'Annotation'}}, |
| 162 | + ]; |
| 163 | + |
| 164 | + const orderProp = [ |
| 165 | + {group: 'Structure', name: 'Annotation'}, |
| 166 | + {group: 'Style', name: 'Color Bars'}, |
| 167 | + {group: 'Style', name: 'Annotation'}, |
| 168 | + ]; |
| 169 | + |
| 170 | + sortMenu(initialArray, orderProp); |
| 171 | + expect(stringify(initialArray)).toBe( |
| 172 | + stringify([ |
| 173 | + {props: {group: 'Style', name: 'Color Bars'}}, |
| 174 | + {props: {group: 'Style', name: 'Annotation'}}, |
| 175 | + {props: {group: 'Structure', name: 'Create'}}, |
| 176 | + {props: {group: 'Structure', name: 'Subplots'}}, |
| 177 | + ]) |
| 178 | + ); |
| 179 | + }); |
| 180 | +}); |
0 commit comments