signal-desktop/ts/test-both/state/ducks/composer_test.ts

206 lines
6 KiB
TypeScript
Raw Normal View History

2021-06-25 16:08:16 +00:00
// Copyright 2021 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import { assert } from 'chai';
2021-08-09 20:06:21 +00:00
import * as sinon from 'sinon';
import { noop } from 'lodash';
2021-06-25 16:08:16 +00:00
import type { ReduxActions } from '../../../state/types';
import {
actions,
getComposerStateForConversation,
getEmptyState,
reducer,
} from '../../../state/ducks/composer';
2021-08-09 20:06:21 +00:00
import { noopAction } from '../../../state/ducks/noop';
import { reducer as rootReducer } from '../../../state/reducer';
2021-06-25 16:08:16 +00:00
import { IMAGE_JPEG } from '../../../types/MIME';
import type { AttachmentDraftType } from '../../../types/Attachment';
import { fakeDraftAttachment } from '../../helpers/fakeAttachment';
2021-06-25 16:08:16 +00:00
describe('both/state/ducks/composer', () => {
const QUOTED_MESSAGE = {
conversationId: '123',
id: 'quoted-message-id',
2021-06-25 16:08:16 +00:00
quote: {
attachments: [],
2021-07-09 19:36:10 +00:00
id: 456,
2021-06-25 16:08:16 +00:00
isViewOnce: false,
2022-05-11 20:59:58 +00:00
isGiftBadge: false,
2021-06-25 16:08:16 +00:00
messageId: '789',
referencedMessageNotFound: false,
},
};
function getRootStateFunction(selectedConversationId?: string) {
2021-08-09 20:06:21 +00:00
const state = rootReducer(undefined, noopAction());
return () => ({
...state,
conversations: {
...state.conversations,
selectedConversationId,
},
});
}
2021-08-09 20:06:21 +00:00
2021-06-25 16:08:16 +00:00
describe('replaceAttachments', () => {
let oldReduxActions: ReduxActions;
before(() => {
oldReduxActions = window.reduxActions;
window.reduxActions = {
...oldReduxActions,
linkPreviews: {
...oldReduxActions?.linkPreviews,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
removeLinkPreview: noop as any,
},
};
});
after(() => {
window.reduxActions = oldReduxActions;
});
2021-06-25 16:08:16 +00:00
it('replaces the attachments state', () => {
const { replaceAttachments } = actions;
2021-08-09 20:06:21 +00:00
const dispatch = sinon.spy();
const attachments: Array<AttachmentDraftType> = [
{
contentType: IMAGE_JPEG,
pending: true,
size: 2433,
path: 'image.jpg',
},
2021-09-24 20:02:30 +00:00
];
2021-08-09 20:06:21 +00:00
replaceAttachments('123', attachments)(
dispatch,
getRootStateFunction('123'),
null
);
2021-06-25 16:08:16 +00:00
2021-08-09 20:06:21 +00:00
const action = dispatch.getCall(0).args[0];
const state = reducer(getEmptyState(), action);
const composerState = getComposerStateForConversation(state, '123');
assert.deepEqual(composerState.attachments, attachments);
2021-06-25 16:08:16 +00:00
});
it('sets the high quality setting to false when there are no attachments', () => {
const { replaceAttachments } = actions;
2021-08-09 20:06:21 +00:00
const dispatch = sinon.spy();
const attachments: Array<AttachmentDraftType> = [];
2021-08-09 20:06:21 +00:00
replaceAttachments('123', attachments)(
dispatch,
getRootStateFunction('123'),
null
);
const action = dispatch.getCall(0).args[0];
const state = reducer(
{
...getEmptyState(),
conversations: {
'123': {
...getComposerStateForConversation(getEmptyState(), '123'),
shouldSendHighQualityAttachments: true,
},
},
2021-08-09 20:06:21 +00:00
},
action
);
const composerState = getComposerStateForConversation(state, '123');
assert.deepEqual(composerState.attachments, attachments);
2021-08-09 20:06:21 +00:00
assert.deepEqual(composerState.attachments, attachments);
assert.isUndefined(composerState.shouldSendHighQualityAttachments);
2021-08-09 20:06:21 +00:00
});
it('does not update redux if the conversation is not selected', () => {
const { replaceAttachments } = actions;
const dispatch = sinon.spy();
const attachments = [fakeDraftAttachment()];
2021-08-09 20:06:21 +00:00
replaceAttachments('123', attachments)(
dispatch,
getRootStateFunction('456'),
null
2021-06-25 16:08:16 +00:00
);
2021-08-09 20:06:21 +00:00
assert.isNull(dispatch.getCall(0));
2021-06-25 16:08:16 +00:00
});
});
describe('resetComposer', () => {
it('returns composer back to empty state', () => {
const { resetComposer } = actions;
const nextState = reducer(getEmptyState(), resetComposer('456'));
2021-06-25 16:08:16 +00:00
const composerState = getComposerStateForConversation(nextState, '456');
2022-12-08 07:43:48 +00:00
assert.deepEqual(nextState, {
...getEmptyState(),
conversations: {
'456': {
...composerState,
messageCompositionId: composerState.messageCompositionId,
},
},
2022-12-08 07:43:48 +00:00
});
2021-06-25 16:08:16 +00:00
});
});
describe('setMediaQualitySetting', () => {
it('toggles the media quality setting', () => {
const { setMediaQualitySetting } = actions;
const state = getEmptyState();
const composerState = getComposerStateForConversation(state, '123');
assert.isUndefined(composerState.shouldSendHighQualityAttachments);
const nextState = reducer(state, setMediaQualitySetting('123', true));
2021-06-25 16:08:16 +00:00
const nextComposerState = getComposerStateForConversation(
nextState,
'123'
);
assert.isTrue(nextComposerState.shouldSendHighQualityAttachments);
2021-06-25 16:08:16 +00:00
const nextNextState = reducer(
nextState,
setMediaQualitySetting('123', false)
);
const nextNextComposerState = getComposerStateForConversation(
nextNextState,
'123'
);
2021-06-25 16:08:16 +00:00
assert.isFalse(nextNextComposerState.shouldSendHighQualityAttachments);
2021-06-25 16:08:16 +00:00
const notMyConvoState = reducer(
nextNextState,
setMediaQualitySetting('456', true)
);
const notMineComposerState = getComposerStateForConversation(
notMyConvoState,
'123'
);
assert.isFalse(
notMineComposerState.shouldSendHighQualityAttachments,
'still false for prev convo'
);
2021-06-25 16:08:16 +00:00
});
});
describe('setQuotedMessage', () => {
it('sets the quoted message', () => {
const { setQuotedMessage } = actions;
const state = getEmptyState();
const nextState = reducer(state, setQuotedMessage('123', QUOTED_MESSAGE));
2021-06-25 16:08:16 +00:00
const composerState = getComposerStateForConversation(nextState, '123');
assert.equal(composerState.quotedMessage?.conversationId, '123');
assert.equal(composerState.quotedMessage?.quote?.id, 456);
2021-06-25 16:08:16 +00:00
});
});
});