{"size": 3166, "ext": "js", "max_stars_repo_path": "src/redux/actions/comments.test.js", "max_stars_repo_name": "farman9849/brillder", "max_stars_repo_stars_event_min_datetime": "2020-07-07T09:41:43.000Z", "max_stars_repo_stars_event_max_datetime": "2020-07-07T09:41:43.000Z", "max_issues_repo_path": "src/redux/actions/comments.test.js", "max_issues_repo_name": "farman9849/brillder", "max_issues_repo_issues_event_min_datetime": "2020-01-15T09:46:33.000Z", "max_issues_repo_issues_event_max_datetime": "2022-03-30T13:29:55.000Z", "max_forks_repo_path": "src/redux/actions/comments.test.js", "max_forks_repo_name": "farman9849/brillder", "max_forks_repo_forks_event_min_datetime": "2020-02-19T15:56:49.000Z", "max_forks_repo_forks_event_max_datetime": "2022-02-14T13:49:20.000Z"}
{"max_stars_count": 1, "max_issues_count": 3555, "max_forks_count": 12, "avg_line_length": 31.9797979798, "max_line_length": 88, "alphanum_fraction": 0.6174984207}
import configureMockStore from 'redux-mock-store';
import thunkMiddleware from 'redux-thunk';
import comments from './comments';
import mockAxios from 'axios';
import types from '../types';

const middlewares = [thunkMiddleware];
const mockStore = configureMockStore(middlewares);

const mockComment = {
    id: 1,
    author: {
        email: "admin@test.com",
        firstName: "Admin",
        lastName: "User"
    },
    text: "Comment Text",
    timestamp: new Date(0)
};

describe("comment actions", () => {
    afterEach(() => {
        jest.clearAllMocks();
    });

    it("returns the correct action on a successful GET", async () => {
        const store = mockStore({ comments: null, error: "" });

        const expectedAction = {
            type: types.GET_COMMENTS_SUCCESS,
            comments: [ mockComment ]
        };

        mockAxios.get.mockResolvedValue({ data: [ mockComment ] });
        await store.dispatch(comments.getComments(1));

        expect(store.getActions()).toContainEqual(expectedAction);
        expect(mockAxios.get).toHaveBeenCalledTimes(1);
    });

    it("returns the correct action on a failed GET", async () => {
        const store = mockStore({ comments: null, error: "" });

        const expectedAction = {
            type: types.GET_COMMENTS_FAILURE,
            error: "Mock error."
        };

        mockAxios.get.mockRejectedValue({ message: "Mock error." });
        await store.dispatch(comments.getComments(1));

        expect(store.getActions()).toContainEqual(expectedAction);
        expect(mockAxios.get).toHaveBeenCalledTimes(1);
    });

    it("returns the correct action on a successful new comment", async () => {
        const store = mockStore({ comments: [], error: "" });

        const expectedAction = {
            type: types.CREATE_COMMENT_SUCCESS,
            newComment: mockComment
        };

        mockAxios.post.mockResolvedValue({ data: mockComment });
        await store.dispatch(comments.createComment({ ...mockComment, id: undefined }));

        expect(store.getActions()).toContainEqual(expectedAction);
        expect(mockAxios.post).toHaveBeenCalledTimes(1);
    });

    it("returns the correct action on a failed new comment", async () => {
        const store = mockStore({ comments: [], error: "" });

        const expectedAction = {
            type: types.CREATE_COMMENT_FAILURE,
            error: "Mock Error."
        };

        mockAxios.post.mockRejectedValue({ message: expectedAction.error });
        await store.dispatch(comments.createComment({ ...mockComment, id: undefined }));

        expect(store.getActions()).toContainEqual(expectedAction);
        expect(mockAxios.post).toHaveBeenCalledTimes(1);
    })

    it("returns the correct action on an edit request", () => {
        const store = mockStore({ comments: [mockComment], error: "" });

        const expectedAction = {
            type: types.EDIT_COMMENT,
            commentId: 1,
            newText: "New Comment Text"
        };

        store.dispatch(comments.editComment(1, "New Comment Text"));

        expect(store.getActions()).toContainEqual(expectedAction);
    });
})