Skip to content

Commit e10e386

Browse files
committed
add alert tests
1 parent a48f5e9 commit e10e386

File tree

2 files changed

+65
-8
lines changed

2 files changed

+65
-8
lines changed

src/modules/alert/index.ts

+8-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {ALERT_CLOSE, ALERT_OPEN, ALERT_REPLAY} from './types';
22

33
// alert styles
4-
const colors = {
4+
export const colors = {
55
PASS: '#73C990', // green
66
FAIL: '#FF4081', // red
77
NOTE: '#9DA5B4', // blue
@@ -23,7 +23,11 @@ const open = {
2323
};
2424

2525
let current: CR.Alert = _alert;
26-
26+
/**
27+
* sets alert color on snackbar DOM element
28+
* @param {CR.Alert} a alert
29+
* @returns CR.Alert alert
30+
*/
2731
function setAlert(a: CR.Alert): CR.Alert {
2832
a.color = colors[a.action] || colors.NOTE;
2933
let statusBarAlert = <HTMLElement>document.getElementsByClassName('cr-alert-replay')[0];
@@ -35,8 +39,8 @@ function setAlert(a: CR.Alert): CR.Alert {
3539
/**
3640
* snackbar Alert reducer
3741
* @param {} alert=_alert
38-
* @param {Action} action
39-
* @returns CR
42+
* @param {Action} action { type: string, payload: { alert } }
43+
* @returns CR.Alert alert
4044
*/
4145
export default function alert(
4246
alert = _alert, action: Action

src/modules/alert/reducer.test.ts

+57-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
/// <reference path="../../typings/globals/jest/index.d.ts" />
22
/// <reference path="../../typings/common/global.d.ts" />
33

4-
import reducer, { _alert } from './index';
4+
import reducer, { _alert, colors } from './index';
5+
import { ALERT_OPEN, ALERT_REPLAY, ALERT_CLOSE } from './types';
56

67
describe('alert reducer', () => {
78

@@ -31,18 +32,70 @@ describe('alert reducer', () => {
3132

3233
it('should open the alert on ALERT_OPEN', () => {
3334
const alert = { };
34-
const action = { type: 'ALERT_OPEN', payload: { alert } };
35+
const action = { type: ALERT_OPEN, payload: { alert } };
3536
expect(reducer({ open: false }, action).open).toBe(true);
3637
});
3738

39+
it('should set the NOTE alert on ALERT_OPEN', () => {
40+
const alert = {
41+
message: 'a message',
42+
action: 'NOTE',
43+
duration: 1500,
44+
};
45+
const action = { type: ALERT_OPEN, payload: { alert } };
46+
const expected = {
47+
message: 'a message',
48+
action: 'NOTE',
49+
duration: 1500,
50+
open: true,
51+
color: colors.NOTE
52+
};
53+
expect(reducer({ open: false }, action)).toEqual(expected);
54+
});
55+
56+
it('should set the FAIL alert on ALERT_OPEN', () => {
57+
const alert = {
58+
message: 'a message',
59+
action: 'FAIL',
60+
duration: 1500,
61+
};
62+
const action = { type: ALERT_OPEN, payload: { alert } };
63+
const expected = {
64+
message: 'a message',
65+
action: 'FAIL',
66+
duration: 1500,
67+
open: true,
68+
color: colors.FAIL
69+
};
70+
expect(reducer({ open: false }, action)).toEqual(expected);
71+
});
72+
73+
it('should set the PASS alert on ALERT_OPEN', () => {
74+
const alert = {
75+
message: 'a message',
76+
action: 'PASS',
77+
duration: 1500,
78+
};
79+
const action = { type: ALERT_OPEN, payload: { alert } };
80+
const expected = {
81+
message: 'a message',
82+
action: 'PASS',
83+
duration: 1500,
84+
open: true,
85+
color: colors.PASS
86+
};
87+
expect(reducer({ open: false }, action)).toEqual(expected);
88+
});
89+
90+
3891
it('should open the alert on ALERT_REPLAY', () => {
3992
const alert = { };
40-
const action = { type: 'ALERT_REPLAY', payload: { alert } };
93+
const action = { type: ALERT_REPLAY, payload: { alert } };
4194
expect(reducer({ open: false }, action).open).toBe(true);
4295
});
4396

4497
it('should close the alert on ALERT_CLOSE', () => {
45-
const action = { type: 'ALERT_CLOSE' };
98+
const action = { type: ALERT_CLOSE };
4699
const alert = { open: true };
47100
expect(reducer(alert, action).open).toBe(false);
48101
});

0 commit comments

Comments
 (0)