Add default model view input types and validation (#1397)

This commit is contained in:
Matt Irvine
2018-05-14 16:20:19 -07:00
committed by GitHub
parent 89c48bbe75
commit 9bd45cf66a
14 changed files with 363 additions and 118 deletions

View File

@@ -0,0 +1,132 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the Source EULA. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as assert from 'assert';
import { Mock, It, Times, MockBehavior } from 'typemoq';
import { ComponentBase, ContainerBase } from 'sql/parts/modelComponents/componentBase';
import { IComponentDescriptor, IModelStore, ComponentEventType } from 'sql/parts/modelComponents/interfaces';
import { ModelStore } from 'sql/parts/modelComponents/modelStore';
import { ChangeDetectorRef } from '@angular/core';
'use strict';
class TestComponent extends ComponentBase {
public descriptor: IComponentDescriptor;
constructor(public modelStore: IModelStore, id: string) {
super(undefined);
this.descriptor = modelStore.createComponentDescriptor('TestComponent', id);
this.baseInit();
}
ngOnInit() { }
setLayout() { }
public addValidation(validation: () => boolean | Thenable<boolean>) {
this._validations.push(validation);
}
}
class TestContainer extends ContainerBase<TestComponent> {
public descriptor: IComponentDescriptor;
constructor(public modelStore: IModelStore, id: string) {
super(undefined);
this.descriptor = modelStore.createComponentDescriptor('TestContainer', id);
this._changeRef = {
detectChanges: () => undefined
} as ChangeDetectorRef;
this.baseInit();
}
ngOnInit() { }
setLayout() { }
public addValidation(validation: () => boolean | Thenable<boolean>) {
this._validations.push(validation);
}
}
suite('ComponentBase Validation Tests', () => {
let testComponent: TestComponent;
let testContainer: TestContainer;
let modelStore: IModelStore;
setup(() => {
modelStore = new ModelStore();
testComponent = new TestComponent(modelStore, 'testComponent');
testContainer = new TestContainer(modelStore, 'testContainer');
});
test('Component validation runs external validations stored in the model store', done => {
assert.equal(testComponent.valid, true, 'Test component validity did not default to true');
let validationCalls = 0;
modelStore.registerValidationCallback(componentId => {
validationCalls += 1;
return Promise.resolve(false);
});
testComponent.validate().then(valid => {
try {
assert.equal(validationCalls, 1, 'External validation was not called once');
assert.equal(valid, false, 'Validate call did not return correct value from the external validation');
assert.equal(testComponent.valid, false, 'Validate call did not update the component valid property');
done();
} catch (err) {
done(err);
}
}, err => done(err));
});
test('Component validation runs default component validations', done => {
assert.equal(testComponent.valid, true, 'Test component validity did not default to true');
let validationCalls = 0;
testComponent.addValidation(() => {
validationCalls += 1;
return false;
});
testComponent.validate().then(valid => {
try {
assert.equal(validationCalls, 1, 'Default validation was not called once');
assert.equal(valid, false, 'Validate call did not return correct value from the default validation');
assert.equal(testComponent.valid, false, 'Validate call did not update the component valid property');
done();
} catch (err) {
done(err);
}
}, err => done(err));
});
test('Container validation reflects child component validity', done => {
assert.equal(testContainer.valid, true, 'Test container validity did not default to true');
testContainer.addToContainer(testComponent.descriptor, undefined);
testComponent.addValidation(() => false);
testComponent.validate().then(() => {
testContainer.validate().then(valid => {
assert.equal(valid, false, 'Validate call did not return correct value for container child validation');
assert.equal(testContainer.valid, false, 'Validate call did not update the container valid property');
done();
}, err => done(err));
}, err => done(err));
});
test('Container child validity changes cause the parent container validity to change', done => {
testContainer.registerEventHandler(event => {
try {
if (event.eventType === ComponentEventType.validityChanged) {
assert.equal(testContainer.valid, false, 'Test container validity did not change to false when child validity changed');
assert.equal(event.args, false, 'ValidityChanged event did not contain the updated container validity');
done();
}
} catch (err) {
done(err);
}
});
testComponent.addValidation(() => false);
testContainer.addToContainer(testComponent.descriptor, undefined);
testComponent.validate();
});
});