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

148 lines
4.2 KiB
TypeScript
Raw Normal View History

2023-01-03 19:55:46 +00:00
// Copyright 2021 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import { assert } from 'chai';
2023-03-20 22:23:53 +00:00
import type { TargetedConversationChangedActionType } from '../../../state/ducks/conversations';
2022-06-16 19:12:50 +00:00
import {
2023-03-20 22:23:53 +00:00
TARGETED_CONVERSATION_CHANGED,
2022-06-16 19:12:50 +00:00
actions as conversationsActions,
} from '../../../state/ducks/conversations';
import { noopAction } from '../../../state/ducks/noop';
import type { StateType } from '../../../state/reducer';
import { reducer as rootReducer } from '../../../state/reducer';
2023-03-02 20:55:40 +00:00
import { actions, AudioPlayerContent } from '../../../state/ducks/audioPlayer';
2023-02-24 23:18:57 +00:00
import type { VoiceNoteAndConsecutiveForPlayback } from '../../../state/selectors/audioPlayer';
const { messageDeleted, messageChanged } = conversationsActions;
const MESSAGE_ID = 'message-id';
2023-02-24 23:18:57 +00:00
function voiceNoteDataForMessage(
messageId: string
): VoiceNoteAndConsecutiveForPlayback {
return {
conversationId: 'convo',
voiceNote: {
id: messageId,
type: 'outgoing',
timestamp: 0,
url: undefined,
source: undefined,
2023-08-16 20:54:39 +00:00
sourceServiceId: undefined,
2023-02-24 23:18:57 +00:00
messageIdForLogging: messageId,
isPlayed: false,
},
consecutiveVoiceNotes: [],
previousMessageId: undefined,
nextMessageTimestamp: undefined,
playbackRate: 1,
2023-02-24 23:18:57 +00:00
};
}
2021-03-24 23:08:44 +00:00
describe('both/state/ducks/audioPlayer', () => {
const getEmptyRootState = (): StateType => {
return rootReducer(undefined, noopAction());
};
const getInitializedState = (): StateType => {
const state = getEmptyRootState();
2023-02-24 23:18:57 +00:00
const updated = rootReducer(
state,
2023-03-02 20:55:40 +00:00
actions.loadVoiceNoteAudio({
2023-02-24 23:18:57 +00:00
voiceNoteData: voiceNoteDataForMessage(MESSAGE_ID),
position: 0,
context: 'context',
ourConversationId: 'convo',
2023-03-02 20:55:40 +00:00
playbackRate: 1,
2023-02-24 23:18:57 +00:00
})
);
2023-03-02 20:55:40 +00:00
const content = updated.audioPlayer.active?.content;
assert.isTrue(content && AudioPlayerContent.isVoiceNote(content));
if (content && AudioPlayerContent.isVoiceNote(content)) {
assert.strictEqual(content.current.id, MESSAGE_ID);
assert.strictEqual(content.context, 'context');
}
return updated;
};
2023-02-24 23:18:57 +00:00
describe('loadMessageAudio', () => {
it("updates `active` in the audioPlayer's state", () => {
const state = getEmptyRootState();
assert.strictEqual(state.audioPlayer.active, undefined);
2023-02-24 23:18:57 +00:00
const updated = rootReducer(
state,
2023-03-02 20:55:40 +00:00
actions.loadVoiceNoteAudio({
2023-02-24 23:18:57 +00:00
voiceNoteData: voiceNoteDataForMessage('test'),
position: 0,
context: 'context',
ourConversationId: 'convo',
2023-03-02 20:55:40 +00:00
playbackRate: 1,
2023-02-24 23:18:57 +00:00
})
);
2023-03-02 20:55:40 +00:00
const content = updated.audioPlayer.active?.content;
assert.isTrue(content && AudioPlayerContent.isVoiceNote(content));
if (content && AudioPlayerContent.isVoiceNote(content)) {
assert.strictEqual(content.current.id, 'test');
assert.strictEqual(content.context, 'context');
}
});
});
2023-02-24 23:18:57 +00:00
it('active is not changed when changing the conversation', () => {
const state = getInitializedState();
const action: TargetedConversationChangedActionType = {
2023-03-20 22:23:53 +00:00
type: TARGETED_CONVERSATION_CHANGED,
payload: { conversationId: 'any' },
};
const updated = rootReducer(state, action);
2023-03-02 20:55:40 +00:00
const content = updated.audioPlayer.active?.content;
assert.isTrue(content && AudioPlayerContent.isVoiceNote(content));
if (content && AudioPlayerContent.isVoiceNote(content)) {
assert.strictEqual(content.current.id, MESSAGE_ID);
}
});
2023-02-24 23:18:57 +00:00
it('resets active.content when message was deleted', () => {
const state = getInitializedState();
const updated = rootReducer(
state,
messageDeleted(MESSAGE_ID, 'conversation-id')
);
2023-02-24 23:18:57 +00:00
assert.strictEqual(updated.audioPlayer.active?.content, undefined);
});
2023-02-24 23:18:57 +00:00
it('resets active.content when message is DOE', () => {
const state = getInitializedState();
const updated = rootReducer(
state,
messageChanged(MESSAGE_ID, 'conversation-id', {
id: MESSAGE_ID,
type: 'incoming',
sent_at: 1,
received_at: 1,
timestamp: 1,
conversationId: 'conversation-id',
deletedForEveryone: true,
})
);
2023-02-24 23:18:57 +00:00
assert.strictEqual(updated.audioPlayer.active?.content, undefined);
});
});