|
| 1 | +/// <reference types="cypress" /> |
| 2 | +import React from 'react' |
| 3 | +import { mount } from 'cypress-react-unit-test' |
| 4 | +import PositiveCounter from './PositiveCounter' |
| 5 | + |
| 6 | +// current version 1.4.3 of cypress-plugin-snapshots only works with |
| 7 | +// objects and images. |
| 8 | +// https://github.com/meinaart/cypress-plugin-snapshots/issues/122 |
| 9 | +it.skip('sanity check: string snapshots work', () => { |
| 10 | + cy.wrap('hello, world').toMatchSnapshot() |
| 11 | +}) |
| 12 | + |
| 13 | +// utility child command that grabs element's HTML and snapshots its as an object |
| 14 | +Cypress.Commands.add('toMatchHTML', { prevSubject: 'element' }, $el => { |
| 15 | + return cy |
| 16 | + .wrap({ |
| 17 | + html: $el[0].outerHTML, |
| 18 | + }) |
| 19 | + .toMatchSnapshot() |
| 20 | +}) |
| 21 | + |
| 22 | +describe('PositiveCounter', () => { |
| 23 | + it('starts with zero', () => { |
| 24 | + mount(<PositiveCounter />) |
| 25 | + cy.contains('Value: 0') |
| 26 | + // previous command yields jQuery element |
| 27 | + // I would like to get its outer HTML which |
| 28 | + // we can do via $el[0].outerHTML shorthand |
| 29 | + .its('0.outerHTML') |
| 30 | + // convert text to JSON object for the snapshot plugin to work |
| 31 | + // https://github.com/meinaart/cypress-plugin-snapshots/issues/122 |
| 32 | + .then(html => ({ |
| 33 | + html, |
| 34 | + })) |
| 35 | + .toMatchSnapshot() |
| 36 | + |
| 37 | + // in other tests we will use utility child command .toMatchHTML() |
| 38 | + }) |
| 39 | + |
| 40 | + it('should render counts', () => { |
| 41 | + mount(<PositiveCounter />) |
| 42 | + cy.get('.increment') |
| 43 | + .click() |
| 44 | + .click() |
| 45 | + .click() |
| 46 | + // make sure the component updates |
| 47 | + cy.contains('Value: 3').toMatchHTML() |
| 48 | + |
| 49 | + cy.get('.increment') |
| 50 | + .click() |
| 51 | + .click() |
| 52 | + .click() |
| 53 | + |
| 54 | + cy.contains('Value: 6').toMatchHTML() |
| 55 | + }) |
| 56 | + |
| 57 | + it('should not go negative', () => { |
| 58 | + mount(<PositiveCounter />) |
| 59 | + cy.get('.increment').click() |
| 60 | + cy.get('.decrement') |
| 61 | + .click() |
| 62 | + .click() |
| 63 | + cy.contains('Value: 0').toMatchHTML() |
| 64 | + }) |
| 65 | + |
| 66 | + it('snapshots the component state', () => { |
| 67 | + mount(<PositiveCounter />) |
| 68 | + cy.get('.increment') |
| 69 | + .click() |
| 70 | + .click() |
| 71 | + .click() |
| 72 | + .click() |
| 73 | + // The component's code set its reference |
| 74 | + // as a property on the "window" object |
| 75 | + // when running inside Cypress. This allows |
| 76 | + // the test to access it. |
| 77 | + cy.window() |
| 78 | + .its('PositiveCounter.state') |
| 79 | + .toMatchSnapshot() |
| 80 | + }) |
| 81 | +}) |
0 commit comments