rivets-bfeach-binder.spec.js 7.88 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() {
        var rootScope, rootNode;
        beforeEach(function() {
            rootScope = new Backbone.Model({
                sub: new Backbone.Model({
                    subKey: 'sub-key',
                    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',
            });
            rootNode = $($.parseHTML('<div>{:constant}<div rv-bfeach-item=":list">key:{:item:key} constant:{:constant}<ul><li rv-bfeach-subitem=":sub:list">sub-key:{:subitem:key} constant:{:constant}<input rv-value=":inputValue" /></li></ul></div></div>'));
        });
        it('returns defined', function() {
            expect(RivetsBFEachUtil).toBeDefined();
        });
        describe('binder', function() {
            var rivetsView;
            beforeEach(function() {
                rivetsView = rivets.bind(rootNode, rootScope);
            });
            it('first-pass', function() {
                var subListHtml = '<ul><!-- rivets: bfeach-subitem --><li>sub-key:A constant:this-is-a-constant<input rv-value=":inputValue"></li><li>sub-key:B constant:this-is-a-constant<input rv-value=":inputValue"></li><li>sub-key:C constant:this-is-a-constant<input rv-value=":inputValue"></li></ul>';
                expect(rootNode.html()).toEqual(
                    'this-is-a-constant' +
                    '<!-- rivets: bfeach-item -->' +
                    '<div>key:A constant:this-is-a-constant' + subListHtml + '</div>' +
                    '<div>key:B constant:this-is-a-constant' + subListHtml + '</div>' +
                    '<div>key:C constant:this-is-a-constant' + subListHtml + '</div>'
                );
            });
            describe('add-insert', function() {
                beforeEach(function() {
                    rootScope.get('list').add({key: 'D'}, {at: -1, parse: true});
                    rootScope.get('list').add({key: 'preA'}, {at: 0, parse: true});
                });
                it('test', function() {
                    var subListHtml = '<ul><!-- rivets: bfeach-subitem --><li>sub-key:A constant:this-is-a-constant<input rv-value=":inputValue"></li><li>sub-key:B constant:this-is-a-constant<input rv-value=":inputValue"></li><li>sub-key:C constant:this-is-a-constant<input rv-value=":inputValue"></li></ul>';
                    expect(rootNode.html()).toEqual(
                        'this-is-a-constant' +
                        '<!-- rivets: bfeach-item -->' +
                        '<div>key:preA constant:this-is-a-constant' + subListHtml + '</div>' +
                        '<div>key:A constant:this-is-a-constant' + subListHtml + '</div>' +
                        '<div>key:B constant:this-is-a-constant' + subListHtml + '</div>' +
                        '<div>key:C constant:this-is-a-constant' + subListHtml + '</div>' +
                        '<div>key:D constant:this-is-a-constant' + subListHtml + '</div>'
                    );
                });
                describe('update-constant', function() {
                    var subListHtml = '<ul><!-- rivets: bfeach-subitem --><li>sub-key:A constant:updated-constant<input rv-value=":inputValue"></li><li>sub-key:B constant:updated-constant<input rv-value=":inputValue"></li><li>sub-key:C constant:updated-constant<input rv-value=":inputValue"></li></ul>';
                    var wantedHtml =
                        'updated-constant' +
                        '<!-- rivets: bfeach-item -->' +
                        '<div>key:preA constant:updated-constant' + subListHtml + '</div>' +
                        '<div>key:A constant:updated-constant' + subListHtml + '</div>' +
                        '<div>key:B constant:updated-constant' + subListHtml + '</div>' +
                        '<div>key:C constant:updated-constant' + subListHtml + '</div>' +
                        '<div>key:D constant:updated-constant' + subListHtml + '</div>';
                    beforeEach(function() {
                        rootScope.set('constant', 'updated-constant');
                    });
                    it('test', function() {
                        expect(rootNode.html()).toEqual(wantedHtml);
                    });
                    describe('unbind+change', function() {
                        beforeEach(function() {
                            rivetsView.unbind();
                            rootScope.set('constant', 'changed-constant');
                        });
                        it('test', function() {
                            expect(rootNode.html()).toEqual(wantedHtml);
                        });
                        describe('bind', function() {
                            beforeEach(function() {
                                rivetsView.bind();
                            });
                            it('test', function() {
                                expect(rootNode.html()).toEqual(wantedHtml.replace(/updated-constant/g, 'changed-constant'));
                            });
                            describe('shift', function() {
                                beforeEach(function() {
                                    rootScope.get('sub').get('list').shift();
                                });
                                it('test', function() {
                                    expect(rootNode.html()).toEqual(wantedHtml.replace(/updated-constant/g, 'changed-constant').replace(/<li>sub-key:A constant:changed-constant<input rv-value=":inputValue"><\/li>/g, ''));
                                    expect(rootScope.get('inputValue')).toBeUndefined();
                                });
                                describe('input-set-value', function() {
                                    beforeEach(function() {
                                        var lastInput = rootNode.find('input:last');
                                        lastInput.trigger('focus');
                                        lastInput[0].value = 'typed';
                                        lastInput.trigger('input');
                                        lastInput.trigger('change').trigger('blur');
                                    });
                                    it('test', function() {
                                        expect(rootScope.get('inputValue')).toEqual('typed');
                                        var inputElementValues = [];
                                        rootNode.find('input').each(function(i) {
                                            inputElementValues.push(this.value);
                                        });
                                        expect(inputElementValues).toEqual([
                                            'typed', 'typed',   // preA
                                            'typed', 'typed',   // A
                                            'typed', 'typed',   // B
                                            'typed', 'typed',   // C
                                            'typed', 'typed',   // D
                                        ]);
                                    });
                                });
                            });
                        });
                    });
                });
            });
        });
    });
});