<commit_before>// ReSharper disable InconsistentNaming
// ReSharper disable UnusedLocals
(function (testFns) {
    "use strict";

    /*********************************************************
    * Breeze configuration and module setup 
    *********************************************************/
    var breeze = testFns.breeze;
    var MetadataStore = breeze.MetadataStore;
    var EntityManager = breeze.EntityManager;
    var EntityQuery = breeze.EntityQuery;
    var EntityState = breeze.EntityState;

    var moduleMetadataStore = new MetadataStore();
    var northwindService = testFns.northwindServiceName;
    var todoService = testFns.todosServiceName;
    var handleFail = testFns.handleFail;

    module("entityExtensionTests", { setup: moduleMetadataStoreSetup });

    // Populate the moduleMetadataStore with Northwind service metadata
    function moduleMetadataStoreSetup() {
        if (!moduleMetadataStore.isEmpty()) return; // got it already

        stop(); // going async for metadata ...
        Q.all(
            moduleMetadataStore.fetchMetadata(northwindService),
            moduleMetadataStore.fetchMetadata(todoService))
        .fail(handleFail)
        .fin(start);
    }

    /*********************************************************
    * add property directly to customer instance
    *********************************************************/
    test("add property directly to customer instance", 2, function () {
        var customerType = moduleMetadataStore.getEntityType("Customer");
        var cust = customerType.createEntity();
        cust.isBeingEdited = ko.observable(false);
        ok(typeof cust["isBeingEdited"] === "function",
            "should have 'isBeingEdited' KO property after adding directly to instance");
        
        var propInfo = customerType.getProperty("isBeingEdited");
        // The Breeze customerType knows nothing about it.
        ok(propInfo === null, "'isBeingEdited' should be unknown to the customer type");
    });
    
    /*********************************************************
    * add unmapped property via constructor
    *********************************************************/
    test("add unmapped 'isBeingEdited' property via constructor", 3, function () {
        var store = cloneModuleMetadataStore();
        
        var Customer = function() {
            this.isBeingEdited = false; // notice it is not a KO property here
        };
        
        store.registerEntityTypeCtor("Customer", Customer);
        
        var customerType = store.getEntityType("Customer");
        var unmapped = customerType.unmappedProperties;

        // Breeze identified the property as "unmapped"
        ok(unmapped.length === 1 && unmapped[0].name === "isBeingEdited",
            "'isBeingEdited' should be the lone unmapped property");

        var cust = customerType.createEntity();

        ok(typeof cust["isBeingEdited"] === "function",
            "should have 'isBeingEdited' KO property via constructor");
        
        // Breeze converted it into a KO property and initialized it
        equal(cust.isBeingEdited(), false,
            "'isBeingEdited' should be a KO 'property' returning false");
    });

    /*********************************************************
    * can 'new' the ctor .. but not a full entity until added to manager
    * not recommended; prefer CreateEntity approach
    *********************************************************/
    test("can 'new' an entity via custom ctor and add to manager", 1, function () {
        var store = cloneModuleMetadataStore();

        var Customer = function () {
            // notice these are not defined as KO properties
            this.CustomerID = testFns.newGuidComb();
            this.isBeingEdited = false; 
        };

        store.registerEntityTypeCtor("Customer", Customer);

        var cust = new Customer(); // not recommended;

        var em = newEm(store);
        em.addEntity(cust);

        // Breeze converted it into a KO property and initialized it
        equal(cust.isBeingEdited(), false,
            "'isBeingEdited' should be a KO 'property' returning false");

    });

    /*********************************************************
    * can add unmapped 'foo' property via constructor
    *********************************************************/
    test("can add unmapped 'foo' property via constructor", 4, function () {
        var store = cloneModuleMetadataStore();
        assertFooPropertyDefined(store, false);

        var Customer = function () {
            this.foo = 42; // doesn't have to be KO observable; will become observable
        };
        store.registerEntityTypeCtor('Customer', Customer);<commit_msg>DocCode: entityExtensionTests clarify how and when breeze converts ctor properties to KO properties See SO http://stackoverflow.com/questions/24086027/why-is-breeze-js-not-mapping-properties-as-dependent-observables/24099949#24099949<commit_after>// ReSharper disable InconsistentNaming
// ReSharper disable UnusedLocals
(function (testFns) {
    "use strict";

    /*********************************************************
    * Breeze configuration and module setup 
    *********************************************************/
    var breeze = testFns.breeze;
    var MetadataStore = breeze.MetadataStore;
    var EntityManager = breeze.EntityManager;
    var EntityQuery = breeze.EntityQuery;
    var EntityState = breeze.EntityState;

    var moduleMetadataStore = new MetadataStore();
    var northwindService = testFns.northwindServiceName;
    var todoService = testFns.todosServiceName;
    var handleFail = testFns.handleFail;

    module("entityExtensionTests", { setup: moduleMetadataStoreSetup });

    // Populate the moduleMetadataStore with Northwind service metadata
    function moduleMetadataStoreSetup() {
        if (!moduleMetadataStore.isEmpty()) return; // got it already

        stop(); // going async for metadata ...
        Q.all(
            moduleMetadataStore.fetchMetadata(northwindService),
            moduleMetadataStore.fetchMetadata(todoService))
        .fail(handleFail)
        .fin(start);
    }

    /*********************************************************
    * add untracked property directly to customer instance
    *********************************************************/
    test("add untracked 'isPartial' property directly to customer instance", 1, function () {

        var em = newEm();
        var cust = em.createEntity('Customer', {
            CustomerID: testFns.newGuidComb()
        });

        // property added to instance
        cust.isPartial = ko.observable(false);

        // can get the type from the entity instance
        var custType = cust.entityType;
        var propInfo = custType.getProperty("isPartial");

        // The Breeze customerType knows nothing about it.
        ok(propInfo === null, "'isPartial' should be unknown to the customer type");
    });
    
    /*********************************************************
    * add unmapped property via constructor
    *********************************************************/
    test("add unmapped 'isPartial' property via constructor", 6, function () {
        var store = cloneModuleMetadataStore();
        
        var Customer = function () {
            // These are simple 'field' properties.
            // Breeze converts them to the right kind of properties  
            // for the prevailing modelLibrary adapter
            // such as KO observable properties.
            this.CustomerID = testFns.newGuidComb();
            this.isPartial = true;
        };
        
        store.registerEntityTypeCtor('Customer', Customer);

        var em = newEm(store);
        var cust = em.createEntity('Customer'); 

        assertExpectedCustomerCtorProperties(cust);
    });

    function assertExpectedCustomerCtorProperties(cust) {
        // can get the type from the entity instance
        var custType = cust.entityType;
        ok(custType, "'cust' is now an entity");

        // Breeze converted both into KO observables
        ok(typeof cust.CustomerID === 'function',
            "'CustomerID' should be a KO observable");

        ok(typeof cust.isPartial === 'function',
            "'isPartial' should be a KO observable");

        if (!custType) {return;} // no remaining tests would pass
        
        var propInfo = custType.getProperty('CustomerID');
        ok(propInfo && !propInfo.isUnmapped && propInfo.isPartOfKey,
            "'CustomerID' should be detected as a mapped property");

        propInfo = custType.getProperty('isPartial');
        ok(propInfo && propInfo.isUnmapped,
            "'isPartial' should be an unmapped property");

        var unmapped = custType.unmappedProperties;
        ok(unmapped.length === 1,
            "'isPartial' should be the lone unmapped property");
    }

    /*********************************************************
    * can 'new' the ctor .. but not a full entity until added to manager
    * not recommended; prefer CreateEntity approach
    *********************************************************/
    test("can 'new' an entity via custom ctor and add to manager", 8, function () {
        var store = cloneModuleMetadataStore();

        var Customer = function () {
            this.CustomerID = testFns.newGuidComb();
            this.isPartial = true; 
        };

        store.registerEntityTypeCtor('Customer', Customer);

        var cust = new Customer(); // not recommended!!

        // they aren't observables until they're added to the manager
        ok(typeof cust.CustomerID !== 'function',
            "'CustomerID' should NOT be a KO observable before adding to em");

        ok(typeof cust.isPartial !== 'function',
            "'isPartial' should NOT be a KO observable before adding to em");

        var em = newEm(store);
        em.addEntity(cust);

        // now they are observables
        assertExpectedCustomerCtorProperties(cust);

    });

    /*********************************************************
    * can add unmapped 'foo' property via constructor
    *********************************************************/
    test("can add unmapped 'foo' property via constructor", 4, function () {
        var store = cloneModuleMetadataStore();
        assertFooPropertyDefined(store, false);

        var Customer = function () {
            this.foo = 42; // doesn't have to be KO observable; will become observable
        };
        store.registerEntityTypeCtor('Customer', Customer);
{'fraction_non_alphanumeric': 0.19268131660850701, 'fraction_numerical': 0.0036782042818070356, 'mean_word_length': 2.9641121495327103, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5706910', 'n_tokens_mistral': 2620, 'n_tokens_neox': 2455, 'n_words': 755}