Last active
May 24, 2017 20:23
-
-
Save ivanbanov/4b74945b71a1aa3a98ebfd66544ca1dd to your computer and use it in GitHub Desktop.
Mock Relay
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
import React from 'react'; | |
import {storiesOf} from '@kadira/storybook'; | |
import mockRelay from '<path-to>/mocks/relay'; | |
import {Component} from '../'; | |
import viewer from './fixtures/viewer'; // json data | |
const relay = { | |
variables: { | |
foo: {}, | |
bar: true, | |
baz: false | |
} | |
}; | |
storiesOf('Component', module) | |
.add('default', () => { | |
const Component = mockRelay(Component); | |
return ( | |
<Component | |
viewer={viewer} | |
relay={relay} | |
/> | |
); | |
}); |
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
import React, {PropTypes} from 'react'; | |
import Relay from 'react-relay'; | |
import type {RelayContainer} from 'react-relay/src/tools/RelayTypes'; | |
const ROOT_QUERY: Object = { | |
name: 'rootQuery', | |
params: {}, | |
queries: {} | |
}; | |
export default function mockRelay(Component: any): MockRelay { | |
return class MockRelay extends React.Component { | |
static displayName = 'MockRelay'; | |
static propTypes = { | |
relay: PropTypes.object | |
}; | |
static defaultProps = { | |
relay: {} | |
}; | |
render () { | |
const Container: RelayContainer = Relay.createContainer( | |
Component, { | |
fragments: {}, | |
initialVariables: {...this.props.relay.variables} | |
} | |
); | |
return ( | |
<Relay.Renderer | |
render={() => <Container {...this.props} />} | |
Container={Container} | |
queryConfig={ROOT_QUERY} | |
environment={Relay.Store} | |
/> | |
); | |
} | |
}; | |
} | |
Relay.injectNetworkLayer({ | |
sendMutation() { | |
return new Promise(resolve => resolve()); | |
}, | |
sendQueries() { | |
return new Promise(resolve => resolve()); | |
}, | |
supports() { | |
return true; | |
} | |
}); |
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
import React from 'react'; | |
import {isContainer, Renderer} from 'react-relay'; | |
import mockRelay from '../relay'; | |
import {shallow} from 'enzyme'; | |
describe('RelayHOC', () => { | |
beforeEach(() => { | |
jest.resetModules(); | |
}); | |
it('should get a RelayRenderer component with the passed component as Container prop', () => { | |
const RelayComponent = mockRelay('div'); | |
const Component = shallow(<RelayComponent />); | |
expect(Component.dive().find(Renderer).length).toBe(1); | |
expect(isContainer(Component.prop('Container'))).toBe(true); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment