Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to inject a window variable in a storybook?

I want to add a React component (called ApplicationForm) to a storybook.

The story book is written this way:

import { configureStore } from '../store';

const store = configureStore();

storiesOf('application from', module)
  .addDecorator(story => <Provider store={store}>{story()} </Provider>)
  .add('all', () => (
      <ApplicationForm />
  ));

The ApplicationForm is created with reduxForm. That's why I need to provide a store in the addDecorator function.

Unfortunately in the configureStore function, one of the reducer has a dependency of a global data window.GLOBAL.

In the storybook I will see the following error:

GLOBAL is not defined
ReferenceError: GLOBAL is not defined
    at initGlobalState (http://localhost:9010/static/preview.bundle.js:86625:3)

How can I inject or simulate such global data in the storybook?

like image 610
Anthony Kong Avatar asked Jun 21 '18 12:06

Anthony Kong


2 Answers

I can add a preview-head.html file in the __stories__ directory.

Inside which I have javascript like this:

<script>
  var data = { };
  window.GLOBAL = {
    data: data
  };  
 </script>

Documentation

like image 85
Anthony Kong Avatar answered Nov 15 '22 23:11

Anthony Kong


The window you're trying to access is the iframe preview. Try to access the window from outside the iframe, the Storybook window.

window.parent.window.GLOBAL

like image 45
Angelo Lucas Avatar answered Nov 16 '22 01:11

Angelo Lucas