Skip to content

Commit

Permalink
configure webpack/jest transform for .graphql files in storybook, glo…
Browse files Browse the repository at this point in the history
…bal decorator patch for storyshots, better mocks
  • Loading branch information
xavxyz committed Apr 5, 2017
1 parent 0d831ae commit a77f2b0
Show file tree
Hide file tree
Showing 13 changed files with 252 additions and 68 deletions.
26 changes: 15 additions & 11 deletions .storybook/config.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
import { configure, addDecorator } from '@kadira/storybook';
import React from 'react';
import { ApolloProvider } from 'react-apollo';

import Theme from '../imports/components/Theme';
import client from './mocks/client';
import { configure /*, addDecorator*/ } from '@kadira/storybook';
// import React from 'react';
// import { ApolloProvider } from 'react-apollo';
// import client from './mocks/client';
// import Theme from '../imports/components/Theme';

const req = require.context('../imports/components', true, /.stories.js$/);

function loadStories() {
req.keys().forEach(filename => req(filename));
}

addDecorator(story => (
<ApolloProvider client={client}>
<Theme>{story()}</Theme>
</ApolloProvider>
));
// failing addDecorator in storyshots
// see:
// - https://github.com/storybooks/storyshots/issues/59
// - https://github.com/storybooks/storyshots/issues/68

// addDecorator(story => (
// <ApolloProvider client={client}>
// <Theme>{story()}</Theme>
// </ApolloProvider>
// ));

configure(loadStories, module);
12 changes: 12 additions & 0 deletions .storybook/globalDecoratorPatch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import React from 'react';
import { ApolloProvider } from 'react-apollo';
import client from './mocks/client';
import Theme from '../imports/components/Theme';

const globalDecoratorPatch = story => (
<ApolloProvider client={client}>
<Theme>{story()}</Theme>
</ApolloProvider>
);

export default globalDecoratorPatch;
39 changes: 39 additions & 0 deletions .storybook/jestGraphql.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// note:
// `jest-transform-graphql` is obsolete at the moment
// once we figure out how to use graphql-tag/loader correctly with jest
// we'll open a PR on the transform package :)

const gql = require('graphql-tag');

// eslint-disable-next-line
function expandImports(source, doc) {
const lines = source.split('\n');
let outputCode = '';

lines.some(line => {
if (line[0] === '#' && line.slice(1).split(' ')[0] === 'import') {
const importFile = line.slice(1).split(' ')[1];
const parseDocument = `require(${importFile})`;
const appendDef = `doc.definitions = doc.definitions.concat(${parseDocument}.definitions);`;
outputCode += appendDef + '\n';
}
return line.length !== 0 && line[0] !== '#';
});

return outputCode;
}

module.exports = {
process(source) {
// line removed here: cacheable from webpack
// https://webpack.github.io/docs/loaders.html#cacheable
const doc = gql`${source}`;
const outputCode = `var doc = ${JSON.stringify(doc)};`;
const importOutputCode = expandImports(source, doc);

// uncomment for debug
// console.log(outputCode + '\n' + importOutputCode + '\n' + `module.exports = doc;`);

return outputCode + '\n' + importOutputCode + '\n' + `module.exports = doc;`;
},
};
4 changes: 2 additions & 2 deletions .storybook/mocks/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import ApolloClient from 'apollo-client';
import { makeExecutableSchema, addMockFunctionsToSchema } from 'graphql-tools';
import { mockNetworkInterfaceWithSchema } from 'apollo-test-utils';
import typeDefs from '../../imports/server/schema';
import mockedData from './mockedData';
import mocks from './mocks';

const schema = makeExecutableSchema({ typeDefs });
addMockFunctionsToSchema({ schema, mockedData });
addMockFunctionsToSchema({ schema, mocks });

const mockNetworkInterface = mockNetworkInterfaceWithSchema({ schema });

Expand Down
89 changes: 89 additions & 0 deletions .storybook/mocks/fakeData.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit a77f2b0

Please sign in to comment.