import "../spec_helper";

describe("Definition#buildModel", function () {
    class 
 extends Factory.Model {
        public id: number;
    }

    const getDef = (cb: Function): Factory.Definition => Factory.define("test", cb);

    it("must build model without given attributes", function () {
        const definition: Factory.Definition = getDef(function () { });
        expect(definition.buildModel<LocalType>()).not.toBeNull();
    });

    it("must build model with given attributes", function () {
        const definition: Factory.Definition = getDef(function () {
            field("id", 1);
        });
        const model = definition.buildModel<LocalType>({id: 1});
        expect(model).not.toBeNull();
    });

    it("must throw error if attribute wasn't defined", function () {
        const definition: Factory.Definition = getDef(() => {});
        expect(function () {
            definition.buildModel({id: 1})
        }).toThrowError("Unexpected data: [\"id\"]");
    });

    it("must define field using this notation", function () {
        const definition = getDef(function () {
            this.id = 234;
        });
        expect(function () {
            definition.buildModel({id: 1})
        }).not.toThrowError("Unexpected data: [\"id\"]");
    });

    it("must define field using passed library", function () {
        const definition = getDef(function (lib) {
            lib.id = 234;
        });
        expect(function () {
            definition.buildModel({id: 1})
        }).not.toThrowError("Unexpected data: [\"id\"]");
    });
});

LocalType