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

    var $ = require('jquery');
    window.jQuery = $;
    var RivetsErrorBinder = require('rivets-error-binder');
    var _ = require('underscore');
    var Backbone = require('backbone');
    var rivets = require('rivets');
    require('backbone-validation');
    var BackboneSeen = require('backbone-seen');
    require('rivets-backbone-adapter');
    _.extend(Backbone.Model.prototype, Backbone.Validation.mixin);
    //rivets.config.rootInterface = ':';

    describe('RivetsErrorBinder', function() {
        it('exists', function() {
            expect(RivetsErrorBinder).toBeUndefined();
        });
    });
    describe('RivetsErrorBinder', function() {
        var Model, Collection;
        var scope;
        var test;
        var view;
        beforeEach(function() {
            jasmine.Clock.useMock();
            Model = BackboneSeen.mixin(Backbone.Model.extend());
            Collection = Backbone.Collection.extend({model: Model});

            scope = new Model({
                test: test = new Model({
                    constant: 'CONSTANT'
                })
            });
            scope.toString = function() {
                return '[Model: test-scope]';
            };
            test.validation = {
                constant: {required: true}
            };
        });
        afterEach(function() {
            view.unbind();
        });


        it('existing', function() {
            var $node = $($.parseHTML('<div><form><input id="_1" rv-error-value="test:constant" /></form></div>'));
            $(document).find('body').append($node[0]);
            var $1 = $node.find('#_1');
            expect($1.length).toEqual(1);
            expect($1.val()).toEqual('');
            view = rivets.bind($node, scope.attributes);
            expect(view).toBeTruthy();
            expect($1.val()).toEqual('CONSTANT');
            //expect($node.html()).toBe('');
            test.set('constant', 'one');
            expect($1.val()).toEqual('one');
            test.set('constant', '');
            expect($1.val()).toEqual('');
            test.set('constant', 'CONSTANT');
            expect($1.val()).toEqual('CONSTANT');

            $1.focus().val('one').change().blur();
            jasmine.Clock.tick(1);
            expect(test.get('constant')).toEqual('one');

            $1.focus().val('').change().blur();
            jasmine.Clock.tick(1);
            expect(test.get('constant')).toEqual('');
            jasmine.Clock.tick(1);
        });
        it('missing', function() {
            var $node = $($.parseHTML('<div><form><input id="_1" rv-error-value="test:missing:child" /></form></div>'));
            $(document).find('body').append($node[0]);
            var $1 = $node.find('#_1');
            expect($1.length).toEqual(1);
            expect($1.val()).toEqual('');
            view = rivets.bind($node, scope.attributes);
            expect(view).toBeTruthy();
            expect($1.val()).toEqual('');

            $1.focus().val('one').change().blur();
            jasmine.Clock.tick(1);
        });
    });
});