2021-09-09 16:29:01 +00:00
|
|
|
// Copyright 2021 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
import { assert } from 'chai';
|
|
|
|
import * as sinon from 'sinon';
|
2021-10-26 19:15:33 +00:00
|
|
|
import type { StateType } from '../../../state/reducer';
|
|
|
|
import { reducer as rootReducer } from '../../../state/reducer';
|
2021-09-09 16:29:01 +00:00
|
|
|
import { noopAction } from '../../../state/ducks/noop';
|
|
|
|
|
2021-10-26 19:15:33 +00:00
|
|
|
import type { PreferredReactionsStateType } from '../../../state/ducks/preferredReactions';
|
2021-09-09 16:29:01 +00:00
|
|
|
import {
|
|
|
|
actions,
|
2022-02-23 18:48:40 +00:00
|
|
|
getEmptyState,
|
2021-09-09 16:29:01 +00:00
|
|
|
reducer,
|
|
|
|
} from '../../../state/ducks/preferredReactions';
|
|
|
|
|
|
|
|
describe('preferred reactions duck', () => {
|
2021-09-09 23:47:30 +00:00
|
|
|
const getEmptyRootState = (): StateType =>
|
|
|
|
rootReducer(undefined, noopAction());
|
2021-09-09 16:29:01 +00:00
|
|
|
|
2021-09-09 23:47:30 +00:00
|
|
|
const getRootState = (
|
|
|
|
preferredReactions: PreferredReactionsStateType
|
|
|
|
): StateType => ({
|
2021-09-09 16:29:01 +00:00
|
|
|
...getEmptyRootState(),
|
|
|
|
preferredReactions,
|
|
|
|
});
|
|
|
|
|
|
|
|
const stateWithOpenCustomizationModal = {
|
2022-02-23 18:48:40 +00:00
|
|
|
...getEmptyState(),
|
2021-09-09 16:29:01 +00:00
|
|
|
customizePreferredReactionsModal: {
|
2021-09-09 23:47:30 +00:00
|
|
|
draftPreferredReactions: ['✨', '❇️', '🎇', '🦈', '💖', '🅿️'],
|
|
|
|
originalPreferredReactions: ['💙', '👍', '👎', '😂', '😮', '😢'],
|
2021-09-09 16:29:01 +00:00
|
|
|
selectedDraftEmojiIndex: undefined,
|
|
|
|
isSaving: false as const,
|
|
|
|
hadSaveError: false,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
const stateWithOpenCustomizationModalAndSelectedEmoji = {
|
|
|
|
...stateWithOpenCustomizationModal,
|
|
|
|
customizePreferredReactionsModal: {
|
|
|
|
...stateWithOpenCustomizationModal.customizePreferredReactionsModal,
|
|
|
|
selectedDraftEmojiIndex: 1,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
let sinonSandbox: sinon.SinonSandbox;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
sinonSandbox = sinon.createSandbox();
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
sinonSandbox.restore();
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('cancelCustomizePreferredReactionsModal', () => {
|
|
|
|
const { cancelCustomizePreferredReactionsModal } = actions;
|
|
|
|
|
|
|
|
it("does nothing if the modal isn't open", () => {
|
|
|
|
const action = cancelCustomizePreferredReactionsModal();
|
2022-02-23 18:48:40 +00:00
|
|
|
const result = reducer(getEmptyState(), action);
|
2021-09-09 16:29:01 +00:00
|
|
|
|
|
|
|
assert.notProperty(result, 'customizePreferredReactionsModal');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('closes the modal if open', () => {
|
|
|
|
const action = cancelCustomizePreferredReactionsModal();
|
|
|
|
const result = reducer(stateWithOpenCustomizationModal, action);
|
|
|
|
|
|
|
|
assert.notProperty(result, 'customizePreferredReactionsModal');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('deselectDraftEmoji', () => {
|
|
|
|
const { deselectDraftEmoji } = actions;
|
|
|
|
|
|
|
|
it('is a no-op if the customization modal is not open', () => {
|
2022-02-23 18:48:40 +00:00
|
|
|
const state = getEmptyState();
|
2021-09-09 16:29:01 +00:00
|
|
|
const action = deselectDraftEmoji();
|
|
|
|
const result = reducer(state, action);
|
|
|
|
|
|
|
|
assert.strictEqual(result, state);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('is a no-op if no emoji is selected', () => {
|
|
|
|
const action = deselectDraftEmoji();
|
|
|
|
const result = reducer(stateWithOpenCustomizationModal, action);
|
|
|
|
|
|
|
|
assert.isUndefined(
|
|
|
|
result.customizePreferredReactionsModal?.selectedDraftEmojiIndex
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('deselects a currently-selected emoji', () => {
|
|
|
|
const action = deselectDraftEmoji();
|
|
|
|
const result = reducer(
|
|
|
|
stateWithOpenCustomizationModalAndSelectedEmoji,
|
|
|
|
action
|
|
|
|
);
|
|
|
|
|
|
|
|
assert.isUndefined(
|
|
|
|
result.customizePreferredReactionsModal?.selectedDraftEmojiIndex
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('openCustomizePreferredReactionsModal', () => {
|
|
|
|
const { openCustomizePreferredReactionsModal } = actions;
|
|
|
|
|
|
|
|
it('opens the customization modal with defaults if no value was stored', () => {
|
2021-09-09 23:47:30 +00:00
|
|
|
const emptyRootState = getEmptyRootState();
|
|
|
|
const rootState = {
|
|
|
|
...emptyRootState,
|
|
|
|
items: {
|
|
|
|
...emptyRootState.items,
|
|
|
|
skinTone: 5,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2021-09-09 16:29:01 +00:00
|
|
|
const dispatch = sinon.spy();
|
2021-09-09 23:47:30 +00:00
|
|
|
openCustomizePreferredReactionsModal()(dispatch, () => rootState, null);
|
2021-09-09 16:29:01 +00:00
|
|
|
const [action] = dispatch.getCall(0).args;
|
|
|
|
|
2021-09-09 23:47:30 +00:00
|
|
|
const result = reducer(rootState.preferredReactions, action);
|
|
|
|
|
|
|
|
const expectedEmoji = ['❤️', '👍🏿', '👎🏿', '😂', '😮', '😢'];
|
2021-09-09 16:29:01 +00:00
|
|
|
|
|
|
|
assert.deepEqual(result.customizePreferredReactionsModal, {
|
2021-09-09 23:47:30 +00:00
|
|
|
draftPreferredReactions: expectedEmoji,
|
|
|
|
originalPreferredReactions: expectedEmoji,
|
2021-09-09 16:29:01 +00:00
|
|
|
selectedDraftEmojiIndex: undefined,
|
|
|
|
isSaving: false,
|
|
|
|
hadSaveError: false,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('opens the customization modal with stored values', () => {
|
2021-09-09 23:47:30 +00:00
|
|
|
const storedPreferredReactionEmoji = ['✨', '❇️', '🎇', '🦈', '💖', '🅿️'];
|
2021-09-09 16:29:01 +00:00
|
|
|
|
|
|
|
const emptyRootState = getEmptyRootState();
|
|
|
|
const state = {
|
|
|
|
...emptyRootState,
|
|
|
|
items: {
|
|
|
|
...emptyRootState.items,
|
|
|
|
preferredReactionEmoji: storedPreferredReactionEmoji,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
const dispatch = sinon.spy();
|
|
|
|
openCustomizePreferredReactionsModal()(dispatch, () => state, null);
|
|
|
|
const [action] = dispatch.getCall(0).args;
|
|
|
|
|
|
|
|
const result = reducer(state.preferredReactions, action);
|
|
|
|
|
|
|
|
assert.deepEqual(result.customizePreferredReactionsModal, {
|
|
|
|
draftPreferredReactions: storedPreferredReactionEmoji,
|
|
|
|
originalPreferredReactions: storedPreferredReactionEmoji,
|
|
|
|
selectedDraftEmojiIndex: undefined,
|
|
|
|
isSaving: false,
|
|
|
|
hadSaveError: false,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('replaceSelectedDraftEmoji', () => {
|
|
|
|
const { replaceSelectedDraftEmoji } = actions;
|
|
|
|
|
|
|
|
it('is a no-op if the customization modal is not open', () => {
|
2022-02-23 18:48:40 +00:00
|
|
|
const state = getEmptyState();
|
2021-09-09 23:47:30 +00:00
|
|
|
const action = replaceSelectedDraftEmoji('🦈');
|
2021-09-09 16:29:01 +00:00
|
|
|
const result = reducer(state, action);
|
|
|
|
|
|
|
|
assert.strictEqual(result, state);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('is a no-op if no emoji is selected', () => {
|
2021-09-09 23:47:30 +00:00
|
|
|
const action = replaceSelectedDraftEmoji('💅');
|
2021-09-09 16:29:01 +00:00
|
|
|
const result = reducer(stateWithOpenCustomizationModal, action);
|
|
|
|
|
|
|
|
assert.strictEqual(result, stateWithOpenCustomizationModal);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('replaces the selected draft emoji and deselects', () => {
|
2021-09-09 23:47:30 +00:00
|
|
|
const action = replaceSelectedDraftEmoji('🐱');
|
2021-09-09 16:29:01 +00:00
|
|
|
const result = reducer(
|
|
|
|
stateWithOpenCustomizationModalAndSelectedEmoji,
|
|
|
|
action
|
|
|
|
);
|
|
|
|
|
|
|
|
assert.deepStrictEqual(
|
|
|
|
result.customizePreferredReactionsModal?.draftPreferredReactions,
|
2021-09-09 23:47:30 +00:00
|
|
|
['✨', '🐱', '🎇', '🦈', '💖', '🅿️']
|
2021-09-09 16:29:01 +00:00
|
|
|
);
|
|
|
|
assert.isUndefined(
|
|
|
|
result.customizePreferredReactionsModal?.selectedDraftEmojiIndex
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('resetDraftEmoji', () => {
|
|
|
|
const { resetDraftEmoji } = actions;
|
|
|
|
|
2021-09-09 23:47:30 +00:00
|
|
|
function getAction(rootState: Readonly<StateType>) {
|
|
|
|
const dispatch = sinon.spy();
|
|
|
|
resetDraftEmoji()(dispatch, () => rootState, null);
|
|
|
|
const [action] = dispatch.getCall(0).args;
|
|
|
|
return action;
|
|
|
|
}
|
|
|
|
|
2021-09-09 16:29:01 +00:00
|
|
|
it('is a no-op if the customization modal is not open', () => {
|
2021-09-09 23:47:30 +00:00
|
|
|
const rootState = getEmptyRootState();
|
|
|
|
const state = rootState.preferredReactions;
|
|
|
|
const action = getAction(rootState);
|
2021-09-09 16:29:01 +00:00
|
|
|
const result = reducer(state, action);
|
|
|
|
|
|
|
|
assert.strictEqual(result, state);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('resets the draft emoji to the defaults', () => {
|
2021-09-09 23:47:30 +00:00
|
|
|
const rootState = getRootState(stateWithOpenCustomizationModal);
|
|
|
|
const action = getAction(rootState);
|
|
|
|
const result = reducer(rootState.preferredReactions, action);
|
2021-09-09 16:29:01 +00:00
|
|
|
|
|
|
|
assert.deepEqual(
|
|
|
|
result.customizePreferredReactionsModal?.draftPreferredReactions,
|
2021-09-09 23:47:30 +00:00
|
|
|
['❤️', '👍', '👎', '😂', '😮', '😢']
|
2021-09-09 16:29:01 +00:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('deselects any selected emoji', () => {
|
2021-09-09 23:47:30 +00:00
|
|
|
const rootState = getRootState(
|
|
|
|
stateWithOpenCustomizationModalAndSelectedEmoji
|
2021-09-09 16:29:01 +00:00
|
|
|
);
|
2021-09-09 23:47:30 +00:00
|
|
|
const action = getAction(rootState);
|
|
|
|
const result = reducer(rootState.preferredReactions, action);
|
2021-09-09 16:29:01 +00:00
|
|
|
|
|
|
|
assert.isUndefined(
|
|
|
|
result.customizePreferredReactionsModal?.selectedDraftEmojiIndex
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('savePreferredReactions', () => {
|
|
|
|
const { savePreferredReactions } = actions;
|
|
|
|
|
2021-09-15 18:59:51 +00:00
|
|
|
// We want to create a fake ConversationController for testing purposes, and we need
|
|
|
|
// to sidestep typechecking to do that.
|
|
|
|
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
|
|
|
2021-09-09 16:29:01 +00:00
|
|
|
let storagePutStub: sinon.SinonStub;
|
2021-09-15 18:59:51 +00:00
|
|
|
let captureChangeStub: sinon.SinonStub;
|
|
|
|
let oldConversationController: any;
|
|
|
|
|
2021-09-09 16:29:01 +00:00
|
|
|
beforeEach(() => {
|
|
|
|
storagePutStub = sinonSandbox.stub(window.storage, 'put').resolves();
|
2021-09-15 18:59:51 +00:00
|
|
|
|
|
|
|
oldConversationController = window.ConversationController;
|
|
|
|
|
|
|
|
captureChangeStub = sinonSandbox.stub();
|
|
|
|
window.ConversationController = {
|
|
|
|
getOurConversationOrThrow: (): any => ({
|
|
|
|
captureChange: captureChangeStub,
|
|
|
|
}),
|
|
|
|
} as any;
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
window.ConversationController = oldConversationController;
|
2021-09-09 16:29:01 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('thunk', () => {
|
2021-09-15 18:59:51 +00:00
|
|
|
it('saves the preferred reaction emoji to local storage', async () => {
|
2021-09-09 16:29:01 +00:00
|
|
|
await savePreferredReactions()(
|
|
|
|
sinon.spy(),
|
|
|
|
() => getRootState(stateWithOpenCustomizationModal),
|
|
|
|
null
|
|
|
|
);
|
|
|
|
|
|
|
|
sinon.assert.calledWith(
|
|
|
|
storagePutStub,
|
|
|
|
'preferredReactionEmoji',
|
|
|
|
stateWithOpenCustomizationModal.customizePreferredReactionsModal
|
|
|
|
.draftPreferredReactions
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2021-09-15 18:59:51 +00:00
|
|
|
it('on success, enqueues a storage service upload', async () => {
|
|
|
|
await savePreferredReactions()(
|
|
|
|
sinon.spy(),
|
|
|
|
() => getRootState(stateWithOpenCustomizationModal),
|
|
|
|
null
|
|
|
|
);
|
|
|
|
|
|
|
|
sinon.assert.calledOnce(captureChangeStub);
|
|
|
|
});
|
|
|
|
|
2021-09-09 16:29:01 +00:00
|
|
|
it('on success, dispatches a pending action followed by a fulfilled action', async () => {
|
|
|
|
const dispatch = sinon.spy();
|
|
|
|
await savePreferredReactions()(
|
|
|
|
dispatch,
|
|
|
|
() => getRootState(stateWithOpenCustomizationModal),
|
|
|
|
null
|
|
|
|
);
|
|
|
|
|
|
|
|
sinon.assert.calledTwice(dispatch);
|
|
|
|
sinon.assert.calledWith(dispatch, {
|
|
|
|
type: 'preferredReactions/SAVE_PREFERRED_REACTIONS_PENDING',
|
|
|
|
});
|
|
|
|
sinon.assert.calledWith(dispatch, {
|
|
|
|
type: 'preferredReactions/SAVE_PREFERRED_REACTIONS_FULFILLED',
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('on failure, dispatches a pending action followed by a rejected action', async () => {
|
|
|
|
storagePutStub.rejects(new Error('something went wrong'));
|
|
|
|
|
|
|
|
const dispatch = sinon.spy();
|
|
|
|
await savePreferredReactions()(
|
|
|
|
dispatch,
|
|
|
|
() => getRootState(stateWithOpenCustomizationModal),
|
|
|
|
null
|
|
|
|
);
|
|
|
|
|
|
|
|
sinon.assert.calledTwice(dispatch);
|
|
|
|
sinon.assert.calledWith(dispatch, {
|
|
|
|
type: 'preferredReactions/SAVE_PREFERRED_REACTIONS_PENDING',
|
|
|
|
});
|
|
|
|
sinon.assert.calledWith(dispatch, {
|
|
|
|
type: 'preferredReactions/SAVE_PREFERRED_REACTIONS_REJECTED',
|
|
|
|
});
|
|
|
|
});
|
2021-09-15 18:59:51 +00:00
|
|
|
|
|
|
|
it('on failure, does not enqueue a storage service upload', async () => {
|
|
|
|
storagePutStub.rejects(new Error('something went wrong'));
|
|
|
|
|
|
|
|
await savePreferredReactions()(
|
|
|
|
sinon.spy(),
|
|
|
|
() => getRootState(stateWithOpenCustomizationModal),
|
|
|
|
null
|
|
|
|
);
|
|
|
|
|
|
|
|
sinon.assert.notCalled(captureChangeStub);
|
|
|
|
});
|
2021-09-09 16:29:01 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('SAVE_PREFERRED_REACTIONS_FULFILLED', () => {
|
|
|
|
const action = {
|
|
|
|
type: 'preferredReactions/SAVE_PREFERRED_REACTIONS_FULFILLED' as const,
|
|
|
|
};
|
|
|
|
|
|
|
|
it("does nothing if the modal isn't open", () => {
|
2022-02-23 18:48:40 +00:00
|
|
|
const result = reducer(getEmptyState(), action);
|
2021-09-09 16:29:01 +00:00
|
|
|
|
|
|
|
assert.notProperty(result, 'customizePreferredReactionsModal');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('closes the modal if open', () => {
|
|
|
|
const result = reducer(stateWithOpenCustomizationModal, action);
|
|
|
|
|
|
|
|
assert.notProperty(result, 'customizePreferredReactionsModal');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('SAVE_PREFERRED_REACTIONS_PENDING', () => {
|
|
|
|
const action = {
|
|
|
|
type: 'preferredReactions/SAVE_PREFERRED_REACTIONS_PENDING' as const,
|
|
|
|
};
|
|
|
|
|
|
|
|
it('marks the modal as "saving"', () => {
|
|
|
|
const result = reducer(stateWithOpenCustomizationModal, action);
|
|
|
|
|
|
|
|
assert.isTrue(result.customizePreferredReactionsModal?.isSaving);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('clears any previous errors', () => {
|
|
|
|
const state = {
|
|
|
|
...stateWithOpenCustomizationModal,
|
|
|
|
customizePreferredReactionsModal: {
|
|
|
|
...stateWithOpenCustomizationModal.customizePreferredReactionsModal,
|
|
|
|
hadSaveError: true,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
const result = reducer(state, action);
|
|
|
|
|
|
|
|
assert.isFalse(result.customizePreferredReactionsModal?.hadSaveError);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('deselects any selected emoji', () => {
|
|
|
|
const result = reducer(
|
|
|
|
stateWithOpenCustomizationModalAndSelectedEmoji,
|
|
|
|
action
|
|
|
|
);
|
|
|
|
|
|
|
|
assert.isUndefined(
|
|
|
|
result.customizePreferredReactionsModal?.selectedDraftEmojiIndex
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('SAVE_PREFERRED_REACTIONS_REJECTED', () => {
|
|
|
|
const action = {
|
|
|
|
type: 'preferredReactions/SAVE_PREFERRED_REACTIONS_REJECTED' as const,
|
|
|
|
};
|
|
|
|
|
|
|
|
it("does nothing if the modal isn't open", () => {
|
2022-02-23 18:48:40 +00:00
|
|
|
const state = getEmptyState();
|
2021-09-09 16:29:01 +00:00
|
|
|
const result = reducer(state, action);
|
|
|
|
|
|
|
|
assert.strictEqual(result, state);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('stops loading', () => {
|
|
|
|
const result = reducer(stateWithOpenCustomizationModal, action);
|
|
|
|
|
|
|
|
assert.isFalse(result.customizePreferredReactionsModal?.isSaving);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('saves that there was an error', () => {
|
|
|
|
const result = reducer(stateWithOpenCustomizationModal, action);
|
|
|
|
|
|
|
|
assert.isTrue(result.customizePreferredReactionsModal?.hadSaveError);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('selectDraftEmojiToBeReplaced', () => {
|
|
|
|
const { selectDraftEmojiToBeReplaced } = actions;
|
|
|
|
|
|
|
|
it('is a no-op if the customization modal is not open', () => {
|
2022-02-23 18:48:40 +00:00
|
|
|
const state = getEmptyState();
|
2021-09-09 16:29:01 +00:00
|
|
|
const action = selectDraftEmojiToBeReplaced(2);
|
|
|
|
const result = reducer(state, action);
|
|
|
|
|
|
|
|
assert.strictEqual(result, state);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('is a no-op if the index is out of range', () => {
|
|
|
|
const action = selectDraftEmojiToBeReplaced(99);
|
|
|
|
const result = reducer(stateWithOpenCustomizationModal, action);
|
|
|
|
|
|
|
|
assert.strictEqual(result, stateWithOpenCustomizationModal);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('sets the index as the selected one', () => {
|
|
|
|
const action = selectDraftEmojiToBeReplaced(3);
|
|
|
|
const result = reducer(stateWithOpenCustomizationModal, action);
|
|
|
|
|
|
|
|
assert.strictEqual(
|
|
|
|
result.customizePreferredReactionsModal?.selectedDraftEmojiIndex,
|
|
|
|
3
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|