-
Notifications
You must be signed in to change notification settings - Fork 0
/
getSet-jasmine-specs.js
175 lines (158 loc) · 8.19 KB
/
getSet-jasmine-specs.js
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
describe("getSet function", function () {
// vars for all tests
var stubParentObject;
// setup and teardown
beforeAll(function () {});
beforeEach(function () {
stubParentObject = {};
});
afterEach(function () {});
// specs
describe("called without arguments", function () {
it("should throw a TypeError for parentObject", function () {
expect(function () {
getSet();
}).toThrowError(TypeError, "getSet: Could not determine argument 'parentObject'. 'parentObject' is of type 'Undefined'.");
});
});
describe("called without a propertyPath", function () {
it("should throw a TypeError for propertyPath", function () {
expect(function () {
getSet(stubParentObject);
}).toThrowError(TypeError, "getSet: Could not determine argument 'propertyPath'. 'propertyPath' is of type 'Undefined'.");
});
});
describe("getting a deep undefined property", function () {
it("should just return undefined", function () {
expect(getSet(stubParentObject, 'foo.bar')).toBeUndefined();
});
it("should not create any path to the deep property that didn't already exist", function () {
expect(stubParentObject.foo).toBeUndefined();
});
});
describe("setting a value on a deep property path", function () {
var testSetValue;
beforeEach(function () {
testSetValue = 123
});
it("should create the path to the property, set the value and return it", function () {
expect(getSet(stubParentObject, 'foo.bar', testSetValue)).toBe(testSetValue);
});
it("should set the value at the full path supplied", function () {
getSet(stubParentObject, 'foo.bar', testSetValue);
expect(stubParentObject.foo.bar).toBe(testSetValue);
});
});
describe("setting a value as a property of a string object", function () {
beforeEach(function () {
stubParentObject.foo = 'some string value';
});
it("should throw a TypeError for that property", function () {
expect(function () {
getSet(stubParentObject, 'foo.bar', 123);
}).toThrowError(TypeError, "getSet: Could not create 'foo.bar'. 'foo' is of type 'String'.");
});
it("should not create the property", function () {
expect(function () {
getSet(stubParentObject, 'foo.bar', 123);
}).toThrowError(TypeError, "getSet: Could not create 'foo.bar'. 'foo' is of type 'String'.");
expect(stubParentObject.foo.bar).toBeUndefined();
});
});
describe("setting a value as a property of a number object", function () {
beforeEach(function () {
stubParentObject.foo = 123;
});
it("should throw a TypeError for that property", function () {
expect(function () {
getSet(stubParentObject, 'foo.bar', 'some string value');
}).toThrowError(TypeError, "getSet: Could not create 'foo.bar'. 'foo' is of type 'Number'.");
});
it("should not create the property", function () {
expect(function () {
getSet(stubParentObject, 'foo.bar', 'some string value');
}).toThrowError(TypeError, "getSet: Could not create 'foo.bar'. 'foo' is of type 'Number'.");
expect(stubParentObject.foo.bar).toBeUndefined();
});
});
describe("called with 'or' option in setValueOrOption, and an optionValue defined", function () {
it("should get the value if the property already existed", function () {
stubParentObject.foo = 123;
expect(getSet(stubParentObject, 'foo', 'or', 456)).toBe(123);
});
it("should set the property if it didn't exist", function () {
getSet(stubParentObject, 'bar', 'or', 789);
expect(stubParentObject.bar).toBe(789);
});
it("should return the passed value if the property didn't exist", function () {
expect(getSet(stubParentObject, 'bar', 'or', 789)).toBe(789);
});
});
describe("called with '+=' operator in setValueOrOption (without optionValue)", function () {
it("should set the value of an undefined path to '+=' and return the value", function () {
expect(getSet(stubParentObject, 'foo', '+=')).toBe('+=');
});
});
describe("called with '-=' operator in setValueOrOption (without optionValue)", function () {
it("should set the value of an undefined path to '-=' and return the value", function () {
expect(getSet(stubParentObject, 'foo', '-=')).toBe('-=');
});
});
describe("called with '+=' operator in setValueOrOption (with numeric optionValue)", function () {
it("should set the value of an undefined path as if it were zero and return the value (Number)", function () {
expect(getSet(stubParentObject, 'foo', '+=', 3)).toBe(3);
});
it("should add the value of optionValue to a defined path and return the value (Number)", function () {
getSet(stubParentObject, 'foo', 3);
expect(getSet(stubParentObject, 'foo', '+=', 3)).toBe(6);
});
it("should set the value of an undefined path and return the value (String)", function () {
expect(getSet(stubParentObject, 'foo', '+=', '3')).toBe('3');
});
it("should concatenate the value of a defined path and return the value (String)", function () {
getSet(stubParentObject, 'foo', '3');
expect(getSet(stubParentObject, 'foo', '+=', '3')).toBe('33');
});
});
describe("called with '-=' operator in setValueOrOption (with numeric optionValue)", function () {
it("should set the value of an undefined path as if it were zero and return the value (Number)", function () {
expect(getSet(stubParentObject, 'foo', '-=', 3)).toBe(-3);
});
it("should subtract the value of optionValue from a defined path and return the value (Number)", function () {
getSet(stubParentObject, 'foo', -3);
expect(getSet(stubParentObject, 'foo', '-=', 3)).toBe(-6);
});
it("should set the value of an undefined path and return the value (String)", function () {
expect(getSet(stubParentObject, 'foo', '-=', '3')).toBe(-3);
});
it("should subtract the value of a defined path and return the value (String)", function () {
getSet(stubParentObject, 'foo', '-3');
expect(getSet(stubParentObject, 'foo', '-=', '3')).toBe(-6);
});
});
describe("called with '+=' operator in setValueOrOption (with a non-numeric optionValue)", function () {
it("should concatenate that value to an empty string", function () {
expect(getSet(stubParentObject, 'foo', '+=', 'some string value')).toBe('some string value');
});
it("should concatenate two string values", function () {
getSet(stubParentObject, 'foo', 'some string value');
expect(getSet(stubParentObject, 'foo', '+=', 'some string value')).toBe('some string valuesome string value');
expect(stubParentObject.foo).toBe('some string valuesome string value');
});
it("should concatenate that value on an existing Number property", function () {
getSet(stubParentObject, 'foo', 123);
expect(getSet(stubParentObject, 'foo', '+=', 'some string value')).toBe('123some string value');
expect(stubParentObject.foo).toBe('123some string value');
});
});
describe("called with '-=' operator in setValueOrOption (with a non-numeric optionValue)", function () {
it("should set and return NaN on a property that didn't exist", function () {
expect(getSet(stubParentObject, 'foo', '-=', 'some string value')).toBeNaN();
});
it("should set and return NaN on an existing Number property", function () {
getSet(stubParentObject, 'foo', 123);
expect(getSet(stubParentObject, 'foo', '-=', 'some string value')).toBeNaN();
expect(stubParentObject.foo).toBeNaN();
});
});
});