1import { websiteOrigin } from '../../apiClient';
2import { openAuthSessionAsync, getAuthSchemeAsync } from '../../native-modules/DevLauncherAuth';
3import { startAuthSessionAsync } from '../startAuthSessionAsync';
4
5const mockOpenAuthSessionAsync = openAuthSessionAsync as jest.Mock;
6const mockGetAuthSchemeAsync = getAuthSchemeAsync as jest.Mock;
7
8describe('startAuthSessionAsync()', () => {
9  beforeEach(() => {
10    mockOpenAuthSessionAsync.mockResolvedValue({
11      type: 'success',
12      url: 'http://yay/123?session_secret=123',
13    });
14  });
15
16  afterEach(() => {
17    mockOpenAuthSessionAsync.mockClear();
18    mockGetAuthSchemeAsync.mockClear();
19  });
20
21  test('signup calls openAuthSessionAsync()', async () => {
22    const fakeScheme = 'test-scheme';
23    mockGetAuthSchemeAsync.mockResolvedValue(fakeScheme);
24
25    expect(openAuthSessionAsync).not.toHaveBeenCalled();
26    expect(getAuthSchemeAsync).not.toHaveBeenCalled();
27    await startAuthSessionAsync('signup');
28    expect(getAuthSchemeAsync).toHaveBeenCalled();
29    expect(openAuthSessionAsync).toHaveBeenCalled();
30    expect(openAuthSessionAsync).toHaveBeenCalledWith(
31      expect.stringContaining('signup'),
32      expect.stringMatching(fakeScheme)
33    );
34    expect(openAuthSessionAsync).toHaveBeenCalledWith(
35      expect.stringContaining(websiteOrigin),
36      expect.stringMatching(fakeScheme)
37    );
38  });
39
40  test('login calls openAuthSessionAsync()', async () => {
41    const fakeScheme = 'another-test-scheme';
42    mockGetAuthSchemeAsync.mockResolvedValue(fakeScheme);
43
44    expect(openAuthSessionAsync).not.toHaveBeenCalled();
45    expect(getAuthSchemeAsync).not.toHaveBeenCalled();
46
47    await startAuthSessionAsync('login');
48
49    expect(openAuthSessionAsync).toHaveBeenCalled();
50    expect(getAuthSchemeAsync).toHaveBeenCalled();
51    expect(openAuthSessionAsync).toHaveBeenCalledWith(
52      expect.stringContaining('login'),
53      expect.stringMatching(fakeScheme)
54    );
55    expect(openAuthSessionAsync).toHaveBeenCalledWith(
56      expect.stringContaining(websiteOrigin),
57      expect.stringMatching(fakeScheme)
58    );
59  });
60});
61