fix(learn): ES6 Getters + Setters tests (#40238)
* Strengthen tests Signed-off-by: nhcarrigan <nhcarrigan@gmail.com> * Improve verbiage Signed-off-by: nhcarrigan <nhcarrigan@gmail.com> * Add second check to setter test Signed-off-by: nhcarrigan <nhcarrigan@gmail.com>
This commit is contained in:
committed by
GitHub
parent
ec8b2dc016
commit
49f6875db5
@ -40,7 +40,7 @@ Getters and setters are important because they hide internal implementation deta
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
Use the <code>class</code> keyword to create a Thermostat class. The constructor accepts a Fahrenheit temperature.
|
||||
Now create a <code>getter</code> and a <code>setter</code> in the class, to obtain the temperature in Celsius.
|
||||
In the class, create a <code>getter</code> to obtain the temperature in Celsius and a <code>setter</code> to set the temperature in Celsius.
|
||||
Remember that <code>C = 5/9 * (F - 32)</code> and <code>F = C * 9.0 / 5 + 32</code>, where <code>F</code> is the value of temperature in Fahrenheit, and <code>C</code> is the value of the same temperature in Celsius.
|
||||
<strong>Note:</strong> When you implement this, you will track the temperature inside the class in one scale, either Fahrenheit or Celsius.
|
||||
This is the power of a getter and a setter. You are creating an API for another user, who can get the correct result regardless of which one you track.
|
||||
@ -57,13 +57,15 @@ tests:
|
||||
- text: <code>class</code> keyword should be used.
|
||||
testString: assert(code.match(/class/g));
|
||||
- text: <code>Thermostat</code> should be able to be instantiated.
|
||||
testString: assert((() => {const t = new Thermostat(32);return typeof t === 'object' && t.temperature === 0;})());
|
||||
testString: assert((() => {const t = new Thermostat(122);return typeof t === 'object'})());
|
||||
- text: When instantiated with a Fahrenheit value, <code>Thermostat</code> should set the correct temperature.
|
||||
testString: assert((() => {const t = new Thermostat(122);return t.temperature === 50})());
|
||||
- text: A <code>getter</code> should be defined.
|
||||
testString: assert((() => {const desc = Object.getOwnPropertyDescriptor(Thermostat.prototype, 'temperature');return !!desc && typeof desc.get === 'function';})());
|
||||
- text: A <code>setter</code> should be defined.
|
||||
testString: assert((() => {const desc = Object.getOwnPropertyDescriptor(Thermostat.prototype, 'temperature');return !!desc && typeof desc.set === 'function';})());
|
||||
- text: Calling the <code>setter</code> should set the temperature.
|
||||
testString: assert((() => {const t = new Thermostat(32); t.temperature = 26;return t.temperature !== 0;})());
|
||||
- text: Calling the <code>setter</code> with a Celsius value should set the temperature.
|
||||
testString: assert((() => {const t = new Thermostat(32); t.temperature = 26; const u = new Thermostat(32); u.temperature = 50; return t.temperature === 26 && u.temperature === 50;})());
|
||||
|
||||
```
|
||||
|
||||
|
Reference in New Issue
Block a user