Akron | b7aab87 | 2021-11-04 17:11:04 +0100 | [diff] [blame] | 1 | define(['state','state/manager'], function (stateClass, stateManagerClass) { |
Akron | 308a603 | 2019-12-05 16:27:34 +0100 | [diff] [blame] | 2 | |
| 3 | describe('KorAP.State', function () { |
| 4 | it('should be initializable', function () { |
| 5 | let s = stateClass.create(); |
| 6 | expect(s.get()).toBeFalsy(); |
| 7 | |
| 8 | s = stateClass.create(true); |
| 9 | expect(s.get()).toBeTruthy(); |
| 10 | }); |
| 11 | |
| 12 | it('should be settable and gettable', function () { |
| 13 | let s = stateClass.create(); |
| 14 | expect(s.get()).toBeFalsy(); |
| 15 | s.set(true); |
| 16 | expect(s.get()).toBeTruthy(); |
| 17 | }); |
| 18 | |
Akron | 72c1c9d | 2021-11-05 10:46:34 +0100 | [diff] [blame] | 19 | it('should accept a default value', function () { |
| 20 | let s = stateClass.create([true, false]); |
| 21 | expect(s.get()).toBeTruthy(); |
| 22 | s.set(false); |
| 23 | expect(s.get()).toBeFalsy(); |
| 24 | |
| 25 | s = stateClass.create([true, false]); |
| 26 | s.setIfNotYet(false); |
| 27 | expect(s.get()).toBeFalsy(); |
| 28 | |
| 29 | s.setIfNotYet(true); |
| 30 | expect(s.get()).toBeFalsy(); |
| 31 | }); |
| 32 | |
Akron | 308a603 | 2019-12-05 16:27:34 +0100 | [diff] [blame] | 33 | it('should be associatable', function () { |
| 34 | let s = stateClass.create(); |
| 35 | |
| 36 | // Create |
| 37 | let obj1 = { |
| 38 | x : false, |
| 39 | setState : function (value) { |
| 40 | this.x = value; |
| 41 | } |
| 42 | }; |
| 43 | |
| 44 | // Create |
| 45 | let obj2 = { |
| 46 | x : true, |
| 47 | setState : function (value) { |
| 48 | this.x = value; |
| 49 | } |
| 50 | }; |
Akron | b69cbf1 | 2020-10-01 13:04:44 +0200 | [diff] [blame] | 51 | |
| 52 | expect(s.associates()).toEqual(0); |
Akron | 308a603 | 2019-12-05 16:27:34 +0100 | [diff] [blame] | 53 | expect(s.get()).toBeFalsy(); |
| 54 | expect(obj1.x).toBeFalsy(); |
| 55 | expect(obj2.x).toBeTruthy(); |
| 56 | |
| 57 | // Associate object with state |
| 58 | s.associate(obj1); |
Akron | b69cbf1 | 2020-10-01 13:04:44 +0200 | [diff] [blame] | 59 | expect(s.associates()).toEqual(1); |
Akron | 308a603 | 2019-12-05 16:27:34 +0100 | [diff] [blame] | 60 | s.associate(obj2); |
Akron | b69cbf1 | 2020-10-01 13:04:44 +0200 | [diff] [blame] | 61 | expect(s.associates()).toEqual(2); |
Akron | 308a603 | 2019-12-05 16:27:34 +0100 | [diff] [blame] | 62 | |
| 63 | expect(s.get()).toBeFalsy(); |
| 64 | expect(obj1.x).toBeFalsy(); |
| 65 | expect(obj2.x).toBeFalsy(); |
| 66 | |
| 67 | s.set(true); |
| 68 | |
| 69 | expect(s.get()).toBeTruthy(); |
| 70 | expect(obj1.x).toBeTruthy(); |
| 71 | expect(obj2.x).toBeTruthy(); |
| 72 | }); |
Akron | 38ed5dc | 2020-10-01 17:33:00 +0200 | [diff] [blame] | 73 | |
| 74 | it('should be clearable', function () { |
| 75 | let s = stateClass.create(); |
| 76 | |
| 77 | // Create |
| 78 | let obj1 = { |
| 79 | x : false, |
| 80 | setState : function (value) { |
| 81 | this.x = value; |
| 82 | } |
| 83 | }; |
| 84 | |
| 85 | // Create |
| 86 | let obj2 = { |
| 87 | x : true, |
| 88 | setState : function (value) { |
| 89 | this.x = value; |
| 90 | } |
| 91 | }; |
| 92 | |
| 93 | expect(s.associates()).toEqual(0); |
| 94 | expect(s.get()).toBeFalsy(); |
| 95 | expect(obj1.x).toBeFalsy(); |
| 96 | expect(obj2.x).toBeTruthy(); |
| 97 | |
| 98 | // Associate object with state |
| 99 | s.associate(obj1); |
| 100 | expect(s.associates()).toEqual(1); |
| 101 | s.associate(obj2); |
| 102 | expect(s.associates()).toEqual(2); |
| 103 | |
| 104 | expect(s.get()).toBeFalsy(); |
| 105 | expect(obj1.x).toBeFalsy(); |
| 106 | expect(obj2.x).toBeFalsy(); |
| 107 | |
| 108 | s.clear(); |
| 109 | |
| 110 | s.set(true); |
| 111 | expect(s.get()).toBeTruthy(); |
| 112 | expect(obj1.x).toBeFalsy(); |
| 113 | expect(obj2.x).toBeFalsy(); |
| 114 | |
| 115 | s.set(false); |
| 116 | expect(s.get()).toBeFalsy(); |
| 117 | expect(obj1.x).toBeFalsy(); |
| 118 | expect(obj2.x).toBeFalsy(); |
| 119 | }); |
Akron | 237abc4 | 2020-10-07 14:14:52 +0200 | [diff] [blame] | 120 | |
| 121 | it('should roll', function () { |
| 122 | let s = stateClass.create(['der','alte','mann']); |
| 123 | |
| 124 | expect(s.get()).toEqual('der'); |
| 125 | s.roll(); |
| 126 | expect(s.get()).toEqual('alte'); |
| 127 | s.roll(); |
| 128 | expect(s.get()).toEqual('mann'); |
| 129 | s.roll(); |
| 130 | expect(s.get()).toEqual('der'); |
| 131 | s.roll(); |
| 132 | expect(s.get()).toEqual('alte'); |
| 133 | |
| 134 | s.set('alte'); |
| 135 | expect(s.get()).toEqual('alte'); |
| 136 | s.roll(); |
| 137 | expect(s.get()).toEqual('mann'); |
| 138 | }); |
Akron | 308a603 | 2019-12-05 16:27:34 +0100 | [diff] [blame] | 139 | }); |
Akron | b7aab87 | 2021-11-04 17:11:04 +0100 | [diff] [blame] | 140 | |
| 141 | describe('KorAP.State.Manager', function () { |
| 142 | |
Akron | b7aab87 | 2021-11-04 17:11:04 +0100 | [diff] [blame] | 143 | it('should be initializable', function () { |
| 144 | |
Akron | 25cd928 | 2021-11-22 16:50:27 +0100 | [diff] [blame] | 145 | const el = document.createElement('input'); |
Akron | b7aab87 | 2021-11-04 17:11:04 +0100 | [diff] [blame] | 146 | let sm = stateManagerClass.create(el); |
| 147 | expect(sm).toBeTruthy(); |
| 148 | |
Akron | 25cd928 | 2021-11-22 16:50:27 +0100 | [diff] [blame] | 149 | expect(sm.toString()).toEqual(""); |
Akron | b7aab87 | 2021-11-04 17:11:04 +0100 | [diff] [blame] | 150 | }); |
| 151 | |
| 152 | |
| 153 | it('should be extensible', function () { |
Akron | 25cd928 | 2021-11-22 16:50:27 +0100 | [diff] [blame] | 154 | |
| 155 | const el = document.createElement('input'); |
Akron | b7aab87 | 2021-11-04 17:11:04 +0100 | [diff] [blame] | 156 | const sm = stateManagerClass.create(el); |
| 157 | expect(sm).toBeTruthy(); |
| 158 | |
| 159 | const s1 = sm.newState('test', [1,2,3]); |
| 160 | |
Akron | 25cd928 | 2021-11-22 16:50:27 +0100 | [diff] [blame] | 161 | expect(sm.toString()).toEqual(""); |
Akron | b7aab87 | 2021-11-04 17:11:04 +0100 | [diff] [blame] | 162 | |
| 163 | s1.set(2); |
| 164 | |
Akron | 25cd928 | 2021-11-22 16:50:27 +0100 | [diff] [blame] | 165 | expect(sm.toString()).toEqual("\"test\":2"); |
Akron | b7aab87 | 2021-11-04 17:11:04 +0100 | [diff] [blame] | 166 | |
| 167 | s1.set(3); |
| 168 | |
Akron | 25cd928 | 2021-11-22 16:50:27 +0100 | [diff] [blame] | 169 | expect(sm.toString()).toEqual("\"test\":3"); |
Akron | b7aab87 | 2021-11-04 17:11:04 +0100 | [diff] [blame] | 170 | |
| 171 | const s2 = sm.newState('glemm', [true,false]); |
| 172 | |
Akron | 25cd928 | 2021-11-22 16:50:27 +0100 | [diff] [blame] | 173 | let serial = JSON.parse('{' + sm.toString() + '}'); |
Akron | b7aab87 | 2021-11-04 17:11:04 +0100 | [diff] [blame] | 174 | expect(serial["test"]).toEqual(3); |
| 175 | expect(serial["glemm"]).toBeUndefined(); |
| 176 | |
| 177 | s2.set(false); |
| 178 | |
Akron | 25cd928 | 2021-11-22 16:50:27 +0100 | [diff] [blame] | 179 | serial = JSON.parse('{' + sm.toString() + '}'); |
Akron | b7aab87 | 2021-11-04 17:11:04 +0100 | [diff] [blame] | 180 | expect(serial["test"]).toEqual(3); |
| 181 | expect(serial["glemm"]).toEqual(false); |
| 182 | }); |
Akron | 25cd928 | 2021-11-22 16:50:27 +0100 | [diff] [blame] | 183 | |
Akron | 25cd928 | 2021-11-22 16:50:27 +0100 | [diff] [blame] | 184 | it('should serialize correctly', function () { |
| 185 | const el = document.createElement('input'); |
| 186 | const sm = stateManagerClass.create(el); |
| 187 | expect(sm).toBeTruthy(); |
| 188 | |
| 189 | const s1 = sm.newState('x', [1,2,3]); |
| 190 | |
| 191 | expect(sm.toString()).toEqual(""); |
| 192 | |
| 193 | s1.set(2); |
| 194 | |
| 195 | expect(sm.toString()).toEqual("\"x\":2"); |
| 196 | |
| 197 | const s2 = sm.newState('y', [true,false]); |
| 198 | s2.set(false) |
| 199 | |
| 200 | const s3 = sm.newState('z', ['a','b','c']); |
| 201 | s3.set('b') |
| 202 | |
| 203 | expect(sm.toString().indexOf("\"x\":2")).not.toEqual(-1); |
| 204 | expect(sm.toString().indexOf("\"y\":false")).not.toEqual(-1); |
| 205 | expect(sm.toString().indexOf("\"z\":\"b\"")).not.toEqual(-1); |
| 206 | expect(sm.toString().indexOf("\"a\":\"d\"")).toEqual(-1); |
| 207 | }); |
Akron | a8d45c4 | 2021-11-23 11:31:31 +0100 | [diff] [blame] | 208 | |
| 209 | it('should accept and not serialize default values', function () { |
| 210 | const el = document.createElement('input'); |
| 211 | const sm = stateManagerClass.create(el); |
| 212 | expect(sm).toBeTruthy(); |
| 213 | |
| 214 | const s1 = sm.newState('test', [1,2,3], 1); |
| 215 | |
| 216 | expect(sm.toString()).toEqual(""); |
| 217 | |
| 218 | s1.set(2); |
| 219 | |
| 220 | expect(sm.toString()).toEqual("\"test\":2"); |
| 221 | |
| 222 | s1.set(3); |
| 223 | |
| 224 | expect(sm.toString()).toEqual("\"test\":3"); |
| 225 | |
| 226 | s1.set(1); |
| 227 | |
| 228 | expect(sm.toString()).toEqual(""); |
| 229 | |
| 230 | s1.set(2); |
| 231 | |
| 232 | expect(sm.toString()).toEqual("\"test\":2"); |
| 233 | }); |
Akron | ed223be | 2024-12-10 13:01:46 +0100 | [diff] [blame] | 234 | |
| 235 | it('should load stored states', function () { |
| 236 | const el = document.createElement('input'); |
| 237 | el.setAttribute("value","\"test\":2"); |
| 238 | const sm = stateManagerClass.create(el); |
| 239 | expect(sm).toBeTruthy(); |
| 240 | |
| 241 | const s1 = sm.newState('test', [1,2,3], 1); |
| 242 | |
| 243 | expect(s1.get()).toEqual(2); |
| 244 | |
| 245 | expect(sm.toString()).toEqual("\"test\":2"); |
| 246 | }); |
Akron | b7aab87 | 2021-11-04 17:11:04 +0100 | [diff] [blame] | 247 | }); |
Akron | 308a603 | 2019-12-05 16:27:34 +0100 | [diff] [blame] | 248 | }); |