1import { buildAsyncRequire } from '../buildAsyncRequire';
2import { loadBundleAsync } from '../loadBundle';
3
4export const asMock = <T extends (...args: any[]) => any>(fn: T): jest.MockedFunction<T> =>
5  fn as jest.MockedFunction<T>;
6
7jest.mock('../loadBundle', () => ({
8  loadBundleAsync: jest.fn(async () => {}),
9}));
10
11const originalEnv = process.env.NODE_ENV;
12beforeEach(() => {
13  process.env.NODE_ENV = 'development';
14});
15
16afterAll(() => {
17  process.env.NODE_ENV = originalEnv;
18});
19
20it(`builds required object`, async () => {
21  const asyncRequire = buildAsyncRequire();
22  expect(asyncRequire).toBeInstanceOf(Function);
23});
24
25it(`loads the module with \`loadBundleAsync\` if the module has not been loaded already`, async () => {
26  const asyncRequire = buildAsyncRequire();
27
28  const myModule = asyncRequire('/bacon.bundle?platform=ios');
29  expect(myModule).toEqual(expect.any(Promise));
30
31  // Did attempt to fetch the bundle
32  expect(loadBundleAsync).toBeCalledWith('/bacon.bundle?platform=ios');
33});
34