-
Notifications
You must be signed in to change notification settings - Fork 20
/
51-generator-yield-expression.js
66 lines (49 loc) · 1.64 KB
/
51-generator-yield-expression.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
// 51: Generator - Yield Expressions
// To do: make all tests pass, leave the assert lines unchanged!
describe('generator - `yield` is used to pause and resume a generator function', () => {
function* generatorFunction() {
yield 'hello';
yield 'world';
}
let generator;
beforeEach(function() {
generator = generatorFunction();
});
it('converting a generator to an array resumes the generator until all values are received', () => {
let values = Array.from(generator);
assert.deepEqual(values, ['hello', 'world']);
});
describe('after the first `generator.next()` call', function() {
it('the value is "hello"', function() {
const {value} = generator.next();
assert.equal(value, 'hello');
});
it('and `done` is false', function() {
const {done} = generator.next();
assert.equal(done, false);
});
});
describe('after the second `next()` call', function() {
let secondItem;
beforeEach(function() {
generator.next();
secondItem = generator.next();
});
it('`value` is "world"', function() {
let {value} = secondItem;
assert.equal(value, 'world');
});
it('and `done` is still false', function() {
const done = secondItem.done;
assert.equal(done, false);
});
});
describe('after stepping past the last element, calling `next()` that often', function() {
it('`done` property equals true, since there is nothing more to iterator over', function() {
generator.next();
generator.next();
let {done} = generator.next();
assert.equal(done, true);
});
});
});