Created
May 10, 2020 09:34
-
-
Save gpsarkar/3a82d65324ff0255b7851ec745c53264 to your computer and use it in GitHub Desktop.
storybook enzyme spec interation
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
import React from 'react'; | |
import MyComponent from '../src/components/MyComponent'; | |
import { shallow, mount, render } from 'enzyme'; | |
import { configure } from 'enzyme'; | |
import Adapter from 'enzyme-adapter-react-16'; | |
configure({ adapter: new Adapter() }); | |
import { specs, describe, it } from 'storybook-addon-specifications' | |
import expect from "expect"; | |
export default { | |
title: '__IntegrationTests', | |
component: MyComponent, | |
}; | |
/** Story with specs */ | |
export const MyStory1 = () => { | |
const story = <MyComponent />; | |
setTimeout(()=>{ | |
specs(() => describe('MyStory1 Tests', function () { | |
it('Should load', function () { | |
let wrapper = mount(story, {attachTo: document.getElementById('story')}); | |
expect(wrapper.text()).toContain('Hello World'); | |
setTimeout(()=>{wrapper.find('button').simulate('click')}, 2000); | |
}) | |
})) | |
}, 0); | |
return <div id="story"></div>; | |
} | |
/** Story with decorator and enzyme for interaction, useful for snapshot testing */ | |
export const MyStory2 = () => <MyComponent />; | |
MyStory2.story = { | |
decorators: [storyFn => { | |
setTimeout(()=>{ | |
let wrapper = mount(story, {attachTo: document.getElementById('story')}); | |
setTimeout(()=>{wrapper.find('button').simulate('click')}, 2000); | |
}, 0); | |
return (<div id='story'> {storyFn()} </div>) | |
}], | |
}; | |
/** Story with decorator, enzyme and specs for interaction, useful for assertions along with snapshot testing */ | |
export const MyStory3 = () => <MyComponent />; | |
MyStory3.story = { | |
decorators: [storyFn => { | |
setTimeout(()=>{ | |
specs(() => describe('MyStorie3 Tests', function () { | |
it('Should load', function () { | |
let wrapper = mount(story, {attachTo: document.getElementById('story')}); | |
expect(wrapper.text()).toContain('Hello World'); | |
setTimeout(()=>{wrapper.find('button').simulate('click')}, 2000); | |
}) | |
})) | |
}, 0); | |
return (<div id='story'>{storyFn()}</div>) | |
}], | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment