Tesla Inc. plans to invest $5 billion building a factory in China as the trade war with the U.S. makes establishing production in the world’s fastest-growing auto market more imperative for the electric-car pioneer, a person familiar with the matter said.