"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const schematics_1 = require("@angular-devkit/schematics");
const testing_1 = require("@angular-devkit/schematics/testing");
const path = require("path");
const config_1 = require("@schematics/angular/utility/config");
describe('ng-add schematics', () => {
    const collectionPath = path.join(__dirname, '../collection.json');
    const runner = new testing_1.SchematicTestRunner('cli-schematics', collectionPath);
    let tree;
    const ngJsonConfig = {
        defaultProject: 'testProj',
        projects: {
            testProj: {
                architect: {
                    serve: {}
                }
            }
        }
    };
    const pkgJsonConfig = {
        dependencies: {},
        devDependencies: {}
    };
    beforeEach(() => {
        tree = new testing_1.UnitTestTree(new schematics_1.EmptyTree());
        tree.create('/angular.json', JSON.stringify(ngJsonConfig));
        tree.create('/package.json', JSON.stringify(pkgJsonConfig));
    });
    it('should create the needed files correctly', () => {
        expect(tree).toBeTruthy();
        expect(tree.exists('/angular.json')).toBeTruthy();
        expect(tree.exists('/package.json')).toBeTruthy();
        expect(JSON.parse(tree.readContent('/angular.json')).projects['testProj'].architect).toBeTruthy();
        const pkgJsonData = JSON.parse(tree.readContent('/package.json'));
        expect(Object.keys(pkgJsonData).length).toBeGreaterThan(0);
    });
    it('should add packages to package.json dependencies', () => {
        runner.runSchematic('ng-add', {}, tree);
        const pkgJsonData = JSON.parse(tree.readContent('/package.json'));
        expect(pkgJsonData.dependencies).toBeTruthy();
        expect(pkgJsonData.devDependencies).toBeTruthy();
    });
    it('should add the correct igniteui-angular packages to package.json dependencies', () => {
        runner.runSchematic('ng-add', {}, tree);
        const pkgJsonData = JSON.parse(tree.readContent('/package.json'));
        expect(pkgJsonData.dependencies['jszip']).toBeTruthy();
        expect(pkgJsonData.dependencies['hammerjs']).toBeTruthy();
    });
    it('should add hammer.js to the workspace', () => {
        runner.runSchematic('ng-add', {}, tree);
        const workspace = config_1.getWorkspace(tree);
        const currentProjectName = workspace.defaultProject;
        expect(workspace.projects[currentProjectName].architect.test.options.scripts.filter(d => d.includes('hammerjs')).length)
            .toBeGreaterThan(0);
        expect(workspace.projects[currentProjectName].architect.build.options.scripts.filter(d => d.includes('hammerjs')).length)
            .toBeGreaterThan(0);
    });
    it('should add hammer.js to package.json dependencies', () => {
        runner.runSchematic('ng-add', {}, tree);
        const pkgJsonData = JSON.parse(tree.readContent('/package.json'));
        expect(pkgJsonData.dependencies['hammerjs']).toBeTruthy();
    });
    it('should add the CLI only to devDependencies', () => {
        runner.runSchematic('ng-add', {}, tree);
        const pkgJsonData = JSON.parse(tree.readContent('/package.json'));
        expect(pkgJsonData.devDependencies['igniteui-cli']).toBeTruthy();
        expect(pkgJsonData.dependencies['igniteui-cli']).toBeFalsy();
    });
    it('should properly add polyfills', () => {
        const polyfills = `
// import 'core-js/es6/object';
// import 'core-js/es6/function';
/** a comment */
// import 'core-js/es6/reflect';
// import 'core-js/es6/set';

/** comment */
// import 'web-animations-js';  // Run \`npm install --save web-animations-js\`.
`;
        const result = `
import 'core-js/es6/object';
import 'core-js/es6/function';
/** a comment */
import 'core-js/es6/reflect';
import 'core-js/es6/set';
/** ES7 \`Object.entries\` needed for igxGrid to render in IE. */
import 'core-js/es7/object';

/** comment */
import 'web-animations-js';  // Run \`npm install --save web-animations-js\`.
`;
        tree.create('src/polyfills.ts', polyfills);
        runner.runSchematic('ng-add', { polyfills: true }, tree);
        expect(tree.readContent('src/polyfills.ts').replace(/\r\n/g, '\n')).toEqual(result.replace(/\r\n/g, '\n'));
    });
    it('should properly add web animations', () => {
        runner.runSchematic('ng-add', {}, tree);
        const pkgJsonData = JSON.parse(tree.readContent('/package.json'));
        expect(pkgJsonData.dependencies['web-animations-js']).toBeTruthy();
    });
});
