"use strict";
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
Object.defineProperty(exports, "__esModule", { value: true });
const internal_1 = require("../../../internal");
const CdmCollectionHelperFunctions_1 = require("./CdmCollectionHelperFunctions");
// tslint:disable-next-line: max-func-body-length
describe('Cdm/CdmCollection/CdmTraitCollection', () => {
    it('TestCdmTraitCollectionAdd', () => {
        const manifest = CdmCollectionHelperFunctions_1.generateManifest();
        const trait = new internal_1.CdmTraitDefinition(manifest.ctx, 'traitName', undefined);
        const otherTrait = new internal_1.CdmTraitDefinition(manifest.ctx, 'Name of other trait', undefined);
        manifest.traitCache = new Map();
        const addedTrait = manifest.exhibitsTraits.push(trait);
        const addedOtherTrait = manifest.exhibitsTraits.push(otherTrait);
        const listOfArgs = [[internal_1.constants.INCREMENTAL_PATTERN_PARAMETER_NAME, 'test'], ['fullDataPartitionPatternName', 'name']];
        const addedIncrementalTrait = manifest.exhibitsTraits.push(internal_1.constants.INCREMENTAL_TRAIT_NAME, listOfArgs);
        expect(manifest.traitCache)
            .toBeUndefined();
        expect(manifest.exhibitsTraits.length)
            .toEqual(3);
        expect(manifest.exhibitsTraits.allItems[0].explicitReference)
            .toEqual(trait);
        expect(manifest.exhibitsTraits.allItems[1].explicitReference)
            .toEqual(otherTrait);
        expect(manifest.exhibitsTraits.allItems[0])
            .toEqual(addedTrait);
        expect(manifest.exhibitsTraits.allItems[1])
            .toEqual(addedOtherTrait);
        expect(manifest.exhibitsTraits.allItems[2])
            .toEqual(addedIncrementalTrait);
        expect(manifest.exhibitsTraits.allItems[2].arguments.length)
            .toEqual(2);
        expect(manifest.exhibitsTraits.allItems[2].arguments.fetchValue(internal_1.constants.INCREMENTAL_PATTERN_PARAMETER_NAME))
            .toEqual('test');
        expect(manifest.exhibitsTraits.allItems[2].arguments.fetchValue('fullDataPartitionPatternName'))
            .toEqual('name');
    });
    it('TestCdmTraitCollectionInsert', () => {
        const manifest = CdmCollectionHelperFunctions_1.generateManifest();
        const trait = new internal_1.CdmTraitReference(manifest.ctx, 'traitName', false, false);
        const otherTrait = new internal_1.CdmTraitReference(manifest.ctx, 'name of other trait', false, false);
        manifest.traitCache = new Map();
        manifest.exhibitsTraits.insert(0, trait);
        manifest.exhibitsTraits.insert(0, otherTrait);
        expect(manifest.traitCache)
            .toBeUndefined();
        expect(manifest.exhibitsTraits.length)
            .toEqual(2);
        expect(manifest.exhibitsTraits.allItems[0])
            .toEqual(otherTrait);
        expect(manifest.exhibitsTraits.allItems[1])
            .toEqual(trait);
        expect(manifest.exhibitsTraits.allItems[0].owner)
            .toEqual(manifest);
    });
    it('CdmTraitCollectionAddRange', () => {
        const manifest = CdmCollectionHelperFunctions_1.generateManifest();
        const trait = new internal_1.CdmTraitDefinition(manifest.ctx, 'traitName', undefined);
        const otherTrait = new internal_1.CdmTraitDefinition(manifest.ctx, 'name of other trait', undefined);
        const traitList = [trait, otherTrait];
        manifest.exhibitsTraits.concat(traitList);
        expect(manifest.exhibitsTraits.length)
            .toEqual(2);
        expect(manifest.exhibitsTraits.allItems[0].explicitReference)
            .toEqual(trait);
        expect(manifest.exhibitsTraits.allItems[1].explicitReference)
            .toEqual(otherTrait);
        expect(manifest.exhibitsTraits.allItems[0].owner)
            .toEqual(manifest);
    });
    it('CdmTraitCollectionRemove', () => {
        const manifest = CdmCollectionHelperFunctions_1.generateManifest();
        const trait = new internal_1.CdmTraitDefinition(manifest.ctx, 'traitName', undefined);
        const otherTrait = new internal_1.CdmTraitDefinition(manifest.ctx, 'name of other trait', undefined);
        manifest.exhibitsTraits.push(trait);
        manifest.exhibitsTraits.push(otherTrait);
        expect(manifest.exhibitsTraits.length)
            .toEqual(2);
        manifest.traitCache = new Map();
        let removed = manifest.exhibitsTraits.remove(trait);
        expect(removed)
            .toBeTruthy();
        expect(manifest.exhibitsTraits.length)
            .toEqual(1);
        expect(manifest.traitCache)
            .toBeUndefined();
        // try to remove a second time
        removed = manifest.exhibitsTraits.remove(trait);
        expect(removed)
            .toBeFalsy();
        expect(manifest.exhibitsTraits.length)
            .toEqual(1);
        expect(manifest.exhibitsTraits.allItems[0].explicitReference)
            .toEqual(otherTrait);
        removed = manifest.exhibitsTraits.remove('name of other trait');
        expect(removed)
            .toBeTruthy();
        expect(manifest.exhibitsTraits.length)
            .toEqual(0);
        manifest.exhibitsTraits.push(trait);
        expect(manifest.exhibitsTraits.length)
            .toEqual(1);
        removed = manifest.exhibitsTraits.remove(manifest.exhibitsTraits.allItems[0]);
        expect(removed)
            .toBeTruthy();
        expect(manifest.exhibitsTraits.length)
            .toEqual(0);
    });
    it('CdmTraitCollectionRemoveAt', () => {
        const manifest = CdmCollectionHelperFunctions_1.generateManifest();
        const trait = new internal_1.CdmTraitDefinition(manifest.ctx, 'traitName', undefined);
        const otherTrait = new internal_1.CdmTraitDefinition(manifest.ctx, 'name of other trait', undefined);
        manifest.exhibitsTraits.push(trait);
        manifest.exhibitsTraits.push(otherTrait);
        manifest.traitCache = new Map();
        manifest.exhibitsTraits.remove(trait);
        expect(manifest.traitCache)
            .toBeUndefined();
        manifest.exhibitsTraits.push(trait);
        manifest.traitCache = new Map();
        manifest.exhibitsTraits.removeAt(1);
        expect(manifest.traitCache)
            .toBeUndefined();
        expect(manifest.exhibitsTraits.length)
            .toEqual(1);
        expect(manifest.exhibitsTraits.allItems[0].explicitReference)
            .toEqual(otherTrait);
    });
    it('CdmTraitCollectionIndexOf', () => {
        const manifest = CdmCollectionHelperFunctions_1.generateManifest();
        const trait = new internal_1.CdmTraitDefinition(manifest.ctx, 'traitName', undefined);
        const otherTrait = new internal_1.CdmTraitDefinition(manifest.ctx, 'name of other trait', undefined);
        manifest.exhibitsTraits.push(trait);
        manifest.exhibitsTraits.push(otherTrait);
        let index = manifest.exhibitsTraits.indexOf(trait);
        expect(index)
            .toEqual(0);
        index = manifest.exhibitsTraits.indexOf(otherTrait);
        expect(index)
            .toEqual(1);
        index = manifest.exhibitsTraits.indexOf(manifest.exhibitsTraits.allItems[0]);
        expect(index)
            .toEqual(0);
        index = manifest.exhibitsTraits.indexOf(manifest.exhibitsTraits.allItems[1]);
        expect(index)
            .toEqual(1);
        index = manifest.exhibitsTraits.indexOf('traitName');
        expect(index)
            .toEqual(0);
        index = manifest.exhibitsTraits.indexOf('name of other trait');
        expect(index)
            .toEqual(1);
    });
    it('CdmTraitCollectionRemoveOnlyFromProperty', () => {
        const manifest = CdmCollectionHelperFunctions_1.generateManifest();
        const trait = new internal_1.CdmTraitReference(manifest.ctx, 'traitName', undefined, undefined);
        const otherTrait = new internal_1.CdmTraitReference(manifest.ctx, 'name of other trait', undefined, undefined);
        manifest.exhibitsTraits.push(trait);
        manifest.exhibitsTraits.push(otherTrait);
        expect(trait.isFromProperty)
            .toBeFalsy();
        expect(otherTrait.isFromProperty)
            .toBeFalsy();
        expect(manifest.exhibitsTraits.length)
            .toEqual(2);
        let removed = manifest.exhibitsTraits.remove(trait, true);
        expect(removed)
            .toBeFalsy();
        expect(manifest.exhibitsTraits.length)
            .toEqual(2);
        otherTrait.isFromProperty = true;
        removed = manifest.exhibitsTraits.remove(otherTrait, true);
        expect(removed)
            .toBeTruthy();
        expect(manifest.exhibitsTraits.length)
            .toEqual(1);
        expect(manifest.exhibitsTraits.allItems[0])
            .toEqual(trait);
    });
    it('CdmTraitCollectionRemovePrioritizeFromProperty', () => {
        const manifest = CdmCollectionHelperFunctions_1.generateManifest();
        const trait = new internal_1.CdmTraitReference(manifest.ctx, 'traitName', undefined, undefined);
        const otherTrait = new internal_1.CdmTraitReference(manifest.ctx, 'name of other trait', undefined, undefined);
        manifest.exhibitsTraits.push(trait);
        manifest.exhibitsTraits.push(otherTrait);
        const traitCopyFromProperty = new internal_1.CdmTraitReference(manifest.ctx, 'traitName', false, false);
        traitCopyFromProperty.isFromProperty = true;
        manifest.exhibitsTraits.push(traitCopyFromProperty);
        expect(trait.isFromProperty)
            .toBeFalsy();
        expect(otherTrait.isFromProperty)
            .toBeFalsy();
        expect(traitCopyFromProperty.isFromProperty)
            .toBeTruthy();
        expect(manifest.exhibitsTraits.length)
            .toEqual(3);
        const removed = manifest.exhibitsTraits.remove('traitName');
        expect(removed)
            .toBeTruthy();
        expect(manifest.exhibitsTraits.length)
            .toEqual(2);
        expect(manifest.exhibitsTraits.allItems[0])
            .toEqual(trait);
        expect(manifest.exhibitsTraits.allItems[1])
            .toEqual(otherTrait);
    });
    it('CdmTraitCollectionRemoveTraitDefinitionPrioritizeFromProperty', () => {
        const manifest = CdmCollectionHelperFunctions_1.generateManifest();
        const trait = new internal_1.CdmTraitDefinition(manifest.ctx, 'traitName', undefined);
        const otherTrait = new internal_1.CdmTraitDefinition(manifest.ctx, 'name of other trait', undefined);
        manifest.exhibitsTraits.push(trait);
        manifest.exhibitsTraits.push(otherTrait);
        manifest.exhibitsTraits.push(trait);
        manifest.exhibitsTraits.allItems[2].isFromProperty = true;
        manifest.exhibitsTraits.push(otherTrait);
        manifest.exhibitsTraits.push(trait);
        manifest.exhibitsTraits.allItems[4].isFromProperty = true;
        manifest.exhibitsTraits.push(otherTrait);
        expect(manifest.exhibitsTraits.length)
            .toEqual(6);
        expect(manifest.exhibitsTraits.allItems[2].isFromProperty)
            .toBeTruthy();
        const removed = manifest.exhibitsTraits.remove(trait);
        expect(removed)
            .toBeTruthy();
        expect(manifest.exhibitsTraits.allItems[0].explicitReference.traitName)
            .toEqual('traitName');
        expect(manifest.exhibitsTraits.allItems[2].explicitReference.traitName)
            .toEqual('name of other trait');
        expect(manifest.exhibitsTraits.allItems[3].explicitReference.traitName)
            .toEqual('traitName');
    });
    it('CdmTraitCollectionIndexOfOnlyFromProperty', () => {
        const manifest = CdmCollectionHelperFunctions_1.generateManifest();
        const trait = new internal_1.CdmTraitDefinition(manifest.ctx, 'traitName', undefined);
        const otherTrait = new internal_1.CdmTraitDefinition(manifest.ctx, 'name of other trait', undefined);
        manifest.exhibitsTraits.push(trait);
        manifest.exhibitsTraits.push(otherTrait);
        expect(manifest.exhibitsTraits.allItems[0].isFromProperty)
            .toBeFalsy();
        expect(manifest.exhibitsTraits.allItems[1].isFromProperty)
            .toBeFalsy();
        let index = manifest.exhibitsTraits.indexOf(trait.traitName, true);
        expect(index)
            .toEqual(-1);
        manifest.exhibitsTraits.push(trait);
        manifest.exhibitsTraits.push(otherTrait);
        manifest.exhibitsTraits.push(trait);
        manifest.exhibitsTraits.push(otherTrait);
        expect(manifest.exhibitsTraits.length)
            .toEqual(6);
        manifest.exhibitsTraits.allItems[2].isFromProperty = true;
        index = manifest.exhibitsTraits.indexOf(trait.traitName, true);
        expect(index)
            .toEqual(2);
        index = manifest.exhibitsTraits.indexOf(trait.traitName);
        expect(index)
            .toEqual(2);
    });
    it('CdmTraitCollectionClear', () => {
        const manifest = CdmCollectionHelperFunctions_1.generateManifest();
        const trait = new internal_1.CdmTraitReference(manifest.ctx, 'traitName', undefined, undefined);
        const otherTrait = new internal_1.CdmTraitReference(manifest.ctx, 'name of other trait', undefined, undefined);
        manifest.exhibitsTraits.push(trait);
        manifest.exhibitsTraits.push(otherTrait);
        manifest.traitCache = new Map();
        manifest.exhibitsTraits.clear();
        expect(manifest.exhibitsTraits.length)
            .toEqual(0);
        expect(manifest.traitCache)
            .toBeUndefined();
    });
});
//# sourceMappingURL=CdmTraitCollection.test.js.map