Skip to content

Commit b8d732e

Browse files
test(parallax): add e2e tests (pmndrs#1542)
* feat: add new demo for testing * test: add cypress and initial tests * fix: add cypress to jest ignore patterns * feat: add start-server-and-test * feat: add snapshots * chore: add parallax tests to root folder scripts * test: move dev deps to root package.json * chore: move cypress folders to root * chore: rename demo folder to test * chore: add README to test app * fix: make sure we're testing ts with test script Co-authored-by: Josh <[email protected]>
1 parent d402597 commit b8d732e

File tree

24 files changed

+2836
-48
lines changed

24 files changed

+2836
-48
lines changed

.github/workflows/main.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,6 @@ jobs:
2424
npm i -g yarn
2525
yarn install
2626
- name: Run tests
27-
run: yarn test:ts && yarn test:cov
27+
run: yarn test
28+
- name: Trial Build
29+
run: yarn build

cypress.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"baseUrl": "http://localhost:3000",
3+
"video": false,
4+
"scrollBehavior": false,
5+
"fixturesFolder": false,
6+
"screenshotOnRunFailure": false,
7+
"ignoreTestFiles": ["**/__snapshots__/*"]
8+
}
Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
exports[`Parallax - vertical > should translate layers as expected #0`] = `
2+
<div
3+
data-testid="container"
4+
class="container"
5+
style="position: absolute; width: 100%; height: 100%; overflow: hidden scroll; transform: translate3d(0px, 0px, 0px);"
6+
></div>
7+
`;
8+
9+
exports[`Parallax - vertical > should translate layers as expected #1`] = `
10+
<div
11+
data-testid="container"
12+
class="container"
13+
style="position: absolute; width: 100%; height: 100%; overflow: hidden scroll; transform: translate3d(0px, 0px, 0px);"
14+
>
15+
<div
16+
style="overflow: hidden; position: absolute; width: 100%; height: 1980px; transform: translate3d(0px, 0px, 0px);"
17+
>
18+
<div
19+
data-testid="opposite-layer"
20+
class="opposite"
21+
style="position: absolute; inset: 0px; background-size: auto; background-repeat: no-repeat; will-change: transform; height: 100%; width: 660px; transform: translate3d(660px, 0px, 0px);"
22+
></div>
23+
<div
24+
data-testid="default-layer"
25+
class="default"
26+
style="position: absolute; inset: 0px; background-size: auto; background-repeat: no-repeat; will-change: transform; width: 100%; height: 660px; transform: translate3d(0px, 660px, 0px);"
27+
></div>
28+
<div
29+
class="flex-center"
30+
style="position: absolute; inset: 0px; background-size: auto; background-repeat: no-repeat; will-change: transform; width: 100%; height: 660px; transform: translate3d(0px, 0px, 0px);"
31+
>
32+
<button>Scroll</button>
33+
</div>
34+
</div>
35+
<div
36+
data-testid="sticky-layer"
37+
class="sticky"
38+
style="position: sticky; inset: 0px; background-size: auto; background-repeat: no-repeat; will-change: transform; width: 100%; height: 660px; transform: translate3d(0px, 0px, 0px);"
39+
>
40+
<div class="sticky-content flex-center">Sticky</div>
41+
</div>
42+
</div>
43+
`;
44+
45+
exports[`Parallax - vertical > should translate layers as expected #2`] = `
46+
<div
47+
data-testid="container"
48+
class="container"
49+
style="position: absolute; width: 100%; height: 100%; overflow: hidden scroll; transform: translate3d(0px, 0px, 0px);"
50+
>
51+
<div
52+
style="overflow: hidden; position: absolute; width: 100%; height: 1980px; transform: translate3d(0px, 0px, 0px);"
53+
>
54+
<div
55+
data-testid="opposite-layer"
56+
class="opposite"
57+
style="position: absolute; inset: 0px; background-size: auto; background-repeat: no-repeat; will-change: transform; height: 100%; width: 660px; transform: translate3d(0px, 0px, 0px);"
58+
></div>
59+
<div
60+
data-testid="default-layer"
61+
class="default"
62+
style="position: absolute; inset: 0px; background-size: auto; background-repeat: no-repeat; will-change: transform; width: 100%; height: 660px; transform: translate3d(0px, 0px, 0px);"
63+
></div>
64+
<div
65+
class="flex-center"
66+
style="position: absolute; inset: 0px; background-size: auto; background-repeat: no-repeat; will-change: transform; width: 100%; height: 660px; transform: translate3d(0px, 0px, 0px);"
67+
>
68+
<button>Scroll</button>
69+
</div>
70+
</div>
71+
<div
72+
data-testid="sticky-layer"
73+
class="sticky"
74+
style="position: sticky; inset: 0px; background-size: auto; background-repeat: no-repeat; will-change: transform; width: 100%; height: 660px; transform: translate3d(0px, 0px, 0px);"
75+
>
76+
<div class="sticky-content flex-center">Sticky</div>
77+
</div>
78+
</div>
79+
`;
80+
81+
exports[`Parallax - horizontal > should translate layers as expected #0`] = `
82+
<div
83+
data-testid="container"
84+
class="container"
85+
style="position: absolute; width: 100%; height: 100%; overflow: scroll hidden; transform: translate3d(0px, 0px, 0px);"
86+
></div>
87+
`;
88+
89+
exports[`Parallax - horizontal > should translate layers as expected #1`] = `
90+
<div
91+
data-testid="container"
92+
class="container"
93+
style="position: absolute; width: 100%; height: 100%; overflow: scroll hidden; transform: translate3d(0px, 0px, 0px);"
94+
>
95+
<div
96+
style="overflow: hidden; position: absolute; height: 100%; width: 3000px; transform: translate3d(0px, 0px, 0px);"
97+
>
98+
<div
99+
data-testid="opposite-layer"
100+
class="opposite"
101+
style="position: absolute; inset: 0px; background-size: auto; background-repeat: no-repeat; will-change: transform; width: 100%; height: 1000px; transform: translate3d(0px, 1000px, 0px);"
102+
></div>
103+
<div
104+
data-testid="default-layer"
105+
class="default"
106+
style="position: absolute; inset: 0px; background-size: auto; background-repeat: no-repeat; will-change: transform; height: 100%; width: 1000px; transform: translate3d(1000px, 0px, 0px);"
107+
></div>
108+
<div
109+
class="flex-center"
110+
style="position: absolute; inset: 0px; background-size: auto; background-repeat: no-repeat; will-change: transform; height: 100%; width: 1000px; transform: translate3d(0px, 0px, 0px);"
111+
>
112+
<button>Scroll</button>
113+
</div>
114+
</div>
115+
<div
116+
data-testid="sticky-layer"
117+
class="sticky"
118+
style="position: sticky; inset: 0px; background-size: auto; background-repeat: no-repeat; will-change: transform; height: 100%; width: 1000px; transform: translate3d(0px, 0px, 0px);"
119+
>
120+
<div class="sticky-content flex-center">Sticky</div>
121+
</div>
122+
</div>
123+
`;
124+
125+
exports[`Parallax - horizontal > should translate layers as expected #2`] = `
126+
<div
127+
data-testid="container"
128+
class="container"
129+
style="position: absolute; width: 100%; height: 100%; overflow: scroll hidden; transform: translate3d(0px, 0px, 0px);"
130+
>
131+
<div
132+
style="overflow: hidden; position: absolute; height: 100%; width: 3000px; transform: translate3d(0px, 0px, 0px);"
133+
>
134+
<div
135+
data-testid="opposite-layer"
136+
class="opposite"
137+
style="position: absolute; inset: 0px; background-size: auto; background-repeat: no-repeat; will-change: transform; width: 100%; height: 1000px; transform: translate3d(0px, 0px, 0px);"
138+
></div>
139+
<div
140+
data-testid="default-layer"
141+
class="default"
142+
style="position: absolute; inset: 0px; background-size: auto; background-repeat: no-repeat; will-change: transform; height: 100%; width: 1000px; transform: translate3d(0px, 0px, 0px);"
143+
></div>
144+
<div
145+
class="flex-center"
146+
style="position: absolute; inset: 0px; background-size: auto; background-repeat: no-repeat; will-change: transform; height: 100%; width: 1000px; transform: translate3d(0px, 0px, 0px);"
147+
>
148+
<button>Scroll</button>
149+
</div>
150+
</div>
151+
<div
152+
data-testid="sticky-layer"
153+
class="sticky"
154+
style="position: sticky; inset: 0px; background-size: auto; background-repeat: no-repeat; will-change: transform; height: 100%; width: 1000px; transform: translate3d(0px, 0px, 0px);"
155+
>
156+
<div class="sticky-content flex-center">Sticky</div>
157+
</div>
158+
</div>
159+
`;
Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
describe('Parallax - vertical', () => {
2+
const HEIGHT = Cypress.config('viewportHeight')
3+
4+
beforeEach(() => {
5+
cy.visit('/vertical')
6+
})
7+
8+
it('should translate layers as expected', () => {
9+
// intial snapshot
10+
cy.findByTestId('container').toMatchSnapshot()
11+
12+
// initial layer positions
13+
cy.findByTestId('default-layer')
14+
.then(layer => layer[0].style.transform)
15+
.then(transform =>
16+
expect(transform).to.equal(`translate3d(0px, ${HEIGHT * 2}px, 0px)`)
17+
)
18+
19+
cy.findByTestId('opposite-layer')
20+
.then(layer => layer[0].style.transform)
21+
.then(transform =>
22+
expect(transform).to.equal(`translate3d(${HEIGHT * 2}px, 0px, 0px)`)
23+
)
24+
25+
cy.findByTestId('sticky-layer')
26+
.should('have.css', 'position', 'absolute')
27+
.then(layer => layer[0].style.transform)
28+
.then(transform =>
29+
expect(transform).to.equal(`translate3d(0px, ${HEIGHT}px, 0px)`)
30+
)
31+
32+
// scroll to next page
33+
cy.findByTestId('container').scrollTo(0, HEIGHT)
34+
// wait for animation to finish
35+
cy.wait(2000)
36+
37+
// halfway snapshot
38+
cy.findByTestId('container').toMatchSnapshot()
39+
40+
// halfway layer positions
41+
cy.findByTestId('default-layer')
42+
.then(layer => layer[0].style.transform)
43+
.then(transform =>
44+
expect(transform).to.equal(`translate3d(0px, ${HEIGHT}px, 0px)`)
45+
)
46+
47+
cy.findByTestId('opposite-layer')
48+
.then(layer => layer[0].style.transform)
49+
.then(transform =>
50+
expect(transform).to.equal(`translate3d(${HEIGHT}px, 0px, 0px)`)
51+
)
52+
53+
cy.findByTestId('sticky-layer')
54+
.should('have.css', 'position', 'sticky')
55+
.then(layer => layer[0].style.transform)
56+
.then(transform =>
57+
expect(transform).to.equal(`translate3d(0px, 0px, 0px)`)
58+
)
59+
60+
// scroll to last page
61+
cy.findByTestId('container').scrollTo('bottom')
62+
// wait for animation again
63+
cy.wait(2000)
64+
65+
// final snapshot
66+
cy.findByTestId('container').toMatchSnapshot()
67+
68+
// final layer positions
69+
cy.findAllByTestId(/-layer/).each(layer =>
70+
expect(layer[0].style.transform).to.equal('translate3d(0px, 0px, 0px)')
71+
)
72+
73+
cy.findByTestId('sticky-layer').should('have.css', 'position', 'sticky')
74+
})
75+
76+
it('should scroll to the correct page with scrollTo', () => {
77+
cy.findByRole('button').click()
78+
79+
cy.findByTestId('container').invoke('scrollTop').should('equal', HEIGHT)
80+
})
81+
})
82+
83+
describe('Parallax - horizontal', () => {
84+
const WIDTH = Cypress.config('viewportWidth')
85+
86+
beforeEach(() => {
87+
cy.visit('/horizontal')
88+
})
89+
90+
it('should translate layers as expected', () => {
91+
// intial snapshot
92+
cy.findByTestId('container').toMatchSnapshot()
93+
94+
// initial layer positions
95+
cy.findByTestId('default-layer')
96+
.then(layer => layer[0].style.transform)
97+
.then(transform =>
98+
expect(transform).to.equal(`translate3d(${WIDTH * 2}px, 0px, 0px)`)
99+
)
100+
101+
cy.findByTestId('opposite-layer')
102+
.then(layer => layer[0].style.transform)
103+
.then(transform =>
104+
expect(transform).to.equal(`translate3d(0px, ${WIDTH * 2}px, 0px)`)
105+
)
106+
107+
cy.findByTestId('sticky-layer')
108+
.should('have.css', 'position', 'absolute')
109+
.then(layer => layer[0].style.transform)
110+
.then(transform =>
111+
expect(transform).to.equal(`translate3d(${WIDTH}px, 0px, 0px)`)
112+
)
113+
114+
// scroll to next page
115+
cy.findByTestId('container').scrollTo(WIDTH, 0)
116+
// wait for animation to finish
117+
cy.wait(2000)
118+
119+
// halfway snapshot
120+
cy.findByTestId('container').toMatchSnapshot()
121+
122+
// halfway layer positions
123+
cy.findByTestId('default-layer')
124+
.then(layer => layer[0].style.transform)
125+
.then(transform =>
126+
expect(transform).to.equal(`translate3d(${WIDTH}px, 0px, 0px)`)
127+
)
128+
129+
cy.findByTestId('opposite-layer')
130+
.then(layer => layer[0].style.transform)
131+
.then(transform =>
132+
expect(transform).to.equal(`translate3d(0px, ${WIDTH}px, 0px)`)
133+
)
134+
135+
cy.findByTestId('sticky-layer')
136+
.should('have.css', 'position', 'sticky')
137+
.then(layer => layer[0].style.transform)
138+
.then(transform =>
139+
expect(transform).to.equal(`translate3d(0px, 0px, 0px)`)
140+
)
141+
142+
// a possible bug in cypress causes this to fail with `yarn cypress` (`cypress open`)
143+
// but will pass with `yarn test` (`cypress run`)
144+
cy.findByTestId('container').scrollTo('right')
145+
// wait for animation again
146+
cy.wait(2000)
147+
148+
// final snapshot
149+
cy.findByTestId('container').toMatchSnapshot()
150+
151+
// final layer positions
152+
cy.findAllByTestId(/-layer/).each(layer =>
153+
expect(layer[0].style.transform).to.equal('translate3d(0px, 0px, 0px)')
154+
)
155+
156+
cy.findByTestId('sticky-layer').should('have.css', 'position', 'sticky')
157+
})
158+
159+
it('should scroll to the correct page with scrollTo', () => {
160+
cy.findByRole('button').click()
161+
162+
cy.findByTestId('container').invoke('scrollLeft').should('equal', WIDTH)
163+
})
164+
})

cypress/plugins/index.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
const { initPlugin } = require('cypress-plugin-snapshots/plugin')
2+
3+
module.exports = (on, config) => {
4+
initPlugin(on, config)
5+
return config
6+
}

cypress/support/commands.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import '@testing-library/cypress/add-commands'

cypress/support/index.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// ***********************************************************
2+
// This example support/index.js is processed and
3+
// loaded automatically before your test files.
4+
//
5+
// This is a great place to put global configuration and
6+
// behavior that modifies Cypress.
7+
//
8+
// You can change the location of this file or turn off
9+
// automatically serving support files with the
10+
// 'supportFile' configuration option.
11+
//
12+
// You can read more here:
13+
// https://on.cypress.io/configuration
14+
// ***********************************************************
15+
16+
// Import commands.js using ES2015 syntax:
17+
import './commands'
18+
import 'cypress-plugin-snapshots/commands'
19+
20+
// Alternatively you can use CommonJS syntax:
21+
// require('./commands')

jest.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ module.exports = {
88
'.+/(types|__snapshots__)/.+',
99
'<rootDir>/node_modules/',
1010
],
11-
modulePathIgnorePatterns: ['dist'],
11+
modulePathIgnorePatterns: ['dist', 'cypress'],
1212
moduleNameMapper: {
1313
'^react$': '<rootDir>/node_modules/react',
1414
},

0 commit comments

Comments
 (0)