Skip to content

Commit 3a899a7

Browse files
committed
setup alert reducer tests
1 parent d80ccc2 commit 3a899a7

File tree

3 files changed

+71
-6
lines changed

3 files changed

+71
-6
lines changed

src/modules/alert/actions.ts

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,25 @@
11
import {ALERT_CLOSE, ALERT_OPEN, ALERT_REPLAY} from './types';
22

3-
export function alertOpen(alert: Object): Redux.ThunkAction<any, {}, {}> {
4-
return (dispatch): void => {
5-
dispatch({ type: ALERT_OPEN, payload: { alert } });
6-
};
3+
/**
4+
* opens the alert
5+
* @param {Object} alert
6+
* @returns alert
7+
*/
8+
export function alertOpen(alert: Object): Action {
9+
return { type: ALERT_OPEN, payload: { alert } };
710
}
811

12+
/**
13+
* re-opens the alert
14+
* @returns ALERT_REPLAY
15+
*/
916
export function alertReplay(): Action {
1017
return { type: ALERT_REPLAY };
1118
}
12-
19+
/**
20+
* closes the alert
21+
* @returns ALERT_CLOSE
22+
*/
1323
export function alertClose(): Action {
1424
return { type: ALERT_CLOSE };
1525
}

src/modules/alert/index.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import {ALERT_CLOSE, ALERT_OPEN, ALERT_REPLAY} from './types';
22

3+
// alert styles
34
const colors = {
45
PASS: '#73C990', // green
56
FAIL: '#FF4081', // red
67
NOTE: '#9DA5B4', // blue
78
};
89

9-
const _alert: CR.Alert = {
10+
// default alert
11+
export const _alert: CR.Alert = {
1012
message: '',
1113
open: false,
1214
action: 'NOTE',
@@ -30,6 +32,12 @@ function setAlert(a: CR.Alert): CR.Alert {
3032
return Object.assign({}, open, a);
3133
}
3234

35+
/**
36+
* snackbar Alert reducer
37+
* @param {} alert=_alert
38+
* @param {Action} action
39+
* @returns CR
40+
*/
3341
export default function alert(
3442
alert = _alert, action: Action
3543
): CR.Alert {

src/modules/alert/reducer.test.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import reducer, { _alert } from './index';
2+
3+
describe('alert reducer', () => {
4+
5+
// mock document.getElementsByClassName
6+
// as it used in styling the alert pop-up
7+
8+
const originalDocument = global.document;
9+
10+
beforeEach(() => {
11+
global.document = Object.assign(document, {
12+
getElementsByClassName: (selector) => [{
13+
style: {
14+
color: 'blue'
15+
}
16+
}]
17+
});
18+
});
19+
20+
afterEach(() => {
21+
global.document = originalDocument;
22+
});
23+
24+
it('should initialize the default alert', () => {
25+
const action = { type: 'unknown' };
26+
expect(reducer(undefined, action)).toEqual(_alert);
27+
});
28+
29+
it('should open the alert on ALERT_OPEN', () => {
30+
const alert = { };
31+
const action = { type: 'ALERT_OPEN', payload: { alert } };
32+
expect(reducer({ open: false }, action).open).toBe(true);
33+
});
34+
35+
it('should open the alert on ALERT_REPLAY', () => {
36+
const alert = { };
37+
const action = { type: 'ALERT_REPLAY', payload: { alert } };
38+
expect(reducer({ open: false }, action).open).toBe(true);
39+
});
40+
41+
it('should close the alert on ALERT_CLOSE', () => {
42+
const action = { type: 'ALERT_CLOSE' };
43+
const alert = { open: true };
44+
expect(reducer(alert, action).open).toBe(false);
45+
});
46+
47+
});

0 commit comments

Comments
 (0)