import Ember from 'ember';
import { module, test } from 'qunit';
import config from '../../../config/environment';
import { initialize } from '../../../initializers/export-application-global';

var application, originalModulePrefix, originalExportApplicationGlobal;

module('ExportApplicationGlobalInitializer', {
  setup: function() {
    originalModulePrefix = config.modulePrefix;
    originalExportApplicationGlobal = config.exportApplicationGlobal;

    Ember.run(function() {
      application = Ember.Application.create();
      application.deferReadiness();
    });
  },

  teardown: function() {
    var classifiedName = Ember.String.classify(config.modulePrefix);
    delete window[classifiedName];
    config.modulePrefix = originalModulePrefix;
    config.exportApplicationGlobal = originalExportApplicationGlobal;
    application = originalModulePrefix = originalExportApplicationGlobal = null;
  }
});

test('it sets the application on window with the classified modulePrefix', function(assert) {
  config.modulePrefix = 'foo';
  var app = { reopen: function(){} };
  initialize(null, app);

  assert.equal(window.Foo, app);
});

test('it sets the application on window with the classified modulePrefix when exportApplicationGlobal is true', function(assert) {
  config.modulePrefix = 'foo';
  config.exportApplicationGlobal = true;
  var app = { reopen: function(){} };
  initialize(null, app);

  assert.equal(window.Foo, app);
});

test('it does not set the global unless exportApplicationGlobal is true', function(assert) {
  config.modulePrefix = 'foo';
  config.exportApplicationGlobal = false;
  var app = { reopen: function(){} };
  initialize(null, app);

  assert.ok(window.Foo !== app);
});

test('it does not set the global if it already exists falsy', function(assert) {
  window.Foo = 'hello';
  config.modulePrefix = 'foo';
  var app = { reopen: function(){} };

  assert.ok(window.Foo !== app);
});

test('it sets a custom global name if specified', function(assert) {
  config.modulePrefix = 'foo';
  config.exportApplicationGlobal = 'Catz';
  var app = { reopen: function(){} };
  initialize(null,  app);

  assert.ok(window.Foo !== app);
  assert.ok(window.Catz === app);
});

test('it sets a custom global name if specified', function(assert) {
  config.modulePrefix = 'foo';
  config.exportApplicationGlobal = 'Catz';
  initialize(null, 'blazorz');

  assert.ok(window.Foo !== 'blazorz');
  assert.ok(window.Catz === 'blazorz');
});

@@ -2206,263 +2206,4 @@
 %7D);%0A
-%0Atest('it sets a custom global name if specified', function(assert) %7B%0A  config.modulePrefix = 'foo';%0A  config.exportApplicationGlobal = 'Catz';%0A  initialize(null, 'blazorz');%0A%0A  assert.ok(window.Foo !== 'blazorz');%0A  assert.ok(window.Catz === 'blazorz');%0A%7D);%0A

remove duplicate test