借助React Native Elements,Jest和MobX MST可以轻松实现现实世界中的ReactNative应用 (Real-world ReactNative apps made easy with React Native Elements, Jest, and MobX MST)
In this post, we’ll build a real-world mobile application in ReactNative. We’ll also explore some of the development practices and libraries, including the following:
code styling and linting tools (Prettier, ESLint, and Arirbnb style guide)
代码的造型和掉毛工具( 更漂亮 , ESLint和Arirbnb风格指南 )
screen navigation using react-navigation
使用React导航进行屏幕导航
user interface using React Native Elements
使用React Native Elements的用户界面
and an important, but often ignored part: unit-testing your application (via Jest and Enzyme).
还有一个重要但经常被忽略的部分:对应用程序进行单元测试(通过Jest和Enzyme )。
So let’s get started!
因此,让我们开始吧!
React中的状态管理 (State management in React)
React and ReactNative have made building Single Page Applications and Mobile Applications fun and easy, but they only cover the view of the applications. State Management and UI design can still be a painful part of building the app.
There are several popular State Management libraries available for React. I’ve used Redux, Mobx, and RxJS. While all three of them are good in their own ways, I’ve enjoyed MobX the most because of its simplicity, elegance, and powerful state management.
Redux, based primarily on the concepts of functional programming and pure functions, tries to solve the complexity of state management by imposing some restrictions on when updates are possible. These restrictions are reflected in three basic principles: a single source of truth, read-only state, and pure functions. You can read more about these principles in the Redux documentation.
While I’m a fan of functional programming, I’ve experienced that you have to deal with a lot of unnecessary boilerplate code when working with Redux. You also have to write code for dispatching actions and transforming state yourself.
Mobx, on the other hand, does this job for you, making it easier to maintain and more fun to work with. You need the right amount of code and restrictions in MobX to achieve superior state management and a good developer experience.
In Redux, you also have to spend a substantial amount of time normalizing and de-normalizing your data. In MobX, you don’t need to normalize the data, and MobX automatically tracks the relations between state and derivations. We’ll go into this later.
RxJS is a reactive programming library for JavaScript. It is different from MobX in that RxJS allows you to react to events while in MobX. You observe the values (or state) and it helps you react to changes in state.
Although both RxJS and MobX provide the ability to perform reactive programming, they are quite different in their approaches.
尽管RxJS和MobX都提供执行React式编程的能力,但是它们的方法却大不相同。
关于我们的应用 (About our app)
The application we’ll be building is for a Book Store. It will mainly consist of two simple views: the Books View and the Authors View.
我们将构建的应用程序用于书店。 它主要包含两个简单的视图:“书籍”视图和“作者”视图。
The app will contain a navigation drawer with two menu options, allowing the user to switch between the two views. The first option will be for navigating to the Books View, and the other option will navigate to the Authors View.
The Books View will contain the list of books, as well as a tab allowing the user to switch between Fiction and Non-Fiction books. The Authors View will containing the list of authors.
图书视图将包含图书列表,以及允许用户在小说和非小说书籍之间切换的选项卡。 作者视图将包含作者列表。
We’ll be installing everything on a Mac OS. Most of the commands will be the same when you have Node installed, but if you face any issues, let me know, (or just google it).
Setup Prettier, ESLint, and the Airbnb style guide for our project 为我们的项目设置更漂亮,ESLint和Airbnb样式指南
Add Drawer and Tabs Navigation using react-navigation 使用react-navigation添加抽屉和标签导航
Test our React components with Jest and Enzyme 用Jest和Enzyme测试我们的React组件
Manage the state of our app using MobX (mobx-state-tree). It will also involve some UI changes and more navigation. We’ll sort and filter the books by genre, and allow the user to see the Book detail screen when the user taps on a book. 使用MobX(mobx-state-tree)管理应用程序的状态。 它还将涉及一些UI更改和更多导航。 我们将按流派对书籍进行排序和过滤,并允许用户在点击书籍时看到“书籍详细信息”屏幕。
Here’s a demo of the Bookstore app we’re going to build:
这是我们将要构建的Bookstore应用程序的演示:
我们不会涵盖的内容 (What we won’t cover)
There are a few things we won’t cover in this article, which you may want to consider in your project:
我们将在本文中介绍一些内容,您可能需要在项目中考虑这些内容:
Tools for adding static type system in JavaScript, like flow and TypeScript
用于在JavaScript中添加静态类型系统的工具,例如flow和TypeScript
Although we will add some styling to our app, we won’t go into details concerning the different options available for adding styles in a ReactNative application. The styled-components library is one of the most popular for both React and ReactNative applications.
We won’t build a separate backend for our application. We will go through integration with the Google Books API, but we’ll use mock data for the most part. 我们不会为我们的应用程序构建单独的后端。 我们将与Google Books API进行集成,但大部分将使用模拟数据。
使用create-react-native-app CLI(CRNA)创建React Native应用程序 (Create a React Native application using create-react-native-app CLI (CRNA))
Create React Native App is a tool created by Facebook and the Expo team that makes it a breeze to get started with a React Native project. We’ll initialize our ReactNative app using CRNA CLI. So let’s get started!
Assuming that you have Node already installed, , we need to install create-react-native-app globally, so that we can initialize a new React Native project for our Book Store.
Once CRNA is done bootstrapping our React Native application, it will show some helpful commands. Let’s change the directory to the newly created CRNA app, and start it.
通过Expo在真实设备上打开CRNA应用 (Opening the CRNA app on a real device via Expo)
When the app is started via npm start, a QR code will be displayed in your terminal. The easiest way to look at our bootstrapped app is using the Expo app. To do that:
Install the Expo client app on your iOS or Android device.
在iOS或Android设备上安装Expo客户端应用。
Make sure that you are connected to the same wireless network as your computer. 确保您已将计算机连接到同一无线网络。
Using the Expo app, scan the QR code from your terminal to open your project. 使用Expo应用程序,从终端扫描QR码以打开您的项目。
在模拟器中打开CRNA应用 (Opening the CRNA app in a simulator)
To run the app on iOS Simulator, you’ll need to install Xcode. To run the app on an Android Virtual Device, you need to setup the Android development environment. Look at the react-native getting started guide for both the setups.
JavaScript is a dynamic language, and doesn’t have a static type system like languages such as C++ and Java. Because of this dynamic nature, JavaScript lacks the kind of tools available for static analysis that many other languages offer.
This results in hard-to-find bugs related to data types, and requires more effort in debugging and troubleshooting these issues, especially for inexperienced JavaScript developers.
Since it’s not a compiled language, error are discovered when the JavaScript code is executed at runtime. There are tools like TypeScript and flow that help catch these kind of errors by adding a static type system to JavaScript, but we won’t be going into either of these tools in this tutorial.
On the other hand, there are linting tools like ESLint available that perform static analysis of the JavaScript code based on configurable rules. They highlight problems in the code that may be potential bugs, which helps developers discover problems in their code before it is executed.
A good linting tool is extremely important to ensure that quality is baked in from the beginning and errors are found early. ESLint also helps you implement style guidelines.
To make sure we write high quality code and have the right tools from the very beginning of our Bookstore project, we’ll start our tutorial by first implementing linting tools. You can learn more about ESLint on their website.
ESLint is fully configurable and customizable. You can set your rules according to your preferences. However, different linting rules configurations have have been provided by the community. One of the popular ones is the Airbnb style guide, and this is the one we’ll use. This will include Airbnb’s ESLint rules, including ECMAScript 6+ and React.
First, we’ll install ESLint by running this command in the terminal:
首先,我们将在终端中运行以下命令来安装ESLint:
We’ll use Airbnb’s eslint-config-airbnb, which contains Airbnb’s ESLint rules, including ECMAScript 6+ and React. It requires specific versions of ESLint, eslint-plugin-import, eslint-plugin-react, and eslint-plugin-jsx-a11y. To list the peer dependencies and versions, run this command:
This will install the necessary dependencies and generate the .eslintrc.js file in the project root directory. The .eslintrc.js file should have the following configurations:
While we have the linting covered with ESLint and the Airbnb style guide, a big part of code quality is consistent code styling. When you’re working on a team, you want to make sure that the code formatting and indentation is consistent throughout the team. Prettier is just the tool for that. It ensures that all the code conforms to a consistent style.
Now, there may be conflicts between the ESLint rules and the code formatting done by Prettier. Fortunately, there is a plugin available called eslint-config-prettier that turns off all rules that are unnecessary or might conflict with Prettier.
NOTE: If ESLint is installed globally, then make sure eslint-plugin-prettier is also installed globally. A globally-installed ESLint cannot find a locally-installed plugin.
To enable eslint-plugin-prettier plugin, update your .eslintrc.js file to add the “prettier” plugin. And to show linting error on Prettier formatting rules, add the “rule” to show error on “prettier/prettier”. Here’s our updated .eslintrc.js:
eslint-config-prettier also ships with a CLI tool to help you check if your configuration contains any rules that are unnecessary or conflict with Prettier. Let’s be proactive and do that.
Now, run the “eslint-check” command to see ESLint and Prettier’s conflicting rules:
现在,运行“ eslint-check”命令以查看ESLint和Prettier的冲突规则:
npm run eslint-check
This will list the conflicting rules in the terminal. Let’s turn off the conflicting rules by updating the .eslintrc.js file. I also prefer singleQuote and trailingComma, so I’ll configure those rules as well. This is what our .eslintrc.js file looks like now:
If you now run eslint with the --fix flag, the code will be automatically formatted according to the Prettier styles.
如果现在使用--fix标志运行eslint ,则代码将根据Prettier样式自动设置格式。
配置VS Code以在保存时运行ESLint (Configure VS Code to run ESLint on save)
We can configure any IDE to automatically run ESLint on Save or as we type. Since we have also configured Prettier along with ESLint, our code will automatically be pretiffied. VS Code is an IDE popular in the JavaScript community, so I’ll show how to setup ESLint’s auto-fix on save using VS Code, but the steps would be similar in any IDE.
我们可以将任何IDE配置为在“保存”或键入时自动运行ESLint。 由于我们还与ESLint一起配置了Prettier,因此将自动美化我们的代码。 VS Code是JavaScript社区中流行的IDE,因此,我将向您展示如何使用VS Code在保存时设置ESLint的自动修复功能,但是步骤在任何IDE中都是相似的。
To configure VS Code to automatically run ESLint on Save, we first need to install the ESLint extension. Go to Extensions, search for the “ESLint” extension, and install it. Once the ESLint extension is installed, go to Preferences > User Settings, and set “eslint.autoFixOnSave” to true. Also make sure that “files.autoSave” is either set to “off”, “onFocusChange” or “onWindowChange”.
Now, open the file App.js. If the ESLint is configured correctly, you should see some linting error, like the “react/prefer-stateless-function”, “react/jsx-filename-extension”, and “no-use-before-define” errors. Let’s turn those “off” in the .eslintrc.js file. I also prefer singleQuote and trailingComma as I mentioned above, so I’ll configure those rules as well.
I know this was a lot of work, considering that we haven’t even started working on our app yet! But trust me, this setup will be very beneficial for your projects in the long run, even if you’re a one person team. When you’re working with other developers, linting and programming standards will go a long way in reducing code defects and ensuring consistency in code style.
You can find the changes made in this section in this branch of the tutorial repository.
您可以在教程资料库的此分支中找到本节中所做的更改。
使用React导航的抽屉和标签导航 (Drawer and Tabs Navigation using react-navigation)
In this section, we’ll add the Drawer and Tabs Navigation using react-navigation.
在本节中,我们将使用react-navigation添加“抽屉和标签导航”。
Our Bookstore app will contain a navigation drawer with two menu options. The first menu item for the AuthorsScreen, containing the list of authors. The second menu item for the BooksScreen, containing the list of books.
Tapping on a book will take the user to the BookDetail Screen. For navigation between the different views, we’ll use React Navigation to add navigation to our app. So let’s install it first:
an Author module allowing the users to browse list of authors 作者模块,允许用户浏览作者列表
a Books module, containing the list of books. 书籍模块,其中包含书籍列表。
The Author and Book modules will be implemented using the StackNavigator from React Navigation. Think of StackNavigator as the history stack in a web browser. When the user clicks on a link, the URL is pushed to the browser history stack, and removed from the top of the history stack when the user presses the back button.
For BooksScreen and AuthorsScreen, we’ll simply add two stateless react components for now, with some buttons to test our screen navigation and drawer functionality:
In our application, we’ll add a Drawer which will maintain the menu for our Author and Book modules. We’ll implement the drawer using React Navigation’s createDrawerNavigator.
The first menu in the drawer will be for the Author module, and the second for the Book module. Author and Book Stack Navigators will both be inside the main DrawerStack.
We used createDrawerNavigator() from react-navigation to implement the Drawer Navigation. This renders the Drawer content, along with the menu options for Books and Authors.
And after making the above changes, here’s what our UI looks like when we click on the “Open Drawer” button and navigate between screens.
进行了上述更改之后,这就是我们单击“打开抽屉”按钮并在屏幕之间导航时的UI外观。
目录结构 (Directory Structure)
It’s important to think about your application and how you’ll structure of your files and resources in the beginning of the project. While there are several ways you could structure your application code, I prefer co-locating files and tests using a feature-based architecture. Co-locating files related to a particular feature or module has a number of benefits.
Let’s create an src directory where we’ll keep all our source files. Inside it, create two directories: one for the book view, named “book”, and the other for the author view, named “author”.
Create index.js files within each of the two directories we just added. These files will export the components for each of our views. Move the code from App.js for the BookView and AuthorView components into these files, and import them instead.
It’s important to note that refactoring should be a big part of the development workflow. We should continuously refactor our code to prepare ourselves for future changes and challenges. This has a big impact on productivity and change management in the long run.
Our app should still work as it was before the refactor. Here’s the file diff of our recent changes.
我们的应用程序应该仍然可以像重构前一样正常工作。 这是我们最近更改的文件差异 。
Each of the screens will have a title, which means that we’ll be duplicating the same code along with the styles. To keep our code DRY, let’s move the title to a separate file src/components/Title.js, and reuse it where needed. We’ll also move the main views into a new parent directory src/views to keep them separate from other components.
The business requirement for our app is to have three tabs in the books view, to show all books by default, and additional tabs to show filtered books for the fiction and non-fiction books. Let’s use the createBottomTabNavigator from react-navigation to implement the Tab Navigation.
We should also add a title on every screen to identify the currently selected screen. Let’s create a separate directory src/components for all the common components, and create a file for our Title component inside this new directory.
Note that we’ve also added style to the xt> component, importing both StyleSheet and Text from react-native.
请注意,我们还为 xt>组件添加了style , from react- native导入both Styl样式t an文本。
We’ll add the Title to each view component, providing the title text in the props. Also, since the Authors view just contains a list of authors, we don’t need a StackNavigator for it, so we’ll change it to a plain React component. Here’s what our src/views/author/index.js file looks like now:
Now, when we open the Books menu from the drawer, we’re able to switch tabs by clicking on the tabs at the bottom.
现在,当我们从抽屉中打开“书籍”菜单时,我们可以通过单击底部的标签来切换标签。
With those changes, we have our app’s navigations all done. Here’s the diff for our recent changes.
进行这些更改后,我们的应用程序导航全部完成。 这是我们最近变化的区别 。
React本机元素 (React Native Elements)
There are several UI component libraries for adding React Native components with style. Some of the more poular ones are React Native ElementsNativeBase, and Ignite. We’ll be using React Native Elements for our Bookstore app. So let’s first install react-native-elements:
For the Authors List, we’ll use the data and code from the ListItem demo. We’ll revisit ListItem into more detail when we implement the Book List screen.
用Jest和Enzyme测试ReactNative组件 (Testing ReactNative components with Jest and Enzyme)
In this section, we’ll add some unit tests using Jest and Enzyme.
在本节中,我们将使用Jest和Enzyme添加一些单元测试。
开玩笑和酶设置 (Jest and Enzyme setup)
Having unit tests for your code is really important so that you can have confidence in your code when you want to change something. It really pays off when you’re adding more features, and you can make changes without the fear of breaking some existing functionality of your application as a result of the change. You know that your unit tests provide the safety net for your application from leaking out any defects into the production.
We’ll use Jest as our testing framework along with Airbnb’s JavaScript testing utility Enzyme. Enzyme has a flexible and intuitive interface that makes it very easy to assert, manipulate, and traverse React Components.
The create-react-native-app kit already includes all the related Jest libraries and configurations. To work with Enzyme, we need to install enzyme and some related dependencies. Since we’re using React 16, we’ll be adding react-dom@16 and enzyme-adapter-react-16.
标题组件的酶和快照测试 (Enzyme and snapshot tests for our Title component)
Now, we’re all set to add Enzyme tests. I prefer having tests co-located with my code. Let’s create a simple test for our Title component by adding a test file next to our Title component. In this test, we’ll simply shallow render the Title component, create a snapshot, and verify the component styles. Create the file src/components/__tests__/Title.js, with the following content:
Basically, the toMatchSnapshot() call renders your component and creates a snapshot in the __snapshots__ directory (if the snapshot doesn’t already exist). After that, each time you re-run your tests, Jest will compare the output of the rendered component with that of the snapshot, and will fail if there is a mismatch. It will show the difference between the expected and the actual output. You can then review the differences, and if this difference is valid due to some change that you’ve implemented, you can re-run the tests with an -u flag, which signals Jest to update the snapshot with the new updates.
Here’s the diff for our changes so far for Jest and Enzyme test, including the generated snapshot.
这是到目前为止我们对Jest和Enzyme测试所做的更改 (包括生成的快照)的差异 。
酶转json序列化器 (enzyme-to-json serializer)
If you open up the snapshot file (src/components/__tests__/__snapshots__/Title.js.snap), you’ll notice that the content is not very readable. It is obfuscated by the code from the Enzyme wrappers, since we’re using Enzyme to render our component. Fortunately, there is the enzyme-to-json library available that converts the Enzyme wrappers to a format compatible with Jest snapshot testing.
Since we now expect the snapshot to be different from the previous snapshot, we’ll pass the -u flag to update the snapshot:
由于现在我们希望快照与之前的快照不同,因此我们将传递-u标志以更新快照:
npm test -- -u
If you open up the snapshot file again, you’ll see that the snapshot for the rendered Title component is correct.
如果再次打开快照文件,将会看到渲染的Title组件的快照是正确的。
We’ll dive more into Jest testing in the later sections.
在后面的部分中,我们将深入研究Jest测试。
使用React Navigation和Mobx Store管理状态 (Managing state with React Navigation and Mobx Store)
MobX或Redux用于状态管理 (MobX or Redux for state management)
While React is great for managing the view of your application, you generally need tools for store management of your application. I say generally, because you may not need a state management library at all — it all depends on the type of application you are building.
There are several state management libraries out there, but the most popular are Redux and MobX. We’ll be using Mobx store for our Bookstore application.
You need to add a lot of boilerplate code. 您需要添加很多样板代码。
You have to write code for dispatching actions and transforming state yourself. 您必须自己编写用于调度动作和转换状态的代码。
It forces you to implement things in a specific way. While this would be a good thing in some applications, I find that the amount of time it takes might not be worth it for many applications. 它迫使您以特定方式实现事物。 尽管在某些应用程序中这是一件好事,但我发现对于许多应用程序而言,花费的时间可能并不值得。
Some advantages of MobX:
MobX的一些优点:
It adds that boilerplate for you, and does it well. I find it very easy to work with, whether it’s initial setup, or adding more functionality. 它为您添加了样板,并且做得很好。 我发现使用它非常容易,无论是初始设置还是添加更多功能。
It doesn’t force you to implement your data flow in a specific way, and you have much more freedom. But again, that might be more problematic than helpful if you don’t setup your MobX stores correctly.. 它不会强迫您以特定的方式实现数据流,而且您拥有更大的自由度。 但是同样,如果您没有正确设置MobX存储,那可能比帮助还麻烦。
I know this is a sensitive topic, and I don’t want to start a debate here, so I’ll leave this topic for another day. But if you want more perspective on this, there are several perspectives on this debate around the internet. Redux and MobX are both great tools for store management.
We’ll be gradually adding functionality to our store instead of adding it all at once, just to show you how easy it is to add more features to MobX stores.
We won’t use Mobx directly, but a wrapper on MobX called mobx-state-tree. They’ve done a fine job of describing themselves, so I’ll just quote them here:
Simply put, mobx-state-tree tries to combine the best features of both immutability (transactionality, traceability and composition) and mutability (discoverability, co-location and encapsulation). — MST Github page
We’ll be using the Google Books API to fetch the books for our app. If you want to follow along, you’ll have to create a project in the Google Developers Console, enable Google Books API on it, and create an API Key in the project. Once you have the API Key, create a file keys.json in the project root, with the following content (replace YOUR_GOOGLE_BOOKS_API_KEY with your API key):
NOTE: If you don’t want to go through this process of getting an API key, don’t worry. We won’t be using the Google API directly, and will mock the data instead.
Google Books API endpoint books/v1/volumes returns an array of items where each item contains information on a specific book. Here’s a cut down version of a book:
{ kind: "books#volume", id: "r_YQVeefU28C", etag: "HeC4avg1XlM", selfLink: "https://www.googleapis.com/books/v1/volumes/r_YQVeefU28C", volumeInfo: { title: "Breaking Everyday Addictions", subtitle: "Finding Freedom from the Things That Trip Us Up", authors: [ "David Hawkins" ], publisher: "Harvest House Publishers", publishedDate: "2008-07-01", description: "Addiction is a rapidly growing problem among Christians and non-Christians alike. Even socially acceptable behaviors, ...", pageCount: 256, printType: "BOOK", categories: [ "Addicts" ], imageLinks: { smallThumbnail: "http://books.google.com/books/content?id=r_YQVeefU28C", thumbnail: "http://books.google.com/books/content?id=r_YQVeefU28C&printsec=frontcover" }, language: "en", previewLink: "http://books.google.com.au/books?id=r_YQVeefU28C&printsec=frontcover", infoLink: "https://play.google.com/store/books/details?id=r_YQVeefU28C&source=gbs_api", canonicalVolumeLink: "https://market.android.com/details?id=book-r_YQVeefU28C" }}
We won’t be using all the fields returned in the API response. So we’ll create our MST model for only the data we need in our ReactNative app. Let’s define our Book model in MST.
In the above MST node definition, our Book model type is defining the shape of our node — of type Book — in the in the MobX State Tree. The types.modeltype in MST is used to describe the shape of an object. Giving the model a name isn’t required, but is recommended for debugging purpose.
The second argument, the properties argument, is a key-value pair, where the key is the name of a property, and the value is its type. In our model, id is the identifier, title is of type string, pageCount is of type number, authors is an array of strings, genre is of type string, inStock of type boolean, and image of type string.
All the data is required by default to create a valid node in the tree, so if we tried to insert a node without a title, MST won’t allow it, and will throw an error.
The genre will be mapped to the categories field (first index value of the categories array) of the Google Books API data. It may or may not be there in the response. Therefore, we’ve made it of type maybe. If the data for genre is not there in the response, genre will be set to null in MST, but if it’s there, it must be of type string for it to be valid.
Since inStock is our own field, and is not returned in the response from the Google Books API, we’ve made it optional and have given it a default value of true. We could have simply assigned it the value true, since for primitive types MST can infer type from the default value. So inStock: true is the same as inStock: t.optional(t.boolean, true).
MST trees are protected by default. This means that only the MST actions can change the state of the tree.
MST树默认情况下受保护。 这意味着只有MST动作才能更改树的状态。
We’ve defined two actions: updateBooks is a function that is only called by the loadBooks function, so we’re not exposing it to the outside world. loadBooks on the other hand, is exposed (we’re returning it), and can be called from outside the BookStore.
Asynchronous actions in MST are written using generators, and always return a promise. In our case, loadBooks needs to be asynchronous, since we’re making an Ajax call to the Google Books API.
We’ll maintain a single instance of the BookStore. If the store already exists, we’ll return the existing store. If not, we’ll create one and return that new store:
store = BookStore.create({ books: {} }) return store}
在我们看来使用MST商店 (Using the MST store in our view)
Let’s start with the All Books view. To do that, we’ll create a new file containing our BookListView component:
让我们从“所有书籍”视图开始。 为此,我们将创建一个包含BookListView组件的新文件:
import React, { Component } from 'react'import { observer } from 'mobx-react'import BookStore from '../../../stores/book'import BookList from './BookList'
As you can see, we’re initializing the BookStore in componentWillMount, and then calling loadBooks() to fetch the books from the Google Books API asynchronously. The BookList component iterates over the books array inside the BookStore, and renders the Book component for each book. Now, we just need to add this BookListView component to AllBooksTab.
If you start the app now, you’ll see that the books are loading as expected.
如果立即启动应用程序,您会看到书籍正在按预期加载。
Note that I’m using Pascal case naming convention for a file that returns a single React component as the default export. For everything else, I use Kebab case. You may decide to choose a different naming convention for your project.
If you run npm start now, you should see a list of books fetched by the Google API.
如果您现在运行npm start ,应该会看到Google API提取的书籍列表。
Here’s the diff for our changes so far.
到目前为止,这是我们所做更改的区别 。
为我们的MST BookStore添加测试 (Adding tests for our MST BookStore)
Let’s add some unit tests for our BookStore. However, our store is talking to our API, which calls the Google API. We can add integration tests for our store, but to add unit tests, we need to mock the API somehow.
A simple way to mock the API is to use Jest Manual Mocks by creating the __mocks__ directory next to our existing api.js file. Inside it, create another api.js, the mocked version of our API fetch calls. Then, we just call jest.mock('../api')in our test to use this mocked version.
MobX状态树中的依赖注入 (Dependency Injection in MobX State Tree)
We won’t be using Jest Manual Mocks. I’d like to show you another feature in MST, and demonstrate how easy it is to mock our API using MST. We’ll use Dependency injection in MobX State Tree to provide an easy way to mock the API calls, making our store easy to test. Note that our MST store can also be tested without Dependency Injection using Jest Mocks, but we’re doing it this way just for demonstration.
It is possible to inject environment-specific data to a state tree by passing an object as the second argument to the BookStore.create() call. This object will be accessible by any model in the tree by calling getEnv(). We’ll be injecting a mock API in our BookStore, so let’s first add the optional api parameter to the default export, and set it to the actual bookApi by default.
I’ve added a delay in response so that the response is not sent immediately. I’ve also created a JSON file with the some data similar to that of the response sent by the Google Books API src/stores/book/mock-api/books.json.
我添加了一个延迟响应,以便不会立即发送响应。 我还创建了一个JSON文件,其中的一些数据类似于Google Books API src/stores/book/mock-api/books.json发送的响应。
Now, we’re ready to inject the mock API into our tests. Create a new test file for our store with the following content:
现在,我们准备将模拟API注入我们的测试中。 使用以下内容为我们的商店创建一个新的测试文件:
// src/stores/book/__tests__/index.jsimport { BookStore } from '../index'import api from '../mock-api/api'
it('bookstore fetches data', async () => { const store = BookStore.create({ books: [] }, { api }) await store.loadBooks() expect(store.books.length).toBe(10)})
Run the store test:
运行商店测试:
npm test src/stores/book/__tests__/index.js
You should see the test pass.
您应该看到测试通过。
添加图书过滤器并应用TDD (Adding the books filter and applying TDD)
I believe in a hybrid approach to Test Driven Development. In my experience, it works best if you add some basic functionality first when starting a project, or when you’re adding a new module or a major functionality from scratch. Once the basic setup and structure is implemented, then TDD works really well.
But I do believe that TDD is the best way to approach a problem space in code. It not only forces you to have better code quality and design, but also ensures that you have atomic unit tests. Additionally it makes sure your unit tests are more focused on testing specific functionality, rather than stuffing too many assertions in a test.
Before we start adding our tests and making changes to our store, I’ll change the delay in our mock API to 300 millisecs to ensure that our tests run faster.
Before we start adding our tests and making changes to our store, I'll change the delay in our mock API to 300 millisecs to ensure that our tests run faster.
We want a filter field in our BookStore model, and a setGenre() action in our store for changing the value of the this filter.
We want a filter field in our BookStore model, and a setGenre() action in our store for changing the value of the this filter .
it(`filter is set when setGenre() is called with a valid filter value`, async () => { store.setGenre('Nonfiction') expect(store.filter).toBe('Nonfiction')})
We want to run tests only for our BookStore, and keep the tests running and watching for changes. They will re-run when the code has been changed. So we’ll use the watch command and use file path pattern matching:
We want to run tests only for our BookStore, and keep the tests running and watching for changes. They will re-run when the code has been changed. So we'll use the watch command and use file path pattern matching:
npm test stores/book -- --watch
The above test should fail, because we haven’t written the code yet to make the test pass. The way that TDD works is that you write an atomic test to test the smallest unit of a business requirement. Then you add code to make just that test pass. You go through the same process iteratively, until you’ve added all the business requirements. To make our test pass, we’ll have to add a filterfield of ENUM type in our BookStore model:
The above test should fail, because we haven't written the code yet to make the test pass. The way that TDD works is that you write an atomic test to test the smallest unit of a business requirement. Then you add code to make just that test pass. You go through the same process iteratively, until you've added all the business requirements. To make our test pass, we'll have to add a filter field of ENUM type in our BookStore model:
And add an MST action which will allow us to change the filter value:
And add an MST action which will allow us to change the filter value:
const setGenre = genre => { self.filter = genre}
return { //... setGenre,}
With these two changes, we should be in the green. Let’s also add a negative test for an invalid filter value:
With these two changes, we should be in the green. Let's also add a negative test for an invalid filter value:
it(`filter is NOT set when setGenre() is called with an invalid filter value`, async () => { expect(() => store.setGenre('Adventure')).toThrow()})
And this test should also pass. This is because we’re using an ENUM type in our MST store, and the only allowed values are All, Fiction, and Nonfiction.
And this test should also pass. This is because we're using an ENUM type in our MST store, and the only allowed values are All , Fiction , and Nonfiction .
Here’s the diff of our recent changes.
Here's the diff of our recent changes .
Sorting and filtering the books (Sorting and filtering the books)
The first index value in the categories field of the mock data categorizes the book as Fiction or Nonfiction. We will use it to filter the books for our Fiction and Nonfiction tabs, respectively.
The first index value in the categories field of the mock data categorizes the book as Fiction or Nonfiction . We will use it to filter the books for our Fiction and Nonfiction tabs, respectively.
We also want our books to always be sorted by title. Let’s add a test for this:
We also want our books to always be sorted by title. Let's add a test for this:
Let’s first add a test for sorting the books:
Let's first add a test for sorting the books:
it(`Books are sorted by title`, async () => { const books = store.sortedBooks expect(books[0].title).toBe('By The Book') expect(books[1].title).toBe('Jane Eyre')})
To make our test pass, we’ll add a view named sortedBooks in our BookStoremodel:
To make our test pass, we'll add a view named sortedBooks in our BookStore model:
get sortedBooks() { return self.books.sort(sortFn)},
And with this change, we should be in the green again.
And with this change, we should be in the green again.
About MST Views (About MST Views)
We just added the sortedBooks view in our BookStore model. To understand how MST Views work, we’ll have to understand MobX. The key concept behind MobX is: anything that can be derived from the application state should be derived, automatically.
We just added the sortedBooks view in our BookStore model. To understand how MST Views work, we'll have to understand MobX. The key concept behind MobX is: anything that can be derived from the application state should be derived, automatically.
In this egghead.io video, the MobX creator Michel Weststrate explains the key concepts behind MobX. I’ll quote a key concept here:
In this egghead.io video , the MobX creator Michel Weststrate explains the key concepts behind MobX. I'll quote a key concept here:
MobX is built around four core concepts. Actions, observable state, computed values, and reactions… Find the smallest amount of state you need, and derive all the other things… — Michel Weststrate
MobX is built around four core concepts. Actions, observable state, computed values, and reactions… Find the smallest amount of state you need, and derive all the other things… — Michel Weststrate
The computed values should be pure functions, and in terms of depending only on observable values or other computed values they should have no side effects. Computed properties are lazily evaluated, and their value is evaluated only when their value is requested. The computed values are also cached in MobX, and this cached value is returned when this computed property is accessed. When there’s a change in any of the observable values being used in it, the Computed property is recomputed.
The computed values should be pure functions, and in terms of depending only on observable values or other computed values they should have no side effects. Computed properties are lazily evaluated, and their value is evaluated only when their value is requested. The computed values are also cached in MobX, and this cached value is returned when this computed property is accessed. When there's a change in any of the observable values being used in it, the Computed property is recomputed.
MST Views are derived from the current observable state. Views can be with or without arguments. Views without arguments are basically Computed values from MobX, defined using getter functions. When an observable value is changed from an MST action, the affected view gets recomputed, triggering a change (reaction) in the @observer components.
MST Views are derived from the current observable state. Views can be with or without arguments. Views without arguments are basically Computed values from MobX, defined using getter functions. When an observable value is changed from an MST action, the affected view gets recomputed, triggering a change (reaction) in the @observer components.
Adding tests for genre filter (Adding tests for genre filter)
We know that there are seven Nonfiction books in the mock data. Let’s now add a test for filtering by genre:
We know that there are seven Nonfiction books in the mock data. Let's now add a test for filtering by genre :
it(`Books are sorted by title`, async () => { store.setGenre('Nonfiction') const books = store.sortedBooks expect(books.length).toBe(7)})
To make filtering by genre work, we’ll add a genre field of string type in our Book model, and map it to the volumeInfo.categories[0] received from the API response. We’ll also change the sortedBooks view getter in our BookStoremodel to filter the books before sorting them:
To make filtering by genre work, we'll add a genre field of string type in our Book model, and map it to the volumeInfo.categories[0] received from the API response. We'll also change the sortedBooks view getter in our BookStore model to filter the books before sorting them:
Update the UI on tab change (Update the UI on tab change)
NOTE: From here on, we’ll use the mock data for our actual API calls instead of making Ajax requests to Google Books API. To do this, I’ve changed the bookApi in the stores/book/index.js to point to the mock API (./mock-api/api.js).
NOTE : From here on, we'll use the mock data for our actual API calls instead of making Ajax requests to Google Books API. To do this, I've changed the bookApi in the stores/book/index.js to point to the mock API ( ./mock-api/api.js ).
Note also that the display for all three tabs (“All”, “Fiction” and “NonFiction”) is similar. The layout and format of the items would be the same, but the only difference is the data that they’ll display. And since MobX allows us to keep our data completely separate from the view, we can get rid of the three separate views, and use the same component for all the three tabs.
Note also that the display for all three tabs (“All”, “Fiction” and “NonFiction”) is similar. The layout and format of the items would be the same, but the only difference is the data that they'll display. And since MobX allows us to keep our data completely separate from the view, we can get rid of the three separate views, and use the same component for all the three tabs.
This means that we don’t need the three separate tabs anymore. So we’ll delete the book-type-tabs.js file, and use the BookListView component directly in our TabNavigator for all three tabs. We’ll use the tabBarOnPress callback to trigger the call to setGenre() in our BookStore. The routeName, available on the navigation state object, is passed in to setGenre() to update the filter when user presses a tab.
This means that we don't need the three separate tabs anymore. So we'll delete the book-type-tabs.js file, and use the BookListView component directly in our TabNavigator for all three tabs. We'll use the tabBarOnPress callback to trigger the call to setGenre() in our BookStore . The routeName , available on the navigation state object, is passed in to setGenre() to update the filter when user presses a tab.
Note that we’re wrapping createBottomTabNavigator in MobX observer. This is what converts a React component class or stand-alone render function into a reactive component. In our case, we want the filter in our BookStore to change when tabBarOnPress is called.
Note that we're wrapping createBottomTabNavigator in MobX observer . This is what converts a React component class or stand-alone render function into a reactive component. In our case, we want the filter in our BookStore to change when tabBarOnPress is called.
We’ll also change the view to get sortedBooks instead of books.
We'll also change the view to get sortedBooks instead of books.
Our Book list just lists the name and author of each book, but we haven’t added any styling to it yet. Let’s do that using the ListItem component from react-native-elements. This is a simple change:
Our Book list just lists the name and author of each book, but we haven't added any styling to it yet. Let's do that using the ListItem component from react-native-elements . This is a simple change:
// src/views/book/components/Book.js
import { ListItem } from 'react-native-elements'
export default observer(({ book }) => ( ))
And here’s what our view looks like now:
And here's what our view looks like now:
![BookList with react-native-elements.png](./BookList with react-native-elements.png)
![BookList with react-native-elements.png](./BookList with react-native-elements.png)
Here’s the diff of our recent changes.
Here's the diff of our recent changes .
Add Book details (Add Book details)
We’ll add a field selectedBook to our BookStore which will point to the selected Book model.
We'll add a field selectedBook to our BookStore which will point to the selected Book model.
selectedBook: t.maybe(t.reference(Book))
We’re using a MST reference for our selectedBook observable. References in MST stores make it easy to make references to data and interact with it, while keeping the data normalized in the background.
We're using a MST reference for our selectedBook observable. References in MST stores make it easy to make references to data and interact with it, while keeping the data normalized in the background.
We’ll also add an action to change this reference:
We'll also add an action to change this reference:
const selectBook = book => { self.selectedBook = book}
When a user taps on a book in the BookListView, we want to navigate the user to the BookDetail screen. So we’ll create a showBookDetail function for this, and pass it as a prop to the child components:
When a user taps on a book in the BookListView , we want to navigate the user to the BookDetail screen. So we'll create a showBookDetail function for this, and pass it as a prop to the child components:
// src/views/book/components/BookListView.jsconst showBookDetail = book => { this.store.selectBook(book) this.props.navigation.navigate('BookDetail')}
In the Book component, we call the above showBookDetail function on onPressevent on the Book ListItem:
In the Book component, we call the above showBookDetail function on onPress event on the Book ListItem :
// src/views/book/components/Book.js
onPress={() => showBookDetail(book)}
Let’s now create the BookDetailView that will be displayed when a user presses a book:
Let's now create the BookDetailView that will be displayed when a user presses a book:
// src/views/book/components/BookDetailView.js
export default observer(() => { const store = BkStore() const book = store.selectedBook
Previously we only had tabs, but now we want to show the detail when the user taps on a book. So we’ll export a createStackNavigator instead of exporting createBottomTabNavigator directly. The createStackNavigator will have two screens on the stack, the BookList and the BookDetail screen:
Previously we only had tabs, but now we want to show the detail when the user taps on a book. So we'll export a createStackNavigator instead of exporting createBottomTabNavigator directly. The createStackNavigator will have two screens on the stack, the BookList and the BookDetail screen:
Note that we’re having the List view and the Detail view inside the createStackNavigator. This is because we want to share the the same BookDetailView only with different content (filtered books). If we wanted a different detail view to show up from different tabs, then we would have created two separate StackNavigators, and included them inside a TabNavigator. Something like this:
Note that we're having the List view and the Detail view inside the createStackNavigator . This is because we want to share the the same BookDetailView only with different content (filtered books). If we wanted a different detail view to show up from different tabs, then we would have created two separate StackNavigators, and included them inside a TabNavigator. 像这样:
Abstract Factory:提供一个创建一系列相关或相互依赖对象的接口,而无需指定它们具体的类。 Adapter:将一个类的接口转换成客户希望的另外一个接口。A d a p t e r模式使得原本由于接口不兼容而不能一起工作的那些类可以一起工作。 Bridge:将抽象部分与它的实现部分分离,使它们都可以独立地变化。 Builder:将一个复杂对象的构建与它的表示分离,使得同
import java.util.LinkedList;
public class CaseInsensitiveTrie {
/**
字典树的Java实现。实现了插入、查询以及深度优先遍历。
Trie tree's java implementation.(Insert,Search,DFS)
Problem Description
Igna
/*
2013年3月11日20:37:32
地点:北京潘家园
功能:完成用户格式化输入多个值
目的:学习scanf函数的使用
*/
# include <stdio.h>
int main(void)
{
int i, j, k;
printf("please input three number:\n"); //提示用
数据表中有记录的time字段(属性为timestamp)其值为:“0000-00-00 00:00:00”
程序使用select 语句从中取数据时出现以下异常:
java.sql.SQLException:Value '0000-00-00' can not be represented as java.sql.Date
java.sql.SQLException: Valu