Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save gpsarkar/3a82d65324ff0255b7851ec745c53264 to your computer and use it in GitHub Desktop.
Save gpsarkar/3a82d65324ff0255b7851ec745c53264 to your computer and use it in GitHub Desktop.
storybook enzyme spec interation
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