Skip to content

Commit

Permalink
!!!TASK: Delete obsolete redux-store elements for flash messages
Browse files Browse the repository at this point in the history
  • Loading branch information
grebaldi committed Jun 11, 2024
1 parent b8fa987 commit 03c503d
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 177 deletions.
108 changes: 1 addition & 107 deletions packages/neos-ui-redux-store/src/UI/FlashMessages/index.spec.js
Original file line number Diff line number Diff line change
@@ -1,117 +1,11 @@
import {actionTypes, actions, reducer} from './index';

import {actionTypes as system} from '../../System/index';
import {actionTypes, actions} from './index';

test(`should export actionTypes`, () => {
expect(actionTypes).not.toBe(undefined);
expect(typeof (actionTypes.ADD)).toBe('string');
expect(typeof (actionTypes.REMOVE)).toBe('string');
});

test(`should export action creators`, () => {
expect(actions).not.toBe(undefined);
expect(typeof (actions.add)).toBe('function');
expect(typeof (actions.remove)).toBe('function');
});

test(`should export a reducer`, () => {
expect(reducer).not.toBe(undefined);
expect(typeof (reducer)).toBe('function');
});

test(`The reducer should return a plain JS object as the initial state.`, () => {
const nextState = reducer(undefined, {
type: system.INIT
});

expect(typeof nextState).toBe('object');
});

test(`The "add" action should throw an error if no arguments where passed.`, () => {
const fn = () => reducer(undefined, actions.add());

expect(fn).toThrowError(
'Empty or non existent "id" passed to the addFlashMessage reducer. Please specify a string containing a random id.'
);
});

test(`The "add" action should throw an error no "message" was passed.`, () => {
const fn = () => reducer(undefined, actions.add('myMessageId', null));

expect(fn).toThrowError(
'Empty or non existent "message" passed to the addFlashMessage reducer. Please specify a string containing your desired message.'
);
});

test(`The "add" action should throw an error if an invalid "severity" was passed.`, () => {
const fn = () => reducer(undefined, actions.add('myMessageId', 'myMessage', null));

expect(fn).toThrowError(
'Invalid "severity" specified while adding a new FlashMessage. Allowed severities are success error info.'
);
});

test(`
The "add" action should be able to add the passed data as a new flashMessage
item.`, () => {
const state = {};
const nextState = reducer(state, actions.add('myMessageId', 'myMessage', 'error', 300));

const addedMessage = nextState.myMessageId;

expect(addedMessage).toEqual({
severity: 'error',
id: 'myMessageId',
message: 'myMessage',
timeout: 300
});
});

test(`
The "add" action should normalize the severity to lowercase for the new
flashMessage item.`, () => {
const state = {};
const nextState1 = reducer(state, actions.add('myMessageId', 'myMessage', 'Error', 300));
const nextState2 = reducer(state, actions.add('myMessageId', 'myMessage', 'ERROR', 300));
const nextState3 = reducer(state, actions.add('myMessageId', 'myMessage', 'eRrOr', 300));

const addedMessage1 = nextState1.myMessageId;
const addedMessage2 = nextState2.myMessageId;
const addedMessage3 = nextState3.myMessageId;

expect(addedMessage1.severity).toBe('error');
expect(addedMessage2.severity).toBe('error');
expect(addedMessage3.severity).toBe('error');
});

test(`
The "add" action should set a default timeout of "0" if none was passed for
the new flashMessage item.`, () => {
const state = {};
const nextState = reducer(state, actions.add('myMessageId', 'myMessage', 'error'));

const addedMessage = nextState.myMessageId;

expect(addedMessage.timeout).toBe(0);
});

test(`
The "remove" action should be able to remove an added flashMessage item for
the passed key.`, () => {
const state = {
someMessage: 'someMessage',
anotherMessage: 'anotherMessage'
};

const nextState1 = reducer(state, actions.remove('someMessage'));
const nextState2 = reducer(state, actions.remove('anotherMessage'));
const nextState3 = reducer(nextState1, actions.remove('anotherMessage'));

expect(nextState1).toEqual({
anotherMessage: 'anotherMessage'
});
expect(nextState2).toEqual({
someMessage: 'someMessage'
});
expect(nextState3).toEqual({});
});
69 changes: 2 additions & 67 deletions packages/neos-ui-redux-store/src/UI/FlashMessages/index.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,10 @@
import produce from 'immer';
import {action as createAction, ActionType} from 'typesafe-actions';

import {InitAction} from '../../System';

export interface FlashMessage extends Readonly<{
severity: string;
id: string;
message: string;
timeout: number;
}> {}

export interface State extends Readonly<{
[propName: string]: FlashMessage;
}> {}

export const defaultState: State = {};

//
// Export the action types
//
export enum actionTypes {
ADD = '@neos/neos-ui/UI/FlashMessages/ADD',
REMOVE = '@neos/neos-ui/UI/FlashMessages/REMOVE'
ADD = '@neos/neos-ui/UI/FlashMessages/ADD'
}

/**
Expand All @@ -39,59 +22,11 @@ const add = (id: string, message: string, severity: string, timeout: number = 0)
timeout
}));

/**
* Removes a flash message
*
* @param {String} id The flashMessage id to delete.
*/
const remove = (id: string) => createAction(actionTypes.REMOVE, ({
id
}));

//
// Export the actions
//
export const actions = {
add,
remove
add
};

export type Action = ActionType<typeof actions>;

//
// Export the reducer
//
export const reducer = (state: State = defaultState, action: InitAction | Action) => produce(state, draft => {
switch (action.type) {
case actionTypes.ADD: {
const message = action.payload;
const allowedSeverities = ['success', 'error', 'info'];
const {id, severity} = message;
const messageContents = message.message;

if (!id || id.length < 0) {
throw new Error('Empty or non existent "id" passed to the addFlashMessage reducer. Please specify a string containing a random id.');
}

if (!messageContents || messageContents.length < 0) {
throw new Error('Empty or non existent "message" passed to the addFlashMessage reducer. Please specify a string containing your desired message.');
}

if (!severity || allowedSeverities.indexOf(severity.toLowerCase()) < 0) {
throw new Error(`Invalid "severity" specified while adding a new FlashMessage. Allowed severities are ${allowedSeverities.join(' ')}.`);
}
message.severity = message.severity.toLowerCase();
draft[message.id] = message;
break;
}
case actionTypes.REMOVE: {
delete draft[action.payload.id];
break;
}
}
});

//
// Export the selectors
//
export const selectors = {};
3 changes: 0 additions & 3 deletions packages/neos-ui-redux-store/src/UI/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import * as ContentTree from './ContentTree';
// Export the reducer state shape interface
//
export interface State {
flashMessages: FlashMessages.State;
fullScreen: FullScreen.State;
keyboardShortcutModal: KeyboardShortcutModal.State;
leftSideBar: LeftSideBar.State;
Expand Down Expand Up @@ -98,7 +97,6 @@ export const actions = {
// Export the reducer
//
export const reducer = combineReducers({
flashMessages: FlashMessages.reducer,
fullScreen: FullScreen.reducer,
keyboardShortcutModal: KeyboardShortcutModal.reducer,
leftSideBar: LeftSideBar.reducer,
Expand All @@ -122,7 +120,6 @@ export const reducer = combineReducers({
// Export the selectors
//
export const selectors = {
FlashMessages: FlashMessages.selectors,
FullScreen: FullScreen.selectors,
KeyboardShortcutModal: KeyboardShortcutModal.selectors,
LeftSideBar: LeftSideBar.selectors,
Expand Down

0 comments on commit 03c503d

Please sign in to comment.