forked from thinkb4/a-walk-in-javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclasses.test.js
156 lines (119 loc) · 3.38 KB
/
classes.test.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
describe('DAY 8: classes', () => {
it(`complete the Dog class constructor code to fulfill the test conditions`, () => {
/**
*
*/
class Dog {
/**
*
* @returns {Dog}
*/
constructor () {
this.favoriteThing = `food`;
}
}
let myDog1 = new Dog('fido');
expect(myDog1.name).toBe('fido');
expect(myDog1.favoriteThing).toBe(`food`);
let myDog2 = new Dog();
expect(myDog2.name).toBe(`lonely dog without a name`);
expect(myDog2.favoriteThing).toBe(`food`);
});
it(`Dog should extend from Mammal`, () => {
/**
*
*/
class Mammal {
/**
* @returns { Mammal }
*/
constructor () {
this.hasFur = true;
this.isWarmBlooded = true;
}
}
/**
* @extends Mammal
*/
class Dog {
/**
*
* @returns {Dog}
*/
constructor () {
this.favoriteThing = `food`;
}
}
let myDog = new Dog();
expect(myDog).toBeInstanceOf(Dog);
expect(myDog).toBeInstanceOf(Mammal);
expect(myDog.hasFur).toBe(true);
expect(myDog.favoriteThing).toBe(`food`);
});
it(`Fix the methods of Dog class so that they correctly belong to the "class" or to the "instance"`, () => {
/**
*
*/
class Dog {
/**
* @returns {Dog}
*/
constructor () {
this.favoriteThing = `food`;
}
/**
* @returns {string}
*/
static jump () {
return `jumping`;
}
/**
* @returns {string}
*/
bark () {
return `barking`;
}
}
let myDog = new Dog();
expect(myDog.jump()).toBe('jumping');
expect(() => myDog.bark()).toThrow('myDog.bark is not a function');
expect(Dog.bark()).toBe('barking');
});
it(`use the following code as a starting point
change favoriteThing into a "private" property
add the necessary setter and getter
`, () => {
/**
*
*/
class Dog {
/**
* @returns {Dog}
*/
constructor () {
this.favoriteThing = `food`;
this[Symbol(`dogName`)] = '';
}
/**
* @param {string} name
*/
set name (name) {
let dogNameSymbol = Object.getOwnPropertySymbols(this)[0];
this[dogNameSymbol] = name;
}
/**
* @returns {string}
*/
get name () {
let dogNameSymbol = Object.getOwnPropertySymbols(this)[0];
return this[dogNameSymbol];
}
}
let myDog = new Dog();
myDog.name = `Copernicus`;
myDog.favoriteThing = `eat Aristarchus' food`;
expect(myDog.name).toBe('Copernicus');
expect(myDog.favoriteThing).toBe(`eat Aristarchus' food`);
expect(Object.getOwnPropertyNames(myDog)).toEqual([]);
});
});