backbone-model-overlay.spec.js 7.19 KB
define(function(require) {
    'use strict';

    var $ = require('jquery');
    window.jQuery = $;
    var _ = require('underscore');
    var Backbone = require('backbone');
    var BackboneModelOverlay = require('backbone-model-overlay');

    describe('BackboneModelOverlay', function() {
        var staticParentData = {inParent: 'Parent'};
        var staticChildData = {inChild: 'Child'};
        it('exists', function() {
            expect(BackboneModelOverlay).toBeDefined();
        });
        var model, parent;
        var attributes;
        var perAttributeChange;
        var previous;
        function doBeforeEach(parentData, childData) {
            var options;
            if (parentData) {
                parent = new Backbone.Model(parentData);
                options = {parent: parent};
            } else {
                parent = null;
            }
            model = new BackboneModelOverlay(childData, options);
            attributes = [];
            perAttributeChange = {};
            previous = [];
            model.on('change', function() {
                attributes.push(model.toJSON());
                previous.push(model.previousAttributes());
                _.each(model.changedAttributes(), function(value, key) {
                    if (perAttributeChange[key] === undefined) {
                        perAttributeChange[key] = [value];
                    } else {
                        perAttributeChange[key].push(value);
                    }
                });
            });
        }
        describe('no-parent', function() {
            describe('empty', function() {
                beforeEach(function() {
                    doBeforeEach();
                });
                it('get', function() {
                    expect(perAttributeChange).toEqual({});
                    expect(previous).toEqual([]);
                    expect(attributes).toEqual([]);
                    expect(model.get('missing')).toBeUndefined();
                    expect(model.toJSON()).toEqual({});
                });
            });
            describe('baseline', function() {
                beforeEach(function() {
                    doBeforeEach(null, staticChildData);
                    model.set('newChild', 'OldChild', {overlay: true});
                    model.set('newChild', 'NewChild', {overlay: true});
                });
                it('get', function() {
                    expect(perAttributeChange).toEqual({
                        newChild: ['OldChild', 'NewChild'],
                    });
                    expect(previous).toEqual([
                        {inChild: 'Child'},
                        {inChild: 'Child', newChild: 'OldChild'},
                    ]);
                    expect(attributes).toEqual([
                        {inChild: 'Child', newChild: 'OldChild'},
                        {inChild: 'Child', newChild: 'NewChild'},
                    ]);
                    expect(model.get('missing')).toBeUndefined();
                    expect(model.toJSON()).toEqual({inChild: 'Child', newChild: 'NewChild'});
                });
            });
        });
        describe('with-parent', function() {
            beforeEach(function() {
                doBeforeEach(staticParentData, staticChildData);
            });
            describe('initial', function() {
                it('get', function() {
                    expect(parent.get('missing')).toBeUndefined();
                    expect(model.get('missing')).toBeUndefined();

                    expect(parent.get('inParent')).toEqual('Parent');
                    expect(model.get('inParent')).toEqual('Parent');

                    expect(parent.get('inChild')).toBeUndefined();
                    expect(model.get('inChild')).toEqual('Child');

                    expect(attributes).toEqual([]);
                    expect(model.toJSON()).toEqual({inParent: 'Parent', inChild: 'Child'});
                });
            });
            describe('add', function() {
                beforeEach(function() {
                    parent.set('newParent', 'OldParent');
                    model.set('newChild', 'OldChild', {overlay: true});
                    parent.set('newParent', 'NewParent');
                    model.set('newChild', 'NewChild', {overlay: true});
                    model.set('passThruParent', 'PassThruParent');
                });
                it('get:perAttributeChange', function() {
                    expect(perAttributeChange).toEqual({
                        newParent: ['OldParent', 'OldParent', 'NewParent', 'NewParent', 'NewParent'],
                        newChild: ['OldChild', 'NewChild'],
                        passThruParent: ['PassThruParent'],
                    });
                });
                it('get:previous', function() {
                    _.each(previous, function(v) {
                        //console.log('111', JSON.stringify(v));
                    });
                    expect(previous).toEqual([
                        {inParent: 'Parent'},
                        {inParent: 'Parent', inChild: 'Child'},
                        {inParent: 'Parent', newParent: 'OldParent', inChild: 'Child'},
                        {inParent: 'Parent', newParent: 'OldParent', inChild: 'Child', newChild: 'OldChild'},
                        {inParent: 'Parent', newParent: 'OldParent', inChild: 'Child', newChild: 'NewChild'},
                    ]);
                });
                it('get:attributes', function() {
                    expect(attributes).toEqual([
                        {inParent: 'Parent', newParent: 'OldParent', inChild: 'Child'},
                        {inParent: 'Parent', newParent: 'OldParent', inChild: 'Child', newChild: 'OldChild'},
                        {inParent: 'Parent', newParent: 'NewParent', inChild: 'Child', newChild: 'OldChild'},
                        {inParent: 'Parent', newParent: 'NewParent', inChild: 'Child', newChild: 'NewChild'},
                        {inParent: 'Parent', newParent: 'NewParent', inChild: 'Child', newChild: 'NewChild', passThruParent: 'PassThruParent'},
                    ]);
                });
                it('get:parent:separate-checks', function() {
                    expect(parent.get('missing')).toBeUndefined();
                    expect(model.get('missing')).toBeUndefined();

                    expect(parent.get('inParent')).toEqual('Parent');
                    expect(parent.get('newParent')).toEqual('NewParent');
                    expect(model.get('inParent')).toEqual('Parent');
                    expect(model.get('newParent')).toEqual('NewParent');
                });
                it('get:child:separate-checks:1', function() {
                    expect(parent.get('inChild')).toBeUndefined();
                    expect(parent.get('newChild')).toBeUndefined();
                    expect(model.get('inChild')).toEqual('Child');
                });
                it('get:child:separate-checks:2', function() {
                    expect(model.get('newChild')).toEqual('NewChild');
                    expect(model.toJSON()).toEqual({inParent: 'Parent', newParent: 'NewParent', inChild: 'Child', newChild: 'NewChild', passThruParent: 'PassThruParent'});
                });
            });
        });
    });
});