1import { mockFetchReturn } from '../../test-utils';
2import { getDevSessionsAsync } from '../getDevSessionsAsync';
3
4const mockFns = [fetch] as jest.Mock[];
5
6describe('getDevSessionsAsync()', () => {
7  beforeEach(() => {
8    mockFns.forEach((fn) => fn.mockClear());
9  });
10
11  test('authenticated call', async () => {
12    expect(fetch).not.toHaveBeenCalled();
13
14    await getDevSessionsAsync({ isAuthenticated: true, installationID: '321', isDevice: true });
15
16    expect(fetch).toHaveBeenCalledWith(
17      expect.stringContaining('/development-sessions'),
18      expect.any(Object)
19    );
20  });
21
22  test('does not use installationId if sessions are found via authenticated request', async () => {
23    mockFetchReturn({ data: [{ test: '123' }] });
24
25    expect(fetch).not.toHaveBeenCalled();
26
27    await getDevSessionsAsync({ isAuthenticated: true, installationID: '321', isDevice: true });
28
29    expect(fetch).toHaveBeenCalledWith(
30      expect.stringContaining('/development-sessions'),
31      expect.any(Object)
32    );
33
34    expect(fetch).not.toHaveBeenCalledWith(
35      expect.stringContaining('/development-sessions?deviceId'),
36      expect.any(Object)
37    );
38  });
39
40  test('uses installationId if no sessions are found via authenticated request', async () => {
41    mockFetchReturn({ data: [] });
42
43    expect(fetch).not.toHaveBeenCalled();
44
45    await getDevSessionsAsync({ isAuthenticated: true, installationID: '321', isDevice: true });
46
47    expect(fetch).toHaveBeenCalledWith(
48      expect.stringContaining('/development-sessions'),
49      expect.any(Object)
50    );
51
52    expect(fetch).toHaveBeenCalledWith(
53      expect.stringContaining('/development-sessions?deviceId'),
54      expect.any(Object)
55    );
56  });
57
58  test('installationId used when no authorized sessions are found', async () => {
59    mockFetchReturn({ data: [] });
60
61    expect(fetch).not.toHaveBeenCalled();
62
63    await getDevSessionsAsync({ isAuthenticated: false, installationID: '321', isDevice: true });
64
65    expect(fetch).toHaveBeenCalledWith(
66      expect.stringContaining(`/development-sessions?deviceId=321`),
67      expect.any(Object)
68    );
69  });
70});
71