forked from NativeScript/NativeScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapplication-settings-tests.ts
More file actions
203 lines (176 loc) · 7.07 KB
/
application-settings-tests.ts
File metadata and controls
203 lines (176 loc) · 7.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
// <snippet module="application-settings" title="application-settings">
// # Application Settings
// Using application settings methods requires to load "application settings" module.
// ``` JavaScript
var appSettings = require("application-settings");
// ```
// </snippet>
var TKUnit = require("./TKUnit");
var stringKey:string = "stringKey";
var boolKey: string = "boolKey";
var numberKey: string = "numberKey";
var noStringKey: string = "noStringKey";
var noBoolKey: string = "noBoolKey";
var noNumberKey: string = "noNumberKey";
// <snippet module="application-settings" title="application-settings">
// ## Working with string, number and boolean values
// </snippet>
export var testBoolean = function () {
appSettings.setBoolean(boolKey, false);
var boolValueBefore = appSettings.getBoolean(boolKey);
TKUnit.assert(false === boolValueBefore, "Cannot set boolean to false, currently it is: " + appSettings.getBoolean(boolKey));
// <snippet module="application-settings" title="application-settings">
// ### Set and get boolean value and provide default value in case it is not set
// ``` JavaScript
appSettings.setBoolean("boolKey", true);
var boolValue = appSettings.getBoolean("boolKey", false);
// ```
// </snippet>
TKUnit.assert(true === boolValue, "Cannot set boolean to true");
TKUnit.assert(true === appSettings.getBoolean(boolKey), "Cannot set boolean to true (no default)");
};
export var testString = function () {
// <snippet module="application-settings" title="application-settings">
// ### Set and get string value
// ``` JavaScript
appSettings.setString("stringKey", "String value");
var stringValue = appSettings.getString("stringKey");
// ```
// </snippet>
TKUnit.assert("String value" === stringValue, "Cannot set string value");
};
export var testNumber = function () {
// <snippet module="application-settings" title="application-settings">
// ### Set and get numeric value.
// We use `toFixed()` here in order to avoid floating point errors - ex: `54.321` becoming `54.320999999537`.
// Beware the result of `toFixed()` is a string not a number therefore you cannot use `===` or `!==` when comparing with a number.
// ``` JavaScript
appSettings.setNumber("numberKey", 54.321);
var value = parseFloat(appSettings.getNumber("numberKey").toFixed(3));
// ```
// </snippet>
TKUnit.assert(54.321 === value, "Cannot set number value 54.321 != " + value);
};
export var testDefaults = function () {
// <snippet module="application-settings" title="application-settings">
// ### Reading values that are not set before while providing default value
// ``` JavaScript
var defaultValue = appSettings.getString("noStringKey", "No string value");
//// will return "No string value" if there is no value for "noStringKey"
// ```
// </snippet>
TKUnit.assert("No string value" === defaultValue, "Bad default string value");
TKUnit.assert(true === appSettings.getBoolean(noBoolKey, true), "Bad default boolean value");
TKUnit.assert(123.45 === appSettings.getNumber(noNumberKey, 123.45), "Bad default number value");
}
export var testDefaultsWithNoDefaultValueProvided = function () {
// <snippet module="application-settings" title="application-settings">
// ### Reading values that are not set before not providing default value
// ``` JavaScript
var defaultValue = appSettings.getString("noStringKey");
//// will return undefined if there is no value for "noStringKey"
// ```
// </snippet>
TKUnit.assert("undefined" === typeof defaultValue, "Default string value is not undefined");
TKUnit.assert("undefined" === typeof appSettings.getBoolean(noBoolKey), "Default boolean value is not undefined");
TKUnit.assert("undefined" === typeof appSettings.getNumber(noNumberKey), "Default number value is not undefined");
};
// <snippet module="application-settings" title="application-settings">
// ## Other functions
// </snippet>
export var testHasKey = function () {
// <snippet module="application-settings" title="application-settings">
// ### Checking for existence of value for key
// ``` JavaScript
var hasKey = appSettings.hasKey("noBoolKey");
//// will return false if there is no value for "noBoolKey"
// ```
// </snippet>
TKUnit.assert(!hasKey, "There is a key: " + noBoolKey);
TKUnit.assert(!appSettings.hasKey(noStringKey), "There is a key: " + noStringKey);
TKUnit.assert(!appSettings.hasKey(noNumberKey), "There is a key: " + noNumberKey);
TKUnit.assert(appSettings.hasKey(boolKey), "There is no key: " + boolKey);
TKUnit.assert(appSettings.hasKey(stringKey), "There is no key: " + stringKey);
TKUnit.assert(appSettings.hasKey(numberKey), "There is no key: " + numberKey);
};
export var testRemove = function () {
// <snippet module="application-settings" title="application-settings">
// ### Removing value for key
// ``` JavaScript
appSettings.remove("boolKey");
// ```
// </snippet>
TKUnit.assert(!appSettings.hasKey(boolKey), "Failed to remove key: " + boolKey);
appSettings.remove(stringKey);
TKUnit.assert(!appSettings.hasKey(stringKey), "Failed to remove key: " + stringKey);
appSettings.remove(numberKey);
TKUnit.assert(!appSettings.hasKey(numberKey), "Failed to remove key: " + numberKey);
};
export var testInvalidKey = function () {
try {
appSettings.hasKey(undefined);
TKUnit.assert(false, "There is a key undefined");
}
catch (e) {
// we should receive an exception here
}
try {
appSettings.hasKey(null);
TKUnit.assert(false, "There is a key null");
}
catch (e) {
// we should receive an exception here
}
try {
appSettings.hasKey(123);
TKUnit.assert(false, "There is a key number");
}
catch (e) {
// we should receive an exception here
}
appSettings.hasKey("string");
};
export var testInvalidValue = function () {
try {
appSettings.setBoolean(boolKey, "str");
TKUnit.assert(false, "There is a key undefined");
}
catch (e) {
// we should receive an exception here
}
try {
appSettings.setBoolean(boolKey, 123);
TKUnit.assert(false, "There is a key undefined");
}
catch (e) {
// we should receive an exception here
}
try {
appSettings.setString(boolKey, true);
TKUnit.assert(false, "There is a key undefined");
}
catch (e) {
// we should receive an exception here
}
try {
appSettings.setString(boolKey, 123);
TKUnit.assert(false, "There is a key undefined");
}
catch (e) {
// we should receive an exception here
}
try {
appSettings.setNumber(boolKey, true);
TKUnit.assert(false, "There is a key undefined");
}
catch (e) {
// we should receive an exception here
}
try {
appSettings.setNumber(boolKey, "123");
TKUnit.assert(false, "There is a key undefined");
}
catch (e) {
// we should receive an exception here
}
};