forked from cypress-io/cypress-example-recipes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
spec.js
47 lines (37 loc) · 1.47 KB
/
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
/// <reference types="cypress" />
/* global window */
const MutationObserver = window.MutationObserver || window.WebKitMutationObserver
// helper function to observe DOM changes
function observeDOM (obj, callback) {
if (!obj || obj.nodeType !== window.Node.ELEMENT_NODE) {
throw new Error('can not observe this element')
}
const obs = new MutationObserver(callback)
obs.observe(obj, { childList: true, subtree: true })
}
describe('Stub loading of resources', () => {
it('requested image can be exchanged', () => {
cy.visit('index.html')
let imgStr
// load mock image data from the fixture file
// and save in a local variable
cy.fixture('picture.png', 'base64')
.then((imgString) => {
imgStr = imgString
})
// start watching the image container for new elements added
cy.get('#img-container').then(($element) => {
let el = $element[0]
observeDOM(el, (mutations) => {
// we could check the type of the added node
// but for now we are assuming it is a new image node
mutations[0].addedNodes[0].src = `data:image/png;base64,${imgStr}`
})
})
cy.get('button').click()
// image element will have "naturalWidth" set when it loads
// the page tries to load "images/rose.png" with dimensions 70x46
// but the test will substitute "cypress/fixtures/picture.png" that is 100x100
cy.get('#img-container > img').should(($img) => expect($img[0].naturalWidth).to.equal(100))
})
})