Created
October 18, 2017 15:55
-
-
Save xogeny/d9edb2fa0dc84d20d12a3770595f6dd2 to your computer and use it in GitHub Desktop.
Cypress testing of Storybook
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Tests built around our Storybook | |
describe('Storybook', () => { | |
beforeEach(() => { | |
// Visiting our app before each test removes any state build up from | |
// previous tests. Visiting acts as if we closed a tab and opened a fresh one. | |
// In this case, we are using the publicly accessible AirBnB react-dates Storybook | |
cy.visit('http://airbnb.io/react-dates/') | |
}) | |
// Let's build some tests around the DateRangePicker | |
context('DateRangePicker', () => { | |
it('should visit the default story in this collection', () => { | |
cy.get('a[data-name="default"]').click() | |
// NB - In storybook 3.1.x, you'd need to do this instead | |
// cy.get('a[title="Open default"]').click() | |
// Now get the iframe for the components and make assertions on that. | |
cy.get('#storybook-preview-iframe').then(($iframe) => { | |
const doc = $iframe.contents(); | |
iget(doc, "#startDate").click(); | |
iget(doc, "#root > div > div:nth-child(2) > div > a").should('have.text', "Show Info"); | |
}) | |
}) | |
}) | |
// Now let's build some tests around DayPicker | |
context("DayPicker", () => { | |
// Since the DayPicker isn't the default story, we need to open it each time... | |
beforeEach(() => { | |
cy.get('div[data-name="DayPicker"]').click(); | |
// NB - In Storybook v3.1.x, you'd need to do this instead | |
// cy.get('a[title="Open DayPicker"]').click(); | |
}) | |
it('should visit the vertical day picker', () => { | |
cy.get('a[data-name="vertical"]').click(); | |
// NB - In Storybook v3.1.x, you'd need to do this instead | |
// cy.get('a[title="Open vertical"]').click(); | |
// Now get the iframe for the components and make assertions on that. | |
cy.get('#storybook-preview-iframe').then(($iframe) => { | |
const doc = $iframe.contents(); | |
// This is an implicit assertion that this element exists | |
iget(doc, 'div[aria-label="Calendar"]'); | |
}) | |
}) | |
}) | |
}) | |
function iget(doc, selector) { | |
return cy.wrap(doc.find(selector)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The following visits an iframe directly removing the need to interact with the navigation to activate a specific story.