2

嗨,我亲爱的社区成员。在测试处理复选框单击事件时,我仅在 IE 8 中遇到问题。这是一个 JSBIN 链接,您可以在其中看到问题:http: //jsbin.com/jicijilo/1/

抱歉,我不得不在其中注入整个 mocha 适配器和 expectjs 源代码,因为显然只有 mozilla 可以处理像这些https://raw.githubusercontent.com/teddyzeenny/ember-mocha-adapter/master/adapter.js这样的原始源代码

测试本身非常简单,我有一个问题列表,每个问题都有一个布尔值的活动属性。我有一个标题复选框,可以切换所有问题的“活动”属性。然而,在 ie8 中,当我们使用 click helper 点击复选框时,观察者似乎没有被触发!下面是测试的核心代码:

    <script>
      //APPLICATION CODE STARTS HERE
        window.AS = Ember.Application.create({
            rootElement: '#ember-testing'
        });
        mocha.setup('bdd');

        Ember.onLoad('Ember.Application', function(Application) {

            Application.initializer({
                name: 'tests',
                initialize: function(container, application) {
                    Ember.testing = true;
                }

            });
        });


        Ember.Test.adapter = Ember.Test.MochaAdapter.create();
        AS.setupForTesting();
        AS.ApplicationAdapter = DS.FixtureAdapter.extend({simulateRemoteResponse: false});
        AS.injectTestHelpers();
        AS.Router.map(function() {
            this.resource('questions', {path: '/'});
        });

        AS.Question = DS.Model.extend({
            name: DS.attr('string'),
            active: DS.attr('boolean')
        });

        AS.Question.FIXTURES = [
            {
                "id": 1,
                "name": "What is your age?",
                "active": false
            },
            {
                "id": 2,
                "name": "What is your name?",
                "active": false
            }
        ];

        AS.QuestionsRoute = Ember.Route.extend({
            model: function(params) {
                return this.get('store').find('question');
            }
        });

        AS.QuestionsController = Ember.ArrayController.extend({
          toggleAll: false,
          onToggleAllChange: function(){
            var toggleAll = this.get("toggleAll");
            this.get('content').forEach(function(question){
              question.set("active", toggleAll);
            });
          }.observes('toggleAll')
        });

        </script>



    <script>

        describe("Testing", function() {


            beforeEach(function() {
                AS.reset();
                visit("/");
            });

            it("test header toggle", function() {

                click($("input[name='toggleAll']")).then(function() {
                    var chkd = $("input[name='toggleAll']")[0].checked;
                    $(".question input").each(function(index, dom){
                      expect(dom.checked).to.be(chkd);
                    });
                });
            });
        });

        $(document).ready(function() {
            mocha.run();
        });

    </script>
</head>
<body>

    <script type="text/x-handlebars" data-template-name="questions">
        <div>{{input type="checkbox" name="toggleAll" checked=toggleAll}} Toggle active</div>  
        {{#each}}
             <div class="question">{{input type="checkbox" checked=active}} {{name}}</div>
        {{/each}}
    </script>

    <div id="mocha"></div>
    <div id="ember-testing" style="border:1px solid #CCC;"></div>
</body>

测试在 ie8 中失败,但如果您手动单击复选框,观察者会被触发。您的帮助将不胜感激。谢谢,迪

4

1 回答 1

1

所以事实证明,在 Ember.Checkbox 中正在寻找更改事件,但我猜在测试时,点击事件不会在 ie8 中触发更改事件,所以这是我必须做的才能让我的测试在 ie8 中通过:

        it("test header toggle", function() {

            click($("input[name='toggleAll']")).then(function() {
                $("input[name='toggleAll']").trigger("change");//explicitly trigger change event
                var chkd = $("input[name='toggleAll']")[0].checked;
                $(".question input").each(function(index, dom){
                  expect(dom.checked).to.be(chkd);
                });
            });
        });
于 2014-04-02T16:37:46.157 回答