|
| 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 | +}) |
0 commit comments