kopia lustrzana https://gitlab.com/soapbox-pub/soapbox
Refactor some reducers with Immutable.Record
rodzic
7a7fafc8ed
commit
b5432ad8de
|
@ -1,21 +1,11 @@
|
||||||
import {
|
import { Record as ImmutableRecord } from 'immutable';
|
||||||
Map as ImmutableMap,
|
|
||||||
List as ImmutableList,
|
|
||||||
OrderedSet as ImmutableOrderedSet,
|
|
||||||
} from 'immutable';
|
|
||||||
|
|
||||||
import reducer from '../admin';
|
import reducer from '../admin';
|
||||||
|
|
||||||
describe('admin reducer', () => {
|
describe('admin reducer', () => {
|
||||||
it('should return the initial state', () => {
|
it('should return the initial state', () => {
|
||||||
expect(reducer(undefined, {})).toEqual(ImmutableMap({
|
const result = reducer(undefined, {});
|
||||||
reports: ImmutableMap(),
|
expect(ImmutableRecord.isRecord(result)).toBe(true);
|
||||||
openReports: ImmutableOrderedSet(),
|
expect(result.needsReboot).toBe(false);
|
||||||
users: ImmutableMap(),
|
|
||||||
latestUsers: ImmutableOrderedSet(),
|
|
||||||
awaitingApproval: ImmutableOrderedSet(),
|
|
||||||
configs: ImmutableList(),
|
|
||||||
needsReboot: false,
|
|
||||||
}));
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
|
@ -1,6 +1,11 @@
|
||||||
import { List as ImmutableList } from 'immutable';
|
import { Record as ImmutableRecord, List as ImmutableList } from 'immutable';
|
||||||
|
|
||||||
import * as actions from 'soapbox/actions/alerts';
|
import {
|
||||||
|
ALERT_SHOW,
|
||||||
|
ALERT_DISMISS,
|
||||||
|
ALERT_CLEAR,
|
||||||
|
} from 'soapbox/actions/alerts';
|
||||||
|
import { applyActions } from 'soapbox/test_helpers';
|
||||||
|
|
||||||
import reducer from '../alerts';
|
import reducer from '../alerts';
|
||||||
|
|
||||||
|
@ -9,66 +14,65 @@ describe('alerts reducer', () => {
|
||||||
expect(reducer(undefined, {})).toEqual(ImmutableList());
|
expect(reducer(undefined, {})).toEqual(ImmutableList());
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should handle ALERT_SHOW', () => {
|
describe('ALERT_SHOW', () => {
|
||||||
const state = ImmutableList([]);
|
it('imports the alert', () => {
|
||||||
const action = {
|
const action = {
|
||||||
type: actions.ALERT_SHOW,
|
type: ALERT_SHOW,
|
||||||
title: 'alert_title',
|
title: 'alert_title',
|
||||||
message: 'this is an alert message',
|
message: 'this is an alert message',
|
||||||
};
|
};
|
||||||
expect(reducer(state, action).toJS()).toMatchObject([
|
|
||||||
{
|
const expected = [{
|
||||||
key: 0,
|
key: 0,
|
||||||
message: 'this is an alert message',
|
message: 'this is an alert message',
|
||||||
title: 'alert_title',
|
title: 'alert_title',
|
||||||
},
|
}];
|
||||||
]);
|
|
||||||
});
|
|
||||||
|
|
||||||
// it('should handle ALERT_DISMISS', () => {
|
const result = reducer(undefined, action);
|
||||||
// const state = ImmutableList([
|
expect(ImmutableRecord.isRecord(result.get(0))).toBe(true);
|
||||||
// {
|
expect(result.toJS()).toMatchObject(expected);
|
||||||
// key: 0,
|
|
||||||
// message: 'message_1',
|
|
||||||
// title: 'title_1',
|
|
||||||
// },
|
|
||||||
// {
|
|
||||||
// key: 1,
|
|
||||||
// message: 'message_2',
|
|
||||||
// title: 'title_2',
|
|
||||||
// },
|
|
||||||
// ]);
|
|
||||||
// const action = {
|
|
||||||
// type: actions.ALERT_DISMISS,
|
|
||||||
// alert: { key: 0 },
|
|
||||||
// };
|
|
||||||
// expect(reducer(state, action).toJS()).toMatchObject([
|
|
||||||
// {
|
|
||||||
// key: 1,
|
|
||||||
// message: 'message_2',
|
|
||||||
// title: 'title_2',
|
|
||||||
// }
|
|
||||||
// ]);
|
|
||||||
// });
|
|
||||||
|
|
||||||
it('should handle ALERT_CLEAR', () => {
|
|
||||||
const state = ImmutableList([
|
|
||||||
{
|
|
||||||
key: 0,
|
|
||||||
message: 'message_1',
|
|
||||||
title: 'title_1',
|
|
||||||
},
|
|
||||||
{
|
|
||||||
key: 1,
|
|
||||||
message: 'message_2',
|
|
||||||
title: 'title_2',
|
|
||||||
},
|
|
||||||
]);
|
|
||||||
const action = {
|
|
||||||
type: actions.ALERT_CLEAR,
|
|
||||||
};
|
|
||||||
expect(reducer(state, action).toJS()).toMatchObject({
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('ALERT_CLEAR', () => {
|
||||||
|
it('deletes the alerts', () => {
|
||||||
|
const actions = [{
|
||||||
|
type: ALERT_SHOW,
|
||||||
|
title: 'Oops!',
|
||||||
|
message: 'Server is down',
|
||||||
|
}, {
|
||||||
|
type: ALERT_SHOW,
|
||||||
|
title: 'Uh-oh!',
|
||||||
|
message: 'Shit done fucked up',
|
||||||
|
}, {
|
||||||
|
type: ALERT_CLEAR,
|
||||||
|
}];
|
||||||
|
|
||||||
|
const result = applyActions(undefined, actions, reducer);
|
||||||
|
expect(result.isEmpty()).toBe(true);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('ALERT_DISMISS', () => {
|
||||||
|
it('deletes an individual alert', () => {
|
||||||
|
const actions = [{
|
||||||
|
type: ALERT_SHOW,
|
||||||
|
title: 'Oops!',
|
||||||
|
message: 'Server is down',
|
||||||
|
}, {
|
||||||
|
type: ALERT_SHOW,
|
||||||
|
title: 'Uh-oh!',
|
||||||
|
message: 'Shit done fucked up',
|
||||||
|
}, {
|
||||||
|
type: ALERT_DISMISS,
|
||||||
|
alert: {
|
||||||
|
key: 0,
|
||||||
|
},
|
||||||
|
}];
|
||||||
|
|
||||||
|
const result = applyActions(undefined, actions, reducer);
|
||||||
|
expect(result.size).toEqual(1);
|
||||||
|
expect(result.get(0).key).toEqual(1);
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
|
@ -1,9 +1,11 @@
|
||||||
import { Map as ImmutableMap } from 'immutable';
|
import { Record as ImmutableRecord } from 'immutable';
|
||||||
|
|
||||||
import reducer from '../meta';
|
import reducer from '../meta';
|
||||||
|
|
||||||
describe('meta reducer', () => {
|
describe('meta reducer', () => {
|
||||||
it('should return the initial state', () => {
|
it('should return the initial state', () => {
|
||||||
expect(reducer(undefined, {})).toEqual(ImmutableMap());
|
const result = reducer(undefined, {});
|
||||||
|
expect(ImmutableRecord.isRecord(result)).toBe(true);
|
||||||
|
expect(result.instance_fetch_failed).toBe(false);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
|
@ -2,6 +2,7 @@ import {
|
||||||
Map as ImmutableMap,
|
Map as ImmutableMap,
|
||||||
List as ImmutableList,
|
List as ImmutableList,
|
||||||
Set as ImmutableSet,
|
Set as ImmutableSet,
|
||||||
|
Record as ImmutableRecord,
|
||||||
OrderedSet as ImmutableOrderedSet,
|
OrderedSet as ImmutableOrderedSet,
|
||||||
fromJS,
|
fromJS,
|
||||||
is,
|
is,
|
||||||
|
@ -20,7 +21,7 @@ import {
|
||||||
ADMIN_USERS_APPROVE_SUCCESS,
|
ADMIN_USERS_APPROVE_SUCCESS,
|
||||||
} from '../actions/admin';
|
} from '../actions/admin';
|
||||||
|
|
||||||
const initialState = ImmutableMap({
|
const ReducerRecord = ImmutableRecord({
|
||||||
reports: ImmutableMap(),
|
reports: ImmutableMap(),
|
||||||
openReports: ImmutableOrderedSet(),
|
openReports: ImmutableOrderedSet(),
|
||||||
users: ImmutableMap(),
|
users: ImmutableMap(),
|
||||||
|
@ -126,7 +127,7 @@ function handleReportDiffs(state, reports) {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
export default function admin(state = initialState, action) {
|
export default function admin(state = ReducerRecord(), action) {
|
||||||
switch(action.type) {
|
switch(action.type) {
|
||||||
case ADMIN_CONFIG_FETCH_SUCCESS:
|
case ADMIN_CONFIG_FETCH_SUCCESS:
|
||||||
case ADMIN_CONFIG_UPDATE_SUCCESS:
|
case ADMIN_CONFIG_UPDATE_SUCCESS:
|
||||||
|
|
|
@ -1,12 +1,13 @@
|
||||||
import {
|
import {
|
||||||
Map as ImmutableMap,
|
Map as ImmutableMap,
|
||||||
|
Record as ImmutableRecord,
|
||||||
OrderedSet as ImmutableOrderedSet,
|
OrderedSet as ImmutableOrderedSet,
|
||||||
fromJS,
|
fromJS,
|
||||||
} from 'immutable';
|
} from 'immutable';
|
||||||
|
|
||||||
import { ADMIN_LOG_FETCH_SUCCESS } from 'soapbox/actions/admin';
|
import { ADMIN_LOG_FETCH_SUCCESS } from 'soapbox/actions/admin';
|
||||||
|
|
||||||
const initialState = ImmutableMap({
|
const ReducerRecord = ImmutableRecord({
|
||||||
items: ImmutableMap(),
|
items: ImmutableMap(),
|
||||||
index: ImmutableOrderedSet(),
|
index: ImmutableOrderedSet(),
|
||||||
total: 0,
|
total: 0,
|
||||||
|
@ -34,7 +35,7 @@ const importItems = (state, items, total) => {
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
export default function admin_log(state = initialState, action) {
|
export default function admin_log(state = ReducerRecord(), action) {
|
||||||
switch(action.type) {
|
switch(action.type) {
|
||||||
case ADMIN_LOG_FETCH_SUCCESS:
|
case ADMIN_LOG_FETCH_SUCCESS:
|
||||||
return importItems(state, action.items, action.total);
|
return importItems(state, action.items, action.total);
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
import { Map as ImmutableMap, List as ImmutableList } from 'immutable';
|
import { Record as ImmutableRecord, List as ImmutableList } from 'immutable';
|
||||||
|
|
||||||
import {
|
import {
|
||||||
ALERT_SHOW,
|
ALERT_SHOW,
|
||||||
|
@ -6,21 +6,38 @@ import {
|
||||||
ALERT_CLEAR,
|
ALERT_CLEAR,
|
||||||
} from '../actions/alerts';
|
} from '../actions/alerts';
|
||||||
|
|
||||||
const initialState = ImmutableList([]);
|
const AlertRecord = ImmutableRecord({
|
||||||
|
key: 0,
|
||||||
|
title: '',
|
||||||
|
message: '',
|
||||||
|
severity: 'info',
|
||||||
|
actionLabel: '',
|
||||||
|
actionLink: '',
|
||||||
|
});
|
||||||
|
|
||||||
|
const initialState = ImmutableList();
|
||||||
|
|
||||||
|
// Get next key based on last alert
|
||||||
|
const getNextKey = state => state.size > 0 ? state.last().get('key') + 1 : 0;
|
||||||
|
|
||||||
|
// Import the alert
|
||||||
|
const importAlert = (state, alert) => {
|
||||||
|
const key = getNextKey(state);
|
||||||
|
const record = AlertRecord({ ...alert, key });
|
||||||
|
return state.push(record);
|
||||||
|
};
|
||||||
|
|
||||||
|
// Delete an alert by its key
|
||||||
|
const deleteAlert = (state, alert) => {
|
||||||
|
return state.filterNot(item => item.key === alert.key);
|
||||||
|
};
|
||||||
|
|
||||||
export default function alerts(state = initialState, action) {
|
export default function alerts(state = initialState, action) {
|
||||||
switch(action.type) {
|
switch(action.type) {
|
||||||
case ALERT_SHOW:
|
case ALERT_SHOW:
|
||||||
return state.push(ImmutableMap({
|
return importAlert(state, action);
|
||||||
key: state.size > 0 ? state.last().get('key') + 1 : 0,
|
|
||||||
title: action.title,
|
|
||||||
message: action.message,
|
|
||||||
severity: action.severity || 'info',
|
|
||||||
actionLabel: action.actionLabel,
|
|
||||||
actionLink: action.actionLink,
|
|
||||||
}));
|
|
||||||
case ALERT_DISMISS:
|
case ALERT_DISMISS:
|
||||||
return state.filterNot(item => item.get('key') === action.alert.key);
|
return deleteAlert(state, action.alert);
|
||||||
case ALERT_CLEAR:
|
case ALERT_CLEAR:
|
||||||
return state.clear();
|
return state.clear();
|
||||||
default:
|
default:
|
||||||
|
|
|
@ -1,12 +1,14 @@
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
import { Map as ImmutableMap } from 'immutable';
|
import { Record as ImmutableRecord } from 'immutable';
|
||||||
|
|
||||||
import { INSTANCE_FETCH_FAIL } from 'soapbox/actions/instance';
|
import { INSTANCE_FETCH_FAIL } from 'soapbox/actions/instance';
|
||||||
|
|
||||||
const initialState = ImmutableMap();
|
const ReducerRecord = ImmutableRecord({
|
||||||
|
instance_fetch_failed: false,
|
||||||
|
});
|
||||||
|
|
||||||
export default function meta(state = initialState, action) {
|
export default function meta(state = ReducerRecord(), action) {
|
||||||
switch(action.type) {
|
switch(action.type) {
|
||||||
case INSTANCE_FETCH_FAIL:
|
case INSTANCE_FETCH_FAIL:
|
||||||
return state.set('instance_fetch_failed', true);
|
return state.set('instance_fetch_failed', true);
|
||||||
|
|
|
@ -32,8 +32,7 @@ import {
|
||||||
} from '../actions/notifications';
|
} from '../actions/notifications';
|
||||||
import { TIMELINE_DELETE } from '../actions/timelines';
|
import { TIMELINE_DELETE } from '../actions/timelines';
|
||||||
|
|
||||||
// Record for the whole reducer
|
const ReducerRecord = ImmutableRecord({
|
||||||
const NotificationsRecord = ImmutableRecord({
|
|
||||||
items: ImmutableOrderedMap(),
|
items: ImmutableOrderedMap(),
|
||||||
hasMore: true,
|
hasMore: true,
|
||||||
top: false,
|
top: false,
|
||||||
|
@ -182,7 +181,7 @@ const importMarker = (state, marker) => {
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
export default function notifications(state = NotificationsRecord(), action) {
|
export default function notifications(state = ReducerRecord(), action) {
|
||||||
switch(action.type) {
|
switch(action.type) {
|
||||||
case NOTIFICATIONS_EXPAND_REQUEST:
|
case NOTIFICATIONS_EXPAND_REQUEST:
|
||||||
return state.set('isLoading', true);
|
return state.set('isLoading', true);
|
||||||
|
|
Ładowanie…
Reference in New Issue