forked from cypress-io/cypress-example-recipes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
image-spec.js
65 lines (56 loc) · 1.9 KB
/
image-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
/// <reference types="Cypress" />
describe('intercept', () => {
it('spies on loading a static image', () => {
cy.intercept('/images/*').as('image')
cy.visit('/pics.html')
cy.wait('@image')
// reload the page, it should send another request
cy.reload()
cy.wait('@image')
})
it('spies on loading a static image (2nd test)', () => {
cy.intercept('/images/*').as('image')
cy.visit('/pics.html')
cy.wait('@image')
})
it('stubs a static image', () => {
// 🐅 -> 🦘
cy.intercept({ url: '/images/*' }, {
fixture: 'roo.jpg',
headers: {
'content-type': 'image/jpeg',
'cache-control': 'public, max-age=0',
},
}).as('image')
cy.visit('/pics.html')
// we DO see the roo image, but again, just like the test above
// cannot wait for it using cy.wait
})
it('stubs a static image using fixture', () => {
// 🐅 -> 🦘
cy.intercept('/images/*', { fixture: 'roo.jpg' })
cy.visit('/pics.html')
// we DO see the roo image, but again, just like the test above
// cannot wait for it using cy.wait
// confirm the fixture has loaded by looking at its dimensions
// use "closeTo" assertion because sometimes browsers renders
// images with subpixel accuracy
cy.get('img').invoke('width').should('closeTo', 300, 1)
cy.get('img').invoke('height').should('closeTo', 450, 1)
})
it('redirects static image', () => {
// instead of serving an image from a fixture
// we can redirect the request for the image
// to another route
cy.intercept({
method: 'GET',
url: '/images/tiger.jpg',
}, (req) => {
// /__root/* is a special URL that can serve a file from the
// project's root. Normally this is used during bundling
// but here we are using it to serve another image
req.redirect('/__root/cypress/fixtures/roo.jpg')
})
cy.visit('/pics.html')
})
})