Add tests for account-notes action

merge-requests/1463/head
Justin 2022-05-27 14:08:41 -04:00
rodzic 8dfb8c4bb2
commit 8433ff70b0
8 zmienionych plików z 194 dodań i 73 usunięć

Wyświetl plik

@ -0,0 +1,106 @@
import { __stub } from 'soapbox/api';
import { mockStore } from 'soapbox/jest/test-helpers';
import rootReducer from 'soapbox/reducers';
import { normalizeAccount } from '../../normalizers';
import { changeAccountNoteComment, initAccountNoteModal, submitAccountNote } from '../account-notes';
describe('submitAccountNote()', () => {
let store;
beforeEach(() => {
const state = rootReducer(undefined, {})
.set('account_notes', { edit: { account_id: 1, comment: 'hello' } });
store = mockStore(state);
});
describe('with a successful API request', () => {
beforeEach(() => {
__stub((mock) => {
mock.onPost('/api/v1/accounts/1/note').reply(200, {});
});
});
it('post the note to the API', async() => {
const expectedActions = [
{ type: 'ACCOUNT_NOTE_SUBMIT_REQUEST' },
{ type: 'MODAL_CLOSE', modalType: undefined },
{ type: 'ACCOUNT_NOTE_SUBMIT_SUCCESS', relationship: {} },
];
await store.dispatch(submitAccountNote());
const actions = store.getActions();
expect(actions).toEqual(expectedActions);
});
});
describe('with an unsuccessful API request', () => {
beforeEach(() => {
__stub((mock) => {
mock.onPost('/api/v1/accounts/1/note').networkError();
});
});
it('should dispatch failed action', async() => {
const expectedActions = [
{ type: 'ACCOUNT_NOTE_SUBMIT_REQUEST' },
{
type: 'ACCOUNT_NOTE_SUBMIT_FAIL',
error: new Error('Network Error'),
},
];
await store.dispatch(submitAccountNote());
const actions = store.getActions();
expect(actions).toEqual(expectedActions);
});
});
});
describe('initAccountNoteModal()', () => {
let store;
beforeEach(() => {
const state = rootReducer(undefined, {})
.set('relationships', { 1: { note: 'hello' } });
store = mockStore(state);
});
it('dispatches the proper actions', async() => {
const account = normalizeAccount({
id: '1',
acct: 'justin-username',
display_name: 'Justin L',
avatar: 'test.jpg',
verified: true,
});
const expectedActions = [
{ type: 'ACCOUNT_NOTE_INIT_MODAL', account, comment: 'hello' },
{ type: 'MODAL_OPEN', modalType: 'ACCOUNT_NOTE' },
];
await store.dispatch(initAccountNoteModal(account));
const actions = store.getActions();
expect(actions).toEqual(expectedActions);
});
});
describe('changeAccountNoteComment()', () => {
let store;
beforeEach(() => {
const state = rootReducer(undefined, {});
store = mockStore(state);
});
it('dispatches the proper actions', async() => {
const comment = 'hello world';
const expectedActions = [
{ type: 'ACCOUNT_NOTE_CHANGE_COMMENT', comment },
];
await store.dispatch(changeAccountNoteComment(comment));
const actions = store.getActions();
expect(actions).toEqual(expectedActions);
});
});

Wyświetl plik

@ -0,0 +1,82 @@
import { AxiosError } from 'axios';
import { AnyAction } from 'redux';
import api from '../api';
import { openModal, closeModal } from './modals';
import type { Account } from 'soapbox/types/entities';
const ACCOUNT_NOTE_SUBMIT_REQUEST = 'ACCOUNT_NOTE_SUBMIT_REQUEST';
const ACCOUNT_NOTE_SUBMIT_SUCCESS = 'ACCOUNT_NOTE_SUBMIT_SUCCESS';
const ACCOUNT_NOTE_SUBMIT_FAIL = 'ACCOUNT_NOTE_SUBMIT_FAIL';
const ACCOUNT_NOTE_INIT_MODAL = 'ACCOUNT_NOTE_INIT_MODAL';
const ACCOUNT_NOTE_CHANGE_COMMENT = 'ACCOUNT_NOTE_CHANGE_COMMENT';
const submitAccountNote = () => (dispatch: React.Dispatch<AnyAction>, getState: any) => {
dispatch(submitAccountNoteRequest());
const id = getState().getIn(['account_notes', 'edit', 'account_id']);
return api(getState)
.post(`/api/v1/accounts/${id}/note`, {
comment: getState().getIn(['account_notes', 'edit', 'comment']),
})
.then(response => {
dispatch(closeModal());
dispatch(submitAccountNoteSuccess(response.data));
})
.catch(error => dispatch(submitAccountNoteFail(error)));
};
function submitAccountNoteRequest() {
return {
type: ACCOUNT_NOTE_SUBMIT_REQUEST,
};
}
function submitAccountNoteSuccess(relationship: any) {
return {
type: ACCOUNT_NOTE_SUBMIT_SUCCESS,
relationship,
};
}
function submitAccountNoteFail(error: AxiosError) {
return {
type: ACCOUNT_NOTE_SUBMIT_FAIL,
error,
};
}
const initAccountNoteModal = (account: Account) => (dispatch: React.Dispatch<AnyAction>, getState: any) => {
const comment = getState().getIn(['relationships', account.get('id'), 'note']);
dispatch({
type: ACCOUNT_NOTE_INIT_MODAL,
account,
comment,
});
dispatch(openModal('ACCOUNT_NOTE'));
};
function changeAccountNoteComment(comment: string) {
return {
type: ACCOUNT_NOTE_CHANGE_COMMENT,
comment,
};
}
export {
submitAccountNote,
initAccountNoteModal,
changeAccountNoteComment,
ACCOUNT_NOTE_SUBMIT_REQUEST,
ACCOUNT_NOTE_SUBMIT_SUCCESS,
ACCOUNT_NOTE_SUBMIT_FAIL,
ACCOUNT_NOTE_INIT_MODAL,
ACCOUNT_NOTE_CHANGE_COMMENT,
};

Wyświetl plik

@ -1,67 +0,0 @@
import api from '../api';
import { openModal, closeModal } from './modals';
export const ACCOUNT_NOTE_SUBMIT_REQUEST = 'ACCOUNT_NOTE_SUBMIT_REQUEST';
export const ACCOUNT_NOTE_SUBMIT_SUCCESS = 'ACCOUNT_NOTE_SUBMIT_SUCCESS';
export const ACCOUNT_NOTE_SUBMIT_FAIL = 'ACCOUNT_NOTE_SUBMIT_FAIL';
export const ACCOUNT_NOTE_INIT_MODAL = 'ACCOUNT_NOTE_INIT_MODAL';
export const ACCOUNT_NOTE_CHANGE_COMMENT = 'ACCOUNT_NOTE_CHANGE_COMMENT';
export function submitAccountNote() {
return (dispatch, getState) => {
dispatch(submitAccountNoteRequest());
const id = getState().getIn(['account_notes', 'edit', 'account_id']);
api(getState).post(`/api/v1/accounts/${id}/note`, {
comment: getState().getIn(['account_notes', 'edit', 'comment']),
}).then(response => {
dispatch(closeModal());
dispatch(submitAccountNoteSuccess(response.data));
}).catch(error => dispatch(submitAccountNoteFail(error)));
};
}
export function submitAccountNoteRequest() {
return {
type: ACCOUNT_NOTE_SUBMIT_REQUEST,
};
}
export function submitAccountNoteSuccess(relationship) {
return {
type: ACCOUNT_NOTE_SUBMIT_SUCCESS,
relationship,
};
}
export function submitAccountNoteFail(error) {
return {
type: ACCOUNT_NOTE_SUBMIT_FAIL,
error,
};
}
export function initAccountNoteModal(account) {
return (dispatch, getState) => {
const comment = getState().getIn(['relationships', account.get('id'), 'note']);
dispatch({
type: ACCOUNT_NOTE_INIT_MODAL,
account,
comment,
});
dispatch(openModal('ACCOUNT_NOTE'));
};
}
export function changeAccountNoteComment(comment) {
return {
type: ACCOUNT_NOTE_CHANGE_COMMENT,
comment,
};
}

Wyświetl plik

@ -11,7 +11,7 @@ export function openModal(type: string, props?: any) {
}
/** Close the modal */
export function closeModal(type: string) {
export function closeModal(type?: string) {
return {
type: MODAL_CLOSE,
modalType: type,

Wyświetl plik

@ -2,7 +2,7 @@ import React from 'react';
import { defineMessages, injectIntl, FormattedMessage } from 'react-intl';
import { connect } from 'react-redux';
import { initAccountNoteModal } from 'soapbox/actions/account_notes';
import { initAccountNoteModal } from 'soapbox/actions/account-notes';
import {
followAccount,
unfollowAccount,

Wyświetl plik

@ -3,7 +3,7 @@ import React from 'react';
import { defineMessages, injectIntl, FormattedMessage } from 'react-intl';
import { connect } from 'react-redux';
import { changeAccountNoteComment, submitAccountNote } from 'soapbox/actions/account_notes';
import { changeAccountNoteComment, submitAccountNote } from 'soapbox/actions/account-notes';
import { closeModal } from 'soapbox/actions/modals';
import { Modal, Text } from 'soapbox/components/ui';
import { makeGetAccount } from 'soapbox/selectors';

Wyświetl plik

@ -7,7 +7,7 @@ import {
ACCOUNT_NOTE_SUBMIT_REQUEST,
ACCOUNT_NOTE_SUBMIT_FAIL,
ACCOUNT_NOTE_SUBMIT_SUCCESS,
} from '../actions/account_notes';
} from '../actions/account-notes';
const EditRecord = ImmutableRecord({
isSubmitting: false,
@ -39,4 +39,4 @@ export default function account_notes(state: State = ReducerRecord(), action: An
default:
return state;
}
}
}

Wyświetl plik

@ -3,7 +3,7 @@ import { get } from 'lodash';
import { STREAMING_FOLLOW_RELATIONSHIPS_UPDATE } from 'soapbox/actions/streaming';
import { ACCOUNT_NOTE_SUBMIT_SUCCESS } from '../actions/account_notes';
import { ACCOUNT_NOTE_SUBMIT_SUCCESS } from '../actions/account-notes';
import {
ACCOUNT_FOLLOW_SUCCESS,
ACCOUNT_FOLLOW_REQUEST,