describe('Task List: Custom EmptyTemplateComponent', () => {
    let fixture: ComponentFixture<EmptyTemplateComponent>;
    let translateService: TranslateService;
    let taskListService: TaskListService;

    setupTestBed({
        imports: [ProcessTestingModule, TaskListModule, DataTableModule],
        declarations: [EmptyTemplateComponent]
    });

    beforeEach(() => {
        translateService = TestBed.get(TranslateService);
        taskListService = TestBed.get(TaskListService);
        spyOn(translateService, 'get').and.callFake((key) => {
            return of(key);
        });
        spyOn(taskListService, 'findTasksByState').and.returnValue(of(fakeEmptyTask));
        fixture = TestBed.createComponent(EmptyTemplateComponent);
        fixture.detectChanges();
    });

    afterEach(() => {
        fixture.destroy();
    });

    it('should render the custom template', (done) => {
        fixture.detectChanges();
        fixture.whenStable().then(() => {
            expect(fixture.debugElement.query(By.css('#custom-id'))).not.toBeNull();
            expect(fixture.debugElement.query(By.css('.adf-empty-content'))).toBeNull();
            done();
        });
    });
});
describe('Custom CustomEmptyAppListTemplateComponent', () => {
    let fixture: ComponentFixture<CustomEmptyAppListTemplateComponent>;

    setupTestBed({
        imports: [ProcessTestingModule],
        declarations: [CustomEmptyAppListTemplateComponent],
        schemas: [ CUSTOM_ELEMENTS_SCHEMA ]
    });

    beforeEach(() => {
        fixture = TestBed.createComponent(CustomEmptyAppListTemplateComponent);
    });

    afterEach(() => {
        fixture.destroy();
    });

    it('should render the custom no-apps template', async(() => {
        fixture.detectChanges();
        fixture.whenStable().then(() => {
            let title: any = fixture.debugElement.queryAll(By.css('#custom-id'));
            expect(title.length).toBe(1);
            expect(title[0].nativeElement.innerText).toBe('No Apps');
        });
    }));
});
    describe('Creating an empty custom template - EmptyTemplateComponent', () => {
        let fixtureEmpty: ComponentFixture<EmptyTemplateComponent>;

        setupTestBed({
            imports: [ProcessServiceCloudTestingModule, TaskListCloudModule],
            declarations: [EmptyTemplateComponent],
            schemas: [CUSTOM_ELEMENTS_SCHEMA]
        });

        beforeEach(() => {
            fixtureEmpty = TestBed.createComponent(EmptyTemplateComponent);
            fixtureEmpty.detectChanges();
        });

        afterEach(() => {
            fixtureEmpty.destroy();
        });

        it('should render the custom template', async(() => {
            fixtureEmpty.whenStable().then(() => {
                fixtureEmpty.detectChanges();
                expect(fixtureEmpty.debugElement.query(By.css('#custom-id'))).not.toBeNull();
                expect(fixtureEmpty.debugElement.query(By.css('.adf-empty-content'))).toBeNull();
            });
        }));
    });
    describe('Injecting custom colums for tasklist - CustomTaskListComponent', () => {
        let fixtureCustom: ComponentFixture<CustomTaskListComponent>;
        let componentCustom: CustomTaskListComponent;

        setupTestBed({
            imports: [CoreModule.forRoot()],
            declarations: [TaskListCloudComponent, CustomTaskListComponent],
            providers: [TaskListCloudService]
        });

        beforeEach(() => {
            fixtureCustom = TestBed.createComponent(CustomTaskListComponent);
            fixtureCustom.detectChanges();
            componentCustom = fixtureCustom.componentInstance;
        });

        afterEach(() => {
            fixtureCustom.destroy();
        });

        it('should create instance of CustomTaskListComponent', () => {
            expect(componentCustom instanceof CustomTaskListComponent).toBe(true, 'should create CustomTaskListComponent');
        });

        it('should fetch custom schemaColumn from html', () => {
            fixture.detectChanges();
            expect(componentCustom.taskList.columnList).toBeDefined();
            expect(componentCustom.taskList.columns[0]['title']).toEqual('ADF_CLOUD_TASK_LIST.PROPERTIES.NAME');
            expect(componentCustom.taskList.columns[1]['title']).toEqual('ADF_CLOUD_TASK_LIST.PROPERTIES.CREATED');
            expect(componentCustom.taskList.columns.length).toEqual(3);
        });

    });
describe('Process List: Custom EmptyTemplateComponent', () => {
    let fixture: ComponentFixture<EmptyTemplateComponent>;
    let processService: ProcessService;

    setupTestBed({
        imports: [ProcessTestingModule, ProcessListModule, DataTableModule],
        declarations: [EmptyTemplateComponent]
    });

    beforeEach(() => {
        fixture = TestBed.createComponent(EmptyTemplateComponent);
        processService = TestBed.get(ProcessService);
        spyOn(processService, 'getProcessInstances').and.returnValue(of(fakeProcessInstancesEmpty));
        fixture.detectChanges();
    });

    afterEach(() => {
        fixture.destroy();
    });

    it('should render the custom template', (done) => {
        fixture.whenStable().then(() => {
            fixture.detectChanges();
            let title = fixture.debugElement.query(By.css('#custom-id'));
            expect(title).not.toBeNull();
            expect(title.nativeElement.innerText).toBe('No Process Instance');
            expect(fixture.debugElement.query(By.css('.adf-empty-content'))).toBeNull();
            done();
        });
    });
});
describe('CustomProcessListComponent', () => {
    let fixture: ComponentFixture<CustomProcessListComponent>;
    let component: CustomProcessListComponent;

    setupTestBed({
        imports: [CoreModule.forRoot()],
        declarations: [ProcessInstanceListComponent, CustomProcessListComponent],
        providers: [ProcessService]
    });

    beforeEach(() => {
        fixture = TestBed.createComponent(CustomProcessListComponent);
        fixture.detectChanges();
        component = fixture.componentInstance;
    });

    it('should create instance of CustomProcessListComponent', () => {
        expect(component instanceof CustomProcessListComponent).toBe(true, 'should create CustomProcessListComponent');
    });

    it('should fetch custom schemaColumn from html', () => {
        fixture.detectChanges();
        expect(component.processList.columns).toBeDefined();
        expect(component.processList.columns.length).toEqual(3);
        expect(component.processList.columns[1]['title']).toEqual('ADF_PROCESS_LIST.PROPERTIES.END_DATE');
        expect(component.processList.columns[2]['title']).toEqual('ADF_PROCESS_LIST.PROPERTIES.CREATED');
    });
});
describe('Custom CustomEmptyAppListCloudTemplateComponent', () => {
    let fixture: ComponentFixture<CustomEmptyAppListCloudTemplateComponent>;

    setupTestBed({
        imports: [ProcessServiceCloudTestingModule],
        declarations: [CustomEmptyAppListCloudTemplateComponent],
        schemas: [ CUSTOM_ELEMENTS_SCHEMA ]
    });

    beforeEach(() => {
        fixture = TestBed.createComponent(CustomEmptyAppListCloudTemplateComponent);
    });

    afterEach(() => {
        fixture.destroy();
    });

    it('should render the custom empty template', async(() => {
        fixture.detectChanges();
        fixture.whenStable().then(() => {
            const title: any =  fixture.nativeElement.querySelector('#custom-id');
            expect(title.innerText).toBe('No Apps Found');
        });
    }));
});
describe('TagService', () => {

    let service: TagService;

    setupTestBed({
        imports: [ContentTestingModule]
    });

    beforeEach(() => {
        service = TestBed.get(TagService);
    });

    beforeEach(() => {
        jasmine.Ajax.install();
    });

    afterEach(() => {
        jasmine.Ajax.uninstall();
    });

    describe('Content tests', () => {

        it('getTagsByNodeId catch errors call', (done) => {
            service.getTagsByNodeId('fake-node-id').subscribe(() => {
            }, () => {
                done();
            });

            jasmine.Ajax.requests.mostRecent().respondWith({
                status: 403
            });
        });

        it('delete tag should trigger a refresh event', (done) => {
            service.refresh.subscribe(() => {
                done();
            });

            service.removeTag('fake-node-id', 'fake-tag');

            jasmine.Ajax.requests.mostRecent().respondWith({
                status: 200
            });
        });

        it('add tag should trigger a refresh event', (done) => {
            service.refresh.subscribe(() => {
                done();
            });

            service.addTag('fake-node-id', 'fake-tag');

            jasmine.Ajax.requests.mostRecent().respondWith({
                status: 200
            });
        });
    });

});
describe('AnalyticsService', () => {

    let service: AnalyticsService;

    setupTestBed({
        imports: [InsightsTestingModule]
    });

    beforeEach(() => {
        service = TestBed.get(AnalyticsService);
    });

    beforeEach(() => {
        jasmine.Ajax.install();
    });

    afterEach(() => {
        jasmine.Ajax.uninstall();
    });

    describe('Content tests', () => {

        it('should return the report list by appId', (done) => {
            service.getReportList(1).subscribe(
                (reportList) => {
                    expect(reportList).toBeDefined();
                    expect(reportList.length).toEqual(2);
                    expect(reportList[0].name).toEqual('Fake Report 1');
                    expect(reportList[1].name).toEqual('Fake Report 2');
                    done();
                }
            );

            jasmine.Ajax.requests.mostRecent().respondWith({
                'status': 200,
                contentType: 'application/json',
                responseText: JSON.stringify(fakeReportList)
            });
        });

        it('should return the report by report name', (done) => {
            service.getReportByName('Fake Report 2').subscribe(
                (report) => {
                    expect(report).toBeDefined();
                    expect(report).not.toBeNull();
                    expect(report.id).toEqual('2');
                    done();
                }
            );

            jasmine.Ajax.requests.mostRecent().respondWith({
                'status': 200,
                contentType: 'application/json',
                responseText: JSON.stringify(fakeReportList)
            });
        });
    });
});
describe('ContentColumn', () => {

    let documentList: DocumentListComponent;
    let columnList: ContentColumnListComponent;
    let logService: LogService;

    setupTestBed({
        imports: [ContentTestingModule],
        schemas: [CUSTOM_ELEMENTS_SCHEMA]
    });

    beforeEach(() => {
        documentList = (TestBed.createComponent(DocumentListComponent).componentInstance as DocumentListComponent);
        logService = TestBed.get(LogService);
        columnList = new ContentColumnListComponent(documentList, logService);

        documentList.ngOnInit();
    });

    it('should register model within parent column list', () => {
        spyOn(columnList, 'registerColumn').and.callThrough();

        let column = new ContentColumnComponent(columnList, logService);
        column.ngAfterContentInit();

        expect(columnList.registerColumn).toHaveBeenCalled();

        let columns = documentList.data.getColumns();
        expect(columns.length).toBe(1);
        expect(columns[0]).toBe(column);
    });

    it('should setup screen reader title for thumbnail column', () => {
        let column = new ContentColumnComponent(columnList, logService);
        column.key = '$thumbnail';
        column.ngOnInit();

        expect(column.srTitle).toBe('Thumbnail');
    });

    it('should register on init', () => {
        let column = new ContentColumnComponent(columnList, logService);
        spyOn(column, 'register').and.callThrough();

        column.ngAfterContentInit();
        expect(column.register).toHaveBeenCalled();
    });

    it('should require action list to register action with', () => {
        let column = new ContentColumnComponent(columnList, logService);
        expect(column.register()).toBeTruthy();

        column = new ContentColumnComponent(null, logService);
        expect(column.register()).toBeFalsy();
    });

});