"use strict";

import {expect} from "chai"
import {Datasource} from "../../../source/data/datasource.mjs";
import {DataUrl} from "../../../source/types/dataurl.mjs";


describe('Datasource', function () {


    it('should instance of Datasource ', function () {
        expect(new Datasource()).to.be.instanceof(Datasource)
    });

    describe('Options', function () {
        it('setOption should change value', function () {
            const datasource = new Datasource();
            expect(datasource.getOption('default')).to.be.undefined
            datasource.setOption('default', true)
            expect(datasource.getOption('default')).to.be.true
        });

        it('setOptions should set all values', function () {
            const datasource = new Datasource();
            expect(datasource.getOption('default')).to.be.undefined
            datasource.setOptions({default: true})
            expect(datasource.getOption('default')).to.be.true
        });

        it('setOptions should parse json string', function () {
            const datasource = new Datasource();
            datasource.setOptions('{"default":true,"nested":{"flag":1}}');
            expect(datasource.getOption('default')).to.be.true
            expect(datasource.getOption('nested.flag')).to.be.equal(1)
        });

        it('setOptions should parse data url json', function () {
            const datasource = new Datasource();
            const dataUrl = new DataUrl('{"default":true}', 'application/json', false).toString();
            datasource.setOptions(dataUrl);
            expect(datasource.getOption('default')).to.be.true
        });

        it('setOptions should throw on invalid json string', function () {
            const datasource = new Datasource();
            expect(() => datasource.setOptions('{')).to.throw(Error);
        });

    })

    describe('rw', function () {
        const datasource = new Datasource();
        it('read should throw exeption', function () {
            expect(() => datasource.read()).to.throw(Error);
        });
        
        it('write should throw exeption', function () {
            expect(() => datasource.read()).to.throw(Error);
        });


    })
    
    describe('get/set', function () {
        const datasource = new Datasource();
        it('get should return undefined', function () {
            expect( datasource.get()).to.be.eql({});
        });
        
        it('write should throw exeption', function () {
            expect( datasource.get()).to.be.eql({});
            expect( datasource.set({a:'myvalue'})).to.be.instanceof(Datasource);
            expect( datasource.get()).to.be.eql({a:'myvalue'});
        });


    })


})
