Skip to content

Commit

Permalink
small fix and unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
marcomura committed Feb 21, 2025
1 parent f51751d commit 78fdba9
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 2 deletions.
106 changes: 106 additions & 0 deletions src/analytics.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
import { viewScreenEvent } from './analytics';

interface MockedData {
getFirstAAID_value?: boolean;
}

const mockedData: MockedData = {};

jest.mock('./container', () => ({
Container: { siteManager: { getFirstAAID: () => mockedData.getFirstAAID_value } },
}));

function setProcessPlatform(platform: NodeJS.Platform) {
Object.defineProperty(process, 'platform', {
value: platform,
writable: false,
});
}

describe('viewScreenEvent', () => {
const originalPlatform = process.platform;

beforeAll(() => {
setProcessPlatform('win32');
});

beforeEach(() => {
mockedData.getFirstAAID_value = true;
});

afterAll(() => {
setProcessPlatform(originalPlatform);
});

it('should create a screen event with the correct screen name', async () => {
const screenName = 'testScreen';
const event = await viewScreenEvent(screenName);
expect(event.name).toEqual(screenName);
expect(event.screenEvent.attributes).toBeUndefined();
});

it('should exclude from activity if screen name is atlascodeWelcomeScreen', async () => {
const screenName = 'atlascodeWelcomeScreen';
const event = await viewScreenEvent(screenName);
expect(event.screenEvent.attributes.excludeFromActivity).toBeTruthy();
});

it('should include site information if provided (cloud)', async () => {
const screenName = 'testScreen';
const site: any = {
id: 'siteId',
product: { name: 'Jira', key: 'jira' },
isCloud: true,
};
const event = await viewScreenEvent(screenName, site);
expect(event.screenEvent.attributes.instanceType).toEqual('cloud');
expect(event.screenEvent.attributes.hostProduct).toEqual('Jira');
});

it('should include site information if provided (server)', async () => {
const screenName = 'testScreen';
const site: any = {
id: 'siteId',
product: { name: 'Jira', key: 'jira' },
isCloud: false,
};
const event = await viewScreenEvent(screenName, site);
expect(event.screenEvent.attributes.instanceType).toEqual('server');
expect(event.screenEvent.attributes.hostProduct).toEqual('Jira');
});

it('should include product information if provided', async () => {
const screenName = 'testScreen';
const product = { name: 'Bitbucket', key: 'bitbucket' };
const event = await viewScreenEvent(screenName, undefined, product);
expect(event.screenEvent.attributes.hostProduct).toEqual('Bitbucket');
});

it('should set platform based on process.platform (win32)', async () => {
setProcessPlatform('win32');
const screenName = 'testScreen';
const event = await viewScreenEvent(screenName);
expect(event.screenEvent.platform).toEqual('windows');
});

it('should set platform based on process.platform (darwin)', async () => {
setProcessPlatform('darwin');
const screenName = 'testScreen';
const event = await viewScreenEvent(screenName);
expect(event.screenEvent.platform).toEqual('mac');
});

it('should set platform based on process.platform (linux)', async () => {
setProcessPlatform('linux');
const screenName = 'testScreen';
const event = await viewScreenEvent(screenName);
expect(event.screenEvent.platform).toEqual('linux');
});

it('should set platform based on process.platform (aix)', async () => {
setProcessPlatform('aix');
const screenName = 'testScreen';
const event = await viewScreenEvent(screenName);
expect(event.screenEvent.platform).toEqual('desktop');
});
});
4 changes: 2 additions & 2 deletions src/analytics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -667,13 +667,13 @@ function instanceType(
site?: DetailedSiteInfo,
product?: Product,
): Record<string, any> {
eventProps.attributes = eventProps.attributes || {};

if (product) {
eventProps.attributes = eventProps.attributes || {};
eventProps.attributes.hostProduct = product.name;
}

if (site && !isEmptySiteInfo(site)) {
eventProps.attributes = eventProps.attributes || {};
eventProps.attributes.instanceType = site.isCloud ? 'cloud' : 'server';
eventProps.attributes.hostProduct = site.product.name;
}
Expand Down

0 comments on commit 78fdba9

Please sign in to comment.