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