blob: 286cb1cdc52f8be404b4ba73efa6d7f9c28ceb04 [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
143 const el = document.createElement('input');
144
145 it('should be initializable', function () {
146
147 let sm = stateManagerClass.create(el);
148 expect(sm).toBeTruthy();
149
150 expect(sm.toString()).toEqual("{}");
151 });
152
153
154 it('should be extensible', function () {
155 const sm = stateManagerClass.create(el);
156 expect(sm).toBeTruthy();
157
158 const s1 = sm.newState('test', [1,2,3]);
159
160 expect(sm.toString()).toEqual("{}");
161
162 s1.set(2);
163
164 expect(sm.toString()).toEqual("{\"test\":2}");
165
166 s1.set(3);
167
168 expect(sm.toString()).toEqual("{\"test\":3}");
169
170 const s2 = sm.newState('glemm', [true,false]);
171
172 let serial = JSON.parse(sm.toString());
173 expect(serial["test"]).toEqual(3);
174 expect(serial["glemm"]).toBeUndefined();
175
176 s2.set(false);
177
178 serial = JSON.parse(sm.toString());
179 expect(serial["test"]).toEqual(3);
180 expect(serial["glemm"]).toEqual(false);
181 });
182 });
Akron308a6032019-12-05 16:27:34 +0100183});