Skip to content
This repository has been archived by the owner on Apr 13, 2023. It is now read-only.

Commit

Permalink
create failing test
Browse files Browse the repository at this point in the history
  • Loading branch information
James Baxley committed Aug 22, 2016
1 parent f81d9dc commit 1fc3b12
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 8 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"main": "index.js",
"scripts": {
"pretest": "npm run compile",
"test": "mocha --require ./test/fixtures/setup.js --reporter spec --full-trace --recursive ./lib/test && jest",
"test": "mocha --require ./test/fixtures/setup.js --reporter spec --full-trace --recursive ./lib/test/react-web/server && jest",
"posttest": "npm run lint",
"filesize": "npm run compile:browser && ./scripts/filesize.js --file=./dist/index.min.js --maxGzip=15",
"compile": "tsc",
Expand Down
3 changes: 1 addition & 2 deletions src/graphql.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ export default function graphql(

setInitialProps() {
if (this.type === DocumentType.Mutation) {
this.createWrappedMutation(this.props);
this.createWrappedMutation(this.props);
return;
}

Expand All @@ -346,7 +346,6 @@ export default function graphql(
opts.query = document;
return this.client.query(opts);
};

queryData = assign({
errors: null, loading: false, variables, refetch, fetchMore,
}, result);
Expand Down
8 changes: 3 additions & 5 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ export function getChildFromComponent(component) {
function getQueriesFromTree(
{ component, context = {}, queries = []}: QueryTreeArgument, fetch: boolean = true
) {

if (!component) return;

// stateless function
Expand All @@ -50,18 +49,17 @@ function getQueriesFromTree(

let newContext = context;
if (Component.getChildContext) newContext = assign({}, context, Component.getChildContext());

context = newContext;

// see if there is a fetch data method
if (typeof type.fetchData === 'function' && fetch) {
const query = type.fetchData(ownProps, newContext);
const query = type.fetchData(ownProps, context);
if (query) queries.push({ query, component });
}

getQueriesFromTree({
component: getChildFromComponent(Component),
context: newContext,
context,
queries,
});
} else if (props && props.children) {
Expand All @@ -71,14 +69,14 @@ function getQueriesFromTree(
queries,
}));
}

return { queries, context };
}

// XXX component Cache
export function getDataFromTree(app, ctx: any = {}, fetch: boolean = true): Promise<any> {

let { context, queries } = getQueriesFromTree({ component: app, context: ctx }, fetch);

// no queries found, nothing to do
if (!queries.length) return Promise.resolve(context);

Expand Down
51 changes: 51 additions & 0 deletions test/react-web/server/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,57 @@ describe('SSR', () => {
;
});

it('should not require `ApolloProvider` to be the root component', (done) => {

const query = gql`{ currentUser { firstName } }`;
const data = { currentUser: { firstName: 'James' } };
const networkInterface = mockNetworkInterface(
{ request: { query }, result: { data }, delay: 50 }
);
const apolloClient = new ApolloClient({ networkInterface });

const WrappedElement = graphql(query)(({ data }) => (
<div>{data.loading ? 'loading' : data.currentUser.firstName}</div>
));

class MyRootContainer extends React.Component<any, any> {

constructor(props) {
super(props);
this.state = { color: 'purple' };
}

getChildContext() {
return { color: this.state.color };
}

render() {
return <div>{this.props.children}</div>;
}
}

(MyRootContainer as any).childContextTypes = {
color: React.PropTypes.string,
};

const app = (
<MyRootContainer>
<ApolloProvider client={apolloClient}>
<WrappedElement />
</ApolloProvider>
</MyRootContainer>
);

getDataFromTree(app)
.then(() => {
const markup = ReactDOM.renderToString(app);
expect(markup).to.match(/James/);
done();
})
.catch(done)
;
});

it('should run return the initial state for hydration', (done) => {
const query = gql`{ currentUser { firstName } }`;
const data = { currentUser: { firstName: 'James' } };
Expand Down

0 comments on commit 1fc3b12

Please sign in to comment.