blob: 4f7185961759e6bb74879a4702a68c8e76b99a80 [file] [log] [blame]
Akron308a6032019-12-05 16:27:34 +01001define(['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 };
Akronb69cbf12020-10-01 13:04:44 +020037
38 expect(s.associates()).toEqual(0);
Akron308a6032019-12-05 16:27:34 +010039 expect(s.get()).toBeFalsy();
40 expect(obj1.x).toBeFalsy();
41 expect(obj2.x).toBeTruthy();
42
43 // Associate object with state
44 s.associate(obj1);
Akronb69cbf12020-10-01 13:04:44 +020045 expect(s.associates()).toEqual(1);
Akron308a6032019-12-05 16:27:34 +010046 s.associate(obj2);
Akronb69cbf12020-10-01 13:04:44 +020047 expect(s.associates()).toEqual(2);
Akron308a6032019-12-05 16:27:34 +010048
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 });
Akron38ed5dc2020-10-01 17:33:00 +020059
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 });
Akron308a6032019-12-05 16:27:34 +0100106 });
107});