blob: 661f39421688a4186987827a0ad976f768fc2da3 [file] [log] [blame]
Akronb7aab872021-11-04 17:11:04 +01001define(['state','state/manager'], function (stateClass, stateManagerClass) {
Akron308a6032019-12-05 16:27:34 +01002
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
Akron72c1c9d2021-11-05 10:46:34 +010019 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
Akron308a6032019-12-05 16:27:34 +010033 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 };
Akronb69cbf12020-10-01 13:04:44 +020051
52 expect(s.associates()).toEqual(0);
Akron308a6032019-12-05 16:27:34 +010053 expect(s.get()).toBeFalsy();
54 expect(obj1.x).toBeFalsy();
55 expect(obj2.x).toBeTruthy();
56
57 // Associate object with state
58 s.associate(obj1);
Akronb69cbf12020-10-01 13:04:44 +020059 expect(s.associates()).toEqual(1);
Akron308a6032019-12-05 16:27:34 +010060 s.associate(obj2);
Akronb69cbf12020-10-01 13:04:44 +020061 expect(s.associates()).toEqual(2);
Akron308a6032019-12-05 16:27:34 +010062
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 });
Akron38ed5dc2020-10-01 17:33:00 +020073
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 });
Akron237abc42020-10-07 14:14:52 +0200120
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 });
Akron308a6032019-12-05 16:27:34 +0100139 });
Akronb7aab872021-11-04 17:11:04 +0100140
141 describe('KorAP.State.Manager', function () {
142
Akronb7aab872021-11-04 17:11:04 +0100143 it('should be initializable', function () {
144
Akron25cd9282021-11-22 16:50:27 +0100145 const el = document.createElement('input');
Akronb7aab872021-11-04 17:11:04 +0100146 let sm = stateManagerClass.create(el);
147 expect(sm).toBeTruthy();
148
Akron25cd9282021-11-22 16:50:27 +0100149 expect(sm.toString()).toEqual("");
Akronb7aab872021-11-04 17:11:04 +0100150 });
151
152
153 it('should be extensible', function () {
Akron25cd9282021-11-22 16:50:27 +0100154
155 const el = document.createElement('input');
Akronb7aab872021-11-04 17:11:04 +0100156 const sm = stateManagerClass.create(el);
157 expect(sm).toBeTruthy();
158
159 const s1 = sm.newState('test', [1,2,3]);
160
Akron25cd9282021-11-22 16:50:27 +0100161 expect(sm.toString()).toEqual("");
Akronb7aab872021-11-04 17:11:04 +0100162
163 s1.set(2);
164
Akron25cd9282021-11-22 16:50:27 +0100165 expect(sm.toString()).toEqual("\"test\":2");
Akronb7aab872021-11-04 17:11:04 +0100166
167 s1.set(3);
168
Akron25cd9282021-11-22 16:50:27 +0100169 expect(sm.toString()).toEqual("\"test\":3");
Akronb7aab872021-11-04 17:11:04 +0100170
171 const s2 = sm.newState('glemm', [true,false]);
172
Akron25cd9282021-11-22 16:50:27 +0100173 let serial = JSON.parse('{' + sm.toString() + '}');
Akronb7aab872021-11-04 17:11:04 +0100174 expect(serial["test"]).toEqual(3);
175 expect(serial["glemm"]).toBeUndefined();
176
177 s2.set(false);
178
Akron25cd9282021-11-22 16:50:27 +0100179 serial = JSON.parse('{' + sm.toString() + '}');
Akronb7aab872021-11-04 17:11:04 +0100180 expect(serial["test"]).toEqual(3);
181 expect(serial["glemm"]).toEqual(false);
182 });
Akron25cd9282021-11-22 16:50:27 +0100183
Akron25cd9282021-11-22 16:50:27 +0100184 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 });
Akrona8d45c42021-11-23 11:31:31 +0100208
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 });
Akroned223be2024-12-10 13:01:46 +0100234
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 });
Akronb7aab872021-11-04 17:11:04 +0100247 });
Akron308a6032019-12-05 16:27:34 +0100248});