Skip to content

Instantly share code, notes, and snippets.

@Ufosek
Last active August 2, 2019 09:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Ufosek/4260c7f3c60f86625e759d452392bf0c to your computer and use it in GitHub Desktop.
Save Ufosek/4260c7f3c60f86625e759d452392bf0c to your computer and use it in GitHub Desktop.
//
// Our redux component to test
//
export default function ProfileComponent (props) {
const profile = useMySelector(state => state.profile, props)
// Invariant Violation: could not find react-redux context value; please ensure the component is wrapped in a <Provider>
// const profile = useSelector(state => state.profile)
return (
<View>
{ profile.isLoaded &&
<ProfileItem data={ profile.data } />
}
...
</View>
)
}
//
// useMySelector wrapper
//
export function useMySelector(selector, props) {
if (props && props.mockedState) {
return props.mockedState
}
return useSelector(selector)
}
//
// Test
//
describe("ProfileComponent", () => {
describe("when is loaded", () => {
const state = {
isLoaded: true,
data: { ... }
}
const component = TestRenderer.create(<ProfileComponent mockedState={ state } />).root
it("should render ProfileItem", () => {
expect(component.findByType(ProfileItem))
})
it("should pass data to ProfileItem", () => {
expect(component.findByType(ProfileItem).props.data).toEqual(state.data)
})
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment