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(`uses native value`, () => {
22      mockConstants({ executionEnvironment: ExecutionEnvironment.Bare }, manifestObj);
23      const { makeRedirectUri } = require('../AuthSession');
24      // Test that the path is omitted
25      expect(makeRedirectUri({ path: 'bacon', native: 'value:/somn' })).toBe('value:/somn');
26    });
27  });
28});
29
30describe('Managed', () => {
31  describe('Standalone', () => {
32    describeManifestTypes(
33      {
34        scheme: 'demo',
35        hostUri: 'exp.host/@test/test',
36      },
37      {
38        extra: {
39          expoClient: {
40            name: 'wat',
41            slug: 'wat',
42            scheme: 'demo',
43          },
44        },
45      }
46    )((manifestObj) => {
47      it(`creates a redirect URL`, () => {
48        mockConstants(
49          {
50            linkingUri: 'exp://exp.host/@test/test',
51            appOwnership: AppOwnership.Standalone,
52            executionEnvironment: ExecutionEnvironment.Standalone,
53          },
54          manifestObj
55        );
56        const { makeRedirectUri } = require('../AuthSession');
57        expect(makeRedirectUri()).toBe('demo://');
58      });
59    });
60
61    describeManifestTypes(
62      {
63        scheme: 'demo',
64      },
65      {
66        extra: {
67          expoClient: {
68            name: 'wat',
69            slug: 'wat',
70            scheme: 'demo',
71          },
72        },
73      }
74    )((manifestObj) => {
75      it(`creates a redirect URL with a custom path`, () => {
76        mockConstants(
77          {
78            linkingUri: 'exp://exp.host/@test/test',
79            appOwnership: AppOwnership.Standalone,
80            executionEnvironment: ExecutionEnvironment.Standalone,
81          },
82          manifestObj
83        );
84        const { makeRedirectUri } = require('../AuthSession');
85        expect(makeRedirectUri({ path: 'bacon' })).toBe('demo://bacon');
86      });
87    });
88
89    describeManifestTypes(
90      {
91        scheme: 'demo',
92      },
93      {
94        extra: {
95          expoClient: {
96            name: 'wat',
97            slug: 'wat',
98            scheme: 'demo',
99          },
100        },
101      }
102    )((manifestObj) => {
103      it(`uses native instead of generating a value`, () => {
104        mockConstants(
105          {
106            linkingUri: 'exp://exp.host/@test/test',
107            appOwnership: AppOwnership.Standalone,
108            executionEnvironment: ExecutionEnvironment.Standalone,
109          },
110          manifestObj
111        );
112        const { makeRedirectUri } = require('../AuthSession');
113        expect(
114          makeRedirectUri({
115            native: 'native.thing://somn',
116          })
117        ).toBe('native.thing://somn');
118      });
119    });
120
121    describe('Production', () => {
122      describeManifestTypes(
123        {
124          scheme: 'demo',
125          hostUri: 'exp.host/@test/test',
126        },
127        {
128          extra: {
129            expoClient: {
130              name: 'wat',
131              slug: 'wat',
132              scheme: 'demo',
133            },
134          },
135        }
136      )((manifestObj) => {
137        it(`creates a redirect URL`, () => {
138          mockConstants(
139            {
140              linkingUri: 'exp://exp.host/@test/test',
141              appOwnership: AppOwnership.Expo,
142              executionEnvironment: ExecutionEnvironment.StoreClient,
143            },
144            manifestObj
145          );
146          const { makeRedirectUri } = require('../AuthSession');
147
148          expect(makeRedirectUri()).toBe('exp://exp.host/@test/test');
149        });
150      });
151
152      describeManifestTypes(
153        {
154          scheme: 'demo',
155          hostUri: 'exp.host/@test/test',
156        },
157        {
158          extra: {
159            expoClient: {
160              name: 'wat',
161              slug: 'wat',
162              scheme: 'demo',
163            },
164          },
165        }
166      )((manifestObj) => {
167        it(`creates a redirect URL with a custom path`, () => {
168          mockConstants(
169            {
170              linkingUri: 'exp://exp.host/@test/test',
171              appOwnership: AppOwnership.Expo,
172              executionEnvironment: ExecutionEnvironment.StoreClient,
173            },
174            manifestObj
175          );
176
177          const { makeRedirectUri } = require('../AuthSession');
178
179          expect(makeRedirectUri({ path: 'bacon' })).toBe('exp://exp.host/@test/test/--/bacon');
180        });
181      });
182    });
183
184    describe('Development', () => {
185      describeManifestTypes(
186        {
187          scheme: 'demo',
188          hostUri: '192.168.1.4:8081',
189          developer: {
190            projectRoot: '/Users/person/myapp',
191            tool: 'expo-cli',
192          },
193        },
194        {
195          extra: {
196            expoClient: {
197              name: 'wat',
198              slug: 'wat',
199              scheme: 'demo',
200              hostUri: '192.168.1.4:8081',
201            },
202            expoGo: {
203              developer: {
204                projectRoot: '/Users/person/myapp',
205                tool: 'expo-cli',
206              },
207            },
208          },
209        }
210      )((manifestObj) => {
211        const devConstants = {
212          linkingUri: 'exp://192.168.1.4:8081/',
213          experienceUrl: 'exp://192.168.1.4:8081',
214          appOwnership: AppOwnership.Expo,
215          executionEnvironment: ExecutionEnvironment.StoreClient,
216        };
217
218        it(`creates a redirect URL`, () => {
219          mockConstants(devConstants, manifestObj);
220          const { makeRedirectUri } = require('../AuthSession');
221          expect(makeRedirectUri()).toBe('exp://192.168.1.4:8081');
222        });
223        it(`prefers localhost`, () => {
224          mockConstants(devConstants, manifestObj);
225          const { makeRedirectUri } = require('../AuthSession');
226          expect(makeRedirectUri({ preferLocalhost: true })).toBe('exp://localhost:8081');
227        });
228        it(`creates a redirect URL with a custom path`, () => {
229          mockConstants(devConstants, manifestObj);
230          const { makeRedirectUri } = require('../AuthSession');
231          expect(makeRedirectUri({ path: 'bacon' })).toBe('exp://192.168.1.4:8081/--/bacon');
232        });
233      });
234    });
235  });
236});
237