Skip to content

Commit

Permalink
chore: refactor for esier extensibility
Browse files Browse the repository at this point in the history
  • Loading branch information
alvarolorentedev committed Aug 2, 2024
1 parent ba16f50 commit 5cc03ad
Show file tree
Hide file tree
Showing 35 changed files with 108 additions and 98 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import React from 'react';
import { render, fireEvent, waitFor, screen } from '@testing-library/react';
import '@testing-library/jest-dom';
import { CCTray } from './index';
import { CCTray } from './';
import { faker } from '@faker-js/faker';

jest.mock('@mui/material/TextField', () => ({
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { CCTray } from './CCTray';
import { CCTray } from './';
import { faker } from '@faker-js/faker';
import { Status } from '../../types/Status';
import { CCTrayConfiguration } from '../../types/CCTrayConfiguration';
import { Status } from '../../../types/Status';
import { CCTrayConfiguration } from '../../../types/CCTrayConfiguration';

const fetchtMock = jest.fn();
jest.mock('electron-fetch', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { State } from '../../types/State';
import { Observer } from '../../types/Observer';
import { CCTrayConfiguration } from '../../types/CCTrayConfiguration';
import { Status } from '../../types/Status';
import { State } from '../../../types/State';
import { Observer } from '../../../types/Observer';
import { CCTrayConfiguration } from '../../../types/CCTrayConfiguration';
import { Status } from '../../../types/Status';
import { XMLParser } from 'fast-xml-parser';
import fetch from 'electron-fetch';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import React from 'react';
import { render, fireEvent, waitFor, screen } from '@testing-library/react';
import '@testing-library/jest-dom';
import { DatadogMonitor } from './';
import { DatadogMonitor } from '.';
import { faker } from '@faker-js/faker';

jest.mock('@mui/material/TextField', () => ({
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { DatadogMonitor } from './DatadogMonitor';
import { DatadogMonitor } from '.';
import { faker } from '@faker-js/faker';
import { Status } from '../../types/Status';
import { DetadogMonitorConfiguration } from '../../types/DetadogMonitorConfiguration';
import { Status } from '../../../types/Status';
import { DetadogMonitorConfiguration } from '../../../types/DetadogMonitorConfiguration';
import { v1 } from '@datadog/datadog-api-client';
import { ServerConfiguration } from '@datadog/datadog-api-client/dist/packages/datadog-api-client-common';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { State } from '../../types/State';
import { Observer } from '../../types/Observer';
import { DetadogMonitorConfiguration } from '../../types/DetadogMonitorConfiguration';
import { State } from '../../../types/State';
import { Observer } from '../../../types/Observer';
import { DetadogMonitorConfiguration } from '../../../types/DetadogMonitorConfiguration';
import { client, v1 } from '@datadog/datadog-api-client';
import { Status } from '../../types/Status';
import { Status } from '../../../types/Status';
import { ServerConfiguration } from '@datadog/datadog-api-client/dist/packages/datadog-api-client-common';
import {
OK,
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { GithubActionConfiguration } from '../../types/GithubActionConfiguration';
import { GithubAction } from './GithubAction';
import { GithubActionConfiguration } from '../../../types/GithubActionConfiguration';
import { GithubAction } from '.';
import { faker } from '@faker-js/faker';
import { Status } from '../../types/Status';
import { Status } from '../../../types/Status';

const requestMock = jest.fn();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Octokit } from 'octokit';
import { Observer } from '../../types/Observer';
import { State } from '../../types/State';
import { GithubActionConfiguration } from '../../types/GithubActionConfiguration';
import { Status } from '../../types/Status';
import { Observer } from '../../../types/Observer';
import { State } from '../../../types/State';
import { GithubActionConfiguration } from '../../../types/GithubActionConfiguration';
import { Status } from '../../../types/Status';

export class GithubAction implements Observer {
private octokit: Octokit;
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { faker } from '@faker-js/faker';
import { Status } from '../../../types/Status';
import { GrafanaConfiguration } from '../../../types/GrafanaConfiguration';
import { Grafana } from './index';
import { Grafana } from './';

const fetchtMock = jest.fn();
jest.mock('electron-fetch', () => {
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { faker } from '@faker-js/faker';
import { Status } from '../../types/Status';
import { NewRelicConfiguration } from '../../types/NewRelicConfiguration';
import { NewRelic } from './NewRelic';
import { Status } from '../../../types/Status';
import { NewRelicConfiguration } from '../../../types/NewRelicConfiguration';
import { NewRelic } from '.';

const fetchtMock = jest.fn();
jest.mock('electron-fetch', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { State } from '../../types/State';
import { Observer } from '../../types/Observer';
import { NewRelicConfiguration } from '../../types/NewRelicConfiguration';
import { Status } from '../../types/Status';
import { State } from '../../../types/State';
import { Observer } from '../../../types/Observer';
import { NewRelicConfiguration } from '../../../types/NewRelicConfiguration';
import { Status } from '../../../types/Status';
import fetch from 'electron-fetch';

export class NewRelic implements Observer {
Expand Down
17 changes: 17 additions & 0 deletions src/extensions/observerBuiderMap.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { MapType } from '../types/MapType';
import { Observer } from '../types/Observer';
import { Sentry } from './sentry/observer';
import { NewRelic } from './newRelic/observer';
import { Grafana } from './grafana/observer';
import { GithubAction } from './github/observer';
import { CCTray } from './cctray/observer';
import { DatadogMonitor } from './datadog/observer';

export const ObserversBuildersMap: MapType<(config: any) => Observer> = {
githubAction: (configuration: any) => new GithubAction(configuration as any),
ccTray: (configuration: any) => new CCTray(configuration as any),
datadogMonitor: (configuration: any) => new DatadogMonitor(configuration as any),
sentry: (configuration: any) => new Sentry(configuration as any),
newRelic: (configuration: any) => new NewRelic(configuration as any),
grafana: (configuration: any) => new Grafana(configuration as any),
};
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,24 @@ import { render, fireEvent, screen } from '@testing-library/react';
import '@testing-library/jest-dom';
import { observersComponentBuilderMap } from './observersComponentBuilderMap';
import { faker } from '@faker-js/faker';
import { GithubAction } from '../../GithubAction';

jest.mock('../../GithubAction', () => ({
jest.mock('./github/component', () => ({
__esModule: true,
GithubAction: (props: any) => <input data-testid={`githubAction`} {...props} />,
}));
jest.mock('../../CCTray', () => ({
jest.mock('./cctray/component', () => ({
__esModule: true,
CCTray: (props: any) => <input data-testid={`ccTray`} {...props} />,
}));
jest.mock('../../DatadogMonitor', () => ({
jest.mock('./datadog/component', () => ({
__esModule: true,
DatadogMonitor: (props: any) => <input data-testid={`datadogMonitor`} {...props} />,
}));
jest.mock('../../Sentry', () => ({
jest.mock('./sentry/component', () => ({
__esModule: true,
Sentry: (props: any) => <input data-testid={`sentry`} {...props} />,
}));
jest.mock('../../NewRelic', () => ({
jest.mock('./newRelic/component', () => ({
__esModule: true,
NewRelic: (props: any) => <input data-testid={`newRelic`} {...props} />,
}));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import React from 'react';
import { MapType } from '../../../../types/MapType';
import { GithubAction } from '../../GithubAction';
import { CCTray } from '../../CCTray';
import { DatadogMonitor } from '../../DatadogMonitor';
import { Sentry } from '../../Sentry';
import { NewRelic } from '../../NewRelic';
import { Grafana } from '../../Grafana';
import { MapType } from '../types/MapType';
import { GithubAction } from './github/component';
import { CCTray } from './cctray/component';
import { DatadogMonitor } from './datadog/component';
import { Sentry } from './sentry/component';
import { NewRelic } from './newRelic/component';
import { Grafana } from './grafana/component';

export const observersComponentBuilderMap: MapType<
(observable: any, index: number, updateFieldWithValue: any, translate: any) => JSX.Element
Expand Down
10 changes: 10 additions & 0 deletions src/extensions/observersList.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { MapType } from '../types/MapType';

export const observersList: MapType<any> = [
{ value: 'githubAction', label: 'Github Action' },
{ value: 'ccTray', label: 'CCTray' },
{ value: 'datadogMonitor', label: 'Datadog Monitor' },
{ value: 'sentry', label: 'Sentry' },
{ value: 'newRelic', label: 'New Relic' },
{ value: 'grafana', label: 'Grafana' },
];
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { MapType } from '../../../../types/MapType';
import { MapType } from '../types/MapType';

export const observersTitleBuilderMap: MapType<(observable: any) => string> = {
githubAction: (observable: any) =>
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Sentry } from './Sentry';
import { Sentry } from '.';
import { faker } from '@faker-js/faker';
import { Status } from '../../types/Status';
import { SentryConfiguration } from '../../types/SentryConfiguration';
import { Status } from '../../../types/Status';
import { SentryConfiguration } from '../../../types/SentryConfiguration';

const fetchtMock = jest.fn();
jest.mock('electron-fetch', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { State } from '../../types/State';
import { Observer } from '../../types/Observer';
import { SentryConfiguration } from '../../types/SentryConfiguration';
import { Status } from '../../types/Status';
import { State } from '../../../types/State';
import { Observer } from '../../../types/Observer';
import { SentryConfiguration } from '../../../types/SentryConfiguration';
import { Status } from '../../../types/Status';
import fetch from 'electron-fetch';

export class Sentry implements Observer {
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { app, ipcMain } from 'electron';
import { appManager } from './main/AppManager';
import { TrayMenu } from './main/TrayMenu';
import { AppWindow } from './main/AppWindow';
import { ObserverManager } from './main/observers/ObserverManager';
import { ObserverManager } from './main/ObserverManager';
import { NotificationManager } from './main/NotificationManager';
import { dirname, resolve, basename } from 'path';
import updateElectronApp from 'update-electron-app';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import { Status } from '../../types/Status';
import { Status } from '../types/Status';
import { ObserverManager } from './ObserverManager';
import { TrayMenu } from '../TrayMenu';
import { NotificationManager } from '../NotificationManager';
import { State } from '../../types/State';
import { GithubAction } from './GithubAction';
import { CCTray } from './CCTray';
import { Sentry } from './Sentry';
import { DatadogMonitor } from './DatadogMonitor';
import { NewRelic } from './NewRelic';
import { TrayMenu } from './TrayMenu';
import { NotificationManager } from './NotificationManager';
import { State } from '../types/State';
import { GithubAction } from '../extensions/github/observer';
import { CCTray } from '../extensions/cctray/observer';
import { Sentry } from '../extensions/sentry/observer';
import { DatadogMonitor } from '../extensions/datadog/observer';
import { NewRelic } from '../extensions/newRelic/observer';

const storeGetMock = jest.fn();

jest.mock('../../store', () => ({
jest.mock('../store', () => ({
store: {
get: (...params: any[]) => storeGetMock(...params),
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,30 +1,16 @@
import { GithubAction } from './GithubAction';
import { CCTray } from './CCTray';
import { store } from '../../store';
import { TrayMenu } from '../TrayMenu';
import { NotificationManager } from '../NotificationManager';
import { State } from '../../types/State';
import { Observer } from '../../types/Observer';
import { ObserverConfiguration } from '../../types/ObserverConfiguration';
import { Status } from '../../types/Status';
import { DatadogMonitor } from './DatadogMonitor';
import { MapType } from '../../types/MapType';
import { Sentry } from './Sentry';
import { NewRelic } from './NewRelic';
import { Grafana } from './grafana';
import { store } from '../store';
import { TrayMenu } from './TrayMenu';
import { NotificationManager } from './NotificationManager';
import { State } from '../types/State';
import { Observer } from '../types/Observer';
import { ObserverConfiguration } from '../types/ObserverConfiguration';
import { Status } from '../types/Status';
import { ObserversBuildersMap } from '../extensions/observerBuiderMap';

export class ObserverManager {
private observers: Observer[];
private globalState: State;
private observersState: State[];
private readonly ObserversBuildersMap: MapType<(config: any) => Observer> = {
githubAction: (configuration: any) => new GithubAction(configuration as any),
ccTray: (configuration: any) => new CCTray(configuration as any),
datadogMonitor: (configuration: any) => new DatadogMonitor(configuration as any),
sentry: (configuration: any) => new Sentry(configuration as any),
newRelic: (configuration: any) => new NewRelic(configuration as any),
grafana: (configuration: any) => new Grafana(configuration as any),
};

constructor(
private tray: TrayMenu,
Expand Down Expand Up @@ -59,7 +45,7 @@ export class ObserverManager {
this.observers = (store.get('observables') as ObserverConfiguration[]).map(
(configuration: ObserverConfiguration) => {
try {
return this.ObserversBuildersMap[configuration.type](configuration);
return ObserversBuildersMap[configuration.type](configuration);
} catch (error) {
console.error(error);
}
Expand Down
8 changes: 4 additions & 4 deletions src/renderer/components/Observers/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import { render, fireEvent, waitFor, screen, within } from '@testing-library/rea
import '@testing-library/jest-dom';
import { Observers } from './index';
import { faker } from '@faker-js/faker';
import { observersComponentBuilderMap } from './helpers/observersComponentBuilderMap';
import { observersTitleBuilderMap } from './helpers/observersTitleBuilderMap';
import { observersComponentBuilderMap } from '../../../extensions/observersComponentBuilderMap';
import { observersTitleBuilderMap } from '../../../extensions/observersTitleBuilderMap';

jest.mock('./helpers/observersComponentBuilderMap', () => ({
jest.mock('../../../extensions/observersComponentBuilderMap', () => ({
__esModule: true,
observersComponentBuilderMap: {
githubAction: jest.fn(),
Expand All @@ -19,7 +19,7 @@ jest.mock('./helpers/observersComponentBuilderMap', () => ({
newRelic: jest.fn(),
},
}));
jest.mock('./helpers/observersTitleBuilderMap', () => ({
jest.mock('../../../extensions/observersTitleBuilderMap', () => ({
__esModule: true,
observersTitleBuilderMap: {
githubAction: jest.fn(),
Expand Down
14 changes: 6 additions & 8 deletions src/renderer/components/Observers/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ import ExpandMoreIcon from '@mui/icons-material/ExpandMore';
import MenuItem from '@mui/material/MenuItem';
import Select from '@mui/material/Select';
import TextField from '@mui/material/TextField';
import { observersComponentBuilderMap } from './helpers/observersComponentBuilderMap';
import { observersTitleBuilderMap } from './helpers/observersTitleBuilderMap';
import { observersComponentBuilderMap } from '../../../extensions/observersComponentBuilderMap';
import { observersTitleBuilderMap } from '../../../extensions/observersTitleBuilderMap';
import { ObserversParams } from '../../../types/ObserversParams';
import { observersList } from '../../../extensions/observersList';

export const Observers = ({ observables, add, remove, update, save, translate }: ObserversParams) => {
const getComponent = (observable: any, index: number, updateFieldWithValue: any): any => {
Expand Down Expand Up @@ -42,12 +43,9 @@ export const Observers = ({ observables, add, remove, update, save, translate }:
label="Observer Type"
onChange={(event: React.ChangeEvent<HTMLInputElement>) => update('type', index, event.target.value)}
>
<MenuItem value={'githubAction'}>Github Action</MenuItem>
<MenuItem value={'ccTray'}>CCTray</MenuItem>
<MenuItem value={'datadogMonitor'}>Datadog Monitor</MenuItem>
<MenuItem value={'sentry'}>Sentry</MenuItem>
<MenuItem value={'newRelic'}>New Relic</MenuItem>
<MenuItem value={'grafana'}>Grafana</MenuItem>
{observersList.map(({ value, lable }: any) => (
<MenuItem value={value}>{lable}</MenuItem>
))}
</Select>
{getComponent(observable, index, update)}
<TextField
Expand Down

0 comments on commit 5cc03ad

Please sign in to comment.