rivets-backbone-adapter-brainfood.spec.js 5.66 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');
                inputElement.setAttribute('rv-value', path);
                var view = rivets.bind(inputElement, model, {
                    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);
                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 | toJSON | jsonStringify', {
            value: function() {
                var primaryEmail = contactMechs.get('PRIMARY_EMAIL');
                return primaryEmail.toJSON();
            },
            parse: function(rawValue) {
                return JSON.parse(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();
            },
        });
        */
    });
});