rivets-bfeach-binder.spec.js 3.2 KB
define(function(require) {
    'use strict';

    var $ = require('jquery');
    window.jQuery = $;
    var RivetsBFEachUtil = require('rivets-bfeach-binder');
    require('rivets-backbone-adapter-brainfood');
    var _ = require('underscore');
    var Backbone = require('backbone');
    var rivets = require('rivets');

    //rivets.config.rootInterface = ':';
    /* global console:false */

    describe('RivetsBFEachUtil', function() {
        it('returns defined', function() {
            expect(RivetsBFEachUtil).toBeDefined();
        });
        var rootScope, rootNode, rivetsView;
        function makeTest(rootData, rootHtml, rootTests) {
            beforeEach(function() {
                rootScope = new Backbone.Model(rootData);
                rootNode = $($.parseHTML(rootHtml));
                rivetsView = rivets.bind(rootNode, rootScope);
            });
            rootTests();
        }
        makeTest({
            sub: new Backbone.Model({
                list: new Backbone.Collection([
                    {key: 'A'},
                    {key: 'B'},
                    {key: 'C'},
                ], {parse: true}),
            }),
            list: new Backbone.Collection([
                {key: 'A'},
                {key: 'B'},
                {key: 'C'},
            ], {parse: true}),
            constant: 'this-is-a-constant',
        }, '<div>{:constant}<div rv-bfeach-item=":list">key:{:item:key} constant:{:constant}</div><div rv-bfeach-item=":sub:list">sub-key:{:item:key} constant:{:constant}</div</div>', function() {
            it('foo', function() {
                expect(rootNode.html()).toEqual(
                    'this-is-a-constant'
                    + '<!-- rivets: bfeach-item -->'
                    + '<div>key:A constant:this-is-a-constant</div>'
                    + '<div>key:B constant:this-is-a-constant</div>'
                    + '<div>key:C constant:this-is-a-constant</div>'
                    + '<!-- rivets: bfeach-item -->'
                    + '<div>sub-key:A constant:this-is-a-constant</div>'
                    + '<div>sub-key:B constant:this-is-a-constant</div>'
                    + '<div>sub-key:C constant:this-is-a-constant</div>'
                );
            });
            describe('foo', function() {
                beforeEach(function() {
                    //rootScope.set('constant', 'updated-constant');
                    rootScope.get('list').add({key: 'D'}, {at: -1, parse: true});
                });
                it('bar', function() {
                    expect(rootNode.html()).toEqual(
                        'updated-constant'
                        + '<!-- rivets: bfeach-item -->'
                        + '<div>key:A constant:updated-constant</div>'
                        + '<div>key:B constant:updated-constant</div>'
                        + '<div>key:C constant:updated-constant</div>'
                        + '<!-- rivets: bfeach-item -->'
                        + '<div>sub-key:A constant:updated-constant</div>'
                        + '<div>sub-key:B constant:updated-constant</div>'
                        + '<div>sub-key:C constant:updated-constant</div>'
                    );
                });
            });
        });
    });
});