/
async.test.js
82 lines (67 loc) · 1.77 KB
/
async.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
import { doAsync, scenario, scenarios, setupForRspec, Stage } from 'js-given';
import { sum } from './sum';
setupForRspec(describe, it);
async function httpRequest(url) {
return { statusCode: 200 };
}
// tag::AsyncStage[]
class AsyncStage extends Stage {
the_url(url) {
this.url = url;
return this;
}
making_an_http_request_to_that_url() {
doAsync(async () => {
const { statusCode } = await httpRequest(this.url);
this.statusCode = statusCode;
});
return this;
}
the_status_code_is(expectedStatusCode) {
expect(this.statusCode).toEqual(expectedStatusCode);
return this;
}
}
// end::AsyncStage[]
// tag::AsyncScenario[]
scenarios('async', AsyncStage, ({ given, when, then }) => {
return {
an_async_scenario_can_be_executed: scenario({}, () => {
given().the_url('https://jsgiven.org');
when().making_an_http_request_to_that_url();
then().the_status_code_is(200);
}),
};
});
// end::AsyncScenario[]
// tag::PromiseStage[]
class PromiseStage extends Stage {
the_url(url) {
this.url = url;
return this;
}
making_an_http_request_to_that_url() {
doAsync(() => {
return httpRequest(this.url).then(({ statusCode }) => {
this.statusCode = statusCode;
});
});
return this;
}
the_status_code_is(expectedStatusCode) {
expect(this.statusCode).toEqual(expectedStatusCode);
return this;
}
}
// end::PromiseStage[]
// tag::PromiseScenario[]
scenarios('async', PromiseStage, ({ given, when, then }) => {
return {
an_async_scenario_with_promises_can_be_executed: scenario({}, () => {
given().the_url('https://jsgiven.org');
when().making_an_http_request_to_that_url();
then().the_status_code_is(200);
}),
};
});
// end::PromiseScenario[]