Fix hide/unhide user and setVerifiedStatus

* add tests for user store
This commit is contained in:
Pavel Mineev
2020-03-25 01:52:46 -05:00
committed by Umputun
parent c9a23698ba
commit e7dfd1efbb
4 changed files with 123 additions and 32 deletions
+1 -1
View File
@@ -1,8 +1,8 @@
import { mockStore } from '@app/testUtils/mockStore';
import { LS_SORT_KEY } from '@app/common/constants';
import { updateSorting } from './actions';
import { COMMENTS_SET_SORT } from './types';
import { LS_SORT_KEY } from '@app/common/constants';
describe('Store comments actions', () => {
beforeAll(() => {
@@ -0,0 +1,24 @@
{
"comments": {
"allComments": [
{
"id": "1",
"user": {
"id": "1"
}
},
{
"id": "2",
"user": {
"id": "2"
}
},
{
"id": "3",
"user": {
"id": "1"
}
}
]
}
}
+71
View File
@@ -0,0 +1,71 @@
import { mockStore } from '@app/testUtils/mockStore';
import { User } from '@app/common/types';
import { LS_HIDDEN_USERS_KEY } from '@app/common/constants';
import { COMMENTS_PATCH } from '@app/store/comments/types';
import INITIAL_STORE from './__mocks__/comments-store.json';
import { setVerifiedStatus, hideUser, unhideUser, unblockUser } from './actions';
import { USER_UNHIDE, USER_HIDE, USER_UNBAN } from './types';
describe('store user actions', () => {
beforeAll(() => {
require('jest-fetch-mock').enableMocks();
});
afterAll(() => {
require('jest-fetch-mock').resetMocks();
});
test('setVerifiedStatus', async () => {
const store = mockStore(INITIAL_STORE);
await store.dispatch(setVerifiedStatus('1', true));
const actions = store.getActions();
expect(actions[0]).toEqual({
type: COMMENTS_PATCH,
ids: ['1', '3'],
patch: { user: { id: '1', verified: true } },
});
});
test('unblockUser', async () => {
const store = mockStore(INITIAL_STORE);
await store.dispatch(unblockUser('1'));
const actions = store.getActions();
expect(actions[0]).toEqual({ type: USER_UNBAN, id: '1' });
expect(actions[1]).toEqual({ type: COMMENTS_PATCH, ids: ['1', '3'], patch: { user: { id: '1', block: false } } });
});
describe('hide/unhide comments of user', () => {
beforeEach(() => {
localStorage.clear();
});
const store = mockStore(INITIAL_STORE);
test('hideUser', async () => {
await store.dispatch(hideUser({ id: '1' } as User));
const actions = store.getActions();
expect(actions[0]).toEqual({ type: USER_HIDE, user: { id: '1' } });
expect(actions[1]).toEqual({ type: COMMENTS_PATCH, ids: ['1', '3'], patch: { hidden: true } });
expect(localStorage.getItem).toHaveBeenCalledWith(LS_HIDDEN_USERS_KEY);
expect(localStorage.setItem).toHaveBeenCalledWith(LS_HIDDEN_USERS_KEY, JSON.stringify({ '1': { id: '1' } }));
});
test('unhideUser', async () => {
localStorage.setItem(LS_HIDDEN_USERS_KEY, JSON.stringify({ '1': { id: '1' } }));
await store.dispatch(unhideUser('1'));
const actions = store.getActions();
expect(actions[2]).toEqual({ type: USER_UNHIDE, id: '1' });
expect(localStorage.getItem).toHaveBeenCalledWith(LS_HIDDEN_USERS_KEY);
expect(localStorage.setItem).toHaveBeenCalledWith(LS_HIDDEN_USERS_KEY, JSON.stringify({}));
});
});
});
+27 -31
View File
@@ -16,7 +16,7 @@ import {
} from './types';
import { unsetCommentMode, fetchComments } from '../comments/actions';
import { IS_STORAGE_AVAILABLE, LS_HIDDEN_USERS_KEY } from '@app/common/constants';
import { getItem } from '@app/common/local-storage';
import { setItem, getItem } from '@app/common/local-storage';
import { updateProvider } from '../provider/actions';
import { COMMENTS_PATCH } from '../comments/types';
@@ -76,18 +76,16 @@ export const blockUser = (
export const unblockUser = (id: User['id']): StoreAction<Promise<void>> => async (dispatch, getState) => {
await api.unblockUser(id);
dispatch({
type: USER_UNBAN,
id,
});
const comments = Object.values(getState().comments).filter(c => c.user.id === id);
dispatch({ type: USER_UNBAN, id });
const comments = Object.values(getState().comments.allComments);
const userComments = comments.filter(comment => comment.user.id === id);
if (!comments.length) return;
if (!userComments.length) return;
const user = comments[0].user;
dispatch({
type: COMMENTS_PATCH,
ids: comments.map(c => c.id),
ids: userComments.map(c => c.id),
patch: { user: { ...user, block: false } },
});
};
@@ -100,30 +98,26 @@ export const fetchHiddenUsers = (): StoreAction<void> => dispatch => {
};
export const hideUser = (user: User): StoreAction<void> => (dispatch, getState) => {
if (IS_STORAGE_AVAILABLE) {
const hiddenUsers = JSON.parse(getItem(LS_HIDDEN_USERS_KEY) || '{}');
hiddenUsers[user.id] = user;
localStorage.setItem(LS_HIDDEN_USERS_KEY, JSON.stringify(hiddenUsers));
}
dispatch({ type: USER_HIDE, user });
const hiddenUsers = JSON.parse(getItem(LS_HIDDEN_USERS_KEY) || '{}');
dispatch({
type: COMMENTS_PATCH,
ids: Object.values(getState().comments)
.filter(c => c.user.id === user.id)
.map(c => c.id),
patch: { hidden: true },
});
hiddenUsers[user.id] = user;
setItem(LS_HIDDEN_USERS_KEY, JSON.stringify(hiddenUsers));
const ids = Object.values(getState().comments.allComments)
.filter(c => c.user.id === user.id)
.map(c => c.id);
dispatch({ type: USER_HIDE, user });
dispatch({ type: COMMENTS_PATCH, ids, patch: { hidden: true } });
};
export const unhideUser = (userId: string): StoreAction<void> => (dispatch, _getState) => {
if (IS_STORAGE_AVAILABLE) {
const hiddenUsers = JSON.parse(getItem(LS_HIDDEN_USERS_KEY) || '{}');
if (Object.prototype.hasOwnProperty.call(hiddenUsers, userId)) {
delete hiddenUsers[userId];
}
localStorage.setItem(LS_HIDDEN_USERS_KEY, JSON.stringify(hiddenUsers));
const hiddenUsers = JSON.parse(getItem(LS_HIDDEN_USERS_KEY) || '{}');
if (Object.prototype.hasOwnProperty.call(hiddenUsers, userId)) {
delete hiddenUsers[userId];
}
setItem(LS_HIDDEN_USERS_KEY, JSON.stringify(hiddenUsers));
dispatch({ type: USER_UNHIDE, id: userId });
@@ -139,13 +133,15 @@ export const setVerifiedStatus = (id: User['id'], status: boolean): StoreAction<
} else {
await api.removeVerifiedStatus(id);
}
const comments = Object.values(getState().comments).filter(c => c.user.id === id);
if (!comments.length) return;
const user = comments[0].user;
const comments = Object.values(getState().comments.allComments);
const userComments = comments.filter(c => c.user.id === id);
if (!userComments.length) return;
const user = userComments[0].user;
dispatch({
type: COMMENTS_PATCH,
ids: comments.map(c => c.id),
ids: userComments.map(c => c.id),
patch: { user: { ...user, verified: status } },
});
};