import "ember-routing-htmlbars";
import run from "ember-metal/run_loop";
import EmberHandlebars from "ember-handlebars";
import EmberView from "ember-views/views/view";
import htmlbarsCompile from "ember-htmlbars/system/compile";
import { set } from "ember-metal/property_set";
import Controller from "ember-runtime/controllers/controller";

var compile;
if (Ember.FEATURES.isEnabled('ember-htmlbars')) {
  compile = htmlbarsCompile;
} else {
  compile = EmberHandlebars.compile;
}

var view;

var appendView = function(view) {
  run(function() { view.appendTo('#qunit-fixture'); });
};

QUnit.module("Handlebars {{link-to}} helper", {
  teardown: function() {
    run(function() {
      if (view) { view.destroy(); }
    });
  }
});


test("should be able to be inserted in DOM when the router is not present", function() {
  var template = "{{#link-to 'index'}}Go to Index{{/link-to}}";
  view = EmberView.create({
    template: compile(template)
  });

  appendView(view);

  equal(view.$().text(), 'Go to Index');
});

test("should be able to be inserted in DOM when the router is not present", function() {
  var template = "{{#link-to 'index'}}Go to Index{{/link-to}}";
  view = EmberView.create({
    template: compile(template)
  });

  appendView(view);

  equal(view.$().text(), 'Go to Index');
});

test("re-renders when title changes", function() {
  var template = "{{link-to title routeName}}";
  view = EmberView.create({
    controller: {
      title: 'foo',
      routeName: 'index'
    },
    template: compile(template)
  });

  appendView(view);

  equal(view.$().text(), 'foo');

  run(function() {
    set(view, 'controller.title', 'bar');
  });

  equal(view.$().text(), 'bar');
});

test("can read bound title", function() {
  var template = "{{link-to title routeName}}";
  view = EmberView.create({
    controller: {
      title: 'foo',
      routeName: 'index'
    },
    template: compile(template)
  });

  appendView(view);

  equal(view.$().text(), 'foo');
});

test("escapes title in non-block form", function() {
  view = EmberView.create({
    title: "<b>blah</b>",

    template: compile("{{link-to view.title}}")
  });

  appendView(view);

  equal(view.$('b').length, 0, 'no <b> were found');
});

test("does not escape title in non-block form when `unescaped` is true", function() {
  view = EmberView.create({
    title: "<b>blah</b>",

    template: compile("{{link-to view.title unescaped=true}}")
  });

  appendView(view);

  equal(view.$('b').length, 1, '<b> was found');
});

test("unwraps controllers", function() {
  var template = "{{#link-to 'index' view.otherController}}Text{{/link-to}}";

  view = EmberView.create({
    otherController: Controller.create({
      model: 'foo'
    }),

    template: compile(template)
  });

  appendView(view);

  equal(view.$().text(), 'Text');
});

@@ -1020,295 +1020,8 @@
 );%0A%0A
-test(%22should be able to be inserted in DOM when the router is not present%22, function() %7B%0A  var template = %22%7B%7B#link-to 'index'%7D%7DGo to Index%7B%7B/link-to%7D%7D%22;%0A  view = EmberView.create(%7B%0A    template: compile(template)%0A  %7D);%0A%0A  appendView(view);%0A%0A  equal(view.$().text(), 'Go to Index');%0A%7D);%0A%0A
 test

Remove Duplicate Test