{"size": 1809, "ext": "js", "max_stars_repo_path": "test/unit/actions/brands.spec.js", "max_stars_repo_name": "lightreign/cannedfindit", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "test/unit/actions/brands.spec.js", "max_issues_repo_name": "lightreign/cannedfindit", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "test/unit/actions/brands.spec.js", "max_forks_repo_name": "lightreign/cannedfindit", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 28.7142857143, "max_line_length": 82, "alphanum_fraction": 0.5461580984}
import thunk from "redux-thunk";
import configureMockStore from "redux-mock-store";
import MockAdapter from "axios-mock-adapter";
import axios from "../../../src/store/api";

import { listBrands, addBrand } from "../../../src/store/actions";
import { LIST_BRANDS, ADD_BRAND, NOTIFY_SUCCESS } from "../../../src/store/types";
import {mockCreateResponse} from "action-utils";

const axiosMock = new MockAdapter(axios);

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

describe("brand actions", () => {
    afterEach(() => {
        axiosMock.reset();
    });

    it( 'should trigger LIST_BRANDS store action on success', async () => {
        const response = [
            [
                {
                    _id : '11',
                    name : 'Newgate'
                },
                {
                    _id : '22',
                    name : 'Oldgate'
                }
            ]
        ];

        axiosMock.onGet("/brand").reply(200, response);

        const store = mockStore();
        await store.dispatch(listBrands());

        expect(store.getActions()).toEqual([
            { type: LIST_BRANDS, brands: response }
        ]);
    });

    it('should trigger ADD_BRAND when creating a new brand', async () => {
        const brandName = 'Crowfield';

        const brand = [ {
            name : brandName
        } ];

        const response = mockCreateResponse(brand, '99');

        axiosMock.onPost("/brand/new", { brand: brandName }).reply(201, response);

        const store = mockStore();
        await store.dispatch(addBrand(brandName));

        expect(store.getActions()).toEqual([
            { type: ADD_BRAND, brand: response },
            { type: NOTIFY_SUCCESS, message: "Brand created successfully" }
        ]);
    });
} );