forked from cypress-io/cypress-example-recipes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjsonp-spec.js
97 lines (84 loc) · 3.07 KB
/
jsonp-spec.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
/// <reference types="Cypress" />
/* global window */
// https://on.cypress.io/intercept
describe('intercept jsonp', { viewportHeight: 500, viewportWidth: 400 }, () => {
it('has jQuery in the app', () => {
cy.visit('/fruits-jsonp') // cy.visit yields the window object
.its('jQuery')
.should('be.a', 'function')
})
it('has working JSONP', () => {
cy.visit('/fruits-jsonp')
// expect 4 fruits
cy.get('.favorite-fruits li').should('have.length', 4)
})
it('spies on JSONP request', () => {
// prepare to intercept JSONP requests that will be something like
// /favorite-fruits-jsonp?fruitsCallback=jQuery3510137647...
cy.intercept({
method: 'GET',
pathname: '/favorite-fruits-jsonp',
query: {
// matches any value in this search parameter
fruitsCallback: '*',
},
}).as('fruits')
cy.visit('/fruits-jsonp')
cy.wait('@fruits')
.its('response').then((res) => {
// let's get the callback method from the URL
const url = new URL(res.url)
const callbackName = url.searchParams.get('fruitsCallback')
expect(callbackName, 'set by jQuery').to.be.a('string')
// the response body is a JavaScript string of the form
// callbackName(["fruit 1", "fruit 2", ...])
// let's get this list of fruits by preparing a temporary
// function for the "res.body" to call
window[callbackName] = (fruits) => {
expect(fruits).to.be.an('array').and.to.have.length(4)
// by wrapping the "fruits" value we pass it to the next Cypress command
cy.wrap(fruits)
}
window.eval(res.body)
// clean up our temporary function
delete window[callbackName]
})
// the previous callback yields the fruits
.then((fruits) => {
// make sure every fruit is displayed on the page
expect(fruits).to.have.length.gt(1)
fruits.forEach((fruit) => {
cy.contains('.favorite-fruits li', fruit)
})
})
})
it('stubs a JSONP request', () => {
// we will reply with these fruits
const fruits = ['apples 🍎', 'grapes 🍇', 'kiwi 🥝']
// prepare to intercept JSONP requests that will be something like
// /favorite-fruits-jsonp?fruitsCallback=jQuery3510137647...
cy.intercept({
method: 'GET',
pathname: '/favorite-fruits-jsonp',
query: {
// matches any value in this search parameter
fruitsCallback: '*',
},
}, (req) => {
// find the name of the callback method the app wants us to call
const url = new URL(req.url)
const callbackName = url.searchParams.get('fruitsCallback')
expect(callbackName, 'set by jQuery').to.be.a('string')
const fruitsText = JSON.stringify(fruits)
const fruitsJavaScript = `${callbackName}(${fruitsText})`
req.reply(fruitsJavaScript)
})
.as('fruits')
cy.visit('/fruits-jsonp')
// make sure the expected fruits are shown
cy.get('.favorite-fruits li').should('have.length', fruits.length)
fruits.forEach((fruit) => {
cy.contains('.favorite-fruits li', fruit)
})
})
})