const Store = require('../');
const ChartistWrapper = require('../../../lib/chartist');
import { ERROR_CHART_CONTENT } from '../message';

describe('store wrapper', () => {

    describe('addChart', () => {

        test('requires a chart content', () => {
            expect(Store._callbacks).toEqual({});

            expect(() => {
                Store.addChart();
            }).toThrow(Error(ERROR_CHART_CONTENT));

            expect(Store._callbacks).toEqual({});
        });

        test('requires a chart type', () => {
            expect(Store._callbacks).toEqual({});

            expect(() => {
                Store.addChart(Store.CHART_CONTENT.ORDERS_RATIO);
            }).toThrow(Error(ChartistWrapper.ERROR_CHART_TYPE));

            expect(Store._callbacks).toEqual({});
        });

        test('adds a proper callback to the module callbacks', () => {
            expect(Store._callbacks).toEqual({});

            Store.addChart(Store.CHART_CONTENT.ORDERS_RATIO, ChartistWrapper.CHART_TYPES.PIE_CHART);

            let keys = Object.keys(Store._callbacks);
            expect(keys.length).toEqual(1);

            expect(Store._callbacks[keys].length).toEqual(2);

            expect(typeof Store._callbacks[keys][0]).toEqual('string');
            expect(Store._callbacks[keys][0]).toEqual('pie');

            expect(typeof Store._callbacks[keys][1]).toEqual('function');
            expect(Store._callbacks[keys][1]).toBe(require('../orders_ratio').convertData);
        });

        test('adds a proper callback to the module callbacks a second time', () => {
            expect(Object.keys(Store._callbacks).length).toEqual(1);

            Store.addChart(Store.CHART_CONTENT.VIEW_OVER_ORDER_CHART, ChartistWrapper.CHART_TYPES.LINE_CHART);

            let keys = Object.keys(Store._callbacks);
            expect(keys.length).toEqual(2);

            const key = keys[1];
            expect(Store._callbacks[key].length).toEqual(2);

            expect(typeof Store._callbacks[key][0]).toEqual('string');
            expect(Store._callbacks[key][0]).toEqual('line');

            expect(typeof Store._callbacks[key][1]).toEqual('function');
            expect(Store._callbacks[key][1]).toBe(require('../view_over_order_chart').convertData);
        });

        test('does not add a callback if its already in the list', () => {
            expect(Object.keys(Store._callbacks).length).toEqual(2);

            Store.addChart(Store.CHART_CONTENT.VIEW_OVER_ORDER_CHART, ChartistWrapper.CHART_TYPES.LINE_CHART);

            let keys = Object.keys(Store._callbacks);
            expect(keys.length).toEqual(2);
        });
    });

    describe('removeChart', () => {

        test('requires a chart content', () => {
            expect(() => {
                Store.removeChart();
            }).toThrow(Error(ERROR_CHART_CONTENT));

            expect(Store._callbacks).not.toEqual({});
        });

        test('removes a callback from the module callbacks', () => {
            Store.removeChart(Store.CHART_CONTENT.ORDERS_RATIO);

            let keys = Object.keys(Store._callbacks);
            expect(keys.length).toEqual(1);
        });

        test('removes the last callback from the module callbacks', () => {
            Store.removeChart(Store.CHART_CONTENT.VIEW_OVER_ORDER_CHART);

            let keys = Object.keys(Store._callbacks);
            expect(keys.length).toEqual(0);
            expect(Store._callbacks).toEqual({});
        });

        test('if chart content is not in callbacks it do nothing', () => {
            Store.removeChart(Store.CHART_CONTENT.VIEW_OVER_ORDER_CHART);

            let keys = Object.keys(Store._callbacks);
            expect(keys.length).toEqual(0);
            expect(Store._callbacks).toEqual({});
        });
    });
});
