###前提・実現したいこと
現在、ReactNativeで、iPad、Windows Tabletをターゲットとしたタブレットアプリを開発しています。
APIから取得したプロジェクト一覧を表示するような画面を実装して、
jestのsnapshotを使って画面のテストを実装してみたのですが、
APIの呼び出し部分のMockの方法が分からず困っています。
ご回答よろしくお願いいたします。
###該当のソースコード
Home.js
lang
1import React, { Component } from 'react'; 2import { 3 AppRegistry, 4 StyleSheet, 5 Text, 6 FlatList, 7 View, 8 TouchableHighlight, 9} from 'react-native'; 10import { connect } from 'react-redux'; 11import { bindActionCreators } from 'redux'; 12import * as actions from '../Actions'; 13import 'fetch-everywhere'; 14 15class Home extends Component { 16 constructor(props) { 17 super(props); 18 this.state = { 19 projects: [], 20 }; 21 } 22 23 componentWillMount = (props) => { 24 const { dispatch } = this.props; 25 // ここでプロジェクト一覧のデータを取得するAPIの呼び出し 26 this.props.actions.getAllProjects(); 27 } 28 29 render() { 30 const props = this.props; 31 const { navigate } = this.props.navigation; 32 33 return ( 34 <View> 35 <FlatList style={styles.list} 36 // APIから取得したデータを取り出す(※ここをMockしたい※) 37 // 今の状態でテストするとdataの中身が空になります 38 data={props.projects.getAllProjects.projects} 39 renderItem={({item}) => 40 <TouchableHighlight onPress={() => navigate('Project', { project: item })} underlayColor='paleturquoise'> 41 <View style={styles.project}> 42 <Text style={styles.projectName}>{item.title}</Text> 43 </View> 44 </TouchableHighlight> 45 } 46 /> 47 </View> 48 ); 49 } 50}; 51 52const styles = StyleSheet.create({ 53 project: { 54 margin: 10, 55 }, 56 list: { 57 marginTop: 20, 58 }, 59 projectName: { 60 fontSize: 20, 61 textAlign: 'left', 62 flex: 1, 63 }, 64}); 65 66const mapStateToProps = (state) => { 67 return { 68 projects: state, 69 } 70} 71 72const mapDispatchToProps = (dispatch) => { 73 return { 74 actions: bindActionCreators(actions, dispatch) 75 } 76} 77 78export default connect(mapStateToProps, mapDispatchToProps)(Home)
index.ios.js(テストコード)
lang
1import 'react-native'; 2import React from 'react'; 3import Index from '../index.ios.js'; 4 5// Note: test renderer must be required after react-native. 6import renderer from 'react-test-renderer'; 7 8it('renders correctly', () => { 9 const tree = renderer.create( 10 <Index /> 11 ).toJSON(); 12 expect(tree).toMatchSnapshot(); 13}); 14
index.ios.js.snap(自動生成されたスナップショット)
// Jest Snapshot v1, https://goo.gl/fbAQLP exports[`renders correctly 1`] = ` <View style={ Object { "alignItems": "center", "backgroundColor": "#F5FCFF", "flex": 1, "justifyContent": "center", } } > <View> <Text accessible={true} allowFontScaling={true} disabled={false} ellipsizeMode="tail" style={ Object { "fontSize": 30, "fontWeight": "bold", "marginTop": 20, "textAlign": "center", } } > プロジェクト一覧 </Text> <RCTScrollView ListHeaderComponent={undefined} // 今の状態だとdataの中身が空 data={Array []} disableVirtualization={false} getItem={[Function]} getItemCount={[Function]} horizontal={false} initialNumToRender={10} keyExtractor={[Function]} maxToRenderPerBatch={10} numColumns={1} onContentSizeChange={[Function]} onEndReachedThreshold={2} onLayout={[Function]} onMomentumScrollEnd={[Function]} onScroll={[Function]} onScrollBeginDrag={[Function]} onScrollEndDrag={[Function]} onViewableItemsChanged={undefined} renderItem={[Function]} renderScrollComponent={[Function]} scrollEventThrottle={50} stickyHeaderIndices={Array []} style={ Object { "marginTop": 20, } } updateCellsBatchingPeriod={50} windowSize={21} > <View /> </RCTScrollView> </View> </View> `;
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/08/03 06:35
2017/08/03 07:41
2017/08/04 01:53