EmailAddress.spec.js 3.16 KB
define(function(require) {
    'use strict';

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

    describe('EmailAddress', function() {
        it('exists', function() {
            expect(EmailAddress).toBeTruthy();
        });
    });
    describe('EmailAddress', function() {
        beforeEach(function() {
             _.extend(Backbone.Model.prototype, Backbone.Validation.mixin);
        });
        it('validation', function() {
            var result, emailAddress;
            emailAddress = new EmailAddress();
            expect(emailAddress.validate()).toBeUndefined();
            expect(emailAddress.isValid()).toBe(true);
            
            emailAddress._isRequired = true;
            // changing _isRequired doesn't have an effect until validate is called
            expect(emailAddress.isValid()).toBe(true);
            result = emailAddress.validate();
            expect(result).not.toBeUndefined();
            expect(emailAddress.isValid()).toBe(false);
            expect(_.keys(result).sort()).toEqual(['emailAddress']);
            expect(result['emailAddress']).toEqual(jasmine.any(String));

            emailAddress.set('emailAddress', 'foo');
            result = emailAddress.validate();
            expect(result).not.toBeUndefined();
            expect(emailAddress.isValid()).toBe(false);
            expect(_.keys(result).sort()).toEqual(['emailAddress']);
            expect(result['emailAddress']).toEqual(jasmine.any(String));

            emailAddress.set('emailAddress', 'name@example.com');
            result = emailAddress.validate();
            expect(result).toBeUndefined();
            expect(emailAddress.isValid()).toBe(true);


            emailAddress.set('emailAddress', null);
            result = emailAddress.validate();
            expect(result).not.toBeUndefined();
            expect(emailAddress.isValid()).toBe(false);
            expect(_.keys(result).sort()).toEqual(['emailAddress']);
            expect(result['emailAddress']).toEqual(jasmine.any(String));

            emailAddress._isRequired = false;
            expect(emailAddress.isValid()).toBe(false);
            expect(emailAddress.validate()).toBeUndefined();
            expect(emailAddress.isValid()).toBe(true);
        });
        it('parse', function() {
            var result, emailAddress;
            emailAddress = new EmailAddress({infoString: 'name@example.com'}, {parse: true});
            expect(emailAddress.get('infoString')).toBeUndefined();
            expect(emailAddress.get('emailAddress')).toEqual('name@example.com');

            emailAddress = new EmailAddress({emailAddress: 'name@example.com'}, {parse: true});
            expect(emailAddress.get('infoString')).toBeUndefined();
            expect(emailAddress.get('emailAddress')).toEqual('name@example.com');

            emailAddress = new EmailAddress({infoString: 'foo', emailAddress: 'name@example.com'}, {parse: true});
            expect(emailAddress.get('infoString')).toBeUndefined();
            expect(emailAddress.get('emailAddress')).toEqual('name@example.com');
        });
    });
});