Skip to content

Instantly share code, notes, and snippets.

@axelnormand
Created July 30, 2018 10:31
Show Gist options
  • Save axelnormand/c626d2c62502f7e634f0ab8abdb14fa2 to your computer and use it in GitHub Desktop.
Save axelnormand/c626d2c62502f7e634f0ab8abdb14fa2 to your computer and use it in GitHub Desktop.
React Native Storybook
import addons from '@storybook/addons';
import { createMockChannel } from 'src/test/storybook/storyshots/mockChannel';
//
// This runs to mock things required for the jest snapshot of a story
//
// In generated test, import this straight as `import 'src/test/storybook/storyshots/setupSnapshotTest'`
// Then require mocked storybook methods
// `const { configure, getStorybook } = require('@storybook/react-native');`
//
// mock some things for the storyshot jest tests
jest.mock('global', () => ({
...global,
fetch: jest.fn(), // stop accidental api calls
}));
// stop accidental api calls
jest.mock('axios');
// mock global and local addDecorator so they are not in snapshot
jest.mock('@storybook/react-native', () => {
const actual = require.requireActual('@storybook/react-native');
return {
...actual,
storiesOf: (...args: any[]) => {
const storybook = actual.storiesOf(...args);
storybook.addDecorator = () => storybook;
return storybook;
},
addDecorator: () => null,
};
});
// mock timers for animated components
jest.useFakeTimers();
// don't need storybook running
addons.setChannel(createMockChannel());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment