From 88f690824c188194adf0bf8b180442b0aa91fa54 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?marcin=20miko=C5=82ajczak?= Date: Mon, 20 Jun 2022 22:44:40 +0200 Subject: [PATCH] Update tests MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: marcin mikołajczak --- app/soapbox/features/backups/index.tsx | 2 - .../reducers/__tests__/compose.test.ts | 96 +++++++++---------- app/soapbox/reducers/backups.tsx | 2 +- app/soapbox/reducers/compose.ts | 49 +++++----- 4 files changed, 76 insertions(+), 73 deletions(-) diff --git a/app/soapbox/features/backups/index.tsx b/app/soapbox/features/backups/index.tsx index 0efc4e15e..bfb2a3f4f 100644 --- a/app/soapbox/features/backups/index.tsx +++ b/app/soapbox/features/backups/index.tsx @@ -8,8 +8,6 @@ import { useAppDispatch, useAppSelector } from 'soapbox/hooks'; import Column from '../ui/components/better_column'; -import type { List as ImmutableList, Map as ImmutableMap } from 'immutable'; - const messages = defineMessages({ heading: { id: 'column.backups', defaultMessage: 'Backups' }, create: { id: 'backups.actions.create', defaultMessage: 'Create backup' }, diff --git a/app/soapbox/reducers/__tests__/compose.test.ts b/app/soapbox/reducers/__tests__/compose.test.ts index 42c276cf6..a1d40fe7c 100644 --- a/app/soapbox/reducers/__tests__/compose.test.ts +++ b/app/soapbox/reducers/__tests__/compose.test.ts @@ -1,4 +1,4 @@ -import { List as ImmutableList, Map as ImmutableMap, fromJS } from 'immutable'; +import { List as ImmutableList, Record as ImmutableRecord, fromJS } from 'immutable'; import * as actions from 'soapbox/actions/compose'; import { ME_FETCH_SUCCESS, ME_PATCH_SUCCESS } from 'soapbox/actions/me'; @@ -6,11 +6,11 @@ import { SETTING_CHANGE } from 'soapbox/actions/settings'; import { TIMELINE_DELETE } from 'soapbox/actions/timelines'; import { normalizeStatus } from 'soapbox/normalizers/status'; -import reducer from '../compose'; +import reducer, { ReducerRecord } from '../compose'; describe('compose reducer', () => { it('returns the initial state by default', () => { - const state = reducer(undefined, {}); + const state = reducer(undefined, {} as any); expect(state.toJS()).toMatchObject({ mounted: 0, sensitive: false, @@ -86,64 +86,64 @@ describe('compose reducer', () => { it('uses \'public\' scope as default', () => { const action = { type: actions.COMPOSE_REPLY, - status: ImmutableMap(), - account: ImmutableMap(), + status: ImmutableRecord({})(), + account: ImmutableRecord({})(), }; expect(reducer(undefined, action).toJS()).toMatchObject({ privacy: 'public' }); }); it('uses \'direct\' scope when replying to a DM', () => { - const state = ImmutableMap({ default_privacy: 'public' }); + const state = ReducerRecord({ default_privacy: 'public' }); const action = { type: actions.COMPOSE_REPLY, - status: ImmutableMap({ visibility: 'direct' }), - account: ImmutableMap(), + status: ImmutableRecord({ visibility: 'direct' })(), + account: ImmutableRecord({})(), }; - expect(reducer(state, action).toJS()).toMatchObject({ privacy: 'direct' }); + expect(reducer(state as any, action).toJS()).toMatchObject({ privacy: 'direct' }); }); it('uses \'private\' scope when replying to a private post', () => { - const state = ImmutableMap({ default_privacy: 'public' }); + const state = ReducerRecord({ default_privacy: 'public' }); const action = { type: actions.COMPOSE_REPLY, - status: ImmutableMap({ visibility: 'private' }), - account: ImmutableMap(), + status: ImmutableRecord({ visibility: 'private' })(), + account: ImmutableRecord({})(), }; - expect(reducer(state, action).toJS()).toMatchObject({ privacy: 'private' }); + expect(reducer(state as any, action).toJS()).toMatchObject({ privacy: 'private' }); }); it('uses \'unlisted\' scope when replying to an unlisted post', () => { - const state = ImmutableMap({ default_privacy: 'public' }); + const state = ReducerRecord({ default_privacy: 'public' }); const action = { type: actions.COMPOSE_REPLY, - status: ImmutableMap({ visibility: 'unlisted' }), - account: ImmutableMap(), + status: ImmutableRecord({ visibility: 'unlisted' })(), + account: ImmutableRecord({})(), }; expect(reducer(state, action).toJS()).toMatchObject({ privacy: 'unlisted' }); }); it('uses \'private\' scope when set as preference and replying to a public post', () => { - const state = ImmutableMap({ default_privacy: 'private' }); + const state = ReducerRecord({ default_privacy: 'private' }); const action = { type: actions.COMPOSE_REPLY, - status: ImmutableMap({ visibility: 'public' }), - account: ImmutableMap(), + status: ImmutableRecord({ visibility: 'public' })(), + account: ImmutableRecord({})(), }; expect(reducer(state, action).toJS()).toMatchObject({ privacy: 'private' }); }); it('uses \'unlisted\' scope when set as preference and replying to a public post', () => { - const state = ImmutableMap({ default_privacy: 'unlisted' }); + const state = ReducerRecord({ default_privacy: 'unlisted' }); const action = { type: actions.COMPOSE_REPLY, - status: ImmutableMap({ visibility: 'public' }), - account: ImmutableMap(), + status: ImmutableRecord({ visibility: 'public' })(), + account: ImmutableRecord({})(), }; expect(reducer(state, action).toJS()).toMatchObject({ privacy: 'unlisted' }); }); it('sets preferred scope on user login', () => { - const state = ImmutableMap({ default_privacy: 'public' }); + const state = ReducerRecord({ default_privacy: 'public' }); const action = { type: ME_FETCH_SUCCESS, me: { pleroma: { settings_store: { soapbox_fe: { defaultPrivacy: 'unlisted' } } } }, @@ -155,7 +155,7 @@ describe('compose reducer', () => { }); it('sets preferred scope on settings change', () => { - const state = ImmutableMap({ default_privacy: 'public' }); + const state = ReducerRecord({ default_privacy: 'public' }); const action = { type: SETTING_CHANGE, path: ['defaultPrivacy'], @@ -168,7 +168,7 @@ describe('compose reducer', () => { }); it('sets default scope on settings save (but retains current scope)', () => { - const state = ImmutableMap({ default_privacy: 'public', privacy: 'public' }); + const state = ReducerRecord({ default_privacy: 'public', privacy: 'public' }); const action = { type: ME_PATCH_SUCCESS, me: { pleroma: { settings_store: { soapbox_fe: { defaultPrivacy: 'unlisted' } } } }, @@ -180,7 +180,7 @@ describe('compose reducer', () => { }); it('should handle COMPOSE_MOUNT', () => { - const state = ImmutableMap({ mounted: 1 }); + const state = ReducerRecord({ mounted: 1 }); const action = { type: actions.COMPOSE_MOUNT, }; @@ -190,7 +190,7 @@ describe('compose reducer', () => { }); it('should handle COMPOSE_UNMOUNT', () => { - const state = ImmutableMap({ mounted: 1 }); + const state = ReducerRecord({ mounted: 1 }); const action = { type: actions.COMPOSE_UNMOUNT, }; @@ -200,7 +200,7 @@ describe('compose reducer', () => { }); it('should handle COMPOSE_SENSITIVITY_CHANGE on Mark Sensitive click, don\'t toggle if spoiler active', () => { - const state = ImmutableMap({ spoiler: true, sensitive: true, idempotencyKey: null }); + const state = ReducerRecord({ spoiler: true, sensitive: true, idempotencyKey: null }); const action = { type: actions.COMPOSE_SENSITIVITY_CHANGE, }; @@ -210,7 +210,7 @@ describe('compose reducer', () => { }); it('should handle COMPOSE_SENSITIVITY_CHANGE on Mark Sensitive click, toggle if spoiler inactive', () => { - const state = ImmutableMap({ spoiler: false, sensitive: true }); + const state = ReducerRecord({ spoiler: false, sensitive: true }); const action = { type: actions.COMPOSE_SENSITIVITY_CHANGE, }; @@ -220,7 +220,7 @@ describe('compose reducer', () => { }); it('should handle COMPOSE_SPOILERNESS_CHANGE on CW button click', () => { - const state = ImmutableMap({ spoiler_text: 'spoiler text', spoiler: true, media_attachments: ImmutableList() }); + const state = ReducerRecord({ spoiler_text: 'spoiler text', spoiler: true, media_attachments: ImmutableList() }); const action = { type: actions.COMPOSE_SPOILERNESS_CHANGE, }; @@ -231,7 +231,7 @@ describe('compose reducer', () => { }); it('should handle COMPOSE_SPOILER_TEXT_CHANGE', () => { - const state = ImmutableMap({ spoiler_text: 'prevtext' }); + const state = ReducerRecord({ spoiler_text: 'prevtext' }); const action = { type: actions.COMPOSE_SPOILER_TEXT_CHANGE, text: 'nexttext', @@ -242,7 +242,7 @@ describe('compose reducer', () => { }); it('should handle COMPOSE_VISIBILITY_CHANGE', () => { - const state = ImmutableMap({ privacy: 'public' }); + const state = ReducerRecord({ privacy: 'public' }); const action = { type: actions.COMPOSE_VISIBILITY_CHANGE, value: 'direct', @@ -254,7 +254,7 @@ describe('compose reducer', () => { describe('COMPOSE_CHANGE', () => { it('should handle text changing', () => { - const state = ImmutableMap({ text: 'prevtext' }); + const state = ReducerRecord({ text: 'prevtext' }); const action = { type: actions.COMPOSE_CHANGE, text: 'nexttext', @@ -266,7 +266,7 @@ describe('compose reducer', () => { }); it('should handle COMPOSE_COMPOSING_CHANGE', () => { - const state = ImmutableMap({ is_composing: true }); + const state = ReducerRecord({ is_composing: true }); const action = { type: actions.COMPOSE_COMPOSING_CHANGE, value: false, @@ -277,7 +277,7 @@ describe('compose reducer', () => { }); it('should handle COMPOSE_SUBMIT_REQUEST', () => { - const state = ImmutableMap({ is_submitting: false }); + const state = ReducerRecord({ is_submitting: false }); const action = { type: actions.COMPOSE_SUBMIT_REQUEST, }; @@ -287,7 +287,7 @@ describe('compose reducer', () => { }); it('should handle COMPOSE_UPLOAD_CHANGE_REQUEST', () => { - const state = ImmutableMap({ is_changing_upload: false }); + const state = ReducerRecord({ is_changing_upload: false }); const action = { type: actions.COMPOSE_UPLOAD_CHANGE_REQUEST, }; @@ -297,17 +297,17 @@ describe('compose reducer', () => { }); it('should handle COMPOSE_SUBMIT_SUCCESS', () => { - const state = ImmutableMap({ privacy: 'public' }); + const state = ReducerRecord({ default_privacy: null, privacy: 'public' }); const action = { type: actions.COMPOSE_SUBMIT_SUCCESS, }; expect(reducer(state, action).toJS()).toMatchObject({ - privacy: undefined, + privacy: null, }); }); it('should handle COMPOSE_SUBMIT_FAIL', () => { - const state = ImmutableMap({ is_submitting: true }); + const state = ReducerRecord({ is_submitting: true }); const action = { type: actions.COMPOSE_SUBMIT_FAIL, }; @@ -317,7 +317,7 @@ describe('compose reducer', () => { }); it('should handle COMPOSE_UPLOAD_CHANGE_FAIL', () => { - const state = ImmutableMap({ is_changing_upload: true }); + const state = ReducerRecord({ is_changing_upload: true }); const action = { type: actions.COMPOSE_UPLOAD_CHANGE_FAIL, }; @@ -327,7 +327,7 @@ describe('compose reducer', () => { }); it('should handle COMPOSE_UPLOAD_REQUEST', () => { - const state = ImmutableMap({ is_uploading: false }); + const state = ReducerRecord({ is_uploading: false }); const action = { type: actions.COMPOSE_UPLOAD_REQUEST, }; @@ -337,7 +337,7 @@ describe('compose reducer', () => { }); it('should handle COMPOSE_UPLOAD_SUCCESS', () => { - const state = ImmutableMap({ media_attachments: ImmutableList() }); + const state = ReducerRecord({ media_attachments: ImmutableList() }); const media = [ { description: null, @@ -363,7 +363,7 @@ describe('compose reducer', () => { }); it('should handle COMPOSE_UPLOAD_FAIL', () => { - const state = ImmutableMap({ is_uploading: true }); + const state = ReducerRecord({ is_uploading: true }); const action = { type: actions.COMPOSE_UPLOAD_FAIL, }; @@ -373,7 +373,7 @@ describe('compose reducer', () => { }); it('should handle COMPOSE_UPLOAD_PROGRESS', () => { - const state = ImmutableMap({ progress: 0 }); + const state = ReducerRecord({ progress: 0 }); const action = { type: actions.COMPOSE_UPLOAD_PROGRESS, loaded: 10, @@ -396,7 +396,7 @@ describe('compose reducer', () => { }); it('should handle COMPOSE_SUGGESTION_TAGS_UPDATE', () => { - const state = ImmutableMap({ tagHistory: ImmutableList([ 'hashtag' ]) }); + const state = ReducerRecord({ tagHistory: ImmutableList([ 'hashtag' ]) }); const action = { type: actions.COMPOSE_SUGGESTION_TAGS_UPDATE, token: 'aaadken3', @@ -419,7 +419,7 @@ describe('compose reducer', () => { }); it('should handle TIMELINE_DELETE - delete status from timeline', () => { - const state = ImmutableMap({ in_reply_to: '9wk6pmImMrZjgrK7iC' }); + const state = ReducerRecord({ in_reply_to: '9wk6pmImMrZjgrK7iC' }); const action = { type: TIMELINE_DELETE, id: '9wk6pmImMrZjgrK7iC', @@ -430,7 +430,7 @@ describe('compose reducer', () => { }); it('should handle COMPOSE_POLL_ADD', () => { - const state = ImmutableMap({ poll: null }); + const state = ReducerRecord({ poll: null }); const initialPoll = Object({ options: [ '', @@ -465,7 +465,7 @@ describe('compose reducer', () => { expires_in: 86400, multiple: false, }); - const state = ImmutableMap({ poll: initialPoll }); + const state = ReducerRecord({ poll: initialPoll }); const action = { type: actions.COMPOSE_POLL_OPTION_CHANGE, index: 0, diff --git a/app/soapbox/reducers/backups.tsx b/app/soapbox/reducers/backups.tsx index 6786827ce..fbbfccef0 100644 --- a/app/soapbox/reducers/backups.tsx +++ b/app/soapbox/reducers/backups.tsx @@ -1,4 +1,4 @@ -import { Map as ImmutableMap, Record as ImmutableRecord, fromJS } from 'immutable'; +import { Map as ImmutableMap, Record as ImmutableRecord } from 'immutable'; import { BACKUPS_FETCH_SUCCESS, diff --git a/app/soapbox/reducers/compose.ts b/app/soapbox/reducers/compose.ts index ad10c903d..3b861fb58 100644 --- a/app/soapbox/reducers/compose.ts +++ b/app/soapbox/reducers/compose.ts @@ -75,14 +75,14 @@ const PollRecord = ImmutableRecord({ multiple: false, }); -const ReducerRecord = ImmutableRecord({ +export const ReducerRecord = ImmutableRecord({ caretPosition: null as number | null, content_type: 'text/plain', default_content_type: 'text/plain', default_privacy: 'public', default_sensitive: false, focusDate: null as Date | null, - idempotencyKey: uuid(), + idempotencyKey: '', id: null as string | null, in_reply_to: null as string | null, is_changing_upload: false, @@ -112,7 +112,7 @@ type Poll = ReturnType; const statusToTextMentions = (state: State, status: ImmutableMap, account: AccountEntity) => { const author = status.getIn(['account', 'acct']); - const mentions = status.get('mentions').map((m: ImmutableMap) => m.get('acct')); + const mentions = status.get('mentions')?.map((m: ImmutableMap) => m.get('acct')) || []; return ImmutableOrderedSet([author]) .concat(mentions) @@ -123,7 +123,7 @@ const statusToTextMentions = (state: State, status: ImmutableMap, a export const statusToMentionsArray = (status: ImmutableMap, account: AccountEntity) => { const author = status.getIn(['account', 'acct']) as string; - const mentions = status.get('mentions').map((m: ImmutableMap) => m.get('acct')); + const mentions = status.get('mentions')?.map((m: ImmutableMap) => m.get('acct')) || []; return ImmutableOrderedSet([author]) .concat(mentions) @@ -140,24 +140,29 @@ export const statusToMentionsAccountIdsArray = (status: StatusEntity, account: A }; function clearAll(state: State) { - return state.withMutations(map => { - map.set('id', null); - map.set('text', ''); - map.set('to', ImmutableOrderedSet()); - map.set('spoiler', false); - map.set('spoiler_text', ''); - map.set('content_type', state.default_content_type); - map.set('is_submitting', false); - map.set('is_changing_upload', false); - map.set('in_reply_to', null); - map.set('quote', null); - map.set('privacy', state.default_privacy); - map.set('sensitive', false); - map.set('media_attachments', ImmutableList()); - map.set('poll', null); - map.set('idempotencyKey', uuid()); - map.set('schedule', null); + return ReducerRecord({ + content_type: state.default_content_type, + privacy: state.default_privacy, + idempotencyKey: uuid(), }); + // state.withMutations(map => { + // map.set('id', null); + // map.set('text', ''); + // map.set('to', ImmutableOrderedSet()); + // map.set('spoiler', false); + // map.set('spoiler_text', ''); + // map.set('content_type', state.COMPOSE_SUBMIT_SUCCESS); + // map.set('is_submitting', false); + // map.set('is_changing_upload', false); + // map.set('in_reply_to', null); + // map.set('quote', null); + // map.set('privacy', state.default_privacy); + // map.set('sensitive', false); + // map.set('media_attachments', ImmutableList()); + // map.set('poll', null); + // map.set('idempotencyKey', uuid()); + // map.set('schedule', null); + // }); } function appendMedia(state: State, media: APIEntity) { @@ -297,7 +302,7 @@ const updateSetting = (state: State, path: string[], value: string) => { } }; -export default function compose(state = ReducerRecord({ resetFileKey: getResetFileKey() }), action: AnyAction) { +export default function compose(state = ReducerRecord({ idempotencyKey: uuid(), resetFileKey: getResetFileKey() }), action: AnyAction) { switch (action.type) { case COMPOSE_MOUNT: return state.set('mounted', state.mounted + 1);