
import {Collection, Model, Types} from "../../../lib/index";
import assert from "assert";

class Product extends Model<Product> {
    structure() {
        return {
            name: Types.String,
            price: Types.Number
        };
    }
}

describe("Collection.map", () => {

    it("map()", () => {

        class Products extends Collection<Product> {
            Model() {
                return Product;
            }
        }

        const products = new Products([
            {name: "Eggs", price: 1.8},
            {name: "Pie", price: 10},
            {name: "Milk", price: 4}
        ]);

        const prices = products.map((product) =>
            product.get("price")
        );

        assert.deepStrictEqual( prices, [1.8, 10, 4] );
    });
    
    it("map(f, context)", () => {
        class Products extends Collection<Product> {
            Model() {
                return Product;
            }
        }

        const products = new Products([
            {name: "Eggs", price: 1.2}
        ]);

        
        const context = {
            changed: false,
            handler() {
                this.changed = true;
            }
        };

        products.map(context.handler, context);

        assert.strictEqual(context.changed, true);
    });
});
