Locating the factory in Shanghai will put it close to Tesla’s Chinese production base after it began supplying batteries for Model 3 cars in China last year.