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