Refactor server dialogue validation for better unit testing.

Victoria & Joao @ Pivotal.
This commit is contained in:
Victoria Henry
2018-03-13 14:47:32 -04:00
committed by Dave Page
parent 156b308fd3
commit 6b03cb78af
10 changed files with 299 additions and 111 deletions

View File

@@ -0,0 +1,101 @@
/////////////////////////////////////////////////////////////
//
// pgAdmin 4 - PostgreSQL Tools
//
// Copyright (C) 2013 - 2018, The pgAdmin Development Team
// This software is released under the PostgreSQL Licence
//
//////////////////////////////////////////////////////////////
import {ModelValidation} from 'sources/browser/server_groups/servers/model_validation';
describe('Server#ModelValidation', () => {
describe('When validating a server parameters', () => {
let model;
let modelValidation;
beforeEach(() => {
model = {
errorModel: jasmine.createSpyObj('errorModel', ['set', 'unset']),
allValues: {},
get: function (key) {
return this.allValues[key];
},
sessAttrs: {},
};
model.isNew = jasmine.createSpy('isNew');
modelValidation = new ModelValidation(model);
});
describe('When all parameters are valid', () => {
beforeEach(() => {
model.isNew.and.returnValue(true);
model.allValues['name'] = 'some name';
model.allValues['username'] = 'some username';
model.allValues['port'] = 'some port';
});
describe('No service id', () => {
it('does not set any error in the model', () => {
model.allValues['host'] = 'some host';
model.allValues['db'] = 'some db';
model.allValues['hostaddr'] = '1.1.1.1';
expect(modelValidation.validate()).toBeNull();
expect(model.errorModel.set).toHaveBeenCalledWith({});
});
});
describe('Service id present', () => {
it('does not set any error in the model', () => {
model.allValues['service'] = 'asdfg';
expect(modelValidation.validate()).toBeNull();
expect(model.errorModel.set).toHaveBeenCalledWith({});
});
});
});
describe('When no parameters are valid', () => {
describe('Service id not present', () => {
it('does not set any error in the model', () => {
expect(modelValidation.validate()).toBe('Name must be specified.');
expect(model.errorModel.set).toHaveBeenCalledTimes(1);
expect(model.errorModel.set).toHaveBeenCalledWith({
name: 'Name must be specified.',
host: 'Either Host name, Address or Service must be specified.',
hostaddr: 'Either Host name, Address or Service must be specified.',
db: 'Maintenance database must be specified.',
username: 'Username must be specified.',
port: 'Port must be specified.'
});
});
});
describe('Host address is not valid', () => {
it('sets the "Host address must be a valid IPv4 or IPv6 address" error', () => {
model.allValues['hostaddr'] = 'something that is not an ip address';
expect(modelValidation.validate()).toBe('Host address must be valid IPv4 or IPv6 address.');
expect(model.errorModel.set).toHaveBeenCalledTimes(1);
expect(model.errorModel.set).toHaveBeenCalledWith({
name: 'Name must be specified.',
hostaddr: 'Host address must be valid IPv4 or IPv6 address.',
db: 'Maintenance database must be specified.',
username: 'Username must be specified.',
port: 'Port must be specified.'
});
});
});
describe('Service id present', () => {
it('does not set any error in the model', () => {
model.allValues['service'] = 'asdfg';
expect(modelValidation.validate()).toBe('Name must be specified.');
expect(model.errorModel.set).toHaveBeenCalledTimes(1);
expect(model.errorModel.set).toHaveBeenCalledWith({
name: 'Name must be specified.',
username: 'Username must be specified.',
port: 'Port must be specified.'
});
});
});
});
});
});