New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@dhis2/app-service-alerts

Package Overview
Dependencies
Maintainers
15
Versions
88
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@dhis2/app-service-alerts - npm Package Compare versions

Comparing version 3.0.0 to 3.1.0

237

build/cjs/__tests__/integration.test.js

@@ -38,4 +38,5 @@ "use strict";

const alert = result.current.alerts[0];
expect(alert).toEqual(expect.objectContaining({
expect(alert).toEqual({
id: 1,
remove: expect.any(Function),
message: 'test',

@@ -45,5 +46,3 @@ options: {

}
}));
expect(alert).toHaveProperty('remove');
expect(typeof alert.remove).toBe('function');
});
});

@@ -85,4 +84,5 @@ it('Can add an alert with dynamic arguments', () => {

const alert = result.current.alerts[0];
expect(alert).toEqual(expect.objectContaining({
expect(alert).toEqual({
id: 1,
remove: expect.any(Function),
message: 'Successfully deleted hendrik',

@@ -92,7 +92,5 @@ options: {

}
}));
expect(alert).toHaveProperty('remove');
expect(typeof alert.remove).toBe('function');
});
});
it('Can remove an alert', () => {
it('Can remove an alert with the hide function from useAlerts', () => {
const wrapper = ({

@@ -107,3 +105,4 @@ children

const {
show
show,
hide
} = (0, _index.useAlert)('test', {

@@ -114,3 +113,4 @@ permanent: true

alerts,
show
show,
hide
};

@@ -124,3 +124,51 @@ }, {

expect(result.current.alerts).toHaveLength(1);
expect(result.current.alerts[0]).toEqual({
id: 1,
remove: expect.any(Function),
message: 'test',
options: {
permanent: true
}
});
(0, _reactHooks.act)(() => {
result.current.hide();
});
expect(result.current.alerts).toHaveLength(0);
});
it('Can remove an alert with the remove function on the alert itself', () => {
const wrapper = ({
children
}) => /*#__PURE__*/_react.default.createElement(_index.AlertsProvider, null, children);
const {
result
} = (0, _reactHooks.renderHook)(() => {
const alerts = (0, _index.useAlerts)();
const {
show,
hide
} = (0, _index.useAlert)('test', {
permanent: true
});
return {
alerts,
show,
hide
};
}, {
wrapper
});
(0, _reactHooks.act)(() => {
result.current.show();
});
expect(result.current.alerts).toHaveLength(1);
expect(result.current.alerts[0]).toEqual({
id: 1,
remove: expect.any(Function),
message: 'test',
options: {
permanent: true
}
});
(0, _reactHooks.act)(() => {
result.current.alerts[0].remove();

@@ -156,3 +204,3 @@ });

});
it('Will create duplicate alerts when show is called multiple times in a render cycle', () => {
it('Will not create duplicate alerts when show is called multiple times in a render cycle', () => {
const wrapper = ({

@@ -182,4 +230,63 @@ children

});
expect(result.current.alerts).toHaveLength(2);
expect(result.current.alerts).toHaveLength(1);
});
it('Will update the alert if show is called with different arguments', () => {
const wrapper = ({
children
}) => /*#__PURE__*/_react.default.createElement(_index.AlertsProvider, null, children);
const {
result
} = (0, _reactHooks.renderHook)(() => {
const alerts = (0, _index.useAlerts)();
const {
show
} = (0, _index.useAlert)(({
message
}) => message, ({
options
}) => options);
return {
alerts,
show
};
}, {
wrapper
}); // Show alert for first time
const payload1 = {
message: 'Message 1',
options: {
permanent: true,
critical: true
}
};
(0, _reactHooks.act)(() => {
result.current.show(payload1);
});
expect(result.current.alerts).toHaveLength(1);
expect(result.current.alerts[0]).toEqual({ ...payload1,
id: 1,
remove: expect.any(Function),
options: { ...payload1.options
}
}); // Show alert for second time
const payload2 = {
message: 'Message 2',
options: {
success: true
}
};
(0, _reactHooks.act)(() => {
result.current.show(payload2);
});
expect(result.current.alerts).toHaveLength(1);
expect(result.current.alerts[0]).toEqual({ ...payload2,
id: 1,
remove: expect.any(Function),
options: { ...payload2.options
}
});
});
it('Will increment IDs when multiple alerts are added', () => {

@@ -231,16 +338,10 @@ const wrapper = ({

const alerts = (0, _index.useAlerts)();
const {
show: show1
} = (0, _index.useAlert)('show 1');
const {
show: show2
} = (0, _index.useAlert)('show 2');
const {
show: show3
} = (0, _index.useAlert)('show 3');
const alert1 = (0, _index.useAlert)('show 1');
const alert2 = (0, _index.useAlert)('show 2');
const alert3 = (0, _index.useAlert)('show 3');
return {
alerts,
show1,
show2,
show3
alert1,
alert2,
alert3
};

@@ -251,9 +352,9 @@ }, {

(0, _reactHooks.act)(() => {
result.current.show1();
result.current.show2();
result.current.show3();
result.current.alert1.show();
result.current.alert2.show();
result.current.alert3.show();
});
expect(result.current.alerts).toHaveLength(3);
(0, _reactHooks.act)(() => {
result.current.alerts[1].remove();
result.current.alert2.hide();
});

@@ -264,2 +365,80 @@ expect(result.current.alerts).toHaveLength(2);

});
it('Gets added into a new slot when show is called after hide', () => {
const wrapper = ({
children
}) => /*#__PURE__*/_react.default.createElement(_index.AlertsProvider, null, children);
const {
result
} = (0, _reactHooks.renderHook)(() => {
const alerts = (0, _index.useAlerts)();
const alert1 = (0, _index.useAlert)('show 1');
const alert2 = (0, _index.useAlert)('show 2');
return {
alerts,
alert1,
alert2
};
}, {
wrapper
});
(0, _reactHooks.act)(() => {
result.current.alert1.show();
result.current.alert2.show();
});
expect(result.current.alerts).toHaveLength(2);
expect(result.current.alerts[0].id).toBe(1);
expect(result.current.alerts[1].id).toBe(2);
(0, _reactHooks.act)(() => {
result.current.alert1.hide();
});
expect(result.current.alerts).toHaveLength(1);
expect(result.current.alerts[0].id).toBe(2);
(0, _reactHooks.act)(() => {
result.current.alert1.show();
});
expect(result.current.alerts).toHaveLength(2);
expect(result.current.alerts[0].id).toBe(2); // now is last item with id 3 because it was "re-added"
expect(result.current.alerts[1].id).toBe(3);
});
it('Gets added into a new slot when show is called after remove is called on an alert', () => {
const wrapper = ({
children
}) => /*#__PURE__*/_react.default.createElement(_index.AlertsProvider, null, children);
const {
result
} = (0, _reactHooks.renderHook)(() => {
const alerts = (0, _index.useAlerts)();
const alert1 = (0, _index.useAlert)('show 1');
const alert2 = (0, _index.useAlert)('show 2');
return {
alerts,
alert1,
alert2
};
}, {
wrapper
});
(0, _reactHooks.act)(() => {
result.current.alert1.show();
result.current.alert2.show();
});
expect(result.current.alerts).toHaveLength(2);
expect(result.current.alerts[0].id).toBe(1);
expect(result.current.alerts[1].id).toBe(2);
(0, _reactHooks.act)(() => {
result.current.alerts[0].remove();
});
expect(result.current.alerts).toHaveLength(1);
expect(result.current.alerts[0].id).toBe(2);
(0, _reactHooks.act)(() => {
result.current.alert1.show();
});
expect(result.current.alerts).toHaveLength(2);
expect(result.current.alerts[0].id).toBe(2); // now is last item with id 3 because it was "re-added"
expect(result.current.alerts[1].id).toBe(3);
});
});

4

build/cjs/__tests__/useAlert.test.js

@@ -12,3 +12,3 @@ "use strict";

describe('useAlert', () => {
it('Renders without crashing', () => {
it('Hook returns a show and hide function', () => {
const wrapper = ({

@@ -25,3 +25,5 @@ children

expect(typeof result.current.show).toBe('function');
expect(result.current).toHaveProperty('hide');
expect(typeof result.current.hide).toBe('function');
});
});

@@ -12,9 +12,8 @@ "use strict";

const noop = () => {
/* Do nothing */
const placeholder = () => {
throw new Error('This function is a placeholder used when creating the AlertsManagerContext, it should be overridden');
};
const defaultAlertsManager = {
remove: noop,
add: noop
add: placeholder
};

@@ -21,0 +20,0 @@

@@ -8,20 +8,26 @@ "use strict";

const createAlertManagerAlert = (alert, id, remove) => ({ ...alert,
id,
remove: () => remove(id)
});
const toVisibleAlertsArray = alertsMap => Array.from(alertsMap.values());
const makeAlertsManager = setAlerts => {
const alertsMap = new Map();
let id = 0;
const remove = id => {
setAlerts(alerts => alerts.filter(alert => alert.id !== id));
};
const add = (alert, alertRef) => {
var _alertRef$current$id, _alertRef$current;
const add = alert => {
setAlerts(alerts => [...alerts, createAlertManagerAlert(alert, ++id, remove)]);
const alertId = (_alertRef$current$id = (_alertRef$current = alertRef.current) === null || _alertRef$current === void 0 ? void 0 : _alertRef$current.id) !== null && _alertRef$current$id !== void 0 ? _alertRef$current$id : ++id;
const alertsMapAlert = { ...alert,
id: alertId,
remove: () => {
alertsMap.delete(alertId);
alertRef.current = null;
setAlerts(toVisibleAlertsArray(alertsMap));
}
};
alertsMap.set(alertId, alertsMapAlert);
setAlerts(toVisibleAlertsArray(alertsMap));
return alertsMapAlert;
};
return {
remove,
add

@@ -28,0 +34,0 @@ };

@@ -13,16 +13,22 @@ "use strict";

const useAlert = (message, options = {}) => {
const alertsManager = (0, _react.useContext)(_AlertsManagerContext.AlertsManagerContext);
const show = props => {
const {
add
} = (0, _react.useContext)(_AlertsManagerContext.AlertsManagerContext);
const alertRef = (0, _react.useRef)(null);
const show = (0, _react.useCallback)(props => {
const resolvedMessage = String(typeof message === 'function' ? message(props) : message);
const resolvedOptions = typeof options === 'function' ? options(props) : options;
const alert = {
alertRef.current = add({
message: resolvedMessage,
options: resolvedOptions
};
alertsManager.add(alert);
};
}, alertRef);
}, [add, message, options]);
const hide = (0, _react.useCallback)(() => {
var _alertRef$current, _alertRef$current$rem;
(_alertRef$current = alertRef.current) === null || _alertRef$current === void 0 ? void 0 : (_alertRef$current$rem = _alertRef$current.remove) === null || _alertRef$current$rem === void 0 ? void 0 : _alertRef$current$rem.call(_alertRef$current);
}, []);
return {
show
show,
hide
};

@@ -29,0 +35,0 @@ };

@@ -31,4 +31,5 @@ import { renderHook, act } from '@testing-library/react-hooks';

const alert = result.current.alerts[0];
expect(alert).toEqual(expect.objectContaining({
expect(alert).toEqual({
id: 1,
remove: expect.any(Function),
message: 'test',

@@ -38,5 +39,3 @@ options: {

}
}));
expect(alert).toHaveProperty('remove');
expect(typeof alert.remove).toBe('function');
});
});

@@ -78,4 +77,5 @@ it('Can add an alert with dynamic arguments', () => {

const alert = result.current.alerts[0];
expect(alert).toEqual(expect.objectContaining({
expect(alert).toEqual({
id: 1,
remove: expect.any(Function),
message: 'Successfully deleted hendrik',

@@ -85,7 +85,5 @@ options: {

}
}));
expect(alert).toHaveProperty('remove');
expect(typeof alert.remove).toBe('function');
});
});
it('Can remove an alert', () => {
it('Can remove an alert with the hide function from useAlerts', () => {
const wrapper = ({

@@ -100,3 +98,4 @@ children

const {
show
show,
hide
} = useAlert('test', {

@@ -107,3 +106,4 @@ permanent: true

alerts,
show
show,
hide
};

@@ -117,3 +117,51 @@ }, {

expect(result.current.alerts).toHaveLength(1);
expect(result.current.alerts[0]).toEqual({
id: 1,
remove: expect.any(Function),
message: 'test',
options: {
permanent: true
}
});
act(() => {
result.current.hide();
});
expect(result.current.alerts).toHaveLength(0);
});
it('Can remove an alert with the remove function on the alert itself', () => {
const wrapper = ({
children
}) => /*#__PURE__*/React.createElement(AlertsProvider, null, children);
const {
result
} = renderHook(() => {
const alerts = useAlerts();
const {
show,
hide
} = useAlert('test', {
permanent: true
});
return {
alerts,
show,
hide
};
}, {
wrapper
});
act(() => {
result.current.show();
});
expect(result.current.alerts).toHaveLength(1);
expect(result.current.alerts[0]).toEqual({
id: 1,
remove: expect.any(Function),
message: 'test',
options: {
permanent: true
}
});
act(() => {
result.current.alerts[0].remove();

@@ -149,3 +197,3 @@ });

});
it('Will create duplicate alerts when show is called multiple times in a render cycle', () => {
it('Will not create duplicate alerts when show is called multiple times in a render cycle', () => {
const wrapper = ({

@@ -175,4 +223,63 @@ children

});
expect(result.current.alerts).toHaveLength(2);
expect(result.current.alerts).toHaveLength(1);
});
it('Will update the alert if show is called with different arguments', () => {
const wrapper = ({
children
}) => /*#__PURE__*/React.createElement(AlertsProvider, null, children);
const {
result
} = renderHook(() => {
const alerts = useAlerts();
const {
show
} = useAlert(({
message
}) => message, ({
options
}) => options);
return {
alerts,
show
};
}, {
wrapper
}); // Show alert for first time
const payload1 = {
message: 'Message 1',
options: {
permanent: true,
critical: true
}
};
act(() => {
result.current.show(payload1);
});
expect(result.current.alerts).toHaveLength(1);
expect(result.current.alerts[0]).toEqual({ ...payload1,
id: 1,
remove: expect.any(Function),
options: { ...payload1.options
}
}); // Show alert for second time
const payload2 = {
message: 'Message 2',
options: {
success: true
}
};
act(() => {
result.current.show(payload2);
});
expect(result.current.alerts).toHaveLength(1);
expect(result.current.alerts[0]).toEqual({ ...payload2,
id: 1,
remove: expect.any(Function),
options: { ...payload2.options
}
});
});
it('Will increment IDs when multiple alerts are added', () => {

@@ -224,16 +331,10 @@ const wrapper = ({

const alerts = useAlerts();
const {
show: show1
} = useAlert('show 1');
const {
show: show2
} = useAlert('show 2');
const {
show: show3
} = useAlert('show 3');
const alert1 = useAlert('show 1');
const alert2 = useAlert('show 2');
const alert3 = useAlert('show 3');
return {
alerts,
show1,
show2,
show3
alert1,
alert2,
alert3
};

@@ -244,9 +345,9 @@ }, {

act(() => {
result.current.show1();
result.current.show2();
result.current.show3();
result.current.alert1.show();
result.current.alert2.show();
result.current.alert3.show();
});
expect(result.current.alerts).toHaveLength(3);
act(() => {
result.current.alerts[1].remove();
result.current.alert2.hide();
});

@@ -257,2 +358,80 @@ expect(result.current.alerts).toHaveLength(2);

});
it('Gets added into a new slot when show is called after hide', () => {
const wrapper = ({
children
}) => /*#__PURE__*/React.createElement(AlertsProvider, null, children);
const {
result
} = renderHook(() => {
const alerts = useAlerts();
const alert1 = useAlert('show 1');
const alert2 = useAlert('show 2');
return {
alerts,
alert1,
alert2
};
}, {
wrapper
});
act(() => {
result.current.alert1.show();
result.current.alert2.show();
});
expect(result.current.alerts).toHaveLength(2);
expect(result.current.alerts[0].id).toBe(1);
expect(result.current.alerts[1].id).toBe(2);
act(() => {
result.current.alert1.hide();
});
expect(result.current.alerts).toHaveLength(1);
expect(result.current.alerts[0].id).toBe(2);
act(() => {
result.current.alert1.show();
});
expect(result.current.alerts).toHaveLength(2);
expect(result.current.alerts[0].id).toBe(2); // now is last item with id 3 because it was "re-added"
expect(result.current.alerts[1].id).toBe(3);
});
it('Gets added into a new slot when show is called after remove is called on an alert', () => {
const wrapper = ({
children
}) => /*#__PURE__*/React.createElement(AlertsProvider, null, children);
const {
result
} = renderHook(() => {
const alerts = useAlerts();
const alert1 = useAlert('show 1');
const alert2 = useAlert('show 2');
return {
alerts,
alert1,
alert2
};
}, {
wrapper
});
act(() => {
result.current.alert1.show();
result.current.alert2.show();
});
expect(result.current.alerts).toHaveLength(2);
expect(result.current.alerts[0].id).toBe(1);
expect(result.current.alerts[1].id).toBe(2);
act(() => {
result.current.alerts[0].remove();
});
expect(result.current.alerts).toHaveLength(1);
expect(result.current.alerts[0].id).toBe(2);
act(() => {
result.current.alert1.show();
});
expect(result.current.alerts).toHaveLength(2);
expect(result.current.alerts[0].id).toBe(2); // now is last item with id 3 because it was "re-added"
expect(result.current.alerts[1].id).toBe(3);
});
});

@@ -5,3 +5,3 @@ import { renderHook } from '@testing-library/react-hooks';

describe('useAlert', () => {
it('Renders without crashing', () => {
it('Hook returns a show and hide function', () => {
const wrapper = ({

@@ -18,3 +18,5 @@ children

expect(typeof result.current.show).toBe('function');
expect(result.current).toHaveProperty('hide');
expect(typeof result.current.hide).toBe('function');
});
});
import React from 'react';
const noop = () => {
/* Do nothing */
const placeholder = () => {
throw new Error('This function is a placeholder used when creating the AlertsManagerContext, it should be overridden');
};
const defaultAlertsManager = {
remove: noop,
add: noop
add: placeholder
};
export const AlertsManagerContext = /*#__PURE__*/React.createContext(defaultAlertsManager);

@@ -1,21 +0,27 @@

const createAlertManagerAlert = (alert, id, remove) => ({ ...alert,
id,
remove: () => remove(id)
});
const toVisibleAlertsArray = alertsMap => Array.from(alertsMap.values());
export const makeAlertsManager = setAlerts => {
const alertsMap = new Map();
let id = 0;
const remove = id => {
setAlerts(alerts => alerts.filter(alert => alert.id !== id));
};
const add = (alert, alertRef) => {
var _alertRef$current$id, _alertRef$current;
const add = alert => {
setAlerts(alerts => [...alerts, createAlertManagerAlert(alert, ++id, remove)]);
const alertId = (_alertRef$current$id = (_alertRef$current = alertRef.current) === null || _alertRef$current === void 0 ? void 0 : _alertRef$current.id) !== null && _alertRef$current$id !== void 0 ? _alertRef$current$id : ++id;
const alertsMapAlert = { ...alert,
id: alertId,
remove: () => {
alertsMap.delete(alertId);
alertRef.current = null;
setAlerts(toVisibleAlertsArray(alertsMap));
}
};
alertsMap.set(alertId, alertsMapAlert);
setAlerts(toVisibleAlertsArray(alertsMap));
return alertsMapAlert;
};
return {
remove,
add
};
};

@@ -1,19 +0,25 @@

import { useContext } from 'react';
import { useContext, useRef, useCallback } from 'react';
import { AlertsManagerContext } from './AlertsManagerContext';
export const useAlert = (message, options = {}) => {
const alertsManager = useContext(AlertsManagerContext);
const show = props => {
const {
add
} = useContext(AlertsManagerContext);
const alertRef = useRef(null);
const show = useCallback(props => {
const resolvedMessage = String(typeof message === 'function' ? message(props) : message);
const resolvedOptions = typeof options === 'function' ? options(props) : options;
const alert = {
alertRef.current = add({
message: resolvedMessage,
options: resolvedOptions
};
alertsManager.add(alert);
};
}, alertRef);
}, [add, message, options]);
const hide = useCallback(() => {
var _alertRef$current, _alertRef$current$rem;
(_alertRef$current = alertRef.current) === null || _alertRef$current === void 0 ? void 0 : (_alertRef$current$rem = _alertRef$current.remove) === null || _alertRef$current$rem === void 0 ? void 0 : _alertRef$current$rem.call(_alertRef$current);
}, []);
return {
show
show,
hide
};
};
import React from 'react';
import { AlertsManagerAlert } from './types';
export declare const AlertsContext: React.Context<AlertsManagerAlert[]>;
import type { Alert } from './types';
export declare const AlertsContext: React.Context<Alert[]>;
import React from 'react';
import { AlertsManager } from './types';
import type { AlertsManager } from './types';
export declare const AlertsManagerContext: React.Context<AlertsManager>;

@@ -1,4 +0,4 @@

import React from 'react';
export declare const AlertsProvider: ({ children }: {
import React, { ReactElement } from 'react';
export declare const AlertsProvider: ({ children, }: {
children: React.ReactNode;
}) => JSX.Element;
}) => ReactElement;

@@ -1,5 +0,2 @@

/// <reference types="react" />
import { AlertsManager, AlertsManagerAlert } from './types';
declare type SetAlertsFunction = React.Dispatch<React.SetStateAction<AlertsManagerAlert[]>>;
export declare const makeAlertsManager: (setAlerts: SetAlertsFunction) => AlertsManager;
export {};
import type { Alert, AlertsManager } from './types';
export declare const makeAlertsManager: (setAlerts: React.Dispatch<React.SetStateAction<Alert[]>>) => AlertsManager;

@@ -1,30 +0,13 @@

/// <reference types="react" />
declare type AlertAction = {
label: string;
onClick: () => void;
};
export declare type AlertOptions = {
actions?: AlertAction[];
className?: string;
critical?: boolean;
dataTest?: string;
duration?: number;
icon?: boolean | React.ElementType;
permanent?: boolean;
success?: boolean;
warning?: boolean;
onHidden?: () => void;
};
export interface AlertsManager {
add: (alert: Alert) => void;
remove: (id: number) => void;
}
import { MutableRefObject } from 'react';
export declare type AlertOptions = Record<string, unknown>;
export declare type Alert = {
id?: number;
remove?: () => void;
message: string;
options: AlertOptions;
};
export interface AlertsManagerAlert extends Alert {
id: number;
remove: () => void;
}
export {};
export declare type AlertRef = MutableRefObject<Alert | null>;
export declare type AlertsMap = Map<number, Alert>;
export declare type AlertsManager = {
add: (alert: Alert, alertRef: AlertRef) => Alert;
};

@@ -1,4 +0,5 @@

import { AlertOptions } from './types';
import type { AlertOptions } from './types';
export declare const useAlert: (message: string | ((props: any) => string), options?: AlertOptions | ((props: any) => AlertOptions)) => {
show: (props?: any) => void;
hide: () => void;
};

@@ -1,1 +0,1 @@

export declare const useAlerts: () => import("./types").AlertsManagerAlert[];
export declare const useAlerts: () => import("./types").Alert[];
{
"name": "@dhis2/app-service-alerts",
"version": "3.0.0",
"version": "3.1.0",
"main": "./build/cjs/index.js",

@@ -5,0 +5,0 @@ "module": "./build/es/index.js",

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc