import Ember from 'ember';
import { moduleFor, test } from 'ember-qunit';
import hasEmberVersion from 'ember-test-helpers/has-ember-version';
const { getOwner } = Ember;

moduleFor('foo:bar', {
  beforeEach() {
    this.register('whatever:lol', Ember.Object.extend({
      isLOL: true
    }));
  },

  afterEach(assert) {
    assert.noDeprecationsOccurred();
  }
});

test('calling getOwner multiple times returns the same object', function(assert) {
  let subject = this.subject();
  assert.equal(getOwner(subject), getOwner(subject));
});

test('it can use getOwner to look things up', function(assert) {
  let subject = this.subject();
  let owner = getOwner(subject);

  let result = owner.lookup('whatever:lol');

  assert.ok(result.isLOL, 'was able to lookup');
});

test('it can use getOwner to register things', function(assert) {
  let subject = this.subject();
  let owner = getOwner(subject);

  owner.register('foo:baz', Ember.Object.extend({
    isBaz: true
  }));

  let result = owner.lookup('foo:baz');

  assert.ok(result.isBaz, 'was able to register and lookup');
});

test('it can use getOwner for the private _lookupFactory', function(assert) {
  let testFn = () => {
    let subject = this.subject();
    let owner = getOwner(subject);

    let Baz = Ember.Object.extend();
    Baz.reopenClass({ isBazFactory: true });

    owner.register('foo:baz', Baz);

    let result = owner._lookupFactory('foo:baz');

    assert.ok(result.isBazFactory, 'was able to register and _lookupFactory');
  };

  // we only can assert that a deprecation occurs when we are using
  // our custom `getOwner` polyfill. For Ember 2.3 - 2.11 we simply confirm
  // the functionality (not the deprecation).
  if (!hasEmberVersion(2,3) || hasEmberVersion(2,12)) {
    assert.deprecations(testFn, [
      'Using "_lookupFactory" is deprecated. Please use container.factoryFor instead.'
    ]);
  } else {
    testFn();
  }
});

test('it can use getOwner for factoryFor', function(assert) {
  let subject = this.subject();
  let owner = getOwner(subject);

  let Baz = Ember.Object.extend();
  Baz.reopenClass({ isBazFactory: true });

  owner.register('foo:baz', Baz);

  let result = owner.factoryFor('foo:baz');

  assert.ok(result.class.isBazFactory, 'was able to register and factoryFor');
  let instance = result.create();
  assert.ok(instance instanceof Baz, 'factoryFor().create() results in a valid instance');
});

test('it can use hasRegistration', function(assert) {
  let subject = this.subject();
  let owner = getOwner(subject);

  let result = owner.hasRegistration('whatever:lol');

  assert.ok(result, 'hasRegistration works');
});

test('it can use inject', function(assert) {
  let subject = this.subject();
  let owner = getOwner(subject);

  owner.inject('whatever:lol', 'baz', 'foo:baz');
  owner.register('foo:baz', Ember.Object.extend({
    isBaz: true
  }));

  let result = owner.lookup('whatever:lol');

  assert.ok(result.get('baz.isBaz'), 'inject worked properly');
});

test('it can use registerOptionsForType', function(assert) {
  let subject = this.subject();
  let owner = getOwner(subject);

  owner.registerOptionsForType('lol', { instantiate: false });

  let result = owner.registeredOptionsForType('lol');

  assert.deepEqual(result, { instantiate: false }, 'registerdOptionsForType worked properly');
});

test('it can use resolveRegistration', function(assert) {
  let subject = this.subject();
  let owner = getOwner(subject);
  let Baz = Ember.Object.extend();

  owner.register('foo:baz', Baz);

  let result = owner.resolveRegistration('foo:baz');

  assert.equal(result, Baz, 'was able to resolveRegistration');
});

test('it can use unregister', function(assert) {
  let subject = this.subject();
  let owner = getOwner(subject);
  let Baz = Ember.Object.extend();
  let result;

  owner.register('foo:baz', Baz);

  result = owner.resolveRegistration('foo:baz');

  assert.equal(result, Baz, 'was able to resolveRegistration');

  owner.unregister('foo:baz');

  result = owner.resolveRegistration('foo:baz');

  assert.equal(result, undefined, 'was able to resolveRegistration');
});

test('it can use ownerInjection', function(assert) {
  let subject = this.subject();
  let owner = getOwner(subject);
  let baz = Ember.Object.create(owner.ownerInjection());
  let bazOwner = getOwner(baz);

  let result = bazOwner.lookup('whatever:lol');

  assert.ok(result.isLOL, 'was able to lookup');
});

@@ -1072,32 +1072,136 @@
  lookup');%0A%7D);%0A%0A
+// In Ember 2.15 %60_lookupFactory%60 was removed (in favor of %60factoryFor%60%0Aif (!hasEmberVersion(2,15)) %7B%0A  
 test('it can use
@@ -1258,24 +1258,26 @@
 n(assert) %7B%0A
+  
   let testFn
@@ -1287,16 +1287,18 @@
 () =%3E %7B%0A
+  
     let 
@@ -1319,24 +1319,26 @@
 .subject();%0A
+  
     let owne
@@ -1361,24 +1361,26 @@
 ject);%0A%0A    
+  
 let Baz = Em
@@ -1396,24 +1396,26 @@
 t.extend();%0A
+  
     Baz.reop
@@ -1452,16 +1452,18 @@
 );%0A%0A    
+ 
+ 
 owner.re
@@ -1483,24 +1483,26 @@
 az', Baz);%0A%0A
+  
     let resu
@@ -1540,24 +1540,26 @@
 baz');%0A%0A    
+ 
+ 
 assert.ok(re
@@ -1625,14 +1625,18 @@
 ');%0A
+  
   %7D;%0A%0A
+  
   //
@@ -1699,16 +1699,18 @@
 e using%0A
+  
   // our
@@ -1777,16 +1777,18 @@
 confirm%0A
+  
   // the
@@ -1825,16 +1825,18 @@
 ation).%0A
+  
   if (!h
@@ -1883,24 +1883,26 @@
 ,12)) %7B%0A    
+  
 assert.depre
@@ -1925,16 +1925,18 @@
 %5B%0A      
+ 
+ 
 'Using %22
@@ -2016,14 +2016,18 @@
     
+  
 %5D);%0A  
+ 
+ 
 %7D el
@@ -2031,16 +2031,18 @@
  else %7B%0A
+  
     test
@@ -2043,31 +2043,37 @@
   testFn();%0A
+  
   %7D%0A
+  
 %7D);
+%0A%7D
 %0A%0Atest('it c

Fix tests to work on Ember 2.15+.