import axios from 'axios';
import MockAdapter from "axios-mock-adapter";
import leadList from './mocks/leadList';
import {BX24Dev} from '../BX24Dev';
import auth from './mocks/auth';
import batchAppOption from './mocks/batchAppOption';
import authExample from './constants/authExample';
import batch from './mocks/batch';


let mock:MockAdapter;
let bx24:BX24Dev;
beforeAll(async () => {
    mock = new MockAdapter(axios);
    mock.onPost('https://oauth.bitrix.info/oauth/token/').reply(201, auth);
    mock.onPost('https://example.bitrix24.ru/rest/batch.json').reply(200, batchAppOption);
    await new Promise(resolve=>{
        bx24=new BX24Dev(authExample, ()=>{
            resolve(true);
        });
    });
    mock.reset();
});

afterEach(() => {
    mock.reset();
});


test('Test callMethod Promise', done => {    
        mock.onPost('https://example.bitrix24.ru/rest/batch.json').reply(200, batch);
        bx24.callBatch({
            getDeals:['crm.deal.list', {}],
            getLeads:['crm.lead.list', {}],
            getByID:['crm.deal.get', {id:123123}]
        }, result=>{
            expect(result['getDeals'].data()).toEqual(batch.result.result.getDeals);
            expect(result['getLeads'].data()).toEqual(leadList.result);

            expect(result['getDeals'].total()).toEqual(batch.result.result_total.getDeals);
            expect(result['getLeads'].total()).toEqual(leadList.total);

            expect(result['getDeals'].error()).toBeUndefined();
            expect(result['getLeads'].error()).toBeUndefined();
            expect(result.getByID.error()).toEqual({
                status: 200,
                ex: { error: '', error_description: 'Not found' }
            });
            done();
        });
});