Skip to content
This repository has been archived by the owner on Mar 23, 2023. It is now read-only.

Commit

Permalink
v1.4.1 linting fixes (#50)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mariya Radchuk authored and ashleynolan committed Jun 14, 2019
1 parent 43509c1 commit 2e35629
Show file tree
Hide file tree
Showing 24 changed files with 23 additions and 503 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).


v1.4.1
------------------------------
*June 14, 2019*

### Changed
- Linting fixes

v1.4.0
------------------------------
*June 5, 2019*
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@justeat/f-validate",
"description": "Fozzie vanilla JS Validation Component",
"version": "1.4.0",
"version": "1.4.1",
"main": "dist/index.js",
"files": [
"dist"
Expand Down
4 changes: 0 additions & 4 deletions src/callbacks.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,17 @@
export const addCallBack = (callBacks, callBack) => {

if (typeof callBack !== 'function') {
throw new TypeError('call back is not a function');
}

callBacks.push(callBack);

};

export const runCallbacks = (callBacks, ...args) => {

if (!callBacks) {
return;
}

callBacks.forEach(callback => {
callback(...args);
});

};
41 changes: 9 additions & 32 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ export const defaultOptions = {
};

const getForm = descriptor => {

if (!descriptor) {
throw new Error('f-validate: expected form name or form node parameter');
}
Expand All @@ -52,25 +51,21 @@ const getForm = descriptor => {
}

return form;

};

const elementsUntouched = (element, current, touchedSelectors) => {

const notInErrorState = !current.field.classList.contains(CONSTANTS.cssClasses.hasError);
const elementsNotTouched = touchedSelectors
.map(childSelector => $.first(childSelector, element))
.filter(el => el && !el.hasAttribute('data-touched'));

// If one select has not been interacted with do not run test method
return notInErrorState && elementsNotTouched.length > 0;

};

export default class FormValidation {

constructor (nameOrNode, options = {}) {
this.options = Object.assign({}, defaultOptions, options);
this.options = { ...defaultOptions, ...options };
this.form = getForm(nameOrNode);
this.fields = this.getFields();

Expand Down Expand Up @@ -119,7 +114,6 @@ export default class FormValidation {
* });
*/
on (callBackEvent, callBack) {

if (!this.callBacks[callBackEvent]) {
this.callBacks[callBackEvent] = [];
}
Expand All @@ -129,7 +123,6 @@ export default class FormValidation {
} catch (exception) {
throw new TypeError(`f-validate: ${callBackEvent} callback must be a function`);
}

}

setSuccess (element) {
Expand All @@ -143,11 +136,9 @@ export default class FormValidation {
}

setFormNoValidate () {

if (!this.options.enableHTML5Validation) {
this.form.setAttribute('novalidate', '');
}

}

/**
Expand All @@ -158,12 +149,10 @@ export default class FormValidation {
* @returns {boolean}
*/
isValid (event, currentElement, eventType) {

let formValid = true;
this.errorMessages = [];

this.fields.forEach(field => {

// currentElement refers to an element that is being validated on blur/keyup
// only validate on blur/keyup if the field is not empty and is not required
if (currentElement && (currentElement.field !== field || (field.value === '' && !testDefinitions.required.condition(field)))) {
Expand Down Expand Up @@ -208,11 +197,9 @@ export default class FormValidation {
runCallbacks(this.callBacks.elementError, field, ruleName);
}
}

});

if (fieldHasValidation) {

if (fieldValid) {
this.setSuccess(field);
} else {
Expand All @@ -229,9 +216,7 @@ export default class FormValidation {
displayInlineMessage(errorElement, errorMessage, field, this.form);
}
}

}

});

if (!formValid) {
Expand All @@ -256,7 +241,6 @@ export default class FormValidation {
}

return formValid;

}

addCustomValidation (name, handler) {
Expand Down Expand Up @@ -288,15 +272,13 @@ export default class FormValidation {
}

displayGroupedMessages (groupedErrorElement) {

let updateElement = groupedErrorElement;

if (!groupedErrorElement) {
updateElement = document.createElement('ul');
updateElement.classList.add(CONSTANTS.cssClasses.formErrors);

this.form.insertBefore(updateElement, this.getGroupedErrorPosition());

} else {
groupedErrorElement.innerHTML = '';
}
Expand All @@ -309,7 +291,6 @@ export default class FormValidation {
}

getGroupedErrorPosition () {

const groupElement = $.first(this.options.groupErrorPlacement, this.form);

if (groupElement) {
Expand All @@ -321,7 +302,6 @@ export default class FormValidation {
}

return this.form.firstChild;

}

/**
Expand All @@ -333,7 +313,6 @@ export default class FormValidation {
* });
*/
validateOn () {

if (this.options.groupErrorPlacement) {
throw new Error('f-validate: validation on \'blur\' or \'keyup\' cannot be performed if errors are grouped');
}
Expand All @@ -344,27 +323,26 @@ export default class FormValidation {

this.fields.forEach(field => {
if (field.hasAttribute(CONSTANTS.validationGroup)) {
$(CONSTANTS.fieldValues, field).forEach(childField =>
$(CONSTANTS.fieldValues, field).forEach(childField => childField.addEventListener(

// Binds each form element within a validation-group to the specified event.
// When this event is triggered the validation-group element will be passed as the element to test.
// The child field is also passed for use within a rule test method
// Null is being passed as the isValid method expects 'field' as its second argument
childField.addEventListener(this.options.validateOn,
this.isValid.bind(this, null, {
field,
childField
})
)
);

this.options.validateOn,
this.isValid.bind(this, null, {
field,
childField
})
));
} else {
// Null is being passed as the isValid method expects 'field' as its second argument
field.addEventListener(this.options.validateOn, this.isValid.bind(this, null, { field }));
}
});
}

// eslint-disable-next-line class-methods-use-this
markFieldAsBlurred (field) {
if (!field.hasAttribute(CONSTANTS.blurredAttr)) {
field.setAttribute(CONSTANTS.blurredAttr, '');
Expand Down Expand Up @@ -396,5 +374,4 @@ export default class FormValidation {
field.addEventListener('keyup', this.isValid.bind(this, null, { field }, 'keyup'));
});
}

}
5 changes: 0 additions & 5 deletions src/messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,13 @@ export const getInlineErrorElement = (field, form) => {
}

if (nextSibling && nextSibling.classList.contains(CONSTANTS.cssClasses.formError)) {

return nextSibling;
}

return false;
};

export const displayInlineMessage = (errorElement, customMessage, field, form) => {

let updateElement = errorElement;
const customErrorEl = getCustomErrorElement(field, form) || field;

Expand All @@ -43,7 +41,6 @@ export const displayInlineMessage = (errorElement, customMessage, field, form) =

updateElement.textContent = customMessage;
updateElement.classList.remove(CONSTANTS.cssClasses.isHidden);

};

export const hideMessage = errorElement => {
Expand All @@ -53,11 +50,9 @@ export const hideMessage = errorElement => {

errorElement.classList.add(CONSTANTS.cssClasses.isHidden);
errorElement.innerHTML = '';

};

const getDefaultMessage = (field, ruleName) => {

if (!testDefinitions[ruleName].defaultMessageValue) {
return testDefinitions[ruleName].defaultMessage;
}
Expand Down
2 changes: 0 additions & 2 deletions src/rules/dateInFuture.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ export default {
condition: field => field.hasAttribute('data-val-dateInFuture'),

test: element => {

const dateToday = new Date();
const currentMonth = dateToday.getMonth() + 1;
const currentYear = dateToday.getFullYear();
Expand All @@ -26,7 +25,6 @@ export default {
}

return selectedYearVal === currentYear && selectedMonthVal >= currentMonth;

},

touchedSelectors: ['[data-val-dateInFuture-type="month"]', '[data-val-dateInFuture-type="year"]'],
Expand Down
20 changes: 2 additions & 18 deletions test/callbacks.test.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import { addCallBack, runCallbacks } from '../src/callbacks';

describe('add callbacks', () => {

it('should allow callbacks to be added to a callback array', () => {

// Arrange
const callBacks = [];
const callBack = jest.fn();
Expand All @@ -13,11 +11,9 @@ describe('add callbacks', () => {

// Assert
expect(callBacks.length).toBe(1);

});

it('should throw exception when string type callbacks are specified', () => {

// Arrange
const callBacks = [];
const callBack = '';
Expand All @@ -30,11 +26,9 @@ describe('add callbacks', () => {
expect(() => {
addCallBack(callBacks, callBack);
}).toThrowError('call back is not a function');

});

it('should throw exception when array type success callbacks options are specified', () => {

// Arrange
const callBacks = [];
const callBack = [];
Expand All @@ -47,11 +41,9 @@ describe('add callbacks', () => {
expect(() => {
addCallBack(callBacks, callBack);
}).toThrowError('call back is not a function');

});

it('should throw exception when object type success callbacks options are specified', () => {

// Arrange
const callBacks = [];
const callBack = {};
Expand All @@ -64,11 +56,9 @@ describe('add callbacks', () => {
expect(() => {
addCallBack(callBacks, callBack);
}).toThrowError('call back is not a function');

});

it('should throw exception when null type success callbacks are added', () => {

// Arrange
const callBacks = [];
const callBack = null;
Expand All @@ -81,15 +71,11 @@ describe('add callbacks', () => {
expect(() => {
addCallBack(callBacks, callBack);
}).toThrowError('call back is not a function');

});

});

describe('run callbacks', () => {

it('should call all callbacks', () => {

// Arrange
const cb1 = jest.fn();
const cb2 = jest.fn();
Expand All @@ -103,25 +89,23 @@ describe('run callbacks', () => {
expect(cb1.mock.calls.length).toBe(1);
expect(cb2.mock.calls.length).toBe(1);
expect(cb3.mock.calls.length).toBe(1);

});

it('should call all callback with arguments', () => {

// Arrange
const cb1 = jest.fn();
const cb2 = jest.fn();
const callbacks = [cb1, cb2];

const arg1 = "string_argument";
const arg1 = 'string_argument';
const arg2 = 42;
const arg3 = true;

// Act
runCallbacks(callbacks, arg1, arg2, arg3);

// Assert
expect(cb1).toHaveBeenCalledTimes(1)
expect(cb1).toHaveBeenCalledTimes(1);
expect(cb1).toHaveBeenCalledWith(arg1, arg2, arg3);

expect(cb2).toHaveBeenCalledTimes(1);
Expand Down
Loading

0 comments on commit 2e35629

Please sign in to comment.