This repository has been archived by the owner on Apr 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 787
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented the ApolloConsumer component. (#1399)
* Implemented the ApolloConsumer component. * Wrapped the withApollo component in the ApolloConsumer component. * Updated the changelog * Removed render prop in favour of children render prop
- Loading branch information
1 parent
08b8077
commit 1ae46f1
Showing
13 changed files
with
196 additions
and
117 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import * as React from 'react'; | ||
import * as PropTypes from 'prop-types'; | ||
import ApolloClient from 'apollo-client'; | ||
const invariant = require('invariant'); | ||
|
||
export interface ApolloConsumerProps { | ||
children: (client: ApolloClient<any>) => React.ReactElement<any>; | ||
} | ||
|
||
const ApolloConsumer: React.StatelessComponent<ApolloConsumerProps> = ( | ||
props, | ||
context, | ||
) => { | ||
invariant( | ||
!!context.client, | ||
`Could not find "client" in the context of ApolloConsumer. Wrap the root component in an <ApolloProvider>`, | ||
); | ||
|
||
return props.children(context.client); | ||
}; | ||
|
||
ApolloConsumer.contextTypes = { | ||
client: PropTypes.object.isRequired, | ||
}; | ||
|
||
export default ApolloConsumer; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import React from 'react'; | ||
import ApolloClient from 'apollo-client'; | ||
import { InMemoryCache as Cache } from 'apollo-cache-inmemory'; | ||
import { ApolloLink } from 'apollo-link'; | ||
|
||
import ApolloProvider from '../../../src/ApolloProvider'; | ||
import ApolloConsumer from '../../../src/ApolloConsumer'; | ||
import { mount } from 'enzyme'; | ||
import invariant from 'invariant'; | ||
|
||
const client = new ApolloClient({ | ||
cache: new Cache(), | ||
link: new ApolloLink((o, f) => f(o)), | ||
}); | ||
|
||
describe('<ApolloConsumer /> component', () => { | ||
it('has a render prop', done => { | ||
mount( | ||
<ApolloProvider client={client}> | ||
<ApolloConsumer> | ||
{clientRender => { | ||
try { | ||
expect(clientRender).toBe(client); | ||
done(); | ||
} catch (e) { | ||
done.fail(e); | ||
} | ||
return null; | ||
}} | ||
</ApolloConsumer> | ||
</ApolloProvider>, | ||
); | ||
}); | ||
|
||
it('renders the content in the render prop', () => { | ||
const wrapper = mount( | ||
<ApolloProvider client={client}> | ||
<ApolloConsumer>{clientRender => <div />}</ApolloConsumer> | ||
</ApolloProvider>, | ||
); | ||
|
||
expect(wrapper.find('div').exists()).toBeTruthy(); | ||
}); | ||
|
||
it('errors if there is no client in the context', () => { | ||
// Prevent Error about missing context type from appearing in the console. | ||
const errorLogger = console.error; | ||
console.error = () => {}; | ||
expect(() => { | ||
mount(<ApolloConsumer render={client => null} />); | ||
}).toThrowError( | ||
'Could not find "client" in the context of ApolloConsumer. Wrap the root component in an <ApolloProvider>', | ||
); | ||
|
||
console.error = errorLogger; | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.