diff --git a/frontend/app/store/comments/actions.test.ts b/frontend/app/store/comments/actions.test.ts index 06c11619..370420bb 100644 --- a/frontend/app/store/comments/actions.test.ts +++ b/frontend/app/store/comments/actions.test.ts @@ -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(() => { diff --git a/frontend/app/store/user/__mocks__/comments-store.json b/frontend/app/store/user/__mocks__/comments-store.json new file mode 100644 index 00000000..1f158c10 --- /dev/null +++ b/frontend/app/store/user/__mocks__/comments-store.json @@ -0,0 +1,24 @@ +{ + "comments": { + "allComments": [ + { + "id": "1", + "user": { + "id": "1" + } + }, + { + "id": "2", + "user": { + "id": "2" + } + }, + { + "id": "3", + "user": { + "id": "1" + } + } + ] + } +} diff --git a/frontend/app/store/user/actions.test.ts b/frontend/app/store/user/actions.test.ts new file mode 100644 index 00000000..72b32542 --- /dev/null +++ b/frontend/app/store/user/actions.test.ts @@ -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({})); + }); + }); +}); diff --git a/frontend/app/store/user/actions.ts b/frontend/app/store/user/actions.ts index 26e022c3..608d6a58 100644 --- a/frontend/app/store/user/actions.ts +++ b/frontend/app/store/user/actions.ts @@ -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> => 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 => dispatch => { }; export const hideUser = (user: User): StoreAction => (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 => (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 } }, }); };