rivets-backbone-adapter-brainfood.spec.js 7.36 KB
define(function(require) {
    'use strict';

    var $ = require('jquery');
    window.jQuery = $;
    var RivetsBackboneAdapterBrainfood = require('rivets-backbone-adapter-brainfood');
    var _ = require('underscore');
    var Backbone = require('backbone');
    var rivets = require('rivets');
    require('backbone-validation');
    _.extend(Backbone.Model.prototype, Backbone.Validation.mixin);
    //rivets.config.rootInterface = ':';
    /* global console:false */

    describe('RivetsBackboneAdapterBrainfood', function() {
        it('returns undefined', function() {
            expect(RivetsBackboneAdapterBrainfood).toBeDefined();
        });
        var ContactMech, ContactMechs;
        beforeEach(function() {
            ContactMech = Backbone.Model.extend({
                defaults: {
                    contactMechId: '',
                    contactMechPurposeTypeId: '',
                    infoString: '',
                    address1: '',
                },
            });
            ContactMechs = Backbone.Collection.extend({
                modelId: function(attrs) {
                    return attrs.contactMechPurposeTypeId;
                },
            });
        });
        var contactMechs, model;
        beforeEach(function() {
            contactMechs = new ContactMechs([
                new ContactMech({
                    contactMechPurposeTypeId: 'PRIMARY_EMAIL',
                    infoString: 'user@example.com',
                }),
                new ContactMech({
                    contactMechPurposeTypeId: 'PRIMARY_ADDRESS',
                    address1: '1234 Main St.',
                }),
            ]);
            model = new Backbone.Model({contactMechs: contactMechs, notBackbone: {}});
        });
        function buildTest(path, options) {
            it(path, function() {
                var inputElement = document.createElement('input');
                var binderName = options.bb ? 'bb-model' : 'value';
                inputElement.setAttribute('rv-' + binderName, path);
                var pojoObjects = [];
                var view = rivets.bind(inputElement, model, {
                    binders: {
                        'bb-model': {
                            bind: function(el) {
                                console.log('bb-model:bind', el);
                            },
                            routine: function(el, value) {
                                var pojoObject = options.parse(value);
                                console.log('bb-model:routine', el, JSON.stringify(pojoObject));
                                pojoObjects.push(pojoObject);
                            },
                            unbind: function(el) {
                                console.log('bb-model:unbind', el);
                            },
                        },
                    },
                    formatters: {
                        toJSON: function(value) {
                            return value.toJSON();
                        },
                        jsonStringify: {
                            read: function(value) {
                                return JSON.stringify(value);
                            },
                            publish: function(value) {
                                return JSON.parse(value);
                            },
                        },
                    },
                });
                function getInputElementValue() {
                    var rawValue = inputElement.value;
                    if (options.parse) {
                        rawValue = options.parse(rawValue);
                    }
                    return rawValue;
                }
                function convertValue(value) {
                    if (typeof value === 'function') {
                        return value();
                    } else {
                        return value;
                    }
                }
                var value = convertValue(options.value);
                if (options.bb) {
                    expect(pojoObjects[0]).toEqual(value);
                } else {
                    expect(getInputElementValue()).toEqual(value);
                }
                if (options.get) {
                    var newValue = convertValue(options.newValue);
                    inputElement.value = newValue;
                    $(inputElement).trigger('input').trigger('change');
                    expect(options.get()).toEqual(newValue);
                    if (options.set) {
                        options.set(value);
                        expect(getInputElementValue()).toEqual(value);
                    }
                } else if (options.set) {
                }
            });
        }
        buildTest(':contactMechs:PRIMARY_EMAIL:infoString', {
            value: 'user@example.com',
            newValue: '(user@example.com)',
            get: function() {
                return contactMechs.get('PRIMARY_EMAIL').get('infoString');
            },
        });
        buildTest(':contactMechs:PRIMARY_ADDRESS:address1', {
            value: '1234 Main St.',
            newValue: '(1234 Main St.)',
            get: function() {
                return contactMechs.get('PRIMARY_ADDRESS').get('address1');
            },
            set: function(value) {
                contactMechs.get('PRIMARY_ADDRESS').set('address1', value);
            },
        });
        buildTest(':contactMechs:PRIMARY_EMAIL:*.infoString', {
            value: 'user@example.com',
        });
        buildTest(':notBackbone:notValid', {
            value: '',
        });
        //buildTest(':contactMechs:*.length', '2');
        buildTest(':contactMechs:PRIMARY_EMAIL', {
            bb: true,
            value: function() {
                return contactMechs.get('PRIMARY_EMAIL').toJSON();
            },
            parse: function(rawValue) {
                return rawValue.toJSON();
            },
        });
        buildTest(':contactMechs:PRIMARY_EMAIL:*', {
            bb: true,
            value: function() {
                return contactMechs.get('PRIMARY_EMAIL').toJSON();
            },
            parse: function(rawValue) {
                return rawValue;
            },
        });
        buildTest(':contactMechs', {
            bb: true,
            value: function() {
                return contactMechs.toJSON();
            },
            parse: function(rawValue) {
                return rawValue.toJSON();
            },
        });
        buildTest(':contactMechs:*', {
            bb: true,
            value: function() {
                return contactMechs.models;
            },
            parse: function(rawValue) {
                return rawValue;
            },
        });
        /*
        buildTest(':contactMechs:PRIMARY_EMAIL:* | jsonStringify', {
            value: function() {
                var primaryEmail = contactMechs.get('PRIMARY_EMAIL');
                return primaryEmail.toJSON();
            },
            newValue: JSON.stringify({
                infoString: 'someone@example.com',
            }),
            parse: function(rawValue) {
                return JSON.parse(rawValue);
            },
            get: function() {
                var primaryEmail = contactMechs.get('PRIMARY_EMAIL');
                console.log('primaryEmail', JSON.stringify(primaryEmail.toJSON()));
                return primaryEmail.toJSON();
            },
        });
        */
    });
});