Akron | 308a603 | 2019-12-05 16:27:34 +0100 | [diff] [blame] | 1 | define(['state'], function (stateClass) { |
| 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 | |
| 19 | it('should be associatable', function () { |
| 20 | let s = stateClass.create(); |
| 21 | |
| 22 | // Create |
| 23 | let obj1 = { |
| 24 | x : false, |
| 25 | setState : function (value) { |
| 26 | this.x = value; |
| 27 | } |
| 28 | }; |
| 29 | |
| 30 | // Create |
| 31 | let obj2 = { |
| 32 | x : true, |
| 33 | setState : function (value) { |
| 34 | this.x = value; |
| 35 | } |
| 36 | }; |
Akron | b69cbf1 | 2020-10-01 13:04:44 +0200 | [diff] [blame] | 37 | |
| 38 | expect(s.associates()).toEqual(0); |
Akron | 308a603 | 2019-12-05 16:27:34 +0100 | [diff] [blame] | 39 | expect(s.get()).toBeFalsy(); |
| 40 | expect(obj1.x).toBeFalsy(); |
| 41 | expect(obj2.x).toBeTruthy(); |
| 42 | |
| 43 | // Associate object with state |
| 44 | s.associate(obj1); |
Akron | b69cbf1 | 2020-10-01 13:04:44 +0200 | [diff] [blame] | 45 | expect(s.associates()).toEqual(1); |
Akron | 308a603 | 2019-12-05 16:27:34 +0100 | [diff] [blame] | 46 | s.associate(obj2); |
Akron | b69cbf1 | 2020-10-01 13:04:44 +0200 | [diff] [blame] | 47 | expect(s.associates()).toEqual(2); |
Akron | 308a603 | 2019-12-05 16:27:34 +0100 | [diff] [blame] | 48 | |
| 49 | expect(s.get()).toBeFalsy(); |
| 50 | expect(obj1.x).toBeFalsy(); |
| 51 | expect(obj2.x).toBeFalsy(); |
| 52 | |
| 53 | s.set(true); |
| 54 | |
| 55 | expect(s.get()).toBeTruthy(); |
| 56 | expect(obj1.x).toBeTruthy(); |
| 57 | expect(obj2.x).toBeTruthy(); |
| 58 | }); |
Akron | 38ed5dc | 2020-10-01 17:33:00 +0200 | [diff] [blame^] | 59 | |
| 60 | it('should be clearable', function () { |
| 61 | let s = stateClass.create(); |
| 62 | |
| 63 | // Create |
| 64 | let obj1 = { |
| 65 | x : false, |
| 66 | setState : function (value) { |
| 67 | this.x = value; |
| 68 | } |
| 69 | }; |
| 70 | |
| 71 | // Create |
| 72 | let obj2 = { |
| 73 | x : true, |
| 74 | setState : function (value) { |
| 75 | this.x = value; |
| 76 | } |
| 77 | }; |
| 78 | |
| 79 | expect(s.associates()).toEqual(0); |
| 80 | expect(s.get()).toBeFalsy(); |
| 81 | expect(obj1.x).toBeFalsy(); |
| 82 | expect(obj2.x).toBeTruthy(); |
| 83 | |
| 84 | // Associate object with state |
| 85 | s.associate(obj1); |
| 86 | expect(s.associates()).toEqual(1); |
| 87 | s.associate(obj2); |
| 88 | expect(s.associates()).toEqual(2); |
| 89 | |
| 90 | expect(s.get()).toBeFalsy(); |
| 91 | expect(obj1.x).toBeFalsy(); |
| 92 | expect(obj2.x).toBeFalsy(); |
| 93 | |
| 94 | s.clear(); |
| 95 | |
| 96 | s.set(true); |
| 97 | expect(s.get()).toBeTruthy(); |
| 98 | expect(obj1.x).toBeFalsy(); |
| 99 | expect(obj2.x).toBeFalsy(); |
| 100 | |
| 101 | s.set(false); |
| 102 | expect(s.get()).toBeFalsy(); |
| 103 | expect(obj1.x).toBeFalsy(); |
| 104 | expect(obj2.x).toBeFalsy(); |
| 105 | }); |
Akron | 308a603 | 2019-12-05 16:27:34 +0100 | [diff] [blame] | 106 | }); |
| 107 | }); |