Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add validation meta #264

Merged
merged 4 commits into from
Feb 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions src/widget-core/meta/InputValidity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import Base from './Base';

export class InputValidity extends Base {
get(key: string | number, value: string) {
const node = this.getNode(key) as HTMLFormElement | undefined;

if (!node) {
return { valid: undefined, message: '' };
}

if (value !== node.value) {
// if the vdom is out of sync with the real dom our
// validation check will be one render behind.
// Call invalidate on the next loop.
setTimeout(() => this.invalidate());
}

return {
valid: node.validity.valid,
message: node.validationMessage
};
}
}

export default InputValidity;
86 changes: 86 additions & 0 deletions tests/widget-core/unit/meta/InputValidity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
const { registerSuite } = intern.getPlugin('jsdom');
const { assert } = intern.getPlugin('chai');
import { createSandbox, SinonStub } from 'sinon';
import InputValidity from '../../../../src/widget-core/meta/InputValidity';

import NodeHandler from '../../../../src/widget-core/NodeHandler';
import WidgetBase from '../../../../src/widget-core/WidgetBase';

const sandbox = createSandbox();
let bindInstance: WidgetBase;
let invalidateStub: SinonStub;
let nodeHandler: NodeHandler;

registerSuite('meta - InputValidity', {
async before() {
bindInstance = new WidgetBase();
},

beforeEach() {
invalidateStub = sandbox.stub();
nodeHandler = new NodeHandler();
},

afterEach() {
sandbox.restore();
},

tests: {
'returns default values when node is not found'() {
sandbox.stub(nodeHandler, 'get').returns(undefined);
const validity = new InputValidity({
invalidate: invalidateStub,
nodeHandler,
bind: bindInstance
});

const { message, valid } = validity.get('test', 'testValue');
assert.strictEqual(message, '');
assert.isUndefined(valid);
},

'returns the validity and message for the element'() {
sandbox.stub(nodeHandler, 'get').returns({
validity: { valid: false },
value: 'testValue',
validationMessage: 'test validation message'
});
const validity = new InputValidity({
invalidate: invalidateStub,
nodeHandler,
bind: bindInstance
});

const { message, valid } = validity.get('test', 'testValue');
assert.strictEqual(message, 'test validation message');
assert.isFalse(valid);
},

async 'calls invalidate if node values do not match'() {
const nodeHandler = new NodeHandler();
const nodeStub = sandbox
.stub(nodeHandler, 'get')
.withArgs('input')
.returns({
validity: { valid: true },
value: 'test1',
validationMessage: ''
});

const validity = new InputValidity({
invalidate: invalidateStub,
nodeHandler,
bind: bindInstance
});

validity.get('input', 'test2');
return new Promise((resolve) => {
setTimeout(() => {
assert.isTrue(invalidateStub.calledOnce);
nodeStub.reset();
resolve();
}, 10);
});
}
}
});
1 change: 1 addition & 0 deletions tests/widget-core/unit/meta/all.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import './meta';
import './Dimensions';
import './Drag';
import './Focus';
import './InputValidity';
import './Intersection';
import './Resize';
import './WebAnimation';