-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththejokelist.steps.js
134 lines (103 loc) · 3.38 KB
/
thejokelist.steps.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
/*
thejokelist.steps.js
by Chris DeFreitas
usage:
$ npx cucumber.js
*/
const { BeforeAll, AfterAll, Given, Then } = require('@cucumber/cucumber')
const assert = require('assert').strict
const axios = require('axios')
const FireFoxWorld = require( "./firefox.world.js" )
const browser = new FireFoxWorld( {} )
// fails to affect "this" property:
// setWorldConstructor( FireFoxWorld )
const log = console.log;
// const url = 'http://localhost:8888/' // netlify-cli server
// const url = 'https://thejokelist.netlify.app/'
const url = 'http://localhost:4000/' // express-graphql
BeforeAll( {timeout: 20 * 1000}, async function() {
log('Init...')
// await this.init()
await browser.init()
})
AfterAll( async function () {
log('Done.')
// await this.quit()
await browser.quit()
})
// initial joke feature
Given('the home page is displayed', async function () {
await browser.open( url )
await browser.sleep( 1000 ) // pause for the initial api call to complete
let result = await browser.exists( '.controls' )
assert.equal( true, result)
})
Then('a joke is displayed', async function () {
let el = await browser.qry( '#title' )
let txt = await el.getText()
assert.equal( true, ( txt !== 'Loading joke...' ))
})
// random joke feature
let lastTitle = null // for testing
Given('the question mark is clicked', async function () {
let el = await browser.qry( '#title' )
lastTitle = await el.getText()
el = await browser.qry( '#random' )
el.click()
await browser.sleep( 1000 ) // pause for the initial api call to complete
})
Then('a new joke appears', async function () {
let el = await browser.qry( '#title' )
let newTitle = await el.getText()
assert.equal( true, ( newTitle !== lastTitle ))
})
// next joke feature
var lastid = null
Given('the Right Arrow is clicked', async function () {
let joke = await browser.exec('return joke;')
lastid = joke.id
let el = await browser.qry( '#next' )
el.click()
await browser.sleep( 1000 ) // pause for the initial api call to complete
})
Then('the next joke with an id larger than the last appears', async function () {
let joke = await browser.exec('return joke;')
let newid = joke.id
assert.equal( true, ( newid > lastid ))
})
// prior joke feature
Given('the Left Arrow is clicked', async function () {
let joke = await browser.exec('return joke;')
lastid = joke.id
let el = await browser.qry( '#last' )
el.click()
await browser.sleep( 1000 ) // pause for the initial api call to complete
})
Then('the next joke with an id smaller than the last appears', async function () {
let joke = await browser.exec('return joke;')
let newid = joke.id
assert.equal( true, ( newid < lastid ))
})
// id() feature
lastid = 22
Given('the id enpoint is called with a joke.id', async function () {
let world = this
let endpoint = url +`graphql/?query={id( id:${lastid} ) {id, title, body} }`
// @ts-ignore
await axios.get( endpoint )
.then(function (response) {
world.response = response
})
.catch(function (error) {
world.response = error.response
// log( 'Endpoint call failed with:\n', error.toString() )
// return false
})
return true
})
Then('the returned status equals 200', function () {
assert.equal( 200, this.response.status )
})
Then('the returned joke has the same joke.id', function () {
assert.equal( lastid, this.response.data.data.id.id )
})