1import { AppOwnership, ExecutionEnvironment } from 'expo-constants';
2
3import { describeManifestTypes, mockConstants } from './ManifestTestUtils';
4
5beforeEach(() => {
6  jest.resetModules();
7  jest.resetAllMocks();
8});
9
10describe('bare', () => {
11  const originalWarn = console.warn;
12  beforeEach(() => {
13    console.warn = jest.fn();
14  });
15  afterEach(() => (console.warn = originalWarn));
16
17  describeManifestTypes(
18    { id: 'test' },
19    { id: 'fake-uuid' }
20  )((manifestObj) => {
21    it(`throws if no scheme is provided or defined`, () => {
22      mockConstants({ executionEnvironment: ExecutionEnvironment.Bare }, manifestObj);
23      const { makeRedirectUri } = require('../AuthSession');
24      expect(() => makeRedirectUri()).toThrowError(/Linking requires a build-time /);
25    });
26    it(`uses native value`, () => {
27      mockConstants({ executionEnvironment: ExecutionEnvironment.Bare }, manifestObj);
28      const { makeRedirectUri } = require('../AuthSession');
29      // Test that the path is omitted
30      expect(makeRedirectUri({ path: 'bacon', native: 'value:/somn' })).toBe('value:/somn');
31    });
32  });
33});
34
35describe('Managed', () => {
36  describe('Standalone', () => {
37    describeManifestTypes(
38      {
39        scheme: 'demo',
40        hostUri: 'exp.host/@test/test',
41      },
42      {
43        extra: {
44          expoClient: {
45            name: 'wat',
46            slug: 'wat',
47            scheme: 'demo',
48          },
49        },
50      }
51    )((manifestObj) => {
52      it(`creates a redirect URL`, () => {
53        mockConstants(
54          {
55            linkingUri: 'exp://exp.host/@test/test',
56            appOwnership: AppOwnership.Standalone,
57            executionEnvironment: ExecutionEnvironment.Standalone,
58          },
59          manifestObj
60        );
61        const { makeRedirectUri } = require('../AuthSession');
62        expect(makeRedirectUri()).toBe('demo://');
63      });
64    });
65
66    describeManifestTypes(
67      {
68        scheme: 'demo',
69      },
70      {
71        extra: {
72          expoClient: {
73            name: 'wat',
74            slug: 'wat',
75            scheme: 'demo',
76          },
77        },
78      }
79    )((manifestObj) => {
80      it(`creates a redirect URL with a custom path`, () => {
81        mockConstants(
82          {
83            linkingUri: 'exp://exp.host/@test/test',
84            appOwnership: AppOwnership.Standalone,
85            executionEnvironment: ExecutionEnvironment.Standalone,
86          },
87          manifestObj
88        );
89        const { makeRedirectUri } = require('../AuthSession');
90        expect(makeRedirectUri({ path: 'bacon' })).toBe('demo://bacon');
91      });
92    });
93
94    describeManifestTypes(
95      {
96        scheme: 'demo',
97      },
98      {
99        extra: {
100          expoClient: {
101            name: 'wat',
102            slug: 'wat',
103            scheme: 'demo',
104          },
105        },
106      }
107    )((manifestObj) => {
108      it(`uses native instead of generating a value`, () => {
109        mockConstants(
110          {
111            linkingUri: 'exp://exp.host/@test/test',
112            appOwnership: AppOwnership.Standalone,
113            executionEnvironment: ExecutionEnvironment.Standalone,
114          },
115          manifestObj
116        );
117        const { makeRedirectUri } = require('../AuthSession');
118        expect(
119          makeRedirectUri({
120            native: 'native.thing://somn',
121          })
122        ).toBe('native.thing://somn');
123      });
124    });
125
126    describe('Production', () => {
127      describeManifestTypes(
128        {
129          scheme: 'demo',
130          hostUri: 'exp.host/@test/test',
131        },
132        {
133          extra: {
134            expoClient: {
135              name: 'wat',
136              slug: 'wat',
137              scheme: 'demo',
138            },
139          },
140        }
141      )((manifestObj) => {
142        it(`creates a redirect URL`, () => {
143          mockConstants(
144            {
145              linkingUri: 'exp://exp.host/@test/test',
146              appOwnership: AppOwnership.Expo,
147              executionEnvironment: ExecutionEnvironment.StoreClient,
148            },
149            manifestObj
150          );
151          const { makeRedirectUri } = require('../AuthSession');
152
153          expect(makeRedirectUri()).toBe('exp://exp.host/@test/test');
154        });
155      });
156
157      describeManifestTypes(
158        {
159          scheme: 'demo',
160          hostUri: 'exp.host/@test/test',
161        },
162        {
163          extra: {
164            expoClient: {
165              name: 'wat',
166              slug: 'wat',
167              scheme: 'demo',
168            },
169          },
170        }
171      )((manifestObj) => {
172        it(`creates a redirect URL with a custom path`, () => {
173          mockConstants(
174            {
175              linkingUri: 'exp://exp.host/@test/test',
176              appOwnership: AppOwnership.Expo,
177              executionEnvironment: ExecutionEnvironment.StoreClient,
178            },
179            manifestObj
180          );
181
182          const { makeRedirectUri } = require('../AuthSession');
183
184          expect(makeRedirectUri({ path: 'bacon' })).toBe('exp://exp.host/@test/test/--/bacon');
185        });
186      });
187    });
188
189    describe('Development', () => {
190      describeManifestTypes(
191        {
192          scheme: 'demo',
193          hostUri: '192.168.1.4:19000',
194          developer: {
195            projectRoot: '/Users/person/myapp',
196            tool: 'expo-cli',
197          },
198        },
199        {
200          extra: {
201            expoClient: {
202              name: 'wat',
203              slug: 'wat',
204              scheme: 'demo',
205              hostUri: '192.168.1.4:19000',
206            },
207            expoGo: {
208              developer: {
209                projectRoot: '/Users/person/myapp',
210                tool: 'expo-cli',
211              },
212            },
213          },
214        }
215      )((manifestObj) => {
216        const devConstants = {
217          linkingUri: 'exp://192.168.1.4:19000/',
218          experienceUrl: 'exp://192.168.1.4:19000',
219          appOwnership: AppOwnership.Expo,
220          executionEnvironment: ExecutionEnvironment.StoreClient,
221        };
222
223        it(`creates a redirect URL`, () => {
224          mockConstants(devConstants, manifestObj);
225          const { makeRedirectUri } = require('../AuthSession');
226          expect(makeRedirectUri()).toBe('exp://192.168.1.4:19000');
227        });
228        it(`prefers localhost`, () => {
229          mockConstants(devConstants, manifestObj);
230          const { makeRedirectUri } = require('../AuthSession');
231          expect(makeRedirectUri({ preferLocalhost: true })).toBe('exp://localhost:19000');
232        });
233        it(`creates a redirect URL with a custom path`, () => {
234          mockConstants(devConstants, manifestObj);
235          const { makeRedirectUri } = require('../AuthSession');
236          expect(makeRedirectUri({ path: 'bacon' })).toBe('exp://192.168.1.4:19000/--/bacon');
237        });
238      });
239    });
240
241    describe('Proxy', () => {
242      describeManifestTypes(
243        {
244          id: 'fake',
245          originalFullName: '@test/originaltest',
246        },
247        {
248          extra: {
249            expoClient: {
250              name: 'wat',
251              slug: 'wat',
252              originalFullName: '@test/originaltest',
253            },
254          },
255        }
256      )((manifestObj) => {
257        it(`creates a redirect URL with useProxy`, () => {
258          mockConstants({}, manifestObj);
259
260          const { makeRedirectUri } = require('../AuthSession');
261
262          // Should create a proxy URL and omit the extra path component
263          expect(makeRedirectUri({ path: 'bacon', useProxy: true })).toBe(
264            'https://auth.expo.io/@test/originaltest'
265          );
266        });
267      });
268    });
269  });
270});
271