'use strict';

const Blueprint = require('../../../lib/models/blueprint');
const MockProject = require('../../helpers/mock-project');
const expect = require('chai').expect;

describe('blueprint - addon', function () {
  describe('Blueprint.lookup', function () {
    let blueprint;

    beforeEach(function () {
      blueprint = Blueprint.lookup('addon');
    });

    describe('entityName', function () {
      let mockProject;

      beforeEach(function () {
        mockProject = new MockProject();
        mockProject.isEmberCLIProject = function () {
          return true;
        };

        blueprint.project = mockProject;
      });

      afterEach(function () {
        mockProject = null;
      });

      it('throws error when current project is an existing ember-cli project', function () {
        expect(() => blueprint.normalizeEntityName('foo')).to.throw(
          'Generating an addon in an existing ember-cli project is not supported.'
        );
      });

      it('works when current project is an existing ember-cli addon', function () {
        mockProject.isEmberCLIAddon = function () {
          return true;
        };

        expect(() => blueprint.normalizeEntityName('foo')).not.to.throw(
          'Generating an addon in an existing ember-cli project is not supported.'
        );
      });

      it('keeps existing behavior by calling Blueprint.normalizeEntityName', function () {
        expect(() => blueprint.normalizeEntityName('foo/')).to.throw(/trailing slash/);
      });
    });
  });

  describe('direct blueprint require', function () {
    let blueprint;
    beforeEach(function () {
      blueprint = require('../../../blueprints/addon');
      blueprint.options = {
        entity: { name: 'my-cool-addon' },
      };
      blueprint._appBlueprint = {
        path: 'test-app-blueprint-path',
      };
      blueprint.path = 'test-blueprint-path';
    });

    describe('generatePackageJson', function () {
      it('works', function () {
        let output = blueprint.updatePackageJson(JSON.stringify({}));
        // string to test ordering
        expect(output).to.equal(
          '\
{\n\
  "name": "my-cool-addon",\n\
  "description": "The default blueprint for ember-cli addons.",\n\
  "keywords": [\n\
    "ember-addon"\n\
  ],\n\
  "scripts": {\n\
    "test:ember-compatibility": "ember try:each"\n\
  },\n\
  "dependencies": {},\n\
  "devDependencies": {\n\
    "ember-disable-prototype-extensions": "^1.1.3",\n\
    "ember-source-channel-url": "^2.0.1",\n\
    "ember-try": "^1.4.0"\n\
  },\n\
  "ember-addon": {\n\
    "configPath": "tests/dummy/config"\n\
  }\n\
}\n'
        );
      });

      it('removes the `private` property', function () {
        let output = blueprint.updatePackageJson(JSON.stringify({}));

        expect(JSON.parse(output).private).to.be.undefined;
      });

      it('overwrites `name`', function () {
        let output = blueprint.updatePackageJson(JSON.stringify({ name: 'OMG' }));
        expect(JSON.parse(output).name).to.eql('my-cool-addon');
      });

      it('overwrites `description`', function () {
        let output = blueprint.updatePackageJson(JSON.stringify({ description: 'OMG' }));
        let json = JSON.parse(output);

        expect(json.description).to.equal('The default blueprint for ember-cli addons.');
      });

      it('moves `ember-cli-babel` from devDependencies to dependencies', function () {
        let output = blueprint.updatePackageJson(
          JSON.stringify({
            devDependencies: {
              'ember-cli-babel': '1.0.0',
            },
          })
        );

        let json = JSON.parse(output);
        expect(json.dependencies).to.deep.equal({
          'ember-cli-babel': '1.0.0',
        });
        expect(json.devDependencies).to.not.have.property('ember-cli-babel');
      });

      it('moves `ember-cli-htmlbars` from devDependencies to dependencies', function () {
        let output = blueprint.updatePackageJson(
          JSON.stringify({
            devDependencies: {
              'ember-cli-htmlbars': '1.0.0',
            },
          })
        );

        let json = JSON.parse(output);
        expect(json.dependencies).to.deep.equal({
          'ember-cli-htmlbars': '1.0.0',
        });
        expect(json.devDependencies).to.not.have.property('ember-cli-htmlbars');
      });

      it('does not push multiple `ember-addon` keywords', function () {
        let output = blueprint.updatePackageJson(
          JSON.stringify({
            keywords: ['ember-addon'],
          })
        );
        let json = JSON.parse(output);
        expect(json.keywords).to.deep.equal(['ember-addon']);
      });

      it('overwrites any version of `ember-disable-prototype-extensions`', function () {
        let output = blueprint.updatePackageJson(
          JSON.stringify({
            devDependencies: {
              'ember-disable-prototype-extensions': '1.1.2',
            },
          })
        );

        let json = JSON.parse(output);
        expect(json.devDependencies['ember-disable-prototype-extensions']).to.equal('^1.1.3');
      });

      it('adds `scripts.test:all`', function () {
        let output = blueprint.updatePackageJson(
          JSON.stringify({
            scripts: {},
          })
        );

        let json = JSON.parse(output);
        expect(json.scripts['test:ember-compatibility']).to.equal('ember try:each');
      });

      it('overwrites `ember-addon.configPath`', function () {
        let output = blueprint.updatePackageJson(
          JSON.stringify({
            'ember-addon': {
              configPath: 'test-path',
            },
          })
        );

        let json = JSON.parse(output);
        expect(json['ember-addon'].configPath).to.equal('tests/dummy/config');
      });

      it('preserves dependency ordering', function () {
        let output = blueprint.updatePackageJson(
          JSON.stringify({
            dependencies: {
              b: '1',
              a: '1',
            },
            devDependencies: {
              b: '1',
              a: '1',
            },
          })
        );

        let json = JSON.parse(output);
        delete json.devDependencies['ember-disable-prototype-extensions'];
        delete json.devDependencies['eslint-plugin-node'];
        delete json.devDependencies['ember-try'];
        delete json.devDependencies['ember-source-channel-url'];
        expect(json.dependencies).to.deep.equal({ a: '1', b: '1' });
        expect(json.devDependencies).to.deep.equal({ a: '1', b: '1' });
      });
    });
  });
});

@@ -1956,705 +1956,8 @@
 ) %7B%0A
-      it('works', function () %7B%0A        let output = blueprint.updatePackageJson(JSON.stringify(%7B%7D));%0A        // string to test ordering%0A        expect(output).to.equal(%0A          '%5C%0A%7B%5Cn%5C%0A  %22name%22: %22my-cool-addon%22,%5Cn%5C%0A  %22description%22: %22The default blueprint for ember-cli addons.%22,%5Cn%5C%0A  %22keywords%22: %5B%5Cn%5C%0A    %22ember-addon%22%5Cn%5C%0A  %5D,%5Cn%5C%0A  %22scripts%22: %7B%5Cn%5C%0A    %22test:ember-compatibility%22: %22ember try:each%22%5Cn%5C%0A  %7D,%5Cn%5C%0A  %22dependencies%22: %7B%7D,%5Cn%5C%0A  %22devDependencies%22: %7B%5Cn%5C%0A    %22ember-disable-prototype-extensions%22: %22%5E1.1.3%22,%5Cn%5C%0A    %22ember-source-channel-url%22: %22%5E2.0.1%22,%5Cn%5C%0A    %22ember-try%22: %22%5E1.4.0%22%5Cn%5C%0A  %7D,%5Cn%5C%0A  %22ember-addon%22: %7B%5Cn%5C%0A    %22configPath%22: %22tests/dummy/config%22%5Cn%5C%0A  %7D%5Cn%5C%0A%7D%5Cn'%0A        );%0A      %7D);%0A%0A
     

Remove old/legacy test.