Skip to content
Snippets Groups Projects
output-by-category.component.spec.ts 3.68 KiB
Newer Older
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { Component, ViewChild } from '@angular/core';

import { OutputByCategoryComponent } from './output-by-category.component';
import { Attribute } from '../../../metamodel/model';
import { ATTRIBUTE_LIST } from '../../../../settings/test-data';

describe('[Search][Output] Component: OutputByCategoryComponent', () => {
    @Component({
        selector: `app-host`,
        template: `
            <app-output-by-category
                [categoryLabel]="categoryLabel"
                [attributeList]="attributeList"
                [outputList]="outputList"
                [isAllSelected]="isAllSelected"
                [isAllUnselected]="isAllUnselected">
            </app-output-by-category>`
    })
    class TestHostComponent {
        @ViewChild(OutputByCategoryComponent, { static: false })
        public testedComponent: OutputByCategoryComponent;
        private categoryLabel = 'Default output category';
        private attributeList: Attribute[] = ATTRIBUTE_LIST;
        private outputList: number[] = [1];
        private isAllSelected = true;
        private isAllUnselected: false;
    }

    let testHostComponent: TestHostComponent;
    let testHostFixture: ComponentFixture<TestHostComponent>;
    let testedComponent: OutputByCategoryComponent;

    beforeEach(async(() => {
        TestBed.configureTestingModule({
            declarations: [
                OutputByCategoryComponent,
                TestHostComponent
Tifenn Guillas's avatar
Tifenn Guillas committed
            ]
        });
        testHostFixture = TestBed.createComponent(TestHostComponent);
        testHostComponent = testHostFixture.componentInstance;
        testHostFixture.detectChanges();
        testedComponent = testHostComponent.testedComponent;
    }));

    it('should create the component', () => {
        expect(testedComponent).toBeTruthy();
    });

    it('#getAttributeListSortedByDisplay() should sort attributeList by output_display', () => {
        const sortedAttributeList: Attribute[] = testedComponent.getAttributeListSortedByDisplay();
        expect(sortedAttributeList[0].id).toBe(2);
        expect(sortedAttributeList[1].id).toBe(1);
    });

    it('#isSelected(idOutput) should return true if output is selected', () => {
        expect(testedComponent.isSelected(1)).toBeTruthy();
    });

    it('#isSelected(idOutput) should return false if output is not selected', () => {
        expect(testedComponent.isSelected(2)).toBeFalsy();
    });

    it('#toggleSelection(idOutput) should remove idOutput from outputList and raise change event', () => {
        const idOutput = 1;
        const expectedOutputList = [];
        testedComponent.change.subscribe((event: number[]) => expect(event).toEqual(expectedOutputList));
        testedComponent.toggleSelection(idOutput);
    });

    it('#toggleSelection(idOutput) should add idOutput to outputList and raise change event', () => {
        const idOutput = 2;
        const expectedOutputList = [1, 2];
        testedComponent.change.subscribe((event: number[]) => expect(event).toEqual(expectedOutputList));
        testedComponent.toggleSelection(idOutput);
    });

    it('#selectAll() should add all outputs to outputList and raise change event', () => {
        const expectedOutputList = [1, 2];
        testedComponent.change.subscribe((event: number[]) => expect(event).toEqual(expectedOutputList));
        testedComponent.selectAll();
    });

    it('#unselectAll() should remove all outputs to outputList and raise change event', () => {
        const expectedOutputList = [];
        testedComponent.change.subscribe((event: number[]) => expect(event).toEqual(expectedOutputList));
        testedComponent.unselectAll();
    });
});