1b5b86a9aSCedric van Puttenimport os from 'os';
2b5b86a9aSCedric van Putten
3*91c8580dSWill Schurmanimport {
4*91c8580dSWill Schurman  getContext,
5*91c8580dSWill Schurman  getRudderAnalyticsClient,
6*91c8580dSWill Schurman  logEventAsync,
7*91c8580dSWill Schurman  resetInternalStateForTesting,
8*91c8580dSWill Schurman  setUserDataAsync,
9*91c8580dSWill Schurman} from '../rudderstackClient';
10b5b86a9aSCedric van Putten
11b5b86a9aSCedric van Puttenjest.mock('ci-info', () => ({ isCI: true, isPR: true, name: 'GitHub Actions' }));
12b5b86a9aSCedric van Putten
13*91c8580dSWill Schurmanjest.mock('@expo/rudder-sdk-node');
14*91c8580dSWill Schurman
15*91c8580dSWill Schurmandescribe(logEventAsync, () => {
16*91c8580dSWill Schurman  beforeEach(() => {
17*91c8580dSWill Schurman    resetInternalStateForTesting();
18*91c8580dSWill Schurman  });
19*91c8580dSWill Schurman
20*91c8580dSWill Schurman  it('logs when the user has been identified', async () => {
21*91c8580dSWill Schurman    await setUserDataAsync('fake', {});
22*91c8580dSWill Schurman    await logEventAsync('Start Project');
23*91c8580dSWill Schurman    expect(getRudderAnalyticsClient().track).toHaveBeenCalledWith({
24*91c8580dSWill Schurman      anonymousId: expect.any(String),
25*91c8580dSWill Schurman      context: getContext(),
26*91c8580dSWill Schurman      event: 'Start Project',
27*91c8580dSWill Schurman      properties: { source: 'expo', source_version: undefined },
28*91c8580dSWill Schurman      userId: 'fake',
29*91c8580dSWill Schurman    });
30*91c8580dSWill Schurman  });
31*91c8580dSWill Schurman
32*91c8580dSWill Schurman  it('does not log when the user has not been identified', async () => {
33*91c8580dSWill Schurman    await logEventAsync('Start Project');
34*91c8580dSWill Schurman    expect(getRudderAnalyticsClient().track).not.toHaveBeenCalled();
35*91c8580dSWill Schurman  });
36*91c8580dSWill Schurman});
37*91c8580dSWill Schurman
38b5b86a9aSCedric van Puttendescribe(getContext, () => {
39b5b86a9aSCedric van Putten  const originalVersion = process.env.__EXPO_VERSION;
40b5b86a9aSCedric van Putten  const knownPlatformNames = {
41b5b86a9aSCedric van Putten    darwin: 'Mac',
42b5b86a9aSCedric van Putten    linux: 'Linux',
43b5b86a9aSCedric van Putten    win32: 'Windows',
44b5b86a9aSCedric van Putten  };
45b5b86a9aSCedric van Putten
46b5b86a9aSCedric van Putten  beforeEach(() => {
47b5b86a9aSCedric van Putten    delete process.env.__EXPO_VERSION;
48b5b86a9aSCedric van Putten  });
49b5b86a9aSCedric van Putten
50b5b86a9aSCedric van Putten  afterAll(() => {
51b5b86a9aSCedric van Putten    process.env.__EXPO_VERSION = originalVersion;
52b5b86a9aSCedric van Putten  });
53b5b86a9aSCedric van Putten
54b5b86a9aSCedric van Putten  it('contains os name and version', () => {
55b5b86a9aSCedric van Putten    expect(getContext().os).toMatchObject({
56b5b86a9aSCedric van Putten      name: knownPlatformNames[os.platform()] || os.platform(),
57b5b86a9aSCedric van Putten      version: os.release(),
58b5b86a9aSCedric van Putten    });
59b5b86a9aSCedric van Putten  });
60b5b86a9aSCedric van Putten
61b5b86a9aSCedric van Putten  it('contains device type and model', () => {
62b5b86a9aSCedric van Putten    expect(getContext().device).toMatchObject({
63b5b86a9aSCedric van Putten      type: knownPlatformNames[os.platform()] || os.platform(),
64b5b86a9aSCedric van Putten      model: knownPlatformNames[os.platform()] || os.platform(),
65b5b86a9aSCedric van Putten    });
66b5b86a9aSCedric van Putten  });
67b5b86a9aSCedric van Putten
68b5b86a9aSCedric van Putten  it('contains app name and version', () => {
69b5b86a9aSCedric van Putten    process.env.__EXPO_VERSION = '1337';
70b5b86a9aSCedric van Putten    expect(getContext().app).toMatchObject({
71b5b86a9aSCedric van Putten      name: 'expo',
72b5b86a9aSCedric van Putten      version: '1337',
73b5b86a9aSCedric van Putten    });
74b5b86a9aSCedric van Putten  });
75b5b86a9aSCedric van Putten
76b5b86a9aSCedric van Putten  it('contains ci name and if its executed from PR', () => {
77b5b86a9aSCedric van Putten    expect(getContext().ci).toMatchObject({
78b5b86a9aSCedric van Putten      name: 'GitHub Actions',
79b5b86a9aSCedric van Putten      isPr: true,
80b5b86a9aSCedric van Putten    });
81b5b86a9aSCedric van Putten  });
82b5b86a9aSCedric van Putten});
83