|
1 | 1 | import { toArray } from "../arrays.js"; |
2 | 2 |
|
3 | 3 | describe("toArray", () => { |
4 | | - test("returns array as-is when input is already an array", () => { |
5 | | - const input = [1, 2, 3]; |
6 | | - expect(toArray(input)).toBe(input); // same reference |
7 | | - }); |
| 4 | + describe("Standard JavaScript arrays", () => { |
| 5 | + test("returns array as-is when input is already an array", () => { |
| 6 | + const input = [1, 2, 3]; |
| 7 | + expect(toArray(input)).toBe(input); // same reference |
| 8 | + }); |
8 | 9 |
|
9 | | - test("converts object values to array", () => { |
10 | | - const input = { a: 1, b: 2, c: 3 }; |
11 | | - expect(toArray(input)).toEqual([1, 2, 3]); |
| 10 | + test("returns empty array for empty array", () => { |
| 11 | + const input = []; |
| 12 | + expect(toArray(input)).toBe(input); |
| 13 | + }); |
12 | 14 | }); |
13 | 15 |
|
14 | | - test("returns empty array for empty object", () => { |
15 | | - expect(toArray({})).toEqual([]); |
| 16 | + describe("Standard YAML array format (from YAML editor)", () => { |
| 17 | + test("converts single configuration object to array", () => { |
| 18 | + const input = { |
| 19 | + sub_button: 2, |
| 20 | + close_on_click: true, |
| 21 | + }; |
| 22 | + expect(toArray(input)).toEqual([{ sub_button: 2, close_on_click: true }]); |
| 23 | + }); |
| 24 | + |
| 25 | + test("wraps nested configuration object in array", () => { |
| 26 | + const input = { |
| 27 | + wheel_opener: "main", |
| 28 | + wheel_buttons: [ |
| 29 | + { sub_button: 2, close_on_click: true }, |
| 30 | + { sub_button: 3, close_on_click: true }, |
| 31 | + ], |
| 32 | + }; |
| 33 | + expect(toArray(input)).toEqual([ |
| 34 | + { |
| 35 | + wheel_opener: "main", |
| 36 | + wheel_buttons: [ |
| 37 | + { sub_button: 2, close_on_click: true }, |
| 38 | + { sub_button: 3, close_on_click: true }, |
| 39 | + ], |
| 40 | + }, |
| 41 | + ]); |
| 42 | + }); |
16 | 43 | }); |
17 | 44 |
|
18 | | - test("returns empty array for null input", () => { |
19 | | - expect(toArray(null)).toEqual([]); |
| 45 | + describe("Bubble Card UI editor format (numbered object keys)", () => { |
| 46 | + test("converts numbered object keys to array", () => { |
| 47 | + const input = { |
| 48 | + 0: { sub_button: 2, close_on_click: true }, |
| 49 | + 1: { sub_button: 3, close_on_click: true }, |
| 50 | + }; |
| 51 | + expect(toArray(input)).toEqual([ |
| 52 | + { sub_button: 2, close_on_click: true }, |
| 53 | + { sub_button: 3, close_on_click: true }, |
| 54 | + ]); |
| 55 | + }); |
| 56 | + |
| 57 | + test("converts numbered object keys with gaps to array", () => { |
| 58 | + const input = { |
| 59 | + 0: { sub_button: 1, close_on_click: true }, |
| 60 | + 2: { sub_button: 3, close_on_click: false }, |
| 61 | + 4: { sub_button: 5, close_on_click: true }, |
| 62 | + }; |
| 63 | + expect(toArray(input)).toEqual([ |
| 64 | + { sub_button: 1, close_on_click: true }, |
| 65 | + { sub_button: 3, close_on_click: false }, |
| 66 | + { sub_button: 5, close_on_click: true }, |
| 67 | + ]); |
| 68 | + }); |
| 69 | + |
| 70 | + test("handles single numbered key as array", () => { |
| 71 | + const input = { |
| 72 | + 0: { sub_button: 1, close_on_click: true }, |
| 73 | + }; |
| 74 | + expect(toArray(input)).toEqual([{ sub_button: 1, close_on_click: true }]); |
| 75 | + }); |
| 76 | + |
| 77 | + test("handles non-sequential numbered keys", () => { |
| 78 | + const input = { |
| 79 | + 5: { sub_button: 6 }, |
| 80 | + 1: { sub_button: 2 }, |
| 81 | + 3: { sub_button: 4 }, |
| 82 | + }; |
| 83 | + // Should preserve order by key value when converting |
| 84 | + expect(toArray(input)).toEqual([{ sub_button: 2 }, { sub_button: 4 }, { sub_button: 6 }]); |
| 85 | + }); |
20 | 86 | }); |
21 | 87 |
|
22 | | - test("returns empty array for undefined input", () => { |
23 | | - expect(toArray(undefined)).toEqual([]); |
| 88 | + describe("Mixed and edge cases", () => { |
| 89 | + test("handles object with mixed key types - treats as single object", () => { |
| 90 | + const input = { |
| 91 | + 0: { sub_button: 1 }, |
| 92 | + name: "test", |
| 93 | + wheel_opener: "main", |
| 94 | + }; |
| 95 | + // Mixed keys should be treated as single object, not numbered array |
| 96 | + expect(toArray(input)).toEqual([ |
| 97 | + { |
| 98 | + 0: { sub_button: 1 }, |
| 99 | + name: "test", |
| 100 | + wheel_opener: "main", |
| 101 | + }, |
| 102 | + ]); |
| 103 | + }); |
| 104 | + |
| 105 | + test("returns empty array for null input", () => { |
| 106 | + expect(toArray(null)).toEqual([]); |
| 107 | + }); |
| 108 | + |
| 109 | + test("returns empty array for undefined input", () => { |
| 110 | + expect(toArray(undefined)).toEqual([]); |
| 111 | + }); |
| 112 | + |
| 113 | + test("returns empty array for non-object, non-array values", () => { |
| 114 | + expect(toArray(42)).toEqual([]); |
| 115 | + expect(toArray("hello")).toEqual([]); |
| 116 | + expect(toArray(true)).toEqual([]); |
| 117 | + }); |
| 118 | + |
| 119 | + test("returns single array for empty object", () => { |
| 120 | + expect(toArray({})).toEqual([{}]); |
| 121 | + }); |
24 | 122 | }); |
25 | 123 |
|
26 | | - test("returns empty array for non-object, non-array values", () => { |
27 | | - expect(toArray(42)).toEqual([]); |
28 | | - expect(toArray("hello")).toEqual([]); |
29 | | - expect(toArray(true)).toEqual([]); |
| 124 | + describe("Real-world bubble card configurations", () => { |
| 125 | + test("handles wheel_buttons from YAML editor", () => { |
| 126 | + const input = [ |
| 127 | + { sub_button: 2, close_on_click: true }, |
| 128 | + { sub_button: 3, close_on_click: true }, |
| 129 | + ]; |
| 130 | + expect(toArray(input)).toBe(input); |
| 131 | + }); |
| 132 | + |
| 133 | + test("handles wheel_buttons from UI editor", () => { |
| 134 | + const input = { |
| 135 | + 0: { sub_button: 2, close_on_click: true }, |
| 136 | + 1: { sub_button: 3, close_on_click: true }, |
| 137 | + }; |
| 138 | + expect(toArray(input)).toEqual([ |
| 139 | + { sub_button: 2, close_on_click: true }, |
| 140 | + { sub_button: 3, close_on_click: true }, |
| 141 | + ]); |
| 142 | + }); |
| 143 | + |
| 144 | + test("handles single wheel configuration from YAML editor", () => { |
| 145 | + const input = { |
| 146 | + wheel_opener: "main", |
| 147 | + wheel_buttons: [{ sub_button: 1 }, { sub_button: 2 }], |
| 148 | + }; |
| 149 | + expect(toArray(input)).toEqual([input]); |
| 150 | + }); |
| 151 | + |
| 152 | + test("handles multiple wheel configurations from UI editor", () => { |
| 153 | + const input = { |
| 154 | + 0: { |
| 155 | + wheel_opener: "main", |
| 156 | + wheel_buttons: [{ sub_button: 1 }, { sub_button: 2 }], |
| 157 | + }, |
| 158 | + 1: { |
| 159 | + wheel_opener: "3", |
| 160 | + wheel_buttons: [{ sub_button: 4 }, { sub_button: 5 }], |
| 161 | + }, |
| 162 | + }; |
| 163 | + expect(toArray(input)).toEqual([ |
| 164 | + { |
| 165 | + wheel_opener: "main", |
| 166 | + wheel_buttons: [{ sub_button: 1 }, { sub_button: 2 }], |
| 167 | + }, |
| 168 | + { |
| 169 | + wheel_opener: "3", |
| 170 | + wheel_buttons: [{ sub_button: 4 }, { sub_button: 5 }], |
| 171 | + }, |
| 172 | + ]); |
| 173 | + }); |
30 | 174 | }); |
31 | 175 | }); |
0 commit comments