PostalAddress.spec.js 2.02 KB
define(function(require) {
    'use strict';

    var PostalAddress= require('ofbiz/model/PostalAddress');
    var _ = require('underscore');
    var Backbone = require('backbone');
    require('backbone-validation');

    describe('PostalAddress', function() {
        it('exists', function() {
            expect(PostalAddress).toBeTruthy();
        });
    });
    describe('PostalAddress', function() {
        beforeEach(function() {
             _.extend(Backbone.Model.prototype, Backbone.Validation.mixin);
        });
        it('validation', function() {
            var result, postalAddress;
            postalAddress = new PostalAddress();
            expect(postalAddress.validate()).toBeUndefined();
            expect(postalAddress.isValid()).toBe(true);

            postalAddress._isRequired = true;
            // changing _isRequired doesn't have an effect until validate is called
            expect(postalAddress.isValid()).toBe(true);
            result = postalAddress.validate()
            expect(result).not.toBeUndefined();
            expect(postalAddress.isValid()).toBe(false);
            expect(_.keys(result).sort()).toEqual(['address1', 'city', 'postalCode']);
            expect(result['address1']).toEqual(jasmine.any(String));
            expect(result['city']).toEqual(jasmine.any(String));
            expect(result['postalCode']).toEqual(jasmine.any(String));

            postalAddress.set('city', 'Anytown');
            result = postalAddress.validate()
            expect(result).not.toBeUndefined();
            expect(postalAddress.isValid()).toBe(false);
            expect(_.keys(result).sort()).toEqual(['address1', 'postalCode']);
            expect(result['address1']).toEqual(jasmine.any(String));
            expect(result['postalCode']).toEqual(jasmine.any(String));


            postalAddress._isRequired = false;
            expect(postalAddress.isValid()).toBe(false);
            expect(postalAddress.validate()).toBeUndefined();
            expect(postalAddress.isValid()).toBe(true);
        });
    });
});