/
gatsby-image.js
74 lines (66 loc) · 1.91 KB
/
gatsby-image.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
const testConfig = {
retries: {
runMode: 2,
openMode: 0,
},
}
function hasSVGPlaceholder(el) {
el.children(`img`)
.should(`have.attr`, `src`)
.and(src => {
expect(src).to.match(/^data:image\/svg\+xml/)
})
}
function hasBase64Placeholder(el) {
el.children(`img`)
.should(`have.attr`, `src`)
.and(src => {
expect(src).to.match(/^data:image\/[a-z]+;base64/)
})
}
function testGatsbyImage(type, testPlaceholder) {
if (testPlaceholder) {
cy.get(`[data-cy="${type}"]`)
.find(".gatsby-image-wrapper")
.each($el => {
const el = cy.wrap($el)
testPlaceholder(el)
})
}
cy.get(`[data-cy="${type}"]`).scrollIntoView({ duration: 500 })
// Wait images for load
cy.wait(1000)
cy.get(`[data-cy="${type}"]`)
.find(".gatsby-image-wrapper > picture > img")
.each(($el, i) => {
cy.wrap($el).should(`have.attr`, `srcset`)
cy.wrap($el).should(`have.attr`, `src`)
cy.wrap($el).matchImageSnapshot(`${type}-${i}`)
})
}
describe(`gatsby-image`, () => {
beforeEach(() => {
cy.visit("/gatsby-image").waitForRouteChange()
})
it(`fluid`, testConfig, () => testGatsbyImage(`fluid`, hasBase64Placeholder))
it(`fixed`, testConfig, () => testGatsbyImage(`fixed`, hasBase64Placeholder))
it(`webp`, testConfig, () => testGatsbyImage(`webp`, hasBase64Placeholder))
it(`traced`, testConfig, () =>
testGatsbyImage(`traced`, hasBase64Placeholder)
)
it(`sqip`, testConfig, () => testGatsbyImage(`sqip`, hasSVGPlaceholder))
it(`english`, testConfig, () => {
testGatsbyImage(`english`, hasBase64Placeholder)
cy.get(`[data-cy="english"] p strong`).should(
"have.text",
"Locale - American English (png)"
)
})
it(`german`, testConfig, () => {
testGatsbyImage(`german`, hasBase64Placeholder)
cy.get(`[data-cy="german"] p strong`).should(
"have.text",
"Locale - German (png)"
)
})
})