forked from bloominstituteoftechnology/Basic-JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproject-3.test.js
More file actions
234 lines (219 loc) · 6.78 KB
/
project-3.test.js
File metadata and controls
234 lines (219 loc) · 6.78 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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/* eslint-disable no-undef */
const {
makeCat,
addProperty,
invokeMethod,
multiplyMysteryNumberByFive,
deleteProperty,
newUser,
hasEmail,
hasProperty,
verifyPassword,
updatePassword,
addFriend,
setUsersToPremium,
sumUserPostLikes,
addCalculateDiscountPriceMethod
} = require('../src/project-3');
describe('Project 3', () => {
describe('makeCat(name, age)', () => {
it('should create a new cat with the name and age properties set', () => {
expect(makeCat('Snowball', 1).name).toBe('Snowball');
expect(makeCat('Snowball', 1).age).toBe(1);
expect(makeCat('Snowball II', 5).name).toBe('Snowball II');
expect(makeCat('Snowball II', 5).age).toBe(5);
});
it('should add a method called meow to the new cat object', () => {
expect(makeCat('Snowball III', 2).meow()).toBe('Meow!');
});
});
describe('addProperty(object, property)', () => {
it('should add the property to the object with a value of null', () => {
const object = {
x: 1,
y: 2
};
const updatedObject = {
x: 1,
y: 2,
z: null
};
expect(addProperty(object, 'z')).toEqual(updatedObject);
});
});
describe('invokeMethod(object, method)', () => {
it('should invoke the method on the object', () => {
const object = {
x: 0,
incrementX() {
this.x++;
}
};
invokeMethod(object, 'incrementX');
expect(object.x).toBe(1);
});
});
describe('multiplyMysteryNumberByFive(mysteryNumberObject)', () => {
it('should return the mysteryNumber property multiplied by five', () => {
const mysteryBox = {
mysteryNumber: 999
};
expect(multiplyMysteryNumberByFive(mysteryBox)).toBe(4995);
mysteryBox.mysteryNumber = -5;
expect(multiplyMysteryNumberByFive(mysteryBox)).toBe(-25);
});
});
describe('deleteProperty(object, property)', () => {
it('should delete the property from the object', () => {
const updatedObject = {
x: 1,
y: 2
};
const object = {
x: 1,
y: 2,
z: null
};
expect(deleteProperty(object, 'z')).toEqual(updatedObject);
});
});
describe('newUser(name, email, password)', () => {
it('should return a new user object with a name, email, and password property that match the arguments', () => {
const user1 = {
name: 'Ben',
email: '[email protected]',
password: 'correcthorsebatterystaple'
};
expect(newUser(user1.name, user1.email, user1.password)).toEqual(user1);
const user2 = {
name: 'Austen',
email: '[email protected]',
password: 'password'
};
expect(newUser(user2.name, user2.email, user2.password)).toEqual(user2);
});
});
describe('hasEmail(user)', () => {
it('should return true if the user object has a value for its email property', () => {
expect(hasEmail({ username: 'SunJieMing', email: '[email protected]' })).toEqual(true);
expect(hasEmail({ username: 'Austen', email: '' })).toEqual(false);
expect(hasEmail({ username: 'Ryan' })).toEqual(false);
});
});
describe('hasProperty(object, property)', () => {
it('should return true if the object has the property that is passed in', () => {
const obj = {
x: true
};
expect(hasProperty(obj, 'x')).toEqual(true);
expect(hasProperty(obj, 'y')).toEqual(false);
});
});
describe('verifyPassword(user, password)', () => {
it('should return true if passwords match', () => {
const user = {
password: 'I love js!'
};
const password = 'I love js!';
expect(verifyPassword(user, password)).toBe(true);
});
it('should return false if passwords do not match', () => {
const user = {
password: 'I love js!'
};
const password = 'Hack this guy!';
expect(verifyPassword(user, password)).toBe(false);
});
});
describe('updatePassword(user, password)', () => {
it('should return the user object with the updated password', () => {
const user = {
password: 'I love js!'
};
const password = 'I love js even more!';
expect(updatePassword(user, password).password).toBe(password);
});
});
describe('addFriend(user, newFriend)', () => {
it('should add a new friend to the end of the friends array property', () => {
const user = {
friends: ['Ben', 'Austen', 'Ryan', 'Mike', 'Young']
};
const newFriend = 'Shay';
expect(addFriend(user, 'Shay').friends.pop()).toBe('Shay');
});
});
describe('setUsersToPremium(users)', () => {
it('should return the users array with each user\'s isPremium property set to true', () => {
const users = [
{ isPremium: false },
{ isPremium: false },
{ isPremium: false },
{ isPremium: false },
{ isPremium: false },
];
const updatedUsers = [
{ isPremium: true },
{ isPremium: true },
{ isPremium: true },
{ isPremium: true },
{ isPremium: true },
];
expect(setUsersToPremium(users)).toEqual(updatedUsers);
});
});
describe('sumUserPostLikes(user)', () => {
it('should return the sum of likes for all user posts', () => {
const user = {
username: 'SunJieMing',
password: 'JavaScript is awesome!',
posts: [
{
id: '1',
title: 'JS adventures!',
likes: 10
},
{
id: '2',
title: 'LambdaSchool forever!',
likes: 100
},
{
id: '3',
title: 'What is a JavaScript?',
likes: 35
},
{
id: '4',
title: 'JS Objects for dummies',
likes: 42
},
{
id: '5',
title: 'Online Education',
likes: 99
}
]
};
expect(sumUserPostLikes(user)).toBe(286);
});
});
describe('addCalculateDiscountPriceMethod(storeItem)', () => {
const storeItem = {
price: 80,
discountPercentage: 0.1
};
const storeItem2 = {
price: 5,
discountPercentage: 0.5
};
it('should add the method \'calculateDiscountPrice\' to the store item object', () => {
expect(addCalculateDiscountPriceMethod(storeItem).calculateDiscountPrice).toBeDefined();
expect(addCalculateDiscountPriceMethod(storeItem2).calculateDiscountPrice).toBeDefined();
});
it('should return the discount price from the new \'calculateDiscountPrice\' method', () => {
expect(addCalculateDiscountPriceMethod(storeItem).calculateDiscountPrice()).toBe(72);
expect(addCalculateDiscountPriceMethod(storeItem2).calculateDiscountPrice()).toBe(2.5);
});
});
});