<commit_before>/*!
 * @license
 * Copyright 2016 Alfresco Software, Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import { describe, expect, it, inject, beforeEachProviders, beforeEach, afterEach } from '@angular/core/testing';
import { TestComponentBuilder } from '@angular/compiler/testing';
import { WebscriptComponent } from '../src/webscript.component';
import { AlfrescoSettingsServiceMock } from '../src/assets/AlfrescoSettingsService.service.mock';
import { HTTP_PROVIDERS } from '@angular/http';

import { AlfrescoAuthenticationService, AlfrescoSettingsService } from 'ng2-alfresco-core';

declare let jasmine: any;

describe('Test ng2-alfresco-webscript', () => {

    let webscriptComponentFixture, element, component;

    beforeEachProviders(() => {
        return [
            HTTP_PROVIDERS,
            {provide: AlfrescoSettingsService, useClass: AlfrescoSettingsServiceMock},
            {provide: AlfrescoAuthenticationService, useClass: AlfrescoAuthenticationService}
        ];
    });

    beforeEach(inject([TestComponentBuilder], (tcb: TestComponentBuilder) => {
        return tcb
            .createAsync(WebscriptComponent)
            .then(fixture => {
                webscriptComponentFixture = fixture;
                element = webscriptComponentFixture.nativeElement;
                component = webscriptComponentFixture.componentInstance;
                component.scriptPath = 'fakePath';
                component.showData = true;
            });
    }));

    describe('View', () => {
        it('webscript html wrapper should be present', () => {
            expect(element.querySelector('#webscript-html-wrapper')).toBeDefined();
        });

        it('webscript wrapper should be hide if showData is false', () => {
            webscriptComponentFixture.detectChanges();
            expect(element.querySelector('#webscript-html-wrapper')).toBe.undefined;
        });

        it('webscript JSON datatable wrapper should be present', () => {
            webscriptComponentFixture.detectChanges();

            expect(element.querySelector('#webscript-json-wrapper')).toBeDefined();
        });

        it('webscript plain text datatable wrapper should be present', () => {
            webscriptComponentFixture.detectChanges();

            expect(element.querySelector('#webscript-plaintext-wrapper')).toBeDefined();
        });
    });

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

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

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

        it('webscript url should be the one configured by the input param', (done) => {
            component.scriptPath = 'sample/folder/Company%20Home';

            component.ngOnChanges().then(() => {
                webscriptComponentFixture.detectChanges();
                let request = jasmine.Ajax.requests.mostRecent();
                expect(request.url).toBe('fakehost/alfresco/service/sample/folder/Company%20Home');
                done();
            });

            jasmine.Ajax.requests.mostRecent().respondWith({
                status: 200,
                contentType: 'text/plain',
                responseText: '<div></div>'
            });
        });

        it('webscript TEXT response should be displayed', (done) => {

            component.scriptPath = 'sample/folder/Company%20Home';
            component.contentType = 'TEXT';

            component.ngOnChanges().then(() => {
                webscriptComponentFixture.detectChanges();
                expect(element.querySelector('#webscript-data-TEXT').innerHTML)
                    .toBe('text test');
                done();
            });

            jasmine.Ajax.requests.mostRecent().respondWith({
                status: 200,
                contentType: 'text/html',
                responseText: 'text test'
            });
        });

        it('webscript JSON response should be displayed', (done) => {

            component.scriptPath = 'sample/folder/Company%20Home';
            component.contentType = 'JSON';

            component.ngOnChanges().then(() => {
                webscriptComponentFixture.detectChanges();
                expect(JSON.parse(element.querySelector('#webscript-data-JSON').innerHTML)[0].name).toBe('Name 1');
                expect(JSON.parse(element.querySelector('#webscript-data-JSON').innerHTML)[1].name).toBe('Name 2');
                done();
            });

            jasmine.Ajax.requests.mostRecent().respondWith({
                status: 200,
                contentType: 'json',
                responseText: [{id: 1, name: 'Name 1'},
                    {id: 2, name: 'Name 2'}]
            });
        });

        it.skip('webscript HTML response should be displayed', (done) => {

            component.scriptPath = 'sample/folder/Company%20Home';
            component.contentType = 'HTML';

            component.ngOnChanges().then(() => {
                webscriptComponentFixture.detectChanges();
                expect(element.querySelector('#webscript-data-HTML').innerHTML)
                    .toBe('&lt;test-element-id&gt;&lt;test-elemt-id&gt;');
                done();
            });

            jasmine.Ajax.requests.mostRecent().respondWith({
                status: 200,
                contentType: 'text/html',<commit_msg>dix test skip<commit_after>/*!
 * @license
 * Copyright 2016 Alfresco Software, Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import { describe, expect, it, inject, beforeEachProviders, beforeEach, afterEach, xit } from '@angular/core/testing';
import { TestComponentBuilder } from '@angular/compiler/testing';
import { WebscriptComponent } from '../src/webscript.component';
import { AlfrescoSettingsServiceMock } from '../src/assets/AlfrescoSettingsService.service.mock';
import { HTTP_PROVIDERS } from '@angular/http';

import { AlfrescoAuthenticationService, AlfrescoSettingsService } from 'ng2-alfresco-core';

declare let jasmine: any;

describe('Test ng2-alfresco-webscript', () => {

    let webscriptComponentFixture, element, component;

    beforeEachProviders(() => {
        return [
            HTTP_PROVIDERS,
            {provide: AlfrescoSettingsService, useClass: AlfrescoSettingsServiceMock},
            {provide: AlfrescoAuthenticationService, useClass: AlfrescoAuthenticationService}
        ];
    });

    beforeEach(inject([TestComponentBuilder], (tcb: TestComponentBuilder) => {
        return tcb
            .createAsync(WebscriptComponent)
            .then(fixture => {
                webscriptComponentFixture = fixture;
                element = webscriptComponentFixture.nativeElement;
                component = webscriptComponentFixture.componentInstance;
                component.scriptPath = 'fakePath';
                component.showData = true;
            });
    }));

    describe('View', () => {
        it('webscript html wrapper should be present', () => {
            expect(element.querySelector('#webscript-html-wrapper')).toBeDefined();
        });

        it('webscript wrapper should be hide if showData is false', () => {
            webscriptComponentFixture.detectChanges();
            expect(element.querySelector('#webscript-html-wrapper')).toBe.undefined;
        });

        it('webscript JSON datatable wrapper should be present', () => {
            webscriptComponentFixture.detectChanges();

            expect(element.querySelector('#webscript-json-wrapper')).toBeDefined();
        });

        it('webscript plain text datatable wrapper should be present', () => {
            webscriptComponentFixture.detectChanges();

            expect(element.querySelector('#webscript-plaintext-wrapper')).toBeDefined();
        });
    });

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

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

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

        it('webscript url should be the one configured by the input param', (done) => {
            component.scriptPath = 'sample/folder/Company%20Home';

            component.ngOnChanges().then(() => {
                webscriptComponentFixture.detectChanges();
                let request = jasmine.Ajax.requests.mostRecent();
                expect(request.url).toBe('fakehost/alfresco/service/sample/folder/Company%20Home');
                done();
            });

            jasmine.Ajax.requests.mostRecent().respondWith({
                status: 200,
                contentType: 'text/plain',
                responseText: '<div></div>'
            });
        });

        it('webscript TEXT response should be displayed', (done) => {

            component.scriptPath = 'sample/folder/Company%20Home';
            component.contentType = 'TEXT';

            component.ngOnChanges().then(() => {
                webscriptComponentFixture.detectChanges();
                expect(element.querySelector('#webscript-data-TEXT').innerHTML)
                    .toBe('text test');
                done();
            });

            jasmine.Ajax.requests.mostRecent().respondWith({
                status: 200,
                contentType: 'text/html',
                responseText: 'text test'
            });
        });

        it('webscript JSON response should be displayed', (done) => {

            component.scriptPath = 'sample/folder/Company%20Home';
            component.contentType = 'JSON';

            component.ngOnChanges().then(() => {
                webscriptComponentFixture.detectChanges();
                expect(JSON.parse(element.querySelector('#webscript-data-JSON').innerHTML)[0].name).toBe('Name 1');
                expect(JSON.parse(element.querySelector('#webscript-data-JSON').innerHTML)[1].name).toBe('Name 2');
                done();
            });

            jasmine.Ajax.requests.mostRecent().respondWith({
                status: 200,
                contentType: 'json',
                responseText: [{id: 1, name: 'Name 1'},
                    {id: 2, name: 'Name 2'}]
            });
        });

        xit('webscript HTML response should be displayed', (done) => {

            component.scriptPath = 'sample/folder/Company%20Home';
            component.contentType = 'HTML';

            component.ngOnChanges().then(() => {
                webscriptComponentFixture.detectChanges();
                expect(element.querySelector('#webscript-data-HTML').innerHTML)
                    .toBe('&lt;test-element-id&gt;&lt;test-elemt-id&gt;');
                done();
            });

            jasmine.Ajax.requests.mostRecent().respondWith({
                status: 200,
                contentType: 'text/html',
{'fraction_non_alphanumeric': 0.12535031847133757, 'fraction_numerical': 0.006794055201698514, 'mean_word_length': 2.253937551809892, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 47, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '4045534', 'n_tokens_mistral': 3050, 'n_tokens_neox': 2880, 'n_words': 614}