1import HMRClient from '../../HMRClient';
2import LoadingView from '../../LoadingView';
3import { fetchThenEvalAsync } from '../fetchThenEval';
4import { loadBundleAsync } from '../loadBundle';
5
6jest.mock('../../getDevServer', () => ({
7  __esModule: true,
8  default: jest.fn(() => ({
9    bundleLoadedFromServer: true,
10    fullBundleUrl:
11      'http://localhost:19000?platform=android&modulesOnly=true&runModule=false&runtimeBytecodeVersion=null',
12    url: 'http://localhost:19000/',
13  })),
14}));
15
16jest.mock('../fetchThenEval', () => ({
17  fetchThenEvalAsync: jest.fn(async (): Promise<void> => {}),
18}));
19
20jest.mock('../../HMRClient', () => ({
21  __esModule: true,
22  default: { registerBundle: jest.fn() },
23}));
24
25jest.mock('../../LoadingView');
26
27const originalEnv = process.env.NODE_ENV;
28
29afterEach(() => {
30  process.env.NODE_ENV = originalEnv;
31
32  if (typeof location !== 'undefined') {
33    delete (global as any).location;
34  }
35});
36
37it('loads a bundle', async () => {
38  process.env.NODE_ENV = 'development';
39  await loadBundleAsync(
40    'Second.bundle?platform=ios&modulesOnly=true&runModule=false&runtimeBytecodeVersion='
41  );
42  expect(LoadingView.showMessage).toBeCalledWith('Downloading...', 'load');
43  expect(LoadingView.hide).toBeCalledWith();
44  const url =
45    'http://localhost:19000/Second.bundle?platform=ios&modulesOnly=true&runModule=false&runtimeBytecodeVersion=';
46  expect(HMRClient.registerBundle).toBeCalledWith(url);
47  expect(fetchThenEvalAsync).toBeCalledWith(url);
48});
49
50it('asserts in production when attempting to load a bundle and the user-defined origin is missing.', async () => {
51  process.env.NODE_ENV = 'production';
52
53  await expect(
54    loadBundleAsync(
55      'Second.bundle?platform=ios&modulesOnly=true&runModule=false&runtimeBytecodeVersion='
56    )
57  ).rejects.toThrow();
58  expect(LoadingView.showMessage).not.toBeCalled();
59  expect(LoadingView.hide).not.toBeCalled();
60  expect(HMRClient.registerBundle).not.toBeCalled();
61  expect(fetchThenEvalAsync).not.toBeCalled();
62});
63
64it('loads a bundle in production with user-defined location.origin', async () => {
65  process.env.NODE_ENV = 'production';
66
67  (global as any).location = {
68    origin: 'https://example.com',
69  };
70
71  await loadBundleAsync('/_expo/js/index.bundle');
72  expect(LoadingView.showMessage).not.toBeCalled();
73  expect(LoadingView.hide).not.toBeCalled();
74  const url = 'https://example.com/_expo/js/index.bundle';
75  expect(HMRClient.registerBundle).not.toBeCalled();
76  expect(fetchThenEvalAsync).toBeCalledWith(url);
77});
78