diff --git a/packages/validate/__tests__/integration/api/index.js b/packages/validate/__tests__/integration/api/index.js deleted file mode 100644 index e6ea9aad..00000000 --- a/packages/validate/__tests__/integration/api/index.js +++ /dev/null @@ -1,277 +0,0 @@ -import validate from '../../../src'; -import { GROUP_ATTRIBUTE, DOTNET_CLASSNAMES } from '../../../src/lib/constants'; -import defaults from '../../../src/lib/defaults'; - -describe('Validate > Integration > API > addGroup', () => { - - it('should add a validation group', async () => { - // expect.assertions(6); - document.body.innerHTML = `
`; - // const form = document.querySelector('.form'); - const input = document.querySelector('#group1-1'); - const validator = validate('form')[0]; - - expect(validator.getState().groups).toEqual({}); - input.setAttribute('required', 'required'); - validator.addGroup([input]); - expect(validator.getState().groups).toEqual({ - group1: { - serverErrorNode: false, - validators: [{ type: 'required' }], - fields: [input], - valid: false - } - }); - - }); - - it('should return leave state unchanged if it cannot add the validation group', async () => { - document.body.innerHTML = ``; - const input = document.querySelector('#group1-1'); - const [ validator ] = validate('form'); - console.warn = jest.fn(); - - expect(validator.getState().groups).toEqual({}); - validator.addGroup([input]); - expect(validator.getState().groups).toEqual({}); - expect(console.warn).toHaveBeenCalled(); - }); - - -}); - -describe('Validate > Integration > API > removeGroup', () => { - - it('should remove a validation group', async () => { - // expect.assertions(6); - document.body.innerHTML = ``; - // const form = document.querySelector('.form'); - const input = document.querySelector('#group1-1'); - const validator = validate('form')[0]; - - expect(validator.getState().groups).toEqual({ - group1: { - serverErrorNode: false, - validators: [{ type: 'required' }], - fields: [input], - valid: false - } - }); - input.removeAttribute('required'); - validator.removeGroup('group1'); - expect(validator.getState().groups).toEqual({}); - - }); - -}); - -describe('Validate > Integration > API > validateGroup', () => { - - it('should validate an individual validation group when called', async () => { - // expect.assertions(6); - document.body.innerHTML = ``; - // const form = document.querySelector('.form'); - const input = document.querySelector('#group1-1'); - const validator = validate('form')[0]; - await validator.validateGroup('group1'); - - expect(document.querySelector(`.${DOTNET_CLASSNAMES.ERROR}`).textContent).toEqual(defaults.messages.required()); - - input.value = "test"; - await validator.validateGroup('group1'); - expect(document.querySelector(`.${DOTNET_CLASSNAMES.ERROR}`)).toBeNull(); - }); - -}); - -describe('Validate > Integration > API > addMethod', () => { - - it('should add a validation method to a group', async () => { - // expect.assertions(6); - document.body.innerHTML = ``; - // const form = document.querySelector('.form'); - const input = document.querySelector('#group1-1'); - const validator = validate('form')[0]; - - expect(validator.getState().groups).toEqual({ - group1: { - serverErrorNode: false, - validators: [{ type: 'required' }], - fields: [input], - valid: false - } - }); - const method = () => false; - const message = 'Custom error'; - validator.addMethod('group1', method, message); - - expect(validator.getState().groups).toEqual({ - group1: { - serverErrorNode: false, - validators: [{ type: 'required' }, { type: 'custom', method, message }], - fields: [input], - valid: false - } - }); - }); - - it('should not add a validation method if parameters are missing', async () => { - // expect.assertions(6); - document.body.innerHTML = ``; - // const form = document.querySelector('.form'); - const input = document.querySelector('#group1-1'); - const validator = validate('form')[0]; - console.warn = jest.fn(); - - expect(validator.getState().groups).toEqual({ - group1: { - serverErrorNode: false, - validators: [{ type: 'required' }], - fields: [input], - valid: false - } - }); - const method = () => false; - const message = 'Custom error'; - - validator.addMethod(undefined, method, message); - expect(validator.getState().groups).toEqual({ - group1: { - serverErrorNode: false, - validators: [{ type: 'required' }], - fields: [input], - valid: false - } - }); - expect(console.warn).toHaveBeenCalled(); - - }); - - it('should not add a validation method if fields cannot be found', async () => { - // expect.assertions(6); - document.body.innerHTML = ``; - // const form = document.querySelector('.form'); - const input = document.querySelector('#group1-1'); - const validator = validate('form')[0]; - - expect(validator.getState().groups).toEqual({ - groupX: { - serverErrorNode: false, - validators: [{ type: 'required' }], - fields: [input], - valid: false - } - }); - const method = () => false; - const message = 'Custom error'; - - validator.addMethod('neither-name-or-group-name', method, message); - expect(validator.getState().groups).toEqual({ - groupX: { - serverErrorNode: false, - validators: [{ type: 'required' }], - fields: [input], - valid: false - } - }); - - }); - - it('Should add a validation method when provided an array of fields and a new group name', () => { - document.body.innerHTML = ``; - // const form = document.querySelector('.form'); - const input = document.querySelector('#group1-1'); - const validator = validate('form')[0]; - - expect(validator.getState().groups).toEqual({ - group1: { - serverErrorNode: false, - validators: [{ type: 'required' }], - fields: [input], - valid: false - } - }); - const method = () => false; - const message = 'Custom error'; - validator.addMethod('CustomGroup', method, message, [ input ]); - - expect(validator.getState().groups).toEqual({ - group1: { - serverErrorNode: false, - validators: [{ type: 'required' }], - fields: [input], - valid: false - }, - CustomGroup: { - serverErrorNode: false, - validators: [{ type: 'custom', method, message }], - fields: [input], - valid: false - } - }); - }); - -}); \ No newline at end of file diff --git a/packages/validate/__tests__/integration/errors/index.js b/packages/validate/__tests__/integration/errors/index.js deleted file mode 100644 index 4f1adc53..00000000 --- a/packages/validate/__tests__/integration/errors/index.js +++ /dev/null @@ -1,93 +0,0 @@ -import validate from '../../../src'; -import { DOTNET_CLASSNAMES } from '../../../src/lib/constants'; -import defaults from '../../../src/lib/defaults'; - -describe('Validate > Integration > errors', () => { - - it('Should render client-side error container as the last element in a label', async () => { - expect.assertions(1); - document.body.innerHTML = ``; - const validator = validate('form')[0]; - await validator.validate(); - //render error message - expect(document.querySelector(`.${DOTNET_CLASSNAMES.ERROR}`).textContent).toEqual(defaults.messages.required()); - }); - - it('Should render a text node error to a server-side error container', async () => { - expect.assertions(3); - document.body.innerHTML = ``; - const [ validator ] = validate('form'); - await validator.validate(); - const errorContainer = document.getElementById('ssec'); - //render error message - expect(errorContainer.firstChild).toBeDefined(); - expect(errorContainer.classList.contains(DOTNET_CLASSNAMES.ERROR)).toEqual(true); - expect(errorContainer.firstChild.textContent).toEqual(defaults.messages.required()); - }); - - it('Should clear a server-rendered error node before rendering a text node error to a server-side error container', async () => { - expect.assertions(4); - document.body.innerHTML = ``; - const [ validator ] = validate('form'); - expect(validator.getState().errors.group1).toEqual('The server dislikes this value'); - await validator.validate(); - const errorContainer = document.getElementById('ssec'); - //render error message - expect(errorContainer.firstChild).toBeDefined(); - expect(errorContainer.classList.contains(DOTNET_CLASSNAMES.ERROR)).toEqual(true); - expect(errorContainer.firstChild.textContent).toEqual(defaults.messages.required()); - }); - - it('Should render the invalid input value in the error message if the {{value}} token is used in the supplied error message', async () => { - expect.assertions(1); - document.body.innerHTML = ``; - const [ validator ] = validate('form'); - await validator.validate(); - const errorContainer = document.getElementById('ssec'); - //render error message - expect(errorContainer.textContent).toEqual('test is not a valid email address'); - }); - -}); \ No newline at end of file diff --git a/packages/validate/__tests__/integration/real-time/index.js b/packages/validate/__tests__/integration/real-time/index.js deleted file mode 100644 index f0c72891..00000000 --- a/packages/validate/__tests__/integration/real-time/index.js +++ /dev/null @@ -1,138 +0,0 @@ -import validate from '../../../src'; -import defaults from '../../../src/lib/defaults'; -import { DOTNET_CLASSNAMES } from '../../../src/lib/constants'; - - -describe('Validate > Integration > Real-time', () => { - - it('should start real-time validation after first form submission', async () => { - expect.assertions(2); - document.body.innerHTML = ``; - - const form = document.querySelector('form'); - const [ validator ] = validate(form); - await validator.validate(); - const validityState = await validator.validate(); - expect(validityState).toEqual(false); - expect(validator.getState().realTimeValidation).toEqual(true); - }); - - it('should remove error message and not replace if field is valid', async () => { - expect.assertions(2); - document.body.innerHTML = ``; - - const form = document.querySelector('form'); - const input = document.querySelector('input'); - const [ validator ] = validate(form); - await validator.validate(); - expect(document.querySelector(`.${DOTNET_CLASSNAMES.ERROR}`)).toBeDefined(); - input.value = 'Super'; - const event = new Event('input', { bubbles: false }); - input.dispatchEvent(event); - expect(document.querySelector(`.${DOTNET_CLASSNAMES.ERROR}`)).toBeNull(); - }); - - - it('should update error message based on real-time validation', async () => { - expect.assertions(2); - document.body.innerHTML = ``; - - const form = document.querySelector('form'); - const input = document.querySelector('input'); - const [ validator ] = validate(form); - await validator.validate(); - expect(document.querySelector(`.${DOTNET_CLASSNAMES.ERROR}`).textContent).toEqual(defaults.messages.required()); - input.value = 'Super'; - const event = new Event('input', { bubbles: false }); - input.dispatchEvent(event); - - //have to game Jest to ensure that the error is rendered in time for the assertion - const nextMsg = await new Promise((resolve, reject) => setTimeout(() => { - resolve(document.querySelector(`.${DOTNET_CLASSNAMES.ERROR}`).textContent); - }, 16)); - - expect(nextMsg).toEqual(defaults.messages.email()); - }); - - it('should trigger real-time validation with appropriate event for the input type', async () => { - expect.assertions(2); - document.body.innerHTML = ``; - - const form = document.querySelector('form'); - const input = document.querySelector('input'); - const [ validator ] = validate(form); - await validator.validate(); - expect(document.querySelector(`.${DOTNET_CLASSNAMES.ERROR}`).textContent).toEqual(defaults.messages.required()); - input.checked = 'checked'; - const event = new Event('change', { bubbles: false }); - input.dispatchEvent(event); - expect(document.querySelector(`.${DOTNET_CLASSNAMES.ERROR}`)).toBeNull(); - }); - - it('should run realtime validation after a new group is added post first validation', async () => { - expect.assertions(3); - document.body.innerHTML = ``; - - const form = document.querySelector('form'); - const [ validator ] = validate(form); - await validator.validate(); - expect(document.querySelector(`.${DOTNET_CLASSNAMES.ERROR}`).textContent).toEqual(defaults.messages.required()); - - const newLabel = document.createElement('label'); - newLabel.textContent = 'Group2'; - newLabel.setAttribute('for', 'group2'); - form.appendChild(newLabel); - - const newInput = document.createElement('input'); - newInput.setAttribute('type', 'text'); - newInput.setAttribute('id', 'group2'); - newInput.setAttribute('name', 'group2'); - newInput.required = true; - form.appendChild(newInput); - - validator.addGroup([newInput]); - await validator.validate(); - expect(document.querySelector(`.${DOTNET_CLASSNAMES.ERROR}`).textContent).toEqual(defaults.messages.required()); - - newInput.value = 'Sample'; - const event = new Event('input', { bubbles: false }); - newInput.dispatchEvent(event); - expect([].slice.call(document.querySelectorAll(`.${DOTNET_CLASSNAMES.ERROR}`)).length).toEqual(1); - }); - -}); \ No newline at end of file diff --git a/packages/validate/__tests__/integration/reset/index.js b/packages/validate/__tests__/integration/reset/index.js deleted file mode 100644 index b148e257..00000000 --- a/packages/validate/__tests__/integration/reset/index.js +++ /dev/null @@ -1,60 +0,0 @@ -import validate from '../../../src'; - -describe('Validate > Integration > Reset', () => { - - it('should clear errors messages from state and DOM, remove error classNames and attributes, remove errors from state', async () => { - document.body.innerHTML = ``; - // const mockState = { - // groups: { - // group1: { - // fields: Array.from(document.getElementsByName('group1')), - // errorMessages: ['This field is required'], - // valid: false - // }, - // group2: { - // fields: Array.from(document.getElementsByName('group2')), - // errorMessages: ['This field is required'], - // valid: false - // } - // }, - // errors: { - // group1: document.getElementById('test-error-node-1'), - // group2: document.getElementById('test-error-node-2') - // } - // }; - const [ validator ] = validate('.form'); - //Validate and set errors in state and DOM - await validator.validate(); - expect(validator.getState().groups.group1.valid).toEqual(false); - expect(validator.getState().groups.group2.valid).toEqual(false); - expect(validator.getState().errors.group1).toEqual(document.querySelector('#group1').previousElementSibling); - expect(validator.getState().errors.group2).toEqual(document.querySelector('#group2').previousElementSibling); - expect(validator.getState().groups.group1.fields[0].parentNode.classList.contains('is--invalid')).toEqual(true); - expect(validator.getState().groups.group2.fields[0].parentNode.classList.contains('is--invalid')).toEqual(true); - expect(validator.getState().groups.group1.fields[0].getAttribute('aria-invalid')).toEqual('true'); - expect(validator.getState().groups.group2.fields[0].getAttribute('aria-invalid')).toEqual('true'); - - //reset to remove errors from state and DOM - validator.getState().form.dispatchEvent(new Event('reset')); - expect(validator.getState().groups.group1.valid).toEqual(true); - expect(validator.getState().groups.group2.valid).toEqual(true); - expect(validator.getState().groups.group1.errorMessages).toEqual([]); - expect(validator.getState().groups.group2.errorMessages).toEqual([]); - expect(validator.getState().errors.group1).toBeUndefined(); - expect(validator.getState().errors.group2).toBeUndefined(); - expect(validator.getState().groups.group1.fields[0].parentNode.classList.contains('is--invalid')).toEqual(false); - expect(validator.getState().groups.group2.fields[0].parentNode.classList.contains('is--invalid')).toEqual(false); - expect(validator.getState().groups.group1.fields[0].getAttribute('aria-invalid')).toEqual(null); - expect(validator.getState().groups.group2.fields[0].getAttribute('aria-invalid')).toEqual(null); - }); - -}); diff --git a/packages/validate/__tests__/integration/submit/index.js b/packages/validate/__tests__/integration/submit/index.js deleted file mode 100644 index dd39eded..00000000 --- a/packages/validate/__tests__/integration/submit/index.js +++ /dev/null @@ -1,53 +0,0 @@ -import validate from '../../../src'; - -describe('Validate > Integration > Submit', () => { - - it('should call the submit function if validation passes', async () => { - expect.assertions(2); - document.body.innerHTML = ``; - - // const form = document.querySelector('form'); - // const button = document.querySelector('button'); - const submit = jest.fn(); - const [ validator ] = validate(document.querySelector('form'), { submit }); - await validator.validate({ target: true, preventDefault(){} }); - // button.click(); - expect(validator.getState().settings.submit).toEqual(submit); - expect(submit).toBeCalled(); - - }); - -}); - -describe('Validate > Integration > preSubmitHook', () => { - - it('should call the submit function if validation passes', async () => { - expect.assertions(2); - document.body.innerHTML = ``; - - // const form = document.querySelector('form'); - // const button = document.querySelector('button'); - const preSubmitHook = jest.fn(); - const [ validator ] = validate(document.querySelector('form'), { preSubmitHook }); - - await validator.validate({ target: true, preventDefault(){} }); - expect(validator.getState().settings.preSubmitHook).toEqual(preSubmitHook); - expect(preSubmitHook).toBeCalled(); - - }); - -}); \ No newline at end of file diff --git a/packages/validate/__tests__/jest/api/api.js b/packages/validate/__tests__/jest/api/api.js new file mode 100644 index 00000000..c3e46957 --- /dev/null +++ b/packages/validate/__tests__/jest/api/api.js @@ -0,0 +1,251 @@ +import validate from "../../../src"; +import { GROUP_ATTRIBUTE, DOTNET_CLASSNAMES } from "../../../src/lib/constants"; +import defaults from "../../../src/lib/defaults"; + +describe("Validate > Integration > API > addGroup", () => { + it("should add a validation group", async () => { + document.body.innerHTML = ``; + const input = document.querySelector("#group1-1"); + const validator = validate("form")[0]; + + expect(validator.getState().groups).toEqual({}); + input.setAttribute("required", "required"); + validator.addGroup([input]); + expect(validator.getState().groups).toEqual({ + group1: { + serverErrorNode: false, + validators: [{ type: "required" }], + fields: [input], + valid: false, + }, + }); + }); + + it("should return leave state unchanged if it cannot add the validation group", async () => { + document.body.innerHTML = ``; + const input = document.querySelector("#group1-1"); + const [validator] = validate("form"); + console.warn = jest.fn(); + + expect(validator.getState().groups).toEqual({}); + validator.addGroup([input]); + expect(validator.getState().groups).toEqual({}); + expect(console.warn).toHaveBeenCalled(); + }); +}); + +describe("Validate > Integration > API > removeGroup", () => { + it("should remove a validation group", async () => { + document.body.innerHTML = ``; + const input = document.querySelector("#group1-1"); + const validator = validate("form")[0]; + + expect(validator.getState().groups).toEqual({ + group1: { + serverErrorNode: false, + validators: [{ type: "required" }], + fields: [input], + valid: false, + }, + }); + input.removeAttribute("required"); + validator.removeGroup("group1"); + expect(validator.getState().groups).toEqual({}); + }); +}); + +describe("Validate > Integration > API > validateGroup", () => { + it("should validate an individual validation group when called", async () => { + document.body.innerHTML = ``; + const input = document.querySelector("#group1-1"); + const validator = validate("form")[0]; + await validator.validateGroup("group1"); + + expect(document.querySelector(`.${DOTNET_CLASSNAMES.ERROR}`).textContent).toEqual(defaults.messages.required()); + + input.value = "test"; + await validator.validateGroup("group1"); + expect(document.querySelector(`.${DOTNET_CLASSNAMES.ERROR}`)).toBeNull(); + }); +}); + +describe("Validate > Integration > API > addMethod", () => { + it("should add a validation method to a group", async () => { + document.body.innerHTML = ``; + const input = document.querySelector("#group1-1"); + const validator = validate("form")[0]; + + expect(validator.getState().groups).toEqual({ + group1: { + serverErrorNode: false, + validators: [{ type: "required" }], + fields: [input], + valid: false, + }, + }); + const method = () => false; + const message = "Custom error"; + validator.addMethod("group1", method, message); + + expect(validator.getState().groups).toEqual({ + group1: { + serverErrorNode: false, + validators: [{ type: "required" }, { type: "custom", method, message }], + fields: [input], + valid: false, + }, + }); + }); + + it("should not add a validation method if parameters are missing", async () => { + document.body.innerHTML = ``; + const input = document.querySelector("#group1-1"); + const validator = validate("form")[0]; + console.warn = jest.fn(); + + expect(validator.getState().groups).toEqual({ + group1: { + serverErrorNode: false, + validators: [{ type: "required" }], + fields: [input], + valid: false, + }, + }); + const method = () => false; + const message = "Custom error"; + + validator.addMethod(undefined, method, message); + expect(validator.getState().groups).toEqual({ + group1: { + serverErrorNode: false, + validators: [{ type: "required" }], + fields: [input], + valid: false, + }, + }); + expect(console.warn).toHaveBeenCalled(); + }); + + it("should not add a validation method if fields cannot be found", async () => { + document.body.innerHTML = ``; + const input = document.querySelector("#group1-1"); + const validator = validate("form")[0]; + + expect(validator.getState().groups).toEqual({ + groupX: { + serverErrorNode: false, + validators: [{ type: "required" }], + fields: [input], + valid: false, + }, + }); + const method = () => false; + const message = "Custom error"; + + validator.addMethod("neither-name-or-group-name", method, message); + expect(validator.getState().groups).toEqual({ + groupX: { + serverErrorNode: false, + validators: [{ type: "required" }], + fields: [input], + valid: false, + }, + }); + }); + + it("Should add a validation method when provided an array of fields and a new group name", () => { + document.body.innerHTML = ``; + const input = document.querySelector("#group1-1"); + const validator = validate("form")[0]; + + expect(validator.getState().groups).toEqual({ + group1: { + serverErrorNode: false, + validators: [{ type: "required" }], + fields: [input], + valid: false, + }, + }); + const method = () => false; + const message = "Custom error"; + validator.addMethod("CustomGroup", method, message, [input]); + + expect(validator.getState().groups).toEqual({ + group1: { + serverErrorNode: false, + validators: [{ type: "required" }], + fields: [input], + valid: false, + }, + CustomGroup: { + serverErrorNode: false, + validators: [{ type: "custom", method, message }], + fields: [input], + valid: false, + }, + }); + }); +}); diff --git a/packages/validate/__tests__/integration/api/validate/bypass-disabled.js b/packages/validate/__tests__/jest/api/validate/bypass-disabled.js similarity index 95% rename from packages/validate/__tests__/integration/api/validate/bypass-disabled.js rename to packages/validate/__tests__/jest/api/validate/bypass-disabled.js index cb15bc7a..8cf457d8 100644 --- a/packages/validate/__tests__/integration/api/validate/bypass-disabled.js +++ b/packages/validate/__tests__/jest/api/validate/bypass-disabled.js @@ -1,32 +1,30 @@ -import validate from '../../../../src'; - -describe('Validate > Integration > api > validate > bypass disabled fields', () => { - - it('should return true for disabled groups regardless of validation criteria and value', async () => { - expect.assertions(1); - document.body.innerHTML = ``; - const [ validator ] = validate('form'); - const validityState = await validator.validate(); - expect(validityState).toEqual(true); - }); -}); +import validate from '../../../../src'; + +describe('Validate > Integration > api > validate > bypass disabled fields', () => { + + it('should return true for disabled groups regardless of validation criteria and value', async () => { + expect.assertions(1); + document.body.innerHTML = ``; + const [ validator ] = validate('form'); + const validityState = await validator.validate(); + expect(validityState).toEqual(true); + }); +}); diff --git a/packages/validate/__tests__/integration/api/validate/dateISO.js b/packages/validate/__tests__/jest/api/validate/dateISO.js similarity index 82% rename from packages/validate/__tests__/integration/api/validate/dateISO.js rename to packages/validate/__tests__/jest/api/validate/dateISO.js index 82bfd613..11f5379b 100644 --- a/packages/validate/__tests__/integration/api/validate/dateISO.js +++ b/packages/validate/__tests__/jest/api/validate/dateISO.js @@ -1,55 +1,43 @@ -import validate from '../../../../src'; -import { DOTNET_CLASSNAMES } from '../../../../src/lib/constants'; - -describe('Validate > Integration > api > validate > dateISO', () => { - //return boolean validityState - //start realtimevalidation - //render errors - //focus on first invalid field - - //return boolean validityState - //submit form - - it('should validate a form based on the data-val dateISO validator returning false, starting realTimeValidation, focusing on first invalid field, and rendering an error message if a field is invalid', async () => { - expect.assertions(4); - document.body.innerHTML = ``; - const input = document.getElementById('group1-1'); - const label = document.getElementById('group1-1-label'); - const validator = validate('form')[0]; - const validityState = await validator.validate(); - expect(validityState).toEqual(false); - // realtimeValidation start - expect(validator.getState().realTimeValidation).toEqual(true); - // // focus on first invalid node - expect(document.activeElement).toEqual(input); - // // render error message - expect(document.querySelector(`.${DOTNET_CLASSNAMES.ERROR}`).textContent).toEqual('DateISO error message'); - }); - - it('should validate a form based on the data-val dateISO validator returning true if valid', async () => { - expect.assertions(1); - document.body.innerHTML = ``; - const validator = validate('form')[0]; - const validityState = await validator.validate(); - expect(validityState).toEqual(true); - }); - +import validate from '../../../../src'; +import { DOTNET_CLASSNAMES } from '../../../../src/lib/constants'; + +describe('Validate > Integration > api > validate > dateISO', () => { + it('should validate a form based on the data-val dateISO validator returning false, starting realTimeValidation, focusing on first invalid field, and rendering an error message if a field is invalid', async () => { + expect.assertions(4); + document.body.innerHTML = ``; + const input = document.getElementById('group1-1'); + const validator = validate('form')[0]; + const validityState = await validator.validate(); + expect(validityState).toEqual(false); + expect(validator.getState().realTimeValidation).toEqual(true); + expect(document.activeElement).toEqual(input); + expect(document.querySelector(`.${DOTNET_CLASSNAMES.ERROR}`).textContent).toEqual('DateISO error message'); + }); + + it('should validate a form based on the data-val dateISO validator returning true if valid', async () => { + expect.assertions(1); + document.body.innerHTML = ``; + const validator = validate('form')[0]; + const validityState = await validator.validate(); + expect(validityState).toEqual(true); + }); + }); \ No newline at end of file diff --git a/packages/validate/__tests__/integration/api/validate/digits.js b/packages/validate/__tests__/jest/api/validate/digits.js similarity index 82% rename from packages/validate/__tests__/integration/api/validate/digits.js rename to packages/validate/__tests__/jest/api/validate/digits.js index 1aea4065..2f78dabf 100644 --- a/packages/validate/__tests__/integration/api/validate/digits.js +++ b/packages/validate/__tests__/jest/api/validate/digits.js @@ -1,55 +1,43 @@ -import validate from '../../../../src'; -import { DOTNET_CLASSNAMES } from '../../../../src/lib/constants'; - -describe('Validate > Integration > api > validate > digits', () => { - //return boolean validityState - //start realtimevalidation - //render errors - //focus on first invalid field - - //return boolean validityState - //submit form - - it('should validate a form based on the data-val digits validator returning false, starting realTimeValidation, focusing on first invalid field, and rendering an error message if a field is invalid', async () => { - expect.assertions(4); - document.body.innerHTML = ``; - const input = document.getElementById('group1'); - const label = document.getElementById('group1-1-label'); - const validator = validate('form')[0]; - const validityState = await validator.validate(); - expect(validityState).toEqual(false); - // realtimeValidation start - expect(validator.getState().realTimeValidation).toEqual(true); - // // focus on first invalid node - expect(document.activeElement).toEqual(input); - // // render error message - expect(document.querySelector(`.${DOTNET_CLASSNAMES.ERROR}`).textContent).toEqual('Digits error message'); - }); - - it('should validate a form based on the data-val digits validator returning true if valid', async () => { - expect.assertions(1); - document.body.innerHTML = ``; - const validator = validate('form')[0]; - const validityState = await validator.validate(); - expect(validityState).toEqual(true); - }); - +import validate from '../../../../src'; +import { DOTNET_CLASSNAMES } from '../../../../src/lib/constants'; + +describe('Validate > Integration > api > validate > digits', () => { + it('should validate a form based on the data-val digits validator returning false, starting realTimeValidation, focusing on first invalid field, and rendering an error message if a field is invalid', async () => { + expect.assertions(4); + document.body.innerHTML = ``; + const input = document.getElementById('group1'); + const validator = validate('form')[0]; + const validityState = await validator.validate(); + expect(validityState).toEqual(false); + expect(validator.getState().realTimeValidation).toEqual(true); + expect(document.activeElement).toEqual(input); + expect(document.querySelector(`.${DOTNET_CLASSNAMES.ERROR}`).textContent).toEqual('Digits error message'); + }); + + it('should validate a form based on the data-val digits validator returning true if valid', async () => { + expect.assertions(1); + document.body.innerHTML = ``; + const validator = validate('form')[0]; + const validityState = await validator.validate(); + expect(validityState).toEqual(true); + }); + }); \ No newline at end of file diff --git a/packages/validate/__tests__/integration/api/validate/email.js b/packages/validate/__tests__/jest/api/validate/email.js similarity index 84% rename from packages/validate/__tests__/integration/api/validate/email.js rename to packages/validate/__tests__/jest/api/validate/email.js index b5ca1947..4536e7a4 100644 --- a/packages/validate/__tests__/integration/api/validate/email.js +++ b/packages/validate/__tests__/jest/api/validate/email.js @@ -1,98 +1,79 @@ -import validate from '../../../../src'; -import { DOTNET_CLASSNAMES } from '../../../../src/lib/constants'; -import defaults from '../../../../src/lib/defaults'; - -describe('Validate > Integration > api > validate > email', () => { - //return boolean validityState - //start realtimevalidation - //render errors - //focus on first invalid field - - //return boolean validityState - //submit form - - it('should validate a form based on the HTML5 email validator returning false, starting realTimeValidation, focusing on first invalid field, and rendering an error message if a field is invalid', async () => { - expect.assertions(4); - document.body.innerHTML = ``; - const input = document.getElementById('group1'); - const label = document.getElementById('group1-label'); - const validator = validate('form')[0]; - const validityState = await validator.validate(); - // //validityState - expect(validityState).toEqual(false); - // //realtimeValidation start - expect(validator.getState().realTimeValidation).toEqual(true); - // //focus on first invalid node - expect(document.activeElement).toEqual(input); - //render error message - expect(document.querySelector(`.${DOTNET_CLASSNAMES.ERROR}`).textContent).toEqual(defaults.messages.email()); - }); - - it('should validate a form based on the HTML5 email validator returning true if valid', async () => { - expect.assertions(1); - document.body.innerHTML = ``; - const validator = validate('form')[0]; - const validityState = await validator.validate(); - // //validityState - expect(validityState).toEqual(true); - }); - - it('should validate a form based on the data-val email validator returning false, starting realTimeValidation, ', async () => { - expect.assertions(4); - document.body.innerHTML = ``; - const input = document.getElementById('group2'); - const validator = validate('form')[0]; - const validityState = await validator.validate(); - //validityState - expect(validityState).toEqual(false); - //realtimeValidation start - expect(validator.getState().realTimeValidation).toEqual(true); - //focus on firstinvalid node - expect(document.activeElement).toEqual(input); - //render error message - expect(document.querySelector(`.${DOTNET_CLASSNAMES.ERROR}`).textContent).toEqual('Email error message'); - - }); - - it('should validate a form based on the data-val email validator returning true if valid', async () => { - expect.assertions(1); - document.body.innerHTML = ``; - const validator = validate('form')[0]; - const validityState = await validator.validate(); - // //validityState - expect(validityState).toEqual(true); - }); -}); +import validate from '../../../../src'; +import { DOTNET_CLASSNAMES } from '../../../../src/lib/constants'; +import defaults from '../../../../src/lib/defaults'; + +describe('Validate > Integration > api > validate > email', () => { + it('should validate a form based on the HTML5 email validator returning false, starting realTimeValidation, focusing on first invalid field, and rendering an error message if a field is invalid', async () => { + expect.assertions(4); + document.body.innerHTML = ``; + const input = document.getElementById('group1'); + const validator = validate('form')[0]; + const validityState = await validator.validate(); + expect(validityState).toEqual(false); + expect(validator.getState().realTimeValidation).toEqual(true); + expect(document.activeElement).toEqual(input); + expect(document.querySelector(`.${DOTNET_CLASSNAMES.ERROR}`).textContent).toEqual(defaults.messages.email()); + }); + + it('should validate a form based on the HTML5 email validator returning true if valid', async () => { + expect.assertions(1); + document.body.innerHTML = ``; + const validator = validate('form')[0]; + const validityState = await validator.validate(); + expect(validityState).toEqual(true); + }); + + it('should validate a form based on the data-val email validator returning false, starting realTimeValidation, ', async () => { + expect.assertions(4); + document.body.innerHTML = ``; + const input = document.getElementById('group2'); + const validator = validate('form')[0]; + const validityState = await validator.validate(); + expect(validityState).toEqual(false); + expect(validator.getState().realTimeValidation).toEqual(true); + expect(document.activeElement).toEqual(input); + expect(document.querySelector(`.${DOTNET_CLASSNAMES.ERROR}`).textContent).toEqual('Email error message'); + + }); + + it('should validate a form based on the data-val email validator returning true if valid', async () => { + expect.assertions(1); + document.body.innerHTML = ``; + const validator = validate('form')[0]; + const validityState = await validator.validate(); + expect(validityState).toEqual(true); + }); +}); diff --git a/packages/validate/__tests__/integration/api/validate/equalto.js b/packages/validate/__tests__/jest/api/validate/equalto.js similarity index 90% rename from packages/validate/__tests__/integration/api/validate/equalto.js rename to packages/validate/__tests__/jest/api/validate/equalto.js index ce8a1095..a75136fa 100644 --- a/packages/validate/__tests__/integration/api/validate/equalto.js +++ b/packages/validate/__tests__/jest/api/validate/equalto.js @@ -1,60 +1,55 @@ -import library from '../../../../src'; -import { validate, assembleValidationGroup } from '../../../../src/lib/validator'; -import { DOTNET_CLASSNAMES } from '../../../../src/lib/constants'; - -describe('Validate > Integration > api > validate > equalto', () => { - - it('should validate a form based on the data-val equalto validator returning false, starting realTimeValidation, focusing on first invalid field, and rendering an error message if a field is invalid', async () => { - expect.assertions(4); - document.body.innerHTML = ``; - const input = document.querySelector('#DoubleConfirmEmail'); - // const label = document.getElementById('DoubleConfirmEmail-label'); - const validator = library('form')[0]; - const validityState = await validator.validate(); - // //validityState - expect(validityState).toEqual(false); - // //realtimeValidation start - expect(validator.getState().realTimeValidation).toEqual(true); - // //focus on first invalid node - expect(document.activeElement).toEqual(input); - //render error message - expect(document.querySelector(`.${DOTNET_CLASSNAMES.ERROR}`).textContent).toEqual('Equalto error message'); - }); - - it('should validate a form based on the data-val equalto validator returning true if valid', async () => { - expect.assertions(1); - document.body.innerHTML = ` - - - `; - const input = document.querySelector('#DoubleConfirmEmail'); - const group = assembleValidationGroup({}, input).DoubleConfirmEmail; - expect(await validate(group, group.validators[0])).toEqual(false); - }); - +import library from '../../../../src'; +import { DOTNET_CLASSNAMES } from '../../../../src/lib/constants'; +import { validate, assembleValidationGroup } from '../../../../src/lib/validator'; + +describe('Validate > Integration > api > validate > equalto', () => { + + it('should validate a form based on the data-val equalto validator returning false, starting realTimeValidation, focusing on first invalid field, and rendering an error message if a field is invalid', async () => { + expect.assertions(4); + document.body.innerHTML = ``; + const input = document.querySelector('#DoubleConfirmEmail'); + const validator = library('form')[0]; + const validityState = await validator.validate(); + expect(validityState).toEqual(false); + expect(validator.getState().realTimeValidation).toEqual(true); + expect(document.activeElement).toEqual(input); + expect(document.querySelector(`.${DOTNET_CLASSNAMES.ERROR}`).textContent).toEqual('Equalto error message'); + }); + + it('should validate a form based on the data-val equalto validator returning true if valid', async () => { + expect.assertions(1); + document.body.innerHTML = ` + + + `; + const input = document.querySelector('#DoubleConfirmEmail'); + const group = assembleValidationGroup({}, input).DoubleConfirmEmail; + expect(await validate(group, group.validators[0])).toEqual(false); + }); + }); \ No newline at end of file diff --git a/packages/validate/__tests__/integration/api/validate/length.js b/packages/validate/__tests__/jest/api/validate/length.js similarity index 90% rename from packages/validate/__tests__/integration/api/validate/length.js rename to packages/validate/__tests__/jest/api/validate/length.js index 667b1616..cb958a70 100644 --- a/packages/validate/__tests__/integration/api/validate/length.js +++ b/packages/validate/__tests__/jest/api/validate/length.js @@ -1,52 +1,48 @@ -import validate from '../../../../src'; -import { DOTNET_CLASSNAMES } from '../../../../src/lib/constants'; - -describe('Validate > Integration > api > validate > length', () => { - - it('should validate a form based on the data-val length validator returning false, starting realTimeValidation, focusing on first invalid field, and rendering an error message if a field is outwith the min and max length range', async () => { - expect.assertions(4); - document.body.innerHTML = ``; - const input = document.getElementById('group1'); - const label = document.getElementById('group1-1-label'); - const validator = validate('form')[0]; - const validityState = await validator.validate(); - expect(validityState).toEqual(false); - // realtimeValidation start - expect(validator.getState().realTimeValidation).toEqual(true); - // // focus on first invalid node - expect(document.activeElement).toEqual(input); - // // render error message - expect(document.querySelector(`.${DOTNET_CLASSNAMES.ERROR}`).textContent).toEqual('Length error message'); - }); - - it('should validate a form based on the data-val length validator returning true if within the min and max length range', async () => { - expect.assertions(1); - document.body.innerHTML = ``; - const validator = validate('form')[0]; - const validityState = await validator.validate(); - expect(validityState).toEqual(true); - }); - +import validate from '../../../../src'; +import { DOTNET_CLASSNAMES } from '../../../../src/lib/constants'; + +describe('Validate > Integration > api > validate > length', () => { + + it('should validate a form based on the data-val length validator returning false, starting realTimeValidation, focusing on first invalid field, and rendering an error message if a field is outwith the min and max length range', async () => { + expect.assertions(4); + document.body.innerHTML = ``; + const input = document.getElementById('group1'); + const validator = validate('form')[0]; + const validityState = await validator.validate(); + expect(validityState).toEqual(false); + expect(validator.getState().realTimeValidation).toEqual(true); + expect(document.activeElement).toEqual(input); + expect(document.querySelector(`.${DOTNET_CLASSNAMES.ERROR}`).textContent).toEqual('Length error message'); + }); + + it('should validate a form based on the data-val length validator returning true if within the min and max length range', async () => { + expect.assertions(1); + document.body.innerHTML = ``; + const validator = validate('form')[0]; + const validityState = await validator.validate(); + expect(validityState).toEqual(true); + }); + }); \ No newline at end of file diff --git a/packages/validate/__tests__/integration/api/validate/max.js b/packages/validate/__tests__/jest/api/validate/max.js similarity index 89% rename from packages/validate/__tests__/integration/api/validate/max.js rename to packages/validate/__tests__/jest/api/validate/max.js index 3421a89f..11a9d57e 100644 --- a/packages/validate/__tests__/integration/api/validate/max.js +++ b/packages/validate/__tests__/jest/api/validate/max.js @@ -1,91 +1,83 @@ -import validate from '../../../../src'; -import { DOTNET_CLASSNAMES } from '../../../../src/lib/constants'; -import defaults from '../../../../src/lib/defaults'; - -describe('Validate > Integration > api > validate > max', () => { - - it('should validate a form based on the HTML5 max validator returning false, starting realTimeValidation, focusing on first invalid field, and rendering an error message if a field is invalid', async () => { - expect.assertions(4); - document.body.innerHTML = ``; - const input = document.getElementById('group1'); - const label = document.getElementById('group1-label'); - const validator = validate('form')[0]; - const validityState = await validator.validate(); - expect(validityState).toEqual(false); - // realtimeValidation start - expect(validator.getState().realTimeValidation).toEqual(true); - // // focus on first invalid node - expect(document.activeElement).toEqual(input); - // // render error message - expect(document.querySelector(`.${DOTNET_CLASSNAMES.ERROR}`).textContent).toEqual(defaults.messages.max({ max: 2 })); - }); - - it('should validate a form based on the data-val equalto validator returning true if valid', async () => { - expect.assertions(4); - document.body.innerHTML = ``; - const input = document.getElementById('group1'); - const label = document.getElementById('group1-label'); - const validator = validate('form')[0]; - const validityState = await validator.validate(); - expect(validityState).toEqual(false); - // realtimeValidation start - expect(validator.getState().realTimeValidation).toEqual(true); - // // focus on first invalid node - expect(document.activeElement).toEqual(input); - // // render error message - expect(document.querySelector(`.${DOTNET_CLASSNAMES.ERROR}`).textContent).toEqual('Max error message'); - }); - - it('should validate a form based on the data-val max validator returning false, starting realTimeValidation, focusing on first invalid field, and rendering an error message if a field is invalid', async () => { - expect.assertions(1); - document.body.innerHTML = ``; - const validator = validate('form')[0]; - const validityState = await validator.validate(); - expect(validityState).toEqual(true); - }); - - it('should validate a form based on the data-val max validator returning true if valid', async () => { - expect.assertions(1); - document.body.innerHTML = ``; - const validator = validate('form')[0]; - const validityState = await validator.validate(); - expect(validityState).toEqual(true); - }); - +import validate from '../../../../src'; +import { DOTNET_CLASSNAMES } from '../../../../src/lib/constants'; +import defaults from '../../../../src/lib/defaults'; + +describe('Validate > Integration > api > validate > max', () => { + + it('should validate a form based on the HTML5 max validator returning false, starting realTimeValidation, focusing on first invalid field, and rendering an error message if a field is invalid', async () => { + expect.assertions(4); + document.body.innerHTML = ``; + const input = document.getElementById('group1'); + const validator = validate('form')[0]; + const validityState = await validator.validate(); + expect(validityState).toEqual(false); + expect(validator.getState().realTimeValidation).toEqual(true); + expect(document.activeElement).toEqual(input); + expect(document.querySelector(`.${DOTNET_CLASSNAMES.ERROR}`).textContent).toEqual(defaults.messages.max({ max: 2 })); + }); + + it('should validate a form based on the data-val equalto validator returning true if valid', async () => { + expect.assertions(4); + document.body.innerHTML = ``; + const input = document.getElementById('group1'); + const validator = validate('form')[0]; + const validityState = await validator.validate(); + expect(validityState).toEqual(false); + expect(validator.getState().realTimeValidation).toEqual(true); + expect(document.activeElement).toEqual(input); + expect(document.querySelector(`.${DOTNET_CLASSNAMES.ERROR}`).textContent).toEqual('Max error message'); + }); + + it('should validate a form based on the data-val max validator returning false, starting realTimeValidation, focusing on first invalid field, and rendering an error message if a field is invalid', async () => { + expect.assertions(1); + document.body.innerHTML = ``; + const validator = validate('form')[0]; + const validityState = await validator.validate(); + expect(validityState).toEqual(true); + }); + + it('should validate a form based on the data-val max validator returning true if valid', async () => { + expect.assertions(1); + document.body.innerHTML = ``; + const validator = validate('form')[0]; + const validityState = await validator.validate(); + expect(validityState).toEqual(true); + }); + }); \ No newline at end of file diff --git a/packages/validate/__tests__/integration/api/validate/maxlength.js b/packages/validate/__tests__/jest/api/validate/maxlength.js similarity index 89% rename from packages/validate/__tests__/integration/api/validate/maxlength.js rename to packages/validate/__tests__/jest/api/validate/maxlength.js index 424de9ae..13f9584d 100644 --- a/packages/validate/__tests__/integration/api/validate/maxlength.js +++ b/packages/validate/__tests__/jest/api/validate/maxlength.js @@ -1,91 +1,83 @@ -import validate from '../../../../src'; -import { DOTNET_CLASSNAMES } from '../../../../src/lib/constants'; -import defaults from '../../../../src/lib/defaults'; - -describe('Validate > Integration > api > validate > maxlength', () => { - - it('should validate a form based on the HTML5 maxlength validator returning false, starting realTimeValidation, focusing on first invalid field, and rendering an error message if a field is invalid', async () => { - expect.assertions(4); - document.body.innerHTML = ``; - const input = document.getElementById('group1'); - const label = document.getElementById('group1-label'); - const validator = validate('form')[0]; - const validityState = await validator.validate(); - expect(validityState).toEqual(false); - // realtimeValidation start - expect(validator.getState().realTimeValidation).toEqual(true); - // // focus on first invalid node - expect(document.activeElement).toEqual(input); - // // render error message - expect(document.querySelector(`.${DOTNET_CLASSNAMES.ERROR}`).textContent).toEqual(defaults.messages.maxlength({ max: 5 })); - }); - - it('should validate a form based on the data-val maxlength validator returning false, starting realTimeValidation, focusing on first invalid field, and rendering an error message if a field is invalid', async () => { - expect.assertions(4); - document.body.innerHTML = ``; - const input = document.getElementById('group1'); - const label = document.getElementById('group1-label'); - const validator = validate('form')[0]; - const validityState = await validator.validate(); - expect(validityState).toEqual(false); - // realtimeValidation start - expect(validator.getState().realTimeValidation).toEqual(true); - // // focus on first invalid node - expect(document.activeElement).toEqual(input); - // // render error message - expect(document.querySelector(`.${DOTNET_CLASSNAMES.ERROR}`).textContent).toEqual('Maxlength error message'); - }); - - it('should validate a form based on the HTML5 maxlength validator returning true if valid', async () => { - expect.assertions(1); - document.body.innerHTML = ``; - const validator = validate('form')[0]; - const validityState = await validator.validate(); - expect(validityState).toEqual(true); - }); - - it('should validate a form based on the data-val maxlength validator returning true if valid', async () => { - expect.assertions(1); - document.body.innerHTML = ``; - const validator = validate('form')[0]; - const validityState = await validator.validate(); - expect(validityState).toEqual(true); - }); - +import validate from '../../../../src'; +import { DOTNET_CLASSNAMES } from '../../../../src/lib/constants'; +import defaults from '../../../../src/lib/defaults'; + +describe('Validate > Integration > api > validate > maxlength', () => { + + it('should validate a form based on the HTML5 maxlength validator returning false, starting realTimeValidation, focusing on first invalid field, and rendering an error message if a field is invalid', async () => { + expect.assertions(4); + document.body.innerHTML = ``; + const input = document.getElementById('group1'); + const validator = validate('form')[0]; + const validityState = await validator.validate(); + expect(validityState).toEqual(false); + expect(validator.getState().realTimeValidation).toEqual(true); + expect(document.activeElement).toEqual(input); + expect(document.querySelector(`.${DOTNET_CLASSNAMES.ERROR}`).textContent).toEqual(defaults.messages.maxlength({ max: 5 })); + }); + + it('should validate a form based on the data-val maxlength validator returning false, starting realTimeValidation, focusing on first invalid field, and rendering an error message if a field is invalid', async () => { + expect.assertions(4); + document.body.innerHTML = ``; + const input = document.getElementById('group1'); + const validator = validate('form')[0]; + const validityState = await validator.validate(); + expect(validityState).toEqual(false); + expect(validator.getState().realTimeValidation).toEqual(true); + expect(document.activeElement).toEqual(input); + expect(document.querySelector(`.${DOTNET_CLASSNAMES.ERROR}`).textContent).toEqual('Maxlength error message'); + }); + + it('should validate a form based on the HTML5 maxlength validator returning true if valid', async () => { + expect.assertions(1); + document.body.innerHTML = ``; + const validator = validate('form')[0]; + const validityState = await validator.validate(); + expect(validityState).toEqual(true); + }); + + it('should validate a form based on the data-val maxlength validator returning true if valid', async () => { + expect.assertions(1); + document.body.innerHTML = ``; + const validator = validate('form')[0]; + const validityState = await validator.validate(); + expect(validityState).toEqual(true); + }); + }); \ No newline at end of file diff --git a/packages/validate/__tests__/integration/api/validate/min.js b/packages/validate/__tests__/jest/api/validate/min.js similarity index 91% rename from packages/validate/__tests__/integration/api/validate/min.js rename to packages/validate/__tests__/jest/api/validate/min.js index 4235a760..0a8049f5 100644 --- a/packages/validate/__tests__/integration/api/validate/min.js +++ b/packages/validate/__tests__/jest/api/validate/min.js @@ -1,89 +1,83 @@ -import validate from '../../../../src'; -import { DOTNET_CLASSNAMES } from '../../../../src/lib/constants'; -import defaults from '../../../../src/lib/defaults'; - -describe('Validate > Integration > api > validate > min', () => { - - it('should validate a form based on the HTML5 min validator returning false, starting realTimeValidation, focusing on first invalid field, and rendering an error message if a field is invalid', async () => { - expect.assertions(4); - document.body.innerHTML = ``; - const input = document.getElementById('group1'); - const validator = validate('form')[0]; - const validityState = await validator.validate(); - expect(validityState).toEqual(false); - // realtimeValidation start - expect(validator.getState().realTimeValidation).toEqual(true); - // // focus on first invalid node - expect(document.activeElement).toEqual(input); - // // render error message - expect(document.querySelector(`.${DOTNET_CLASSNAMES.ERROR}`).textContent).toEqual(defaults.messages.min({ min: 2 })); - }); - - it('should validate a form based on the data-val min validator returning false, starting realTimeValidation, focusing on first invalid field, and rendering an error message if a field is invalid', async () => { - expect.assertions(4); - document.body.innerHTML = ``; - const input = document.getElementById('group1'); - const validator = validate('form')[0]; - const validityState = await validator.validate(); - expect(validityState).toEqual(false); - // realtimeValidation start - expect(validator.getState().realTimeValidation).toEqual(true); - // // focus on first invalid node - expect(document.activeElement).toEqual(input); - // // render error message - expect(document.querySelector(`.${DOTNET_CLASSNAMES.ERROR}`).textContent).toEqual('Min error message'); - }); - - it('should validate a form based on the HTML5 min validator returning true if valid', async () => { - expect.assertions(1); - document.body.innerHTML = ``; - const validator = validate('form')[0]; - const validityState = await validator.validate(); - expect(validityState).toEqual(true); - }); - - it('should validate a form based on the data-val min validator returning true if valid', async () => { - expect.assertions(1); - document.body.innerHTML = ``; - const validator = validate('form')[0]; - const validityState = await validator.validate(); - expect(validityState).toEqual(true); - }); - +import validate from '../../../../src'; +import { DOTNET_CLASSNAMES } from '../../../../src/lib/constants'; +import defaults from '../../../../src/lib/defaults'; + +describe('Validate > Integration > api > validate > min', () => { + + it('should validate a form based on the HTML5 min validator returning false, starting realTimeValidation, focusing on first invalid field, and rendering an error message if a field is invalid', async () => { + expect.assertions(4); + document.body.innerHTML = ``; + const input = document.getElementById('group1'); + const validator = validate('form')[0]; + const validityState = await validator.validate(); + expect(validityState).toEqual(false); + expect(validator.getState().realTimeValidation).toEqual(true); + expect(document.activeElement).toEqual(input); + expect(document.querySelector(`.${DOTNET_CLASSNAMES.ERROR}`).textContent).toEqual(defaults.messages.min({ min: 2 })); + }); + + it('should validate a form based on the data-val min validator returning false, starting realTimeValidation, focusing on first invalid field, and rendering an error message if a field is invalid', async () => { + expect.assertions(4); + document.body.innerHTML = ``; + const input = document.getElementById('group1'); + const validator = validate('form')[0]; + const validityState = await validator.validate(); + expect(validityState).toEqual(false); + expect(validator.getState().realTimeValidation).toEqual(true); + expect(document.activeElement).toEqual(input); + expect(document.querySelector(`.${DOTNET_CLASSNAMES.ERROR}`).textContent).toEqual('Min error message'); + }); + + it('should validate a form based on the HTML5 min validator returning true if valid', async () => { + expect.assertions(1); + document.body.innerHTML = ``; + const validator = validate('form')[0]; + const validityState = await validator.validate(); + expect(validityState).toEqual(true); + }); + + it('should validate a form based on the data-val min validator returning true if valid', async () => { + expect.assertions(1); + document.body.innerHTML = ``; + const validator = validate('form')[0]; + const validityState = await validator.validate(); + expect(validityState).toEqual(true); + }); + }); \ No newline at end of file diff --git a/packages/validate/__tests__/integration/api/validate/minlength.js b/packages/validate/__tests__/jest/api/validate/minlength.js similarity index 89% rename from packages/validate/__tests__/integration/api/validate/minlength.js rename to packages/validate/__tests__/jest/api/validate/minlength.js index 76caf568..92980b38 100644 --- a/packages/validate/__tests__/integration/api/validate/minlength.js +++ b/packages/validate/__tests__/jest/api/validate/minlength.js @@ -1,91 +1,83 @@ -import validate from '../../../../src'; -import { DOTNET_CLASSNAMES } from '../../../../src/lib/constants'; -import defaults from '../../../../src/lib/defaults'; - -describe('Validate > Integration > api > validate > minlength', () => { - - it('should validate a form based on the HTML5 minlength validator returning false, starting realTimeValidation, focusing on first invalid field, and rendering an error message if a field is invalid', async () => { - expect.assertions(4); - document.body.innerHTML = ``; - const input = document.getElementById('group1'); - const label = document.getElementById('group1-label'); - const validator = validate('form')[0]; - const validityState = await validator.validate(); - expect(validityState).toEqual(false); - // realtimeValidation start - expect(validator.getState().realTimeValidation).toEqual(true); - // // focus on first invalid node - expect(document.activeElement).toEqual(input); - // // render error message - expect(document.querySelector(`.${DOTNET_CLASSNAMES.ERROR}`).textContent).toEqual(defaults.messages.minlength({ min: 3 })); - }); - - it('should validate a form based on the data-val minlength validator returning false, starting realTimeValidation, focusing on first invalid field, and rendering an error message if a field is invalid', async () => { - expect.assertions(4); - document.body.innerHTML = ``; - const input = document.getElementById('group1'); - const label = document.getElementById('group1-label'); - const validator = validate('form')[0]; - const validityState = await validator.validate(); - expect(validityState).toEqual(false); - // realtimeValidation start - expect(validator.getState().realTimeValidation).toEqual(true); - // // focus on first invalid node - expect(document.activeElement).toEqual(input); - // // render error message - expect(document.querySelector(`.${DOTNET_CLASSNAMES.ERROR}`).textContent).toEqual('Minlength error message'); - }); - - it('should validate a form based on the HTML5 minlength validator returning true if valid', async () => { - expect.assertions(1); - document.body.innerHTML = ``; - const validator = validate('form')[0]; - const validityState = await validator.validate(); - expect(validityState).toEqual(true); - }); - - it('should validate a form based on the data-val minlength validator returning true if valid', async () => { - expect.assertions(1); - document.body.innerHTML = ``; - const validator = validate('form')[0]; - const validityState = await validator.validate(); - expect(validityState).toEqual(true); - }); - +import validate from '../../../../src'; +import { DOTNET_CLASSNAMES } from '../../../../src/lib/constants'; +import defaults from '../../../../src/lib/defaults'; + +describe('Validate > Integration > api > validate > minlength', () => { + + it('should validate a form based on the HTML5 minlength validator returning false, starting realTimeValidation, focusing on first invalid field, and rendering an error message if a field is invalid', async () => { + expect.assertions(4); + document.body.innerHTML = ``; + const input = document.getElementById('group1'); + const validator = validate('form')[0]; + const validityState = await validator.validate(); + expect(validityState).toEqual(false); + expect(validator.getState().realTimeValidation).toEqual(true); + expect(document.activeElement).toEqual(input); + expect(document.querySelector(`.${DOTNET_CLASSNAMES.ERROR}`).textContent).toEqual(defaults.messages.minlength({ min: 3 })); + }); + + it('should validate a form based on the data-val minlength validator returning false, starting realTimeValidation, focusing on first invalid field, and rendering an error message if a field is invalid', async () => { + expect.assertions(4); + document.body.innerHTML = ``; + const input = document.getElementById('group1'); + const validator = validate('form')[0]; + const validityState = await validator.validate(); + expect(validityState).toEqual(false); + expect(validator.getState().realTimeValidation).toEqual(true); + expect(document.activeElement).toEqual(input); + expect(document.querySelector(`.${DOTNET_CLASSNAMES.ERROR}`).textContent).toEqual('Minlength error message'); + }); + + it('should validate a form based on the HTML5 minlength validator returning true if valid', async () => { + expect.assertions(1); + document.body.innerHTML = ``; + const validator = validate('form')[0]; + const validityState = await validator.validate(); + expect(validityState).toEqual(true); + }); + + it('should validate a form based on the data-val minlength validator returning true if valid', async () => { + expect.assertions(1); + document.body.innerHTML = ``; + const validator = validate('form')[0]; + const validityState = await validator.validate(); + expect(validityState).toEqual(true); + }); + }); \ No newline at end of file diff --git a/packages/validate/__tests__/integration/api/validate/number.js b/packages/validate/__tests__/jest/api/validate/number.js similarity index 89% rename from packages/validate/__tests__/integration/api/validate/number.js rename to packages/validate/__tests__/jest/api/validate/number.js index e215ff89..40607102 100644 --- a/packages/validate/__tests__/integration/api/validate/number.js +++ b/packages/validate/__tests__/jest/api/validate/number.js @@ -1,64 +1,59 @@ -import validate from '../../../../src'; -import { DOTNET_CLASSNAMES } from '../../../../src/lib/constants'; -import defaults from '../../../../src/lib/defaults'; - -describe('Validate > Integration > api > validate > number', () => { - - it('should validate a form based on the HTML5 number validator returning true if valid', async () => { - expect.assertions(1); - document.body.innerHTML = ``; - const validator = validate('form')[0]; - const validityState = await validator.validate(); - expect(validityState).toEqual(true); - }); - - it('should validate a form based on the data-val number validator returning false, starting realTimeValidation, focusing on first invalid field, and rendering an error message if a field is invalid', async () => { - expect.assertions(4); - document.body.innerHTML = ``; - const input = document.getElementById('group1'); - const label = document.getElementById('group1-label'); - const validator = validate('form')[0]; - const validityState = await validator.validate(); - expect(validityState).toEqual(false); - // realtimeValidation start - expect(validator.getState().realTimeValidation).toEqual(true); - // // focus on first invalid node - expect(document.activeElement).toEqual(input); - // // render error message - expect(document.querySelector(`.${DOTNET_CLASSNAMES.ERROR}`).textContent).toEqual('Number error message'); - }); - - it('should validate a form based on the data-val number validator returning true if valid', async () => { - expect.assertions(1); - document.body.innerHTML = ``; - const validator = validate('form')[0]; - const validityState = await validator.validate(); - expect(validityState).toEqual(true); - }); - +import validate from '../../../../src'; +import { DOTNET_CLASSNAMES } from '../../../../src/lib/constants'; + +describe('Validate > Integration > api > validate > number', () => { + + it('should validate a form based on the HTML5 number validator returning true if valid', async () => { + expect.assertions(1); + document.body.innerHTML = ``; + const validator = validate('form')[0]; + const validityState = await validator.validate(); + expect(validityState).toEqual(true); + }); + + it('should validate a form based on the data-val number validator returning false, starting realTimeValidation, focusing on first invalid field, and rendering an error message if a field is invalid', async () => { + expect.assertions(4); + document.body.innerHTML = ``; + const input = document.getElementById('group1'); + const validator = validate('form')[0]; + const validityState = await validator.validate(); + expect(validityState).toEqual(false); + expect(validator.getState().realTimeValidation).toEqual(true); + expect(document.activeElement).toEqual(input); + expect(document.querySelector(`.${DOTNET_CLASSNAMES.ERROR}`).textContent).toEqual('Number error message'); + }); + + it('should validate a form based on the data-val number validator returning true if valid', async () => { + expect.assertions(1); + document.body.innerHTML = ``; + const validator = validate('form')[0]; + const validityState = await validator.validate(); + expect(validityState).toEqual(true); + }); + }); \ No newline at end of file diff --git a/packages/validate/__tests__/integration/api/validate/range.js b/packages/validate/__tests__/jest/api/validate/range.js similarity index 88% rename from packages/validate/__tests__/integration/api/validate/range.js rename to packages/validate/__tests__/jest/api/validate/range.js index cd76477a..b1aa321c 100644 --- a/packages/validate/__tests__/integration/api/validate/range.js +++ b/packages/validate/__tests__/jest/api/validate/range.js @@ -1,143 +1,128 @@ -import validate from '../../../../src'; -import { DOTNET_CLASSNAMES } from '../../../../src/lib/constants'; - -describe('Validate > Integration > api > validate > equalto', () => { - - it('should validate a form based on the data-val range validator returning false, starting realTimeValidation, focusing on first invalid field, and rendering an error message if the value is out of range', async () => { - expect.assertions(4); - document.body.innerHTML = ``; - const input = document.querySelector('#group1'); - const label = document.getElementById('group1-label'); - const validator = validate('form')[0]; - const validityState = await validator.validate(); - // //validityState - expect(validityState).toEqual(false); - // //realtimeValidation start - expect(validator.getState().realTimeValidation).toEqual(true); - // //focus on first invalid node - expect(document.activeElement).toEqual(input); - //render error message - expect(document.querySelector(`.${DOTNET_CLASSNAMES.ERROR}`).textContent).toEqual('Range error message'); - }); - - it('should validate a form based on the data-val range validator returning true if value > min with no max', async () => { - expect.assertions(1); - document.body.innerHTML = ``; - const validator = validate('form')[0]; - const validityState = await validator.validate(); - expect(validityState).toEqual(true); - }); - - it('should validate a form based on the data-val range validator returning false, starting realTimeValidation, focusing on first invalid field, and rendering an error message if the value <= min with no max', async () => { - expect.assertions(4); - document.body.innerHTML = ``; - const input = document.querySelector('#group1'); - const label = document.getElementById('group1-label'); - const validator = validate('form')[0]; - const validityState = await validator.validate(); - // //validityState - expect(validityState).toEqual(false); - // //realtimeValidation start - expect(validator.getState().realTimeValidation).toEqual(true); - // //focus on first invalid node - expect(document.activeElement).toEqual(input); - //render error message - expect(document.querySelector(`.${DOTNET_CLASSNAMES.ERROR}`).textContent).toEqual('Range error message'); - }); - - it('should validate a form based on the data-val range validator returning true if value <= max with no min', async () => { - expect.assertions(1); - document.body.innerHTML = ``; - const validator = validate('form')[0]; - const validityState = await validator.validate(); - expect(validityState).toEqual(true); - }); - - it('should validate a form based on the data-val range validator returning false, starting realTimeValidation, focusing on first invalid field, and rendering an error message if the value > max with no min', async () => { - expect.assertions(4); - document.body.innerHTML = ``; - const input = document.querySelector('#group1'); - const label = document.getElementById('group1-label'); - const validator = validate('form')[0]; - const validityState = await validator.validate(); - // //validityState - expect(validityState).toEqual(false); - // //realtimeValidation start - expect(validator.getState().realTimeValidation).toEqual(true); - // //focus on first invalid node - expect(document.activeElement).toEqual(input); - //render error message - expect(document.querySelector(`.${DOTNET_CLASSNAMES.ERROR}`).textContent).toEqual('Range error message'); - }); - - it('should validate a form based on the data-val range validator returning true if value is in range', async () => { - expect.assertions(1); - document.body.innerHTML = ``; - const validator = validate('form')[0]; - const validityState = await validator.validate(); - expect(validityState).toEqual(true); - }); - +import validate from '../../../../src'; +import { DOTNET_CLASSNAMES } from '../../../../src/lib/constants'; + +describe('Validate > Integration > api > validate > equalto', () => { + + it('should validate a form based on the data-val range validator returning false, starting realTimeValidation, focusing on first invalid field, and rendering an error message if the value is out of range', async () => { + expect.assertions(4); + document.body.innerHTML = ``; + const input = document.querySelector('#group1'); + const validator = validate('form')[0]; + const validityState = await validator.validate(); + expect(validityState).toEqual(false); + expect(validator.getState().realTimeValidation).toEqual(true); + expect(document.activeElement).toEqual(input); + expect(document.querySelector(`.${DOTNET_CLASSNAMES.ERROR}`).textContent).toEqual('Range error message'); + }); + + it('should validate a form based on the data-val range validator returning true if value > min with no max', async () => { + expect.assertions(1); + document.body.innerHTML = ``; + const validator = validate('form')[0]; + const validityState = await validator.validate(); + expect(validityState).toEqual(true); + }); + + it('should validate a form based on the data-val range validator returning false, starting realTimeValidation, focusing on first invalid field, and rendering an error message if the value <= min with no max', async () => { + expect.assertions(4); + document.body.innerHTML = ``; + const input = document.querySelector('#group1'); + const validator = validate('form')[0]; + const validityState = await validator.validate(); + expect(validityState).toEqual(false); + expect(validator.getState().realTimeValidation).toEqual(true); + expect(document.activeElement).toEqual(input); + expect(document.querySelector(`.${DOTNET_CLASSNAMES.ERROR}`).textContent).toEqual('Range error message'); + }); + + it('should validate a form based on the data-val range validator returning true if value <= max with no min', async () => { + expect.assertions(1); + document.body.innerHTML = ``; + const validator = validate('form')[0]; + const validityState = await validator.validate(); + expect(validityState).toEqual(true); + }); + + it('should validate a form based on the data-val range validator returning false, starting realTimeValidation, focusing on first invalid field, and rendering an error message if the value > max with no min', async () => { + expect.assertions(4); + document.body.innerHTML = ``; + const input = document.querySelector('#group1'); + const validator = validate('form')[0]; + const validityState = await validator.validate(); + expect(validityState).toEqual(false); + expect(validator.getState().realTimeValidation).toEqual(true); + expect(document.activeElement).toEqual(input); + expect(document.querySelector(`.${DOTNET_CLASSNAMES.ERROR}`).textContent).toEqual('Range error message'); + }); + + it('should validate a form based on the data-val range validator returning true if value is in range', async () => { + expect.assertions(1); + document.body.innerHTML = ``; + const validator = validate('form')[0]; + const validityState = await validator.validate(); + expect(validityState).toEqual(true); + }); + }); \ No newline at end of file diff --git a/packages/validate/__tests__/integration/api/validate/regex-pattern.js b/packages/validate/__tests__/jest/api/validate/regex-pattern.js similarity index 89% rename from packages/validate/__tests__/integration/api/validate/regex-pattern.js rename to packages/validate/__tests__/jest/api/validate/regex-pattern.js index d47b9316..27c4507a 100644 --- a/packages/validate/__tests__/integration/api/validate/regex-pattern.js +++ b/packages/validate/__tests__/jest/api/validate/regex-pattern.js @@ -1,90 +1,82 @@ -import validate from '../../../../src'; -import { DOTNET_CLASSNAMES } from '../../../../src/lib/constants'; -import defaults from '../../../../src/lib/defaults'; - -describe('Validate > Integration > api > validate > regex/pattern', () => { - it('should validate a form based on the HTML5 pattern validator returning false, staring realTimeValidation, focusing on first invalid field, and rendering an error message if the value does not match', async () => { - expect.assertions(4); - document.body.innerHTML = ``; - const input = document.getElementById('group1'); - const label = document.getElementById('group1-label'); - const validator = validate('form')[0]; - const validityState = await validator.validate(); - expect(validityState).toEqual(false); - // realtimeValidation start - expect(validator.getState().realTimeValidation).toEqual(true); - // // focus on first invalid node - expect(document.activeElement).toEqual(input); - // // render error message - expect(document.querySelector(`.${DOTNET_CLASSNAMES.ERROR}`).textContent).toEqual(defaults.messages.pattern()); - }); - - it('should validate a form based on the data-val regex validator returning false, starting realTimeValidation, focusing on first invalid field, and rendering an error message if a field is invalid', async () => { - expect.assertions(4); - document.body.innerHTML = ``; - const input = document.getElementById('group1'); - const label = document.getElementById('group1-label'); - const validator = validate('form')[0]; - const validityState = await validator.validate(); - expect(validityState).toEqual(false); - // realtimeValidation start - expect(validator.getState().realTimeValidation).toEqual(true); - // // focus on first invalid node - expect(document.activeElement).toEqual(input); - // // render error message - expect(document.querySelector(`.${DOTNET_CLASSNAMES.ERROR}`).textContent).toEqual('Regex error message'); - }); - - it('should validate a form based on the HTML5 pattern validator returning true if the pattern is matched', async () => { - expect.assertions(1); - document.body.innerHTML = ``; - const validator = validate('form')[0]; - const validityState = await validator.validate(); - expect(validityState).toEqual(true); - }); - - it('should validate a form based on the HTML5 pattern validator returning true if the pattern is matched', async () => { - expect.assertions(1); - document.body.innerHTML = ``; - const validator = validate('form')[0]; - const validityState = await validator.validate(); - expect(validityState).toEqual(true); - }); - +import validate from '../../../../src'; +import { DOTNET_CLASSNAMES } from '../../../../src/lib/constants'; +import defaults from '../../../../src/lib/defaults'; + +describe('Validate > Integration > api > validate > regex/pattern', () => { + it('should validate a form based on the HTML5 pattern validator returning false, staring realTimeValidation, focusing on first invalid field, and rendering an error message if the value does not match', async () => { + expect.assertions(4); + document.body.innerHTML = ``; + const input = document.getElementById('group1'); + const validator = validate('form')[0]; + const validityState = await validator.validate(); + expect(validityState).toEqual(false); + expect(validator.getState().realTimeValidation).toEqual(true); + expect(document.activeElement).toEqual(input); + expect(document.querySelector(`.${DOTNET_CLASSNAMES.ERROR}`).textContent).toEqual(defaults.messages.pattern()); + }); + + it('should validate a form based on the data-val regex validator returning false, starting realTimeValidation, focusing on first invalid field, and rendering an error message if a field is invalid', async () => { + expect.assertions(4); + document.body.innerHTML = ``; + const input = document.getElementById('group1'); + const validator = validate('form')[0]; + const validityState = await validator.validate(); + expect(validityState).toEqual(false); + expect(validator.getState().realTimeValidation).toEqual(true); + expect(document.activeElement).toEqual(input); + expect(document.querySelector(`.${DOTNET_CLASSNAMES.ERROR}`).textContent).toEqual('Regex error message'); + }); + + it('should validate a form based on the HTML5 pattern validator returning true if the pattern is matched', async () => { + expect.assertions(1); + document.body.innerHTML = ``; + const validator = validate('form')[0]; + const validityState = await validator.validate(); + expect(validityState).toEqual(true); + }); + + it('should validate a form based on the HTML5 pattern validator returning true if the pattern is matched', async () => { + expect.assertions(1); + document.body.innerHTML = ``; + const validator = validate('form')[0]; + const validityState = await validator.validate(); + expect(validityState).toEqual(true); + }); + }); \ No newline at end of file diff --git a/packages/validate/__tests__/integration/api/validate/remote.js b/packages/validate/__tests__/jest/api/validate/remote.js similarity index 92% rename from packages/validate/__tests__/integration/api/validate/remote.js rename to packages/validate/__tests__/jest/api/validate/remote.js index 5afe33e6..ba7cede4 100644 --- a/packages/validate/__tests__/integration/api/validate/remote.js +++ b/packages/validate/__tests__/jest/api/validate/remote.js @@ -1,142 +1,133 @@ -import mock from 'xhr-mock'; -import validate from '../../../../src'; -import { DOTNET_CLASSNAMES } from '../../../../src/lib/constants'; - -describe('Validate > Integration > api > validate > remote', () => { - - beforeEach(() => mock.setup()); - - afterEach(() => mock.teardown()); - - it('should validate a form based on the HTML5 remote validator returning false, staring realTimeValidation, focusing on first invalid field, and rendering an error message if the remote validation returns an error', async () => { - expect.assertions(4); - - mock.post('/api/validate', { - status: 201, - body: 'Remote error message' - }); - document.body.innerHTML = ``; - const input = document.getElementById('group1'); - const validator = validate('form')[0]; - - const validityState = await validator.validate(); - expect(validityState).toEqual(false); - // realtimeValidation start - expect(validator.getState().realTimeValidation).toEqual(true); - // // focus on first invalid node - expect(document.activeElement).toEqual(input); - // // render error message - expect(document.querySelector(`.${DOTNET_CLASSNAMES.ERROR}`).textContent).toEqual('Remote error message'); - }); - - it('should validate a form based on the HTML5 remote validator returning false, staring realTimeValidation, focusing on first invalid field, and rendering the error message if noe is returned from the remote validation API', async () => { - expect.assertions(4); - - mock.post('/api/validate', { - status: 201, - body: 'Error message from API' - }); - document.body.innerHTML = ``; - const input = document.getElementById('group1'); - const validator = validate('form')[0]; - - const validityState = await validator.validate(); - expect(validityState).toEqual(false); - // realtimeValidation start - expect(validator.getState().realTimeValidation).toEqual(true); - // focus on first invalid node - expect(document.activeElement).toEqual(input); - // render error message - expect(document.querySelector(`.${DOTNET_CLASSNAMES.ERROR}`).textContent).toEqual('Error message from API'); - }); - - it('should validate a form based on the HTML5 remote validator returning false, starting realTimeValidation, focusing on first invalid field, and rendering an error message if the remote validation returns an error via a GET request', async () => { - expect.assertions(4); - - mock.get('/api/validate?group1=Failure&group2=Value%202', { - status: 201, - body: 'false' - }); - document.body.innerHTML = ``; - const input = document.getElementById('group1'); - const validator = validate('form')[0]; - - const validityState = await validator.validate(); - expect(validityState).toEqual(false); - // realtimeValidation start - expect(validator.getState().realTimeValidation).toEqual(true); - // focus on first invalid node - expect(document.activeElement).toEqual(input); - // render error message - expect(document.querySelector(`.${DOTNET_CLASSNAMES.ERROR}`).textContent).toEqual('Remote error message'); - }); - - it('should validate a form based on the data-val remote validator returning true if it passes remote validate', async () => { - expect.assertions(1); - - mock.post('/api/validate', { - status: 201, - body: 'true' - }); - document.body.innerHTML = ``; - const validator = validate('form')[0]; - const validityState = await validator.validate(); - expect(validityState).toEqual(true); - }); - +import mock from 'xhr-mock'; +import validate from '../../../../src'; +import { DOTNET_CLASSNAMES } from '../../../../src/lib/constants'; + +describe('Validate > Integration > api > validate > remote', () => { + + beforeEach(() => mock.setup()); + + afterEach(() => mock.teardown()); + + it('should validate a form based on the HTML5 remote validator returning false, staring realTimeValidation, focusing on first invalid field, and rendering an error message if the remote validation returns an error', async () => { + expect.assertions(4); + + mock.post('/api/validate', { + status: 201, + body: 'Remote error message' + }); + document.body.innerHTML = ``; + const input = document.getElementById('group1'); + const validator = validate('form')[0]; + + const validityState = await validator.validate(); + expect(validityState).toEqual(false); + expect(validator.getState().realTimeValidation).toEqual(true); + expect(document.activeElement).toEqual(input); + expect(document.querySelector(`.${DOTNET_CLASSNAMES.ERROR}`).textContent).toEqual('Remote error message'); + }); + + it('should validate a form based on the HTML5 remote validator returning false, staring realTimeValidation, focusing on first invalid field, and rendering the error message if noe is returned from the remote validation API', async () => { + expect.assertions(4); + + mock.post('/api/validate', { + status: 201, + body: 'Error message from API' + }); + document.body.innerHTML = ``; + const input = document.getElementById('group1'); + const validator = validate('form')[0]; + + const validityState = await validator.validate(); + expect(validityState).toEqual(false); + expect(validator.getState().realTimeValidation).toEqual(true); + expect(document.activeElement).toEqual(input); + expect(document.querySelector(`.${DOTNET_CLASSNAMES.ERROR}`).textContent).toEqual('Error message from API'); + }); + + it('should validate a form based on the HTML5 remote validator returning false, starting realTimeValidation, focusing on first invalid field, and rendering an error message if the remote validation returns an error via a GET request', async () => { + expect.assertions(4); + + mock.get('/api/validate?group1=Failure&group2=Value%202', { + status: 201, + body: 'false' + }); + document.body.innerHTML = ``; + const input = document.getElementById('group1'); + const validator = validate('form')[0]; + + const validityState = await validator.validate(); + expect(validityState).toEqual(false); + expect(validator.getState().realTimeValidation).toEqual(true); + expect(document.activeElement).toEqual(input); + expect(document.querySelector(`.${DOTNET_CLASSNAMES.ERROR}`).textContent).toEqual('Remote error message'); + }); + + it('should validate a form based on the data-val remote validator returning true if it passes remote validate', async () => { + expect.assertions(1); + + mock.post('/api/validate', { + status: 201, + body: 'true' + }); + document.body.innerHTML = ``; + const validator = validate('form')[0]; + const validityState = await validator.validate(); + expect(validityState).toEqual(true); + }); + }); \ No newline at end of file diff --git a/packages/validate/__tests__/integration/api/validate/required.js b/packages/validate/__tests__/jest/api/validate/required.js similarity index 84% rename from packages/validate/__tests__/integration/api/validate/required.js rename to packages/validate/__tests__/jest/api/validate/required.js index 24511f6a..25754dfb 100644 --- a/packages/validate/__tests__/integration/api/validate/required.js +++ b/packages/validate/__tests__/jest/api/validate/required.js @@ -1,100 +1,82 @@ -import validate from '../../../../src'; -import { DOTNET_CLASSNAMES } from '../../../../src/lib/constants'; -import defaults from '../../../../src/lib/defaults'; - -describe('Validate > Integration > api > validate > required', () => { - //return boolean validityState - //start realtimevalidation - //render errors - //focus on first invalid field - - //return boolean validityState - //submit form - - it('should validate a form based on the HTML5 required validator returning false, starting realTimeValidation, focusing on first invalid field, and rendering an error message if a field is invalid', async () => { - expect.assertions(4); - document.body.innerHTML = ``; - const input = document.getElementById('group1-1'); - const label = document.getElementById('group1-1-label'); - const validator = validate('form')[0]; - const validityState = await validator.validate(); - // //validityState - expect(validityState).toEqual(false); - // //realtimeValidation start - expect(validator.getState().realTimeValidation).toEqual(true); - // //focus on first invalid node - expect(document.activeElement).toEqual(input); - //render error message - expect(document.querySelector(`.${DOTNET_CLASSNAMES.ERROR}`).textContent).toEqual(defaults.messages.required()); - }); - - it('should validate a form based on the HTML5 required validator returning true if valid', async () => { - expect.assertions(1); - document.body.innerHTML = ``; - const validator = validate('form')[0]; - const validityState = await validator.validate(); - // //validityState - expect(validityState).toEqual(true); - }); - - it('should validate a form based on the data-val required validator returning false, starting realTimeValidation, focusing on first invalid field, and rendering an error message if a field is invalid', async () => { - expect.assertions(4); - document.body.innerHTML = ``; - const input = document.getElementById('group2'); - const validator = validate('form')[0]; - const validityState = await validator.validate(); - //validityState - expect(validityState).toEqual(false); - //realtimeValidation start - expect(validator.getState().realTimeValidation).toEqual(true); - //focus on firstinvalid node - expect(document.activeElement).toEqual(input); - //render error message - expect(document.querySelector(`.${DOTNET_CLASSNAMES.ERROR}`).textContent).toEqual('Required error message'); - - }); - - it('should validate a form based on the data-val required validator returning true if valid', async () => { - expect.assertions(1); - document.body.innerHTML = ``; - const validator = validate('form')[0]; - const validityState = await validator.validate(); - // //validityState - expect(validityState).toEqual(true); - }); -}); +import validate from '../../../../src'; +import { DOTNET_CLASSNAMES } from '../../../../src/lib/constants'; +import defaults from '../../../../src/lib/defaults'; + +describe('Validate > Integration > api > validate > required', () => { + + it('should validate a form based on the HTML5 required validator returning false, starting realTimeValidation, focusing on first invalid field, and rendering an error message if a field is invalid', async () => { + expect.assertions(4); + document.body.innerHTML = ``; + const input = document.getElementById('group1-1'); + const validator = validate('form')[0]; + const validityState = await validator.validate(); + expect(validityState).toEqual(false); + expect(validator.getState().realTimeValidation).toEqual(true); + expect(document.activeElement).toEqual(input); + expect(document.querySelector(`.${DOTNET_CLASSNAMES.ERROR}`).textContent).toEqual(defaults.messages.required()); + }); + + it('should validate a form based on the HTML5 required validator returning true if valid', async () => { + expect.assertions(1); + document.body.innerHTML = ``; + const validator = validate('form')[0]; + const validityState = await validator.validate(); + expect(validityState).toEqual(true); + }); + + it('should validate a form based on the data-val required validator returning false, starting realTimeValidation, focusing on first invalid field, and rendering an error message if a field is invalid', async () => { + expect.assertions(4); + document.body.innerHTML = ``; + const input = document.getElementById('group2'); + const validator = validate('form')[0]; + const validityState = await validator.validate(); + expect(validityState).toEqual(false); + expect(validator.getState().realTimeValidation).toEqual(true); + expect(document.activeElement).toEqual(input); + expect(document.querySelector(`.${DOTNET_CLASSNAMES.ERROR}`).textContent).toEqual('Required error message'); + + }); + + it('should validate a form based on the data-val required validator returning true if valid', async () => { + expect.assertions(1); + document.body.innerHTML = ``; + const validator = validate('form')[0]; + const validityState = await validator.validate(); + expect(validityState).toEqual(true); + }); +}); diff --git a/packages/validate/__tests__/integration/api/validate/stringlength.js b/packages/validate/__tests__/jest/api/validate/stringlength.js similarity index 90% rename from packages/validate/__tests__/integration/api/validate/stringlength.js rename to packages/validate/__tests__/jest/api/validate/stringlength.js index b2c9b5bd..23ab653c 100644 --- a/packages/validate/__tests__/integration/api/validate/stringlength.js +++ b/packages/validate/__tests__/jest/api/validate/stringlength.js @@ -1,50 +1,46 @@ -import validate from '../../../../src'; -import { DOTNET_CLASSNAMES } from '../../../../src/lib/constants'; - -describe('Validate > Integration > api > validate > stringlength', () => { - - it('should validate a form based on the data-val stringlength validator returning false, starting realTimeValidation, focusing on first invalid field, and rendering an error message if a field is outwith the min and max length range', async () => { - expect.assertions(4); - document.body.innerHTML = ``; - const input = document.getElementById('group1'); - const label = document.getElementById('group1-1-label'); - const validator = validate('form')[0]; - const validityState = await validator.validate(); - expect(validityState).toEqual(false); - // realtimeValidation start - expect(validator.getState().realTimeValidation).toEqual(true); - // // focus on first invalid node - expect(document.activeElement).toEqual(input); - // // render error message - expect(document.querySelector(`.${DOTNET_CLASSNAMES.ERROR}`).textContent).toEqual('Stringlength error message'); - }); - - it('should validate a form based on the data-val stringlength validator returning true if within the min and max length range', async () => { - expect.assertions(1); - document.body.innerHTML = ``; - const validator = validate('form')[0]; - const validityState = await validator.validate(); - expect(validityState).toEqual(true); - }); - +import validate from '../../../../src'; +import { DOTNET_CLASSNAMES } from '../../../../src/lib/constants'; + +describe('Validate > Integration > api > validate > stringlength', () => { + + it('should validate a form based on the data-val stringlength validator returning false, starting realTimeValidation, focusing on first invalid field, and rendering an error message if a field is outwith the min and max length range', async () => { + expect.assertions(4); + document.body.innerHTML = ``; + const input = document.getElementById('group1'); + const validator = validate('form')[0]; + const validityState = await validator.validate(); + expect(validityState).toEqual(false); + expect(validator.getState().realTimeValidation).toEqual(true); + expect(document.activeElement).toEqual(input); + expect(document.querySelector(`.${DOTNET_CLASSNAMES.ERROR}`).textContent).toEqual('Stringlength error message'); + }); + + it('should validate a form based on the data-val stringlength validator returning true if within the min and max length range', async () => { + expect.assertions(1); + document.body.innerHTML = ``; + const validator = validate('form')[0]; + const validityState = await validator.validate(); + expect(validityState).toEqual(true); + }); + }); \ No newline at end of file diff --git a/packages/validate/__tests__/integration/api/validate/url.js b/packages/validate/__tests__/jest/api/validate/url.js similarity index 88% rename from packages/validate/__tests__/integration/api/validate/url.js rename to packages/validate/__tests__/jest/api/validate/url.js index d0578417..f86efb81 100644 --- a/packages/validate/__tests__/integration/api/validate/url.js +++ b/packages/validate/__tests__/jest/api/validate/url.js @@ -1,98 +1,85 @@ -import validate from '../../../../src'; -import { DOTNET_CLASSNAMES } from '../../../../src/lib/constants'; -import defaults from '../../../../src/lib/defaults'; - -describe('Validate > Integration > api > validate > url', () => { - //html5 spec regex approximation: - ///^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))? - - it('should validate a form based on the HTML5 url validator returning false, starting realTimeValidation, focusing on first invalid field, and rendering an error message if a field is invalid', async () => { - expect.assertions(4); - document.body.innerHTML = ``; - const input = document.getElementById('group1'); - const label = document.getElementById('group1-label'); - const validator = validate('form')[0]; - const validityState = await validator.validate(); - // //validityState - expect(validityState).toEqual(false); - // //realtimeValidation start - expect(validator.getState().realTimeValidation).toEqual(true); - // //focus on first invalid node - expect(document.activeElement).toEqual(input); - //render error message - expect(document.querySelector(`.${DOTNET_CLASSNAMES.ERROR}`).textContent).toEqual(defaults.messages.url()); - }); - - it('should validate a form based on the HTML5 url validator returning true if valid', async () => { - expect.assertions(1); - document.body.innerHTML = ``; - const validator = validate('form')[0]; - const validityState = await validator.validate(); - // //validityState - expect(validityState).toEqual(true); - }); - - it('should validate a form based on the data-val email validator returning false, starting realTimeValidation, focusing on first invalid field, and rendering an error message if a field is invalid', async () => { - expect.assertions(4); - document.body.innerHTML = ``; - const input = document.getElementById('group2'); - const validator = validate('form')[0]; - const validityState = await validator.validate(); - //validityState - expect(validityState).toEqual(false); - //realtimeValidation start - expect(validator.getState().realTimeValidation).toEqual(true); - //focus on firstinvalid node - expect(document.activeElement).toEqual(input); - //render error message - expect(document.querySelector(`.${DOTNET_CLASSNAMES.ERROR}`).textContent).toEqual('Url error message'); - - }); - - - it('should validate a form based on the data-val url validator returning true if valid', async () => { - expect.assertions(1); - document.body.innerHTML = ``; - const validator = validate('form')[0]; - const validityState = await validator.validate(); - //validityState - expect(validityState).toEqual(true); - - }); - - +import validate from '../../../../src'; +import { DOTNET_CLASSNAMES } from '../../../../src/lib/constants'; +import defaults from '../../../../src/lib/defaults'; + +describe('Validate > Integration > api > validate > url', () => { + //html5 spec regex approximation: + ///^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))? + + it('should validate a form based on the HTML5 url validator returning false, starting realTimeValidation, focusing on first invalid field, and rendering an error message if a field is invalid', async () => { + expect.assertions(4); + document.body.innerHTML = ``; + const input = document.getElementById('group1'); + const validator = validate('form')[0]; + const validityState = await validator.validate(); + expect(validityState).toEqual(false); + expect(validator.getState().realTimeValidation).toEqual(true); + expect(document.activeElement).toEqual(input); + expect(document.querySelector(`.${DOTNET_CLASSNAMES.ERROR}`).textContent).toEqual(defaults.messages.url()); + }); + + it('should validate a form based on the HTML5 url validator returning true if valid', async () => { + expect.assertions(1); + document.body.innerHTML = ``; + const validator = validate('form')[0]; + const validityState = await validator.validate(); + expect(validityState).toEqual(true); + }); + + it('should validate a form based on the data-val email validator returning false, starting realTimeValidation, focusing on first invalid field, and rendering an error message if a field is invalid', async () => { + expect.assertions(4); + document.body.innerHTML = ``; + const input = document.getElementById('group2'); + const validator = validate('form')[0]; + const validityState = await validator.validate(); + expect(validityState).toEqual(false); + expect(validator.getState().realTimeValidation).toEqual(true); + expect(document.activeElement).toEqual(input); + expect(document.querySelector(`.${DOTNET_CLASSNAMES.ERROR}`).textContent).toEqual('Url error message'); + }); + + it('should validate a form based on the data-val url validator returning true if valid', async () => { + expect.assertions(1); + document.body.innerHTML = ``; + const validator = validate('form')[0]; + const validityState = await validator.validate(); + expect(validityState).toEqual(true); + + }); + + }); \ No newline at end of file diff --git a/packages/validate/__tests__/jest/errors.js b/packages/validate/__tests__/jest/errors.js new file mode 100644 index 00000000..ca4b94fe --- /dev/null +++ b/packages/validate/__tests__/jest/errors.js @@ -0,0 +1,126 @@ +import { + h, + clearError, + clearErrors, +} from '../../src/lib/dom'; +import { DOTNET_CLASSNAMES } from '../../src/lib/constants'; + +describe('Validate > Unit > DOM > clearError', () => { + + it('should delete the errorNode for the group from state', async () => { + document.body.innerHTML = ``; + const mockState = { + groups: { + group1: { + serverErrorNode: false, + fields: Array.from(document.getElementsByName('group1')) + } + }, + errors: { + group1: document.getElementById('test-error-node') + } + }; + clearError('group1')(mockState); + expect(mockState.errors.group1).toBeUndefined(); + }); + + + it('should remove a server-side rendered text node for the group from state', async () => { + document.body.innerHTML = ``; + const errorNode = document.createTextNode('This field is required'); + const serverErrorNode = document.getElementById('test-server-error-node'); + serverErrorNode.appendChild(errorNode); + const mockState = { + groups: { + group1: { + serverErrorNode, + fields: Array.from(document.getElementsByName('group1')) + } + }, + errors: { + group1: errorNode + } + }; + + clearError('group1')(mockState); + expect(mockState.errors.group1).toBeUndefined(); + }); +}); + +describe('Validate > Unit > DOM > clearErrors', () => { + + it('Should remove all errors from state for valid groups', async () => { + document.body.innerHTML = ``; + const mockState = { + groups: { + group1: { + serverErrorNode: false, + fields: Array.from(document.getElementsByName('group1')) + }, + group2: { + serverErrorNode: false, + fields: Array.from(document.getElementsByName('group2')) + } + }, + errors: { + group1: document.getElementById('test-error-node-1'), + group2: document.getElementById('test-error-node-2') + } + }; + clearErrors(mockState); + expect(mockState.errors.group1).toBeUndefined(); + expect(mockState.errors.group2).toBeUndefined(); + }); + + it('should not change state if there are no errors', async () => { + document.body.innerHTML = ``; + const mockState = { + groups: { + group1: { + serverErrorNode: false, + fields: Array.from(document.getElementsByName('group1')) + }, + group2: { + serverErrorNode: false, + fields: Array.from(document.getElementsByName('group2')) + } + }, + errors: {} + }; + const copy = Object.assign({}, mockState); + clearErrors(mockState); + expect(copy).toEqual(mockState); + }); +}); \ No newline at end of file diff --git a/packages/validate/__tests__/unit/initialisation.js b/packages/validate/__tests__/jest/initialisation.js similarity index 99% rename from packages/validate/__tests__/unit/initialisation.js rename to packages/validate/__tests__/jest/initialisation.js index 998243ab..b7eb4b6e 100644 --- a/packages/validate/__tests__/unit/initialisation.js +++ b/packages/validate/__tests__/jest/initialisation.js @@ -4,7 +4,6 @@ import { getSelection } from '../../src/lib/validator/utils'; let validators; const setUpDOM = () => { - // Set up our document body document.body.innerHTML = ``; - const group = { fields: [document.querySelector('#field')] }; - expect(Methods.required(group)).toEqual(false); - }); - - it('should return false for group with no value', () => { - document.body.innerHTML = ``; - const group = { fields: [document.querySelector('#field-1'), document.querySelector('#field-1')] }; - expect(Methods.required(group)).toEqual(false); - }); - - it('should return true for a group with a single required field wuith a value', () => { - document.body.innerHTML = ``; - const group = { fields: [document.querySelector('#field')] }; - expect(Methods.required(group)).toEqual(true); - }); - - it('should return true for group with value', () => { - document.body.innerHTML = ``; - const group = { fields: [document.querySelector('#field-1'), document.querySelector('#field-1')] }; - expect(Methods.required(group)).toEqual(true); - }); - -}); - - -//email -describe('Validate > Unit > Validator > methods > email', () => { - - it('should return true for group with no value that is not required', () => { - document.body.innerHTML = ``; - const group = { - validators: [], - fields: [document.querySelector('#field')] - }; - expect(Methods.email(group)).toEqual(true); - }); - - it('should return false for group containing a non-spec value', () => { - document.body.innerHTML = ``; - const group = { - validators: [], - fields: [document.querySelector('#field')] - }; - expect(Methods.email(group)).toEqual(false); - }); - - it('should return true for group containing an on-spec value', () => { - document.body.innerHTML = ``; - const group = { - validators: [], - fields: [document.querySelector('#field')] - }; - expect(Methods.email(group)).toEqual(true); - }); - -}); - - -//url -describe('Validate > Unit > Validator > methods > url', () => { - - it('should return true for group with no value that is not required', () => { - document.body.innerHTML = ``; - const group = { - validators: [], - fields: [document.querySelector('#field')] - }; - expect(Methods.url(group)).toEqual(true); - }); - - it('should return false for group containing a non-spec value', () => { - document.body.innerHTML = ``; - const group = { - validators: [], - fields: [document.querySelector('#field')] - }; - expect(Methods.url(group)).toEqual(false); - }); - - it('should return true for group containing an on-spec value', () => { - document.body.innerHTML = ``; - const group = { - validators: [], - fields: [document.querySelector('#field')] - }; - expect(Methods.url(group)).toEqual(true); - }); - -}); - -//dateISO -describe('Validate > Unit > Validator > methods > dateISO', () => { - - it('should return true for group with no value that is not required', () => { - document.body.innerHTML = ``; - const group = { - validators: [], - fields: [document.querySelector('#field')] - }; - expect(Methods.dateISO(group)).toEqual(true); - }); - - it('should return false for group containing a non-spec value', () => { - document.body.innerHTML = ``; - const group = { - validators: [], - fields: [document.querySelector('#field')] - }; - expect(Methods.dateISO(group)).toEqual(false); - }); - - it('should return true for group containing an on-spec value', () => { - document.body.innerHTML = ``; - const group = { - validators: [], - fields: [document.querySelector('#field')] - }; - expect(Methods.dateISO(group)).toEqual(true); - }); - -}); - -//number -describe('Validate > Unit > Validator > methods > number', () => { - - it('should return true for group with no value that is not required', () => { - document.body.innerHTML = ``; - const group = { - validators: [], - fields: [document.querySelector('#field')] - }; - expect(Methods.number(group)).toEqual(true); - }); - - it('should return false for group containing a non-spec value', () => { - document.body.innerHTML = ``; - const group = { - validators: [], - fields: [document.querySelector('#field')] - }; - expect(Methods.number(group)).toEqual(false); - }); - - it('should return true for group containing an on-spec value', () => { - document.body.innerHTML = ``; - const group1 = { - validators: [], - fields: [document.querySelector('#field1')] - }; - expect(Methods.number(group1)).toEqual(true); - const group2 = { - validators: [], - fields: [document.querySelector('#field2')] - }; - expect(Methods.number(group2)).toEqual(true); - const group3 = { - validators: [], - fields: [document.querySelector('#field3')] - }; - expect(Methods.number(group3)).toEqual(true); - }); - -}); - - -//digits -describe('Validate > Unit > Validator > methods > digits', () => { - - it('should return true for group with no value that is not required', () => { - document.body.innerHTML = ``; - const group = { - validators: [], - fields: [document.querySelector('#field')] - }; - expect(Methods.digits(group)).toEqual(true); - }); - - it('should return false for group containing a non-spec value', () => { - document.body.innerHTML = ``; - const group1 = { - validators: [], - fields: [document.querySelector('#field1')] - }; - expect(Methods.digits(group1)).toEqual(false); - - const group2 = { - validators: [], - fields: [document.querySelector('#field2')] - }; - expect(Methods.digits(group2)).toEqual(false); - - const group3 = { - validators: [], - fields: [document.querySelector('#field3')] - }; - expect(Methods.digits(group3)).toEqual(false); - }); - - it('should return true for group containing an on-spec value', () => { - document.body.innerHTML = ``; - const group1 = { - validators: [], - fields: [document.querySelector('#field1')] - }; - expect(Methods.digits(group1)).toEqual(true); - }); - -}); - - -//minlength -describe('Validate > Unit > Validator > methods > minlength', () => { - - it('should return true for group with no value that is not required', () => { - document.body.innerHTML = ``; - const group = { - validators: [ - { - type: 'minlength', - params: { min: 3 } - } - ], - fields: [document.querySelector('#field')] - }; - expect(Methods.minlength(group)).toEqual(true); - }); - - it('should return false for group with a value < min', () => { - document.body.innerHTML = ``; - const group = { - validators: [ - { - type: 'minlength', - params: { min: 3 } - } - ], - fields: [document.querySelector('#field')] - }; - expect(Methods.minlength(group)).toEqual(false); - }); - - it('should return true for group with a value => min', () => { - document.body.innerHTML = ``; - const group = { - validators: [ - { - type: 'minlength', - params: { min: 3 } - } - ], - fields: [document.querySelector('#field')] - }; - expect(Methods.minlength(group)).toEqual(true); - }); - -}); - -//maxlength -describe('Validate > Unit > Validator > methods > maxlength', () => { - - it('should return true for group with no value that is not required', () => { - document.body.innerHTML = ``; - const group = { - validators: [ - { - type: 'maxlength', - params: { max: 3 } - } - ], - fields: [document.querySelector('#field')] - }; - expect(Methods.maxlength(group)).toEqual(true); - }); - - it('should return false for group with a value > max', () => { - document.body.innerHTML = ``; - const group = { - validators: [ - { - type: 'maxlength', - params: { max: 5 } - } - ], - fields: [document.querySelector('#field')] - }; - expect(Methods.maxlength(group)).toEqual(false); - }); - - it('should return true for group with a value <= max', () => { - document.body.innerHTML = ``; - const group = { - validators: [ - { - type: 'maxlength', - params: { max: 5 } - } - ], - fields: [document.querySelector('#field')] - }; - expect(Methods.maxlength(group)).toEqual(true); - }); - -}); - -//equalto -describe('Validate > Unit > Validator > methods > equalto', () => { - - it('should return true for groups with no value', () => { - document.body.innerHTML = ``; - const group = { - validators: [ - { - type: 'equalto', - params: { other: [[document.querySelector('#field2')]] } - } - ], - fields: [document.querySelector('#field1')] - }; - expect(Methods.equalto(group)).toEqual(true); - }); - - it('should return false for groups with unequal values', () => { - document.body.innerHTML = ``; - const group = { - validators: [ - { - type: 'equalto', - params: { other: [[document.querySelector('#field2')]] } - } - ], - fields: [document.querySelector('#field1')] - }; - expect(Methods.equalto(group)).toEqual(false); - }); - - it('should return true for groups with unequal values', () => { - document.body.innerHTML = ``; - const group = { - validators: [ - { - type: 'equalto', - params: { other: [[document.querySelector('#field2')]] } - } - ], - fields: [document.querySelector('#field1')] - }; - expect(Methods.equalto(group)).toEqual(true); - }); - -}); - - -//pattern -describe('Validate > Unit > Validator > methods > pattern', () => { - - it('should return true for group with no value that is not required', () => { - document.body.innerHTML = ``; - const group = { - validators: [ - { - type: 'pattern', - params: { regex: /^(pass)$/ } - } - ], - fields: [document.querySelector('#field')] - }; - expect(Methods.pattern(group)).toEqual(true); - }); - - it('should return false for group with a non-matching value', () => { - document.body.innerHTML = ``; - const group = { - validators: [ - { - type: 'pattern', - params: { regex: /^(pass)$/ } - } - ], - fields: [document.querySelector('#field')] - }; - expect(Methods.pattern(group)).toEqual(false); - }); - - it('should return false for group with a matching value', () => { - document.body.innerHTML = ``; - const group = { - validators: [ - { - type: 'pattern', - params: { regex: /^(pass)$/ } - } - ], - fields: [document.querySelector('#field')] - }; - expect(Methods.pattern(group)).toEqual(true); - }); - -}); - - -//regex -describe('Validate > Unit > Validator > methods > regex', () => { - - it('should return true for group with no value that is not required', () => { - document.body.innerHTML = ``; - const group = { - validators: [ - { - type: 'regex', - params: { pattern: /^(pass)$/ } - } - ], - fields: [document.querySelector('#field')] - }; - expect(Methods.regex(group)).toEqual(true); - }); - - it('should return false for group with a non-matching value', () => { - document.body.innerHTML = ``; - const group = { - validators: [ - { - type: 'regex', - params: { pattern: /^(pass)$/ } - } - ], - fields: [document.querySelector('#field')] - }; - expect(Methods.regex(group)).toEqual(false); - }); - - it('should return false for group with a matching value', () => { - document.body.innerHTML = ``; - const group = { - validators: [ - { - type: 'regex', - params: { pattern: /^(pass)$/ } - } - ], - fields: [document.querySelector('#field')] - }; - expect(Methods.regex(group)).toEqual(true); - }); - -}); - -//min -describe('Validate > Unit > Validator > methods > min', () => { - - it('should return true for group with no value that is not required', () => { - document.body.innerHTML = ``; - const group = { - validators: [ - { - type: 'min', - params: { min: 3 } - } - ], - fields: [document.querySelector('#field')] - }; - expect(Methods.min(group)).toEqual(true); - }); - - it('should return false for group with a value < min', () => { - document.body.innerHTML = ``; - const group = { - validators: [ - { - type: 'min', - params: { min: 3 } - } - ], - fields: [document.querySelector('#field')] - }; - expect(Methods.min(group)).toEqual(false); - }); - - it('should return true for group with a value => min', () => { - document.body.innerHTML = ``; - const group = { - validators: [ - { - type: 'min', - params: { min: 3 } - } - ], - fields: [document.querySelector('#field')] - }; - expect(Methods.min(group)).toEqual(true); - }); - -}); - -//max -describe('Validate > Unit > Validator > methods > max', () => { - - it('should return true for group with no value that is not required', () => { - document.body.innerHTML = ``; - const group = { - validators: [ - { - type: 'max', - params: { max: 3 } - } - ], - fields: [document.querySelector('#field')] - }; - expect(Methods.max(group)).toEqual(true); - }); - - it('should return false for group with a value > max', () => { - document.body.innerHTML = ``; - const group = { - validators: [ - { - type: 'max', - params: { max: 3 } - } - ], - fields: [document.querySelector('#field')] - }; - expect(Methods.max(group)).toEqual(false); - }); - - it('should return true for group with a value <= max', () => { - document.body.innerHTML = ``; - const group = { - validators: [ - { - type: 'max', - params: { max: 5 } - } - ], - fields: [document.querySelector('#field')] - }; - expect(Methods.max(group)).toEqual(true); - }); - -}); - - -//stringlength -describe('Validate > Unit > Validator > methods > stringlength', () => { - - it('should return true for group with no value that is not required', () => { - document.body.innerHTML = ``; - const group = { - validators: [ - { - type: 'stringlength', - params: { max: 3 } - } - ], - fields: [document.querySelector('#field')] - }; - expect(Methods.stringlength(group)).toEqual(true); - }); - - it('should return false for group with a value > max', () => { - document.body.innerHTML = ``; - const group = { - validators: [ - { - type: 'stringlength', - params: { max: 5 } - } - ], - fields: [document.querySelector('#field')] - }; - expect(Methods.stringlength(group)).toEqual(false); - }); - - it('should return true for group with a value <= max', () => { - document.body.innerHTML = ``; - const group = { - validators: [ - { - type: 'stringlength', - params: { max: 5 } - } - ], - fields: [document.querySelector('#field')] - }; - expect(Methods.stringlength(group)).toEqual(true); - }); - -}); - - -//length -describe('Validate > Unit > Validator > methods > length', () => { - - it('should return true for group with no value that is not required', () => { - document.body.innerHTML = ``; - const group = { - validators: [ - { - type: 'length', - params: { min: 5, max: 8 } - } - ], - fields: [document.querySelector('#field')] - }; - expect(Methods.length(group)).toEqual(true); - }); - - it('should return false for group with a value > max', () => { - document.body.innerHTML = ``; - const group = { - validators: [ - { - type: 'length', - params: { min: 5, max: 8 } - } - ], - fields: [document.querySelector('#field')] - }; - expect(Methods.length(group)).toEqual(false); - }); - - it('should return false for group with a value < min', () => { - document.body.innerHTML = ``; - const group = { - validators: [ - { - type: 'length', - params: { min: 5, max: 8 } - } - ], - fields: [document.querySelector('#field')] - }; - expect(Methods.length(group)).toEqual(false); - }); - - it('should return true for group with a value >= min and <= max', () => { - document.body.innerHTML = ``; - const group = { - validators: [ - { - type: 'length', - params: { min: 5, max: 8 } - } - ], - fields: [document.querySelector('#field')] - }; - expect(Methods.length(group)).toEqual(true); - }); - -}); - - -//range -describe('Validate > Unit > Validator > methods > range', () => { - - it('should return true for group with no value that is not required', () => { - document.body.innerHTML = ``; - const group = { - validators: [ - { - type: 'range', - params: { min: 5, max: 8 } - } - ], - fields: [document.querySelector('#field')] - }; - expect(Methods.range(group)).toEqual(true); - }); - - it('should return false for group with a value > max', () => { - document.body.innerHTML = ``; - const group = { - validators: [ - { - type: 'range', - params: { min: 5, max: 8 } - } - ], - fields: [document.querySelector('#field')] - }; - expect(Methods.range(group)).toEqual(false); - }); - - it('should return false for group with a value < min', () => { - document.body.innerHTML = ``; - const group = { - validators: [ - { - type: 'range', - params: { min: 5, max: 8 } - } - ], - fields: [document.querySelector('#field')] - }; - expect(Methods.range(group)).toEqual(false); - }); - - it('should return true for group with a value >= min and <= max', () => { - document.body.innerHTML = ``; - const group = { - validators: [ - { - type: 'range', - params: { min: 5, max: 8 } - } - ], - fields: [document.querySelector('#field')] - }; - expect(Methods.range(group)).toEqual(true); - }); - -}); - - -//remote -describe('Validate > Unit > Validator > methods > remote', () => { - - beforeEach(() => mock.setup()); - - afterEach(() => mock.teardown()); - - it('should return false for when the remote validation returns \'false\'', async () => { - - mock.post('/api/validate', { - status: 201, - body: 'false' - }); - - document.body.innerHTML = ``; - const group = { - validators: [], - fields: [document.querySelector('#field')] - }; - const params = { - url: '/api/validate' - }; - const res = await Methods.remote(group, params); - expect(res).toEqual('false'); - }); - - it('should return false for when the remote validation returns "false"', async () => { - - mock.post('/api/validate', { - status: 201, - body: 'true' - }); - - document.body.innerHTML = ``; - const group = { - validators: [], - fields: [document.querySelector('#field')] - }; - const params = { - url: '/api/validate' - }; - const res = await Methods.remote(group, params); - expect(res).toEqual('true'); - }); - - -}); - -//custom -describe('Validate > Unit > Validator > methods > custom', () => { - const customValidator = (value, fields) => value === 'Contrived validator'; - - it('should return true for group with no value that is not required', () => { - document.body.innerHTML = ``; - const group = { - validators: [], - fields: [document.querySelector('#field')] - }; - expect(Methods.custom(customValidator, group)).toEqual(true); - }); - - it('should return false when the custom validation function return false', () => { - document.body.innerHTML = ``; - const group = { - validators: [], - fields: [document.querySelector('#field')] - }; - expect(Methods.custom(customValidator, group)).toEqual(false); - }); - - - it('should return true when the custom validation function return true', () => { - document.body.innerHTML = ``; - const group = { - validators: [], - fields: [document.querySelector('#field')] - }; - expect(Methods.custom(customValidator, group)).toEqual(true); - }); - +import mock from 'xhr-mock'; +import Methods from '../../src/lib/validator/methods'; + +describe('Validate > Unit > Validator > methods > required', () => { + + it('should return false for group containing a single empty field', () => { + document.body.innerHTML = ``; + const group = { fields: [document.querySelector('#field')] }; + expect(Methods.required(group)).toEqual(false); + }); + + it('should return false for group with no value', () => { + document.body.innerHTML = ``; + const group = { fields: [document.querySelector('#field-1'), document.querySelector('#field-1')] }; + expect(Methods.required(group)).toEqual(false); + }); + + it('should return true for a group with a single required field wuith a value', () => { + document.body.innerHTML = ``; + const group = { fields: [document.querySelector('#field')] }; + expect(Methods.required(group)).toEqual(true); + }); + + it('should return true for group with value', () => { + document.body.innerHTML = ``; + const group = { fields: [document.querySelector('#field-1'), document.querySelector('#field-1')] }; + expect(Methods.required(group)).toEqual(true); + }); + +}); + +describe('Validate > Unit > Validator > methods > email', () => { + it('should return true for group with no value that is not required', () => { + document.body.innerHTML = ``; + const group = { + validators: [], + fields: [document.querySelector('#field')] + }; + expect(Methods.email(group)).toEqual(true); + }); + + it('should return false for group containing a non-spec value', () => { + document.body.innerHTML = ``; + const group = { + validators: [], + fields: [document.querySelector('#field')] + }; + expect(Methods.email(group)).toEqual(false); + }); + + it('should return true for group containing an on-spec value', () => { + document.body.innerHTML = ``; + const group = { + validators: [], + fields: [document.querySelector('#field')] + }; + expect(Methods.email(group)).toEqual(true); + }); + +}); + +describe('Validate > Unit > Validator > methods > url', () => { + it('should return true for group with no value that is not required', () => { + document.body.innerHTML = ``; + const group = { + validators: [], + fields: [document.querySelector('#field')] + }; + expect(Methods.url(group)).toEqual(true); + }); + + it('should return false for group containing a non-spec value', () => { + document.body.innerHTML = ``; + const group = { + validators: [], + fields: [document.querySelector('#field')] + }; + expect(Methods.url(group)).toEqual(false); + }); + + it('should return true for group containing an on-spec value', () => { + document.body.innerHTML = ``; + const group = { + validators: [], + fields: [document.querySelector('#field')] + }; + expect(Methods.url(group)).toEqual(true); + }); + +}); + +describe('Validate > Unit > Validator > methods > dateISO', () => { + it('should return true for group with no value that is not required', () => { + document.body.innerHTML = ``; + const group = { + validators: [], + fields: [document.querySelector('#field')] + }; + expect(Methods.dateISO(group)).toEqual(true); + }); + + it('should return false for group containing a non-spec value', () => { + document.body.innerHTML = ``; + const group = { + validators: [], + fields: [document.querySelector('#field')] + }; + expect(Methods.dateISO(group)).toEqual(false); + }); + + it('should return true for group containing an on-spec value', () => { + document.body.innerHTML = ``; + const group = { + validators: [], + fields: [document.querySelector('#field')] + }; + expect(Methods.dateISO(group)).toEqual(true); + }); + +}); + +describe('Validate > Unit > Validator > methods > number', () => { + it('should return true for group with no value that is not required', () => { + document.body.innerHTML = ``; + const group = { + validators: [], + fields: [document.querySelector('#field')] + }; + expect(Methods.number(group)).toEqual(true); + }); + + it('should return false for group containing a non-spec value', () => { + document.body.innerHTML = ``; + const group = { + validators: [], + fields: [document.querySelector('#field')] + }; + expect(Methods.number(group)).toEqual(false); + }); + + it('should return true for group containing an on-spec value', () => { + document.body.innerHTML = ``; + const group1 = { + validators: [], + fields: [document.querySelector('#field1')] + }; + expect(Methods.number(group1)).toEqual(true); + const group2 = { + validators: [], + fields: [document.querySelector('#field2')] + }; + expect(Methods.number(group2)).toEqual(true); + const group3 = { + validators: [], + fields: [document.querySelector('#field3')] + }; + expect(Methods.number(group3)).toEqual(true); + }); + +}); + +describe('Validate > Unit > Validator > methods > digits', () => { + it('should return true for group with no value that is not required', () => { + document.body.innerHTML = ``; + const group = { + validators: [], + fields: [document.querySelector('#field')] + }; + expect(Methods.digits(group)).toEqual(true); + }); + + it('should return false for group containing a non-spec value', () => { + document.body.innerHTML = ``; + const group1 = { + validators: [], + fields: [document.querySelector('#field1')] + }; + expect(Methods.digits(group1)).toEqual(false); + + const group2 = { + validators: [], + fields: [document.querySelector('#field2')] + }; + expect(Methods.digits(group2)).toEqual(false); + + const group3 = { + validators: [], + fields: [document.querySelector('#field3')] + }; + expect(Methods.digits(group3)).toEqual(false); + }); + + it('should return true for group containing an on-spec value', () => { + document.body.innerHTML = ``; + const group1 = { + validators: [], + fields: [document.querySelector('#field1')] + }; + expect(Methods.digits(group1)).toEqual(true); + }); + +}); + +describe('Validate > Unit > Validator > methods > minlength', () => { + + it('should return true for group with no value that is not required', () => { + document.body.innerHTML = ``; + const group = { + validators: [ + { + type: 'minlength', + params: { min: 3 } + } + ], + fields: [document.querySelector('#field')] + }; + expect(Methods.minlength(group)).toEqual(true); + }); + + it('should return false for group with a value < min', () => { + document.body.innerHTML = ``; + const group = { + validators: [ + { + type: 'minlength', + params: { min: 3 } + } + ], + fields: [document.querySelector('#field')] + }; + expect(Methods.minlength(group)).toEqual(false); + }); + + it('should return true for group with a value => min', () => { + document.body.innerHTML = ``; + const group = { + validators: [ + { + type: 'minlength', + params: { min: 3 } + } + ], + fields: [document.querySelector('#field')] + }; + expect(Methods.minlength(group)).toEqual(true); + }); + +}); + +describe('Validate > Unit > Validator > methods > maxlength', () => { + it('should return true for group with no value that is not required', () => { + document.body.innerHTML = ``; + const group = { + validators: [ + { + type: 'maxlength', + params: { max: 3 } + } + ], + fields: [document.querySelector('#field')] + }; + expect(Methods.maxlength(group)).toEqual(true); + }); + + it('should return false for group with a value > max', () => { + document.body.innerHTML = ``; + const group = { + validators: [ + { + type: 'maxlength', + params: { max: 5 } + } + ], + fields: [document.querySelector('#field')] + }; + expect(Methods.maxlength(group)).toEqual(false); + }); + + it('should return true for group with a value <= max', () => { + document.body.innerHTML = ``; + const group = { + validators: [ + { + type: 'maxlength', + params: { max: 5 } + } + ], + fields: [document.querySelector('#field')] + }; + expect(Methods.maxlength(group)).toEqual(true); + }); + +}); + +describe('Validate > Unit > Validator > methods > equalto', () => { + it('should return true for groups with no value', () => { + document.body.innerHTML = ``; + const group = { + validators: [ + { + type: 'equalto', + params: { other: [[document.querySelector('#field2')]] } + } + ], + fields: [document.querySelector('#field1')] + }; + expect(Methods.equalto(group)).toEqual(true); + }); + + it('should return false for groups with unequal values', () => { + document.body.innerHTML = ``; + const group = { + validators: [ + { + type: 'equalto', + params: { other: [[document.querySelector('#field2')]] } + } + ], + fields: [document.querySelector('#field1')] + }; + expect(Methods.equalto(group)).toEqual(false); + }); + + it('should return true for groups with unequal values', () => { + document.body.innerHTML = ``; + const group = { + validators: [ + { + type: 'equalto', + params: { other: [[document.querySelector('#field2')]] } + } + ], + fields: [document.querySelector('#field1')] + }; + expect(Methods.equalto(group)).toEqual(true); + }); + +}); + +describe('Validate > Unit > Validator > methods > pattern', () => { + + it('should return true for group with no value that is not required', () => { + document.body.innerHTML = ``; + const group = { + validators: [ + { + type: 'pattern', + params: { regex: /^(pass)$/ } + } + ], + fields: [document.querySelector('#field')] + }; + expect(Methods.pattern(group)).toEqual(true); + }); + + it('should return false for group with a non-matching value', () => { + document.body.innerHTML = ``; + const group = { + validators: [ + { + type: 'pattern', + params: { regex: /^(pass)$/ } + } + ], + fields: [document.querySelector('#field')] + }; + expect(Methods.pattern(group)).toEqual(false); + }); + + it('should return false for group with a matching value', () => { + document.body.innerHTML = ``; + const group = { + validators: [ + { + type: 'pattern', + params: { regex: /^(pass)$/ } + } + ], + fields: [document.querySelector('#field')] + }; + expect(Methods.pattern(group)).toEqual(true); + }); + +}); + +describe('Validate > Unit > Validator > methods > regex', () => { + + it('should return true for group with no value that is not required', () => { + document.body.innerHTML = ``; + const group = { + validators: [ + { + type: 'regex', + params: { pattern: /^(pass)$/ } + } + ], + fields: [document.querySelector('#field')] + }; + expect(Methods.regex(group)).toEqual(true); + }); + + it('should return false for group with a non-matching value', () => { + document.body.innerHTML = ``; + const group = { + validators: [ + { + type: 'regex', + params: { pattern: /^(pass)$/ } + } + ], + fields: [document.querySelector('#field')] + }; + expect(Methods.regex(group)).toEqual(false); + }); + + it('should return false for group with a matching value', () => { + document.body.innerHTML = ``; + const group = { + validators: [ + { + type: 'regex', + params: { pattern: /^(pass)$/ } + } + ], + fields: [document.querySelector('#field')] + }; + expect(Methods.regex(group)).toEqual(true); + }); + +}); + +describe('Validate > Unit > Validator > methods > min', () => { + it('should return true for group with no value that is not required', () => { + document.body.innerHTML = ``; + const group = { + validators: [ + { + type: 'min', + params: { min: 3 } + } + ], + fields: [document.querySelector('#field')] + }; + expect(Methods.min(group)).toEqual(true); + }); + + it('should return false for group with a value < min', () => { + document.body.innerHTML = ``; + const group = { + validators: [ + { + type: 'min', + params: { min: 3 } + } + ], + fields: [document.querySelector('#field')] + }; + expect(Methods.min(group)).toEqual(false); + }); + + it('should return true for group with a value => min', () => { + document.body.innerHTML = ``; + const group = { + validators: [ + { + type: 'min', + params: { min: 3 } + } + ], + fields: [document.querySelector('#field')] + }; + expect(Methods.min(group)).toEqual(true); + }); + +}); + +describe('Validate > Unit > Validator > methods > max', () => { + it('should return true for group with no value that is not required', () => { + document.body.innerHTML = ``; + const group = { + validators: [ + { + type: 'max', + params: { max: 3 } + } + ], + fields: [document.querySelector('#field')] + }; + expect(Methods.max(group)).toEqual(true); + }); + + it('should return false for group with a value > max', () => { + document.body.innerHTML = ``; + const group = { + validators: [ + { + type: 'max', + params: { max: 3 } + } + ], + fields: [document.querySelector('#field')] + }; + expect(Methods.max(group)).toEqual(false); + }); + + it('should return true for group with a value <= max', () => { + document.body.innerHTML = ``; + const group = { + validators: [ + { + type: 'max', + params: { max: 5 } + } + ], + fields: [document.querySelector('#field')] + }; + expect(Methods.max(group)).toEqual(true); + }); + +}); + +describe('Validate > Unit > Validator > methods > stringlength', () => { + it('should return true for group with no value that is not required', () => { + document.body.innerHTML = ``; + const group = { + validators: [ + { + type: 'stringlength', + params: { max: 3 } + } + ], + fields: [document.querySelector('#field')] + }; + expect(Methods.stringlength(group)).toEqual(true); + }); + + it('should return false for group with a value > max', () => { + document.body.innerHTML = ``; + const group = { + validators: [ + { + type: 'stringlength', + params: { max: 5 } + } + ], + fields: [document.querySelector('#field')] + }; + expect(Methods.stringlength(group)).toEqual(false); + }); + + it('should return true for group with a value <= max', () => { + document.body.innerHTML = ``; + const group = { + validators: [ + { + type: 'stringlength', + params: { max: 5 } + } + ], + fields: [document.querySelector('#field')] + }; + expect(Methods.stringlength(group)).toEqual(true); + }); + +}); + +describe('Validate > Unit > Validator > methods > length', () => { + it('should return true for group with no value that is not required', () => { + document.body.innerHTML = ``; + const group = { + validators: [ + { + type: 'length', + params: { min: 5, max: 8 } + } + ], + fields: [document.querySelector('#field')] + }; + expect(Methods.length(group)).toEqual(true); + }); + + it('should return false for group with a value > max', () => { + document.body.innerHTML = ``; + const group = { + validators: [ + { + type: 'length', + params: { min: 5, max: 8 } + } + ], + fields: [document.querySelector('#field')] + }; + expect(Methods.length(group)).toEqual(false); + }); + + it('should return false for group with a value < min', () => { + document.body.innerHTML = ``; + const group = { + validators: [ + { + type: 'length', + params: { min: 5, max: 8 } + } + ], + fields: [document.querySelector('#field')] + }; + expect(Methods.length(group)).toEqual(false); + }); + + it('should return true for group with a value >= min and <= max', () => { + document.body.innerHTML = ``; + const group = { + validators: [ + { + type: 'length', + params: { min: 5, max: 8 } + } + ], + fields: [document.querySelector('#field')] + }; + expect(Methods.length(group)).toEqual(true); + }); + +}); + +describe('Validate > Unit > Validator > methods > range', () => { + it('should return true for group with no value that is not required', () => { + document.body.innerHTML = ``; + const group = { + validators: [ + { + type: 'range', + params: { min: 5, max: 8 } + } + ], + fields: [document.querySelector('#field')] + }; + expect(Methods.range(group)).toEqual(true); + }); + + it('should return false for group with a value > max', () => { + document.body.innerHTML = ``; + const group = { + validators: [ + { + type: 'range', + params: { min: 5, max: 8 } + } + ], + fields: [document.querySelector('#field')] + }; + expect(Methods.range(group)).toEqual(false); + }); + + it('should return false for group with a value < min', () => { + document.body.innerHTML = ``; + const group = { + validators: [ + { + type: 'range', + params: { min: 5, max: 8 } + } + ], + fields: [document.querySelector('#field')] + }; + expect(Methods.range(group)).toEqual(false); + }); + + it('should return true for group with a value >= min and <= max', () => { + document.body.innerHTML = ``; + const group = { + validators: [ + { + type: 'range', + params: { min: 5, max: 8 } + } + ], + fields: [document.querySelector('#field')] + }; + expect(Methods.range(group)).toEqual(true); + }); + +}); + +describe('Validate > Unit > Validator > methods > remote', () => { + beforeEach(() => mock.setup()); + afterEach(() => mock.teardown()); + + it('should return false for when the remote validation returns \'false\'', async () => { + mock.post('/api/validate', { + status: 201, + body: 'false' + }); + + document.body.innerHTML = ``; + const group = { + validators: [], + fields: [document.querySelector('#field')] + }; + const params = { + url: '/api/validate' + }; + const res = await Methods.remote(group, params); + expect(res).toEqual('false'); + }); + + it('should return false for when the remote validation returns "false"', async () => { + mock.post('/api/validate', { + status: 201, + body: 'true' + }); + document.body.innerHTML = ``; + const group = { + validators: [], + fields: [document.querySelector('#field')] + }; + const params = { + url: '/api/validate' + }; + const res = await Methods.remote(group, params); + expect(res).toEqual('true'); + }); +}); + +describe('Validate > Unit > Validator > methods > custom', () => { + const customValidator = (value, fields) => value === 'Contrived validator'; + + it('should return true for group with no value that is not required', () => { + document.body.innerHTML = ``; + const group = { + validators: [], + fields: [document.querySelector('#field')] + }; + expect(Methods.custom(customValidator, group)).toEqual(true); + }); + + it('should return false when the custom validation function return false', () => { + document.body.innerHTML = ``; + const group = { + validators: [], + fields: [document.querySelector('#field')] + }; + expect(Methods.custom(customValidator, group)).toEqual(false); + }); + + + it('should return true when the custom validation function return true', () => { + document.body.innerHTML = ``; + const group = { + validators: [], + fields: [document.querySelector('#field')] + }; + expect(Methods.custom(customValidator, group)).toEqual(true); + }); }); \ No newline at end of file diff --git a/packages/validate/__tests__/integration/normalise-validators/dateISO.js b/packages/validate/__tests__/jest/normalise-validators/dateISO.js similarity index 97% rename from packages/validate/__tests__/integration/normalise-validators/dateISO.js rename to packages/validate/__tests__/jest/normalise-validators/dateISO.js index ab5e25c7..17e98589 100644 --- a/packages/validate/__tests__/integration/normalise-validators/dateISO.js +++ b/packages/validate/__tests__/jest/normalise-validators/dateISO.js @@ -1,21 +1,21 @@ -import { normaliseValidators } from '../../../src/lib/validator'; - -describe('Validate > Integration > normalise-vaidators > dateISO', () => { - - it('should return the correct validation model for data-val email', async () => { - expect.assertions(1); - document.body.innerHTML = ``; - const input = document.querySelector('#group1'); - expect(normaliseValidators(input)).toEqual([ - { - type: 'dateISO', - message: 'DateISO error message' - } - ]); - }); +import { normaliseValidators } from '../../../src/lib/validator'; + +describe('Validate > Integration > normalise-vaidators > dateISO', () => { + + it('should return the correct validation model for data-val email', async () => { + expect.assertions(1); + document.body.innerHTML = ``; + const input = document.querySelector('#group1'); + expect(normaliseValidators(input)).toEqual([ + { + type: 'dateISO', + message: 'DateISO error message' + } + ]); + }); }); \ No newline at end of file diff --git a/packages/validate/__tests__/integration/normalise-validators/digits.js b/packages/validate/__tests__/jest/normalise-validators/digits.js similarity index 97% rename from packages/validate/__tests__/integration/normalise-validators/digits.js rename to packages/validate/__tests__/jest/normalise-validators/digits.js index 88f2878b..70d5618b 100644 --- a/packages/validate/__tests__/integration/normalise-validators/digits.js +++ b/packages/validate/__tests__/jest/normalise-validators/digits.js @@ -1,21 +1,21 @@ -import { normaliseValidators } from '../../../src/lib/validator'; - -describe('Validate > Integration > normalise-vaidators > dateISO', () => { - - it('should return the correct validation model for data-val email', async () => { - expect.assertions(1); - document.body.innerHTML = ``; - const input = document.querySelector('#group1'); - expect(normaliseValidators(input)).toEqual([ - { - type: 'digits', - message: 'Digits error message' - } - ]); - }); +import { normaliseValidators } from '../../../src/lib/validator'; + +describe('Validate > Integration > normalise-vaidators > dateISO', () => { + + it('should return the correct validation model for data-val email', async () => { + expect.assertions(1); + document.body.innerHTML = ``; + const input = document.querySelector('#group1'); + expect(normaliseValidators(input)).toEqual([ + { + type: 'digits', + message: 'Digits error message' + } + ]); + }); }); \ No newline at end of file diff --git a/packages/validate/__tests__/integration/normalise-validators/email.js b/packages/validate/__tests__/jest/normalise-validators/email.js similarity index 100% rename from packages/validate/__tests__/integration/normalise-validators/email.js rename to packages/validate/__tests__/jest/normalise-validators/email.js diff --git a/packages/validate/__tests__/integration/normalise-validators/equalto.js b/packages/validate/__tests__/jest/normalise-validators/equalto.js similarity index 100% rename from packages/validate/__tests__/integration/normalise-validators/equalto.js rename to packages/validate/__tests__/jest/normalise-validators/equalto.js diff --git a/packages/validate/__tests__/integration/normalise-validators/length.js b/packages/validate/__tests__/jest/normalise-validators/length.js similarity index 100% rename from packages/validate/__tests__/integration/normalise-validators/length.js rename to packages/validate/__tests__/jest/normalise-validators/length.js diff --git a/packages/validate/__tests__/integration/normalise-validators/max.js b/packages/validate/__tests__/jest/normalise-validators/max.js similarity index 100% rename from packages/validate/__tests__/integration/normalise-validators/max.js rename to packages/validate/__tests__/jest/normalise-validators/max.js diff --git a/packages/validate/__tests__/integration/normalise-validators/maxlength.js b/packages/validate/__tests__/jest/normalise-validators/maxlength.js similarity index 100% rename from packages/validate/__tests__/integration/normalise-validators/maxlength.js rename to packages/validate/__tests__/jest/normalise-validators/maxlength.js diff --git a/packages/validate/__tests__/integration/normalise-validators/min.js b/packages/validate/__tests__/jest/normalise-validators/min.js similarity index 100% rename from packages/validate/__tests__/integration/normalise-validators/min.js rename to packages/validate/__tests__/jest/normalise-validators/min.js diff --git a/packages/validate/__tests__/integration/normalise-validators/minlength.js b/packages/validate/__tests__/jest/normalise-validators/minlength.js similarity index 100% rename from packages/validate/__tests__/integration/normalise-validators/minlength.js rename to packages/validate/__tests__/jest/normalise-validators/minlength.js diff --git a/packages/validate/__tests__/integration/normalise-validators/number.js b/packages/validate/__tests__/jest/normalise-validators/number.js similarity index 100% rename from packages/validate/__tests__/integration/normalise-validators/number.js rename to packages/validate/__tests__/jest/normalise-validators/number.js diff --git a/packages/validate/__tests__/integration/normalise-validators/range.js b/packages/validate/__tests__/jest/normalise-validators/range.js similarity index 100% rename from packages/validate/__tests__/integration/normalise-validators/range.js rename to packages/validate/__tests__/jest/normalise-validators/range.js diff --git a/packages/validate/__tests__/integration/normalise-validators/regex-pattern.js b/packages/validate/__tests__/jest/normalise-validators/regex-pattern.js similarity index 100% rename from packages/validate/__tests__/integration/normalise-validators/regex-pattern.js rename to packages/validate/__tests__/jest/normalise-validators/regex-pattern.js diff --git a/packages/validate/__tests__/integration/normalise-validators/remote.js b/packages/validate/__tests__/jest/normalise-validators/remote.js similarity index 100% rename from packages/validate/__tests__/integration/normalise-validators/remote.js rename to packages/validate/__tests__/jest/normalise-validators/remote.js diff --git a/packages/validate/__tests__/integration/normalise-validators/required.js b/packages/validate/__tests__/jest/normalise-validators/required.js similarity index 100% rename from packages/validate/__tests__/integration/normalise-validators/required.js rename to packages/validate/__tests__/jest/normalise-validators/required.js diff --git a/packages/validate/__tests__/integration/normalise-validators/stringlength.js b/packages/validate/__tests__/jest/normalise-validators/stringlength.js similarity index 100% rename from packages/validate/__tests__/integration/normalise-validators/stringlength.js rename to packages/validate/__tests__/jest/normalise-validators/stringlength.js diff --git a/packages/validate/__tests__/integration/normalise-validators/url.js b/packages/validate/__tests__/jest/normalise-validators/url.js similarity index 100% rename from packages/validate/__tests__/integration/normalise-validators/url.js rename to packages/validate/__tests__/jest/normalise-validators/url.js diff --git a/packages/validate/__tests__/integration/plugins/date.js b/packages/validate/__tests__/jest/plugins/date.js similarity index 97% rename from packages/validate/__tests__/integration/plugins/date.js rename to packages/validate/__tests__/jest/plugins/date.js index 254fa9c0..0f8aff9a 100644 --- a/packages/validate/__tests__/integration/plugins/date.js +++ b/packages/validate/__tests__/jest/plugins/date.js @@ -1,561 +1,561 @@ -import validate from '../../../src'; -import { isValidDate, isFutureDate, isPastDate } from '../../../src/lib/plugins/methods/date'; - -describe('Validate > Integration > Plugins > Valid Date', () => { - - it('should add a validation method to a group', async () => { - expect.assertions(2); - document.body.innerHTML = ``; - - const dateErrorNode = document.querySelector('#date-error-message'); - const dayInput = document.querySelector('#dateDay'); - const dayErrorNode = document.querySelector('#date-Day-error-message'); - const monthInput = document.querySelector('#dateMonth'); - const monthErrorNode = document.querySelector('#date-Month-error-message'); - const yearInput = document.querySelector('#dateYear'); - const yearErrorNode = document.querySelector('#date-Year-error-message'); - const [ validator ] = validate('form'); - - expect(validator.getState().groups).toEqual({ - dateDay: { - serverErrorNode: dayErrorNode, - validators: [{ type: 'required', message: 'Enter a day' }], - fields: [dayInput], - valid: false - }, - dateMonth: { - serverErrorNode: monthErrorNode, - validators: [{ type: 'required', message: 'Enter a month' }], - fields: [monthInput], - valid: false - }, - dateYear: { - serverErrorNode: yearErrorNode, - validators: [{ type: 'required', message: 'Enter a year' }], - fields: [yearInput], - valid: false - } - }); - const dateFields = [ dayInput, monthInput, yearInput ]; - const message = 'Enter a valid date'; - validator.addMethod('date', isValidDate, message, dateFields); - - expect(validator.getState().groups).toEqual({ - dateDay: { - serverErrorNode: dayErrorNode, - validators: [{ type: 'required', message: 'Enter a day' }], - fields: [dayInput], - valid: false - }, - dateMonth: { - serverErrorNode: monthErrorNode, - validators: [{ type: 'required', message: 'Enter a month' }], - fields: [monthInput], - valid: false - }, - dateYear: { - serverErrorNode: yearErrorNode, - validators: [{ type: 'required', message: 'Enter a year' }], - fields: [yearInput], - valid: false - }, - date: { - serverErrorNode: dateErrorNode, - validators: [{ type: 'custom', method: isValidDate, message }], - fields: dateFields, - valid: false - } - }); - }); - - it('should return false for a missing day, month, or year', async () => { - document.body.innerHTML = ``; - - const dateErrorNode = document.querySelector('#date-error-message'); - const dayInput = document.querySelector('#dateDay'); - const monthInput = document.querySelector('#dateMonth'); - const yearInput = document.querySelector('#dateYear'); - const [ validator ] = validate('form'); - - const dateFields = [ dayInput, monthInput, yearInput ]; - const message = 'Enter a valid date'; - validator.addMethod('date', isValidDate, message, dateFields); - const validityState = await validator.validate(); - expect(validityState).toEqual(false); - expect(dateErrorNode.textContent).toEqual(message); - }); - - it('should return false for an impossible date', async () => { - document.body.innerHTML = ``; - - const dateErrorNode = document.querySelector('#date-error-message'); - const dayInput = document.querySelector('#dateDay'); - const monthInput = document.querySelector('#dateMonth'); - const yearInput = document.querySelector('#dateYear'); - const [ validator ] = validate('form'); - - const dateFields = [ dayInput, monthInput, yearInput ]; - const message = 'Enter a valid date'; - validator.addMethod('date', isValidDate, message, dateFields); - - const invalidDates = [ - ['-1', '1', '2000'], - ['32', '1', '2000'], - ['31', '2', '2000'], - ['29', '2', '2021'], - ['01', '13', '2021'] - ]; - - await Promise.all(invalidDates.map(async testcase => { - dayInput.value = testcase[0]; - monthInput.value = testcase[1]; - yearInput.value = testcase[2]; - const validityState = await validator.validate(); - expect(validityState).toEqual(false); - expect(dateErrorNode.textContent).toEqual(message); - })); - }); - - it('should return true for a valid date', async () => { - document.body.innerHTML = ``; - - const dayInput = document.querySelector('#dateDay'); - const monthInput = document.querySelector('#dateMonth'); - const yearInput = document.querySelector('#dateYear'); - const [ validator ] = validate('form'); - - const dateFields = [ dayInput, monthInput, yearInput ]; - const message = 'Enter a valid date'; - validator.addMethod('date', isValidDate, message, dateFields); - const validityState = await validator.validate(); - expect(validityState).toEqual(true); - }); - - -}); - -describe('Validate > Integration > Plugins > Date in future', () => { - - it('should add a validation method to a group', async () => { - expect.assertions(2); - document.body.innerHTML = ``; - - const dateErrorNode = document.querySelector('#date-error-message'); - const dayInput = document.querySelector('#dateDay'); - const dayErrorNode = document.querySelector('#date-Day-error-message'); - const monthInput = document.querySelector('#dateMonth'); - const monthErrorNode = document.querySelector('#date-Month-error-message'); - const yearInput = document.querySelector('#dateYear'); - const yearErrorNode = document.querySelector('#date-Year-error-message'); - const [ validator ] = validate('form'); - - expect(validator.getState().groups).toEqual({ - dateDay: { - serverErrorNode: dayErrorNode, - validators: [{ type: 'required', message: 'Enter a day' }], - fields: [dayInput], - valid: false - }, - dateMonth: { - serverErrorNode: monthErrorNode, - validators: [{ type: 'required', message: 'Enter a month' }], - fields: [monthInput], - valid: false - }, - dateYear: { - serverErrorNode: yearErrorNode, - validators: [{ type: 'required', message: 'Enter a year' }], - fields: [yearInput], - valid: false - } - }); - const dateFields = [ dayInput, monthInput, yearInput ]; - const message = 'Enter a date in the future'; - validator.addMethod('date', isFutureDate, message, dateFields); - - expect(validator.getState().groups).toEqual({ - dateDay: { - serverErrorNode: dayErrorNode, - validators: [{ type: 'required', message: 'Enter a day' }], - fields: [dayInput], - valid: false - }, - dateMonth: { - serverErrorNode: monthErrorNode, - validators: [{ type: 'required', message: 'Enter a month' }], - fields: [monthInput], - valid: false - }, - dateYear: { - serverErrorNode: yearErrorNode, - validators: [{ type: 'required', message: 'Enter a year' }], - fields: [yearInput], - valid: false - }, - date: { - serverErrorNode: dateErrorNode, - validators: [{ type: 'custom', method: isFutureDate, message }], - fields: dateFields, - valid: false - } - }); - }); - - it('should return false for a date in the past', async () => { - document.body.innerHTML = ``; - - const dateErrorNode = document.querySelector('#date-error-message'); - const dayInput = document.querySelector('#dateDay'); - const monthInput = document.querySelector('#dateMonth'); - const yearInput = document.querySelector('#dateYear'); - const [ validator ] = validate('form'); - - const dateFields = [ dayInput, monthInput, yearInput ]; - const message = 'Enter a date in the future'; - validator.addMethod('date', isFutureDate, message, dateFields); - - const validityState = await validator.validate(); - expect(validityState).toEqual(false); - expect(dateErrorNode.textContent).toEqual(message); - }); - - it('should return true for a date in the future', async () => { - const currentDateYear = new Date().getFullYear(); - - document.body.innerHTML = ``; - - const dayInput = document.querySelector('#dateDay'); - const monthInput = document.querySelector('#dateMonth'); - const yearInput = document.querySelector('#dateYear'); - const [ validator ] = validate('form'); - - const dateFields = [ dayInput, monthInput, yearInput ]; - const message = 'Enter a date in the future'; - validator.addMethod('date', isFutureDate, message, dateFields); - const validityState = await validator.validate(); - expect(validityState).toEqual(true); - }); - - it('should return false for todays date', async () => { - const currentDateYear = new Date().getFullYear(); - const currentDateMonth = new Date().getMonth() + 1; - const currentDateDay = new Date().getDate(); - - document.body.innerHTML = ``; - - const dayInput = document.querySelector('#dateDay'); - const monthInput = document.querySelector('#dateMonth'); - const yearInput = document.querySelector('#dateYear'); - const [ validator ] = validate('form'); - - const dateFields = [ dayInput, monthInput, yearInput ]; - const message = 'Enter a date in the future'; - validator.addMethod('date', isFutureDate, message, dateFields); - const validityState = await validator.validate(); - expect(validityState).toEqual(false); - }); - -}); - -describe('Validate > Integration > Plugins > Date in Past', () => { - - it('should add a validation method to a group', async () => { - expect.assertions(2); - document.body.innerHTML = ``; - - const dateErrorNode = document.querySelector('#date-error-message'); - const dayInput = document.querySelector('#dateDay'); - const dayErrorNode = document.querySelector('#date-Day-error-message'); - const monthInput = document.querySelector('#dateMonth'); - const monthErrorNode = document.querySelector('#date-Month-error-message'); - const yearInput = document.querySelector('#dateYear'); - const yearErrorNode = document.querySelector('#date-Year-error-message'); - const [ validator ] = validate('form'); - - expect(validator.getState().groups).toEqual({ - dateDay: { - serverErrorNode: dayErrorNode, - validators: [{ type: 'required', message: 'Enter a day' }], - fields: [dayInput], - valid: false - }, - dateMonth: { - serverErrorNode: monthErrorNode, - validators: [{ type: 'required', message: 'Enter a month' }], - fields: [monthInput], - valid: false - }, - dateYear: { - serverErrorNode: yearErrorNode, - validators: [{ type: 'required', message: 'Enter a year' }], - fields: [yearInput], - valid: false - } - }); - const dateFields = [ dayInput, monthInput, yearInput ]; - const message = 'Enter a date in the past'; - validator.addMethod('date', isPastDate, message, dateFields); - - expect(validator.getState().groups).toEqual({ - dateDay: { - serverErrorNode: dayErrorNode, - validators: [{ type: 'required', message: 'Enter a day' }], - fields: [dayInput], - valid: false - }, - dateMonth: { - serverErrorNode: monthErrorNode, - validators: [{ type: 'required', message: 'Enter a month' }], - fields: [monthInput], - valid: false - }, - dateYear: { - serverErrorNode: yearErrorNode, - validators: [{ type: 'required', message: 'Enter a year' }], - fields: [yearInput], - valid: false - }, - date: { - serverErrorNode: dateErrorNode, - validators: [{ type: 'custom', method: isPastDate, message }], - fields: dateFields, - valid: false - } - }); - }); - - it('should return false for a date in the future', async () => { - const currentDateYear = new Date().getFullYear(); - document.body.innerHTML = ``; - - const dateErrorNode = document.querySelector('#date-error-message'); - const dayInput = document.querySelector('#dateDay'); - const monthInput = document.querySelector('#dateMonth'); - const yearInput = document.querySelector('#dateYear'); - const [ validator ] = validate('form'); - - const dateFields = [ dayInput, monthInput, yearInput ]; - const message = 'Enter a date in the past'; - validator.addMethod('date', isPastDate, message, dateFields); - - const validityState = await validator.validate(); - expect(validityState).toEqual(false); - expect(dateErrorNode.textContent).toEqual(message); - }); - - it('should return true for a date in the past', async () => { - document.body.innerHTML = ``; - - const dayInput = document.querySelector('#dateDay'); - const monthInput = document.querySelector('#dateMonth'); - const yearInput = document.querySelector('#dateYear'); - const [ validator ] = validate('form'); - - const dateFields = [ dayInput, monthInput, yearInput ]; - const message = 'Enter a date in the past'; - validator.addMethod('date', isPastDate, message, dateFields); - const validityState = await validator.validate(); - expect(validityState).toEqual(true); - }); - - it('should return true for todays date', async () => { - const currentDateYear = new Date().getFullYear(); - const currentDateMonth = new Date().getMonth() + 1; - const currentDateDay = new Date().getDate(); - - document.body.innerHTML = ``; - - const dayInput = document.querySelector('#dateDay'); - const monthInput = document.querySelector('#dateMonth'); - const yearInput = document.querySelector('#dateYear'); - const [ validator ] = validate('form'); - - const dateFields = [ dayInput, monthInput, yearInput ]; - const message = 'Enter a date in the past'; - validator.addMethod('date', isPastDate, message, dateFields); - const validityState = await validator.validate(); - expect(validityState).toEqual(true); - }); - - +import validate from '../../../src'; +import { isValidDate, isFutureDate, isPastDate } from '../../../src/lib/plugins/methods/date'; + +describe('Validate > Integration > Plugins > Valid Date', () => { + + it('should add a validation method to a group', async () => { + expect.assertions(2); + document.body.innerHTML = ``; + + const dateErrorNode = document.querySelector('#date-error-message'); + const dayInput = document.querySelector('#dateDay'); + const dayErrorNode = document.querySelector('#date-Day-error-message'); + const monthInput = document.querySelector('#dateMonth'); + const monthErrorNode = document.querySelector('#date-Month-error-message'); + const yearInput = document.querySelector('#dateYear'); + const yearErrorNode = document.querySelector('#date-Year-error-message'); + const [ validator ] = validate('form'); + + expect(validator.getState().groups).toEqual({ + dateDay: { + serverErrorNode: dayErrorNode, + validators: [{ type: 'required', message: 'Enter a day' }], + fields: [dayInput], + valid: false + }, + dateMonth: { + serverErrorNode: monthErrorNode, + validators: [{ type: 'required', message: 'Enter a month' }], + fields: [monthInput], + valid: false + }, + dateYear: { + serverErrorNode: yearErrorNode, + validators: [{ type: 'required', message: 'Enter a year' }], + fields: [yearInput], + valid: false + } + }); + const dateFields = [ dayInput, monthInput, yearInput ]; + const message = 'Enter a valid date'; + validator.addMethod('date', isValidDate, message, dateFields); + + expect(validator.getState().groups).toEqual({ + dateDay: { + serverErrorNode: dayErrorNode, + validators: [{ type: 'required', message: 'Enter a day' }], + fields: [dayInput], + valid: false + }, + dateMonth: { + serverErrorNode: monthErrorNode, + validators: [{ type: 'required', message: 'Enter a month' }], + fields: [monthInput], + valid: false + }, + dateYear: { + serverErrorNode: yearErrorNode, + validators: [{ type: 'required', message: 'Enter a year' }], + fields: [yearInput], + valid: false + }, + date: { + serverErrorNode: dateErrorNode, + validators: [{ type: 'custom', method: isValidDate, message }], + fields: dateFields, + valid: false + } + }); + }); + + it('should return false for a missing day, month, or year', async () => { + document.body.innerHTML = ``; + + const dateErrorNode = document.querySelector('#date-error-message'); + const dayInput = document.querySelector('#dateDay'); + const monthInput = document.querySelector('#dateMonth'); + const yearInput = document.querySelector('#dateYear'); + const [ validator ] = validate('form'); + + const dateFields = [ dayInput, monthInput, yearInput ]; + const message = 'Enter a valid date'; + validator.addMethod('date', isValidDate, message, dateFields); + const validityState = await validator.validate(); + expect(validityState).toEqual(false); + expect(dateErrorNode.textContent).toEqual(message); + }); + + it('should return false for an impossible date', async () => { + document.body.innerHTML = ``; + + const dateErrorNode = document.querySelector('#date-error-message'); + const dayInput = document.querySelector('#dateDay'); + const monthInput = document.querySelector('#dateMonth'); + const yearInput = document.querySelector('#dateYear'); + const [ validator ] = validate('form'); + + const dateFields = [ dayInput, monthInput, yearInput ]; + const message = 'Enter a valid date'; + validator.addMethod('date', isValidDate, message, dateFields); + + const invalidDates = [ + ['-1', '1', '2000'], + ['32', '1', '2000'], + ['31', '2', '2000'], + ['29', '2', '2021'], + ['01', '13', '2021'] + ]; + + await Promise.all(invalidDates.map(async testcase => { + dayInput.value = testcase[0]; + monthInput.value = testcase[1]; + yearInput.value = testcase[2]; + const validityState = await validator.validate(); + expect(validityState).toEqual(false); + expect(dateErrorNode.textContent).toEqual(message); + })); + }); + + it('should return true for a valid date', async () => { + document.body.innerHTML = ``; + + const dayInput = document.querySelector('#dateDay'); + const monthInput = document.querySelector('#dateMonth'); + const yearInput = document.querySelector('#dateYear'); + const [ validator ] = validate('form'); + + const dateFields = [ dayInput, monthInput, yearInput ]; + const message = 'Enter a valid date'; + validator.addMethod('date', isValidDate, message, dateFields); + const validityState = await validator.validate(); + expect(validityState).toEqual(true); + }); + + +}); + +describe('Validate > Integration > Plugins > Date in future', () => { + + it('should add a validation method to a group', async () => { + expect.assertions(2); + document.body.innerHTML = ``; + + const dateErrorNode = document.querySelector('#date-error-message'); + const dayInput = document.querySelector('#dateDay'); + const dayErrorNode = document.querySelector('#date-Day-error-message'); + const monthInput = document.querySelector('#dateMonth'); + const monthErrorNode = document.querySelector('#date-Month-error-message'); + const yearInput = document.querySelector('#dateYear'); + const yearErrorNode = document.querySelector('#date-Year-error-message'); + const [ validator ] = validate('form'); + + expect(validator.getState().groups).toEqual({ + dateDay: { + serverErrorNode: dayErrorNode, + validators: [{ type: 'required', message: 'Enter a day' }], + fields: [dayInput], + valid: false + }, + dateMonth: { + serverErrorNode: monthErrorNode, + validators: [{ type: 'required', message: 'Enter a month' }], + fields: [monthInput], + valid: false + }, + dateYear: { + serverErrorNode: yearErrorNode, + validators: [{ type: 'required', message: 'Enter a year' }], + fields: [yearInput], + valid: false + } + }); + const dateFields = [ dayInput, monthInput, yearInput ]; + const message = 'Enter a date in the future'; + validator.addMethod('date', isFutureDate, message, dateFields); + + expect(validator.getState().groups).toEqual({ + dateDay: { + serverErrorNode: dayErrorNode, + validators: [{ type: 'required', message: 'Enter a day' }], + fields: [dayInput], + valid: false + }, + dateMonth: { + serverErrorNode: monthErrorNode, + validators: [{ type: 'required', message: 'Enter a month' }], + fields: [monthInput], + valid: false + }, + dateYear: { + serverErrorNode: yearErrorNode, + validators: [{ type: 'required', message: 'Enter a year' }], + fields: [yearInput], + valid: false + }, + date: { + serverErrorNode: dateErrorNode, + validators: [{ type: 'custom', method: isFutureDate, message }], + fields: dateFields, + valid: false + } + }); + }); + + it('should return false for a date in the past', async () => { + document.body.innerHTML = ``; + + const dateErrorNode = document.querySelector('#date-error-message'); + const dayInput = document.querySelector('#dateDay'); + const monthInput = document.querySelector('#dateMonth'); + const yearInput = document.querySelector('#dateYear'); + const [ validator ] = validate('form'); + + const dateFields = [ dayInput, monthInput, yearInput ]; + const message = 'Enter a date in the future'; + validator.addMethod('date', isFutureDate, message, dateFields); + + const validityState = await validator.validate(); + expect(validityState).toEqual(false); + expect(dateErrorNode.textContent).toEqual(message); + }); + + it('should return true for a date in the future', async () => { + const currentDateYear = new Date().getFullYear(); + + document.body.innerHTML = ``; + + const dayInput = document.querySelector('#dateDay'); + const monthInput = document.querySelector('#dateMonth'); + const yearInput = document.querySelector('#dateYear'); + const [ validator ] = validate('form'); + + const dateFields = [ dayInput, monthInput, yearInput ]; + const message = 'Enter a date in the future'; + validator.addMethod('date', isFutureDate, message, dateFields); + const validityState = await validator.validate(); + expect(validityState).toEqual(true); + }); + + it('should return false for todays date', async () => { + const currentDateYear = new Date().getFullYear(); + const currentDateMonth = new Date().getMonth() + 1; + const currentDateDay = new Date().getDate(); + + document.body.innerHTML = ``; + + const dayInput = document.querySelector('#dateDay'); + const monthInput = document.querySelector('#dateMonth'); + const yearInput = document.querySelector('#dateYear'); + const [ validator ] = validate('form'); + + const dateFields = [ dayInput, monthInput, yearInput ]; + const message = 'Enter a date in the future'; + validator.addMethod('date', isFutureDate, message, dateFields); + const validityState = await validator.validate(); + expect(validityState).toEqual(false); + }); + +}); + +describe('Validate > Integration > Plugins > Date in Past', () => { + + it('should add a validation method to a group', async () => { + expect.assertions(2); + document.body.innerHTML = ``; + + const dateErrorNode = document.querySelector('#date-error-message'); + const dayInput = document.querySelector('#dateDay'); + const dayErrorNode = document.querySelector('#date-Day-error-message'); + const monthInput = document.querySelector('#dateMonth'); + const monthErrorNode = document.querySelector('#date-Month-error-message'); + const yearInput = document.querySelector('#dateYear'); + const yearErrorNode = document.querySelector('#date-Year-error-message'); + const [ validator ] = validate('form'); + + expect(validator.getState().groups).toEqual({ + dateDay: { + serverErrorNode: dayErrorNode, + validators: [{ type: 'required', message: 'Enter a day' }], + fields: [dayInput], + valid: false + }, + dateMonth: { + serverErrorNode: monthErrorNode, + validators: [{ type: 'required', message: 'Enter a month' }], + fields: [monthInput], + valid: false + }, + dateYear: { + serverErrorNode: yearErrorNode, + validators: [{ type: 'required', message: 'Enter a year' }], + fields: [yearInput], + valid: false + } + }); + const dateFields = [ dayInput, monthInput, yearInput ]; + const message = 'Enter a date in the past'; + validator.addMethod('date', isPastDate, message, dateFields); + + expect(validator.getState().groups).toEqual({ + dateDay: { + serverErrorNode: dayErrorNode, + validators: [{ type: 'required', message: 'Enter a day' }], + fields: [dayInput], + valid: false + }, + dateMonth: { + serverErrorNode: monthErrorNode, + validators: [{ type: 'required', message: 'Enter a month' }], + fields: [monthInput], + valid: false + }, + dateYear: { + serverErrorNode: yearErrorNode, + validators: [{ type: 'required', message: 'Enter a year' }], + fields: [yearInput], + valid: false + }, + date: { + serverErrorNode: dateErrorNode, + validators: [{ type: 'custom', method: isPastDate, message }], + fields: dateFields, + valid: false + } + }); + }); + + it('should return false for a date in the future', async () => { + const currentDateYear = new Date().getFullYear(); + document.body.innerHTML = ``; + + const dateErrorNode = document.querySelector('#date-error-message'); + const dayInput = document.querySelector('#dateDay'); + const monthInput = document.querySelector('#dateMonth'); + const yearInput = document.querySelector('#dateYear'); + const [ validator ] = validate('form'); + + const dateFields = [ dayInput, monthInput, yearInput ]; + const message = 'Enter a date in the past'; + validator.addMethod('date', isPastDate, message, dateFields); + + const validityState = await validator.validate(); + expect(validityState).toEqual(false); + expect(dateErrorNode.textContent).toEqual(message); + }); + + it('should return true for a date in the past', async () => { + document.body.innerHTML = ``; + + const dayInput = document.querySelector('#dateDay'); + const monthInput = document.querySelector('#dateMonth'); + const yearInput = document.querySelector('#dateYear'); + const [ validator ] = validate('form'); + + const dateFields = [ dayInput, monthInput, yearInput ]; + const message = 'Enter a date in the past'; + validator.addMethod('date', isPastDate, message, dateFields); + const validityState = await validator.validate(); + expect(validityState).toEqual(true); + }); + + it('should return true for todays date', async () => { + const currentDateYear = new Date().getFullYear(); + const currentDateMonth = new Date().getMonth() + 1; + const currentDateDay = new Date().getDate(); + + document.body.innerHTML = ``; + + const dayInput = document.querySelector('#dateDay'); + const monthInput = document.querySelector('#dateMonth'); + const yearInput = document.querySelector('#dateYear'); + const [ validator ] = validate('form'); + + const dateFields = [ dayInput, monthInput, yearInput ]; + const message = 'Enter a date in the past'; + validator.addMethod('date', isPastDate, message, dateFields); + const validityState = await validator.validate(); + expect(validityState).toEqual(true); + }); + + }); \ No newline at end of file diff --git a/packages/validate/__tests__/integration/plugins/luhn.js b/packages/validate/__tests__/jest/plugins/luhn.js similarity index 88% rename from packages/validate/__tests__/integration/plugins/luhn.js rename to packages/validate/__tests__/jest/plugins/luhn.js index 8665ab92..a0ac58fd 100644 --- a/packages/validate/__tests__/integration/plugins/luhn.js +++ b/packages/validate/__tests__/jest/plugins/luhn.js @@ -1,123 +1,114 @@ -import validate from '../../../src'; -import luhn from '../../../src/lib/plugins/methods/luhn'; - -describe('Validate > Integration > Plugins > Luhn', () => { - - it('should add a validation method to a group', async () => { - expect.assertions(2); - document.body.innerHTML = ``; - // const form = document.querySelector('.form'); - const input = document.querySelector('#group1-1'); - const [ validator ] = validate('form'); - - expect(validator.getState().groups).toEqual({ - group1: { - serverErrorNode: false, - validators: [{ type: 'required' }], - fields: [input], - valid: false - } - }); - const message = 'Custom error'; - validator.addMethod('group1', luhn, message); - - expect(validator.getState().groups).toEqual({ - group1: { - serverErrorNode: false, - validators: [{ type: 'required' }, { type: 'custom', method: luhn, message }], - fields: [input], - valid: false - } - }); - }); - - it('should not validate a clean optional field', async () => { - expect.assertions(1); - document.body.innerHTML = ``; - const [ validator ] = validate('form'); - const message = 'Custom error'; - validator.addMethod('group1', luhn, message); - const validityState = await validator.validate(); - expect(validityState).toEqual(true); - }); - - it('should return false for a malformed credit card number', async () => { - expect.assertions(1); - document.body.innerHTML = ``; - const [ validator ] = validate('form'); - const message = 'Custom error'; - validator.addMethod('group1', luhn, message); - const validityState = await validator.validate(); - expect(validityState).toEqual(false); - }); - - it('should return true for valid credit card numbers', async () => { - const formats = { - 'American Express': 378282246310005, - 'American Express 2': 371449635398431, - 'American Express Corporate': 378734493671000, - 'Australian BankCard': 5610591081018250, - 'Diners Club': 30569309025904, - Discover: 6011111111111117, - 'Discover 2': 6011000990139424, - JCB: 3530111333300000, - 'JCB 2': 3566002020360505, - MasterCard: 5555555555554444, - 'MasterCard 2': 5105105105105100, - Visa: 4111111111111111, - 'Visa 2': 4012888888881881 - }; - expect.assertions(Object.values(formats).length); - document.body.innerHTML = ``; - const [ validator ] = validate('form'); - const field = document.querySelector('#group1-1'); - const message = 'Custom error'; - validator.addMethod('group1', luhn, message); - - for (let format in formats) { - field.value = formats[format]; - const validityState = await validator.validate(); - expect(validityState).toEqual(true); - } - - // Alternative implementation of the test... - // const validityStates = await Promise.all(Object.values(formats).map(format => async () => { - // field.value = formats[format]; - // return await validator.validate(); - // })); - - // const aggregage = validityStates.reduce((acc, curr) => !curr ? false : acc, true); - // expect(aggregage).toEqual(true); - }); - +import validate from '../../../src'; +import luhn from '../../../src/lib/plugins/methods/luhn'; + +describe('Validate > Integration > Plugins > Luhn', () => { + + it('should add a validation method to a group', async () => { + expect.assertions(2); + document.body.innerHTML = ``; + // const form = document.querySelector('.form'); + const input = document.querySelector('#group1-1'); + const [ validator ] = validate('form'); + + expect(validator.getState().groups).toEqual({ + group1: { + serverErrorNode: false, + validators: [{ type: 'required' }], + fields: [input], + valid: false + } + }); + const message = 'Custom error'; + validator.addMethod('group1', luhn, message); + + expect(validator.getState().groups).toEqual({ + group1: { + serverErrorNode: false, + validators: [{ type: 'required' }, { type: 'custom', method: luhn, message }], + fields: [input], + valid: false + } + }); + }); + + it('should not validate a clean optional field', async () => { + expect.assertions(1); + document.body.innerHTML = ``; + const [ validator ] = validate('form'); + const message = 'Custom error'; + validator.addMethod('group1', luhn, message); + const validityState = await validator.validate(); + expect(validityState).toEqual(true); + }); + + it('should return false for a malformed credit card number', async () => { + expect.assertions(1); + document.body.innerHTML = ``; + const [ validator ] = validate('form'); + const message = 'Custom error'; + validator.addMethod('group1', luhn, message); + const validityState = await validator.validate(); + expect(validityState).toEqual(false); + }); + + it('should return true for valid credit card numbers', async () => { + const formats = { + 'American Express': 378282246310005, + 'American Express 2': 371449635398431, + 'American Express Corporate': 378734493671000, + 'Australian BankCard': 5610591081018250, + 'Diners Club': 30569309025904, + Discover: 6011111111111117, + 'Discover 2': 6011000990139424, + JCB: 3530111333300000, + 'JCB 2': 3566002020360505, + MasterCard: 5555555555554444, + 'MasterCard 2': 5105105105105100, + Visa: 4111111111111111, + 'Visa 2': 4012888888881881 + }; + expect.assertions(Object.values(formats).length); + document.body.innerHTML = ``; + const [ validator ] = validate('form'); + const field = document.querySelector('#group1-1'); + const message = 'Custom error'; + validator.addMethod('group1', luhn, message); + + for (let format in formats) { + field.value = formats[format]; + const validityState = await validator.validate(); + expect(validityState).toEqual(true); + } + }); + }); \ No newline at end of file diff --git a/packages/validate/__tests__/unit/reducers.js b/packages/validate/__tests__/jest/reducers.js similarity index 98% rename from packages/validate/__tests__/unit/reducers.js rename to packages/validate/__tests__/jest/reducers.js index 44bbefee..22020a0e 100644 --- a/packages/validate/__tests__/unit/reducers.js +++ b/packages/validate/__tests__/jest/reducers.js @@ -1,7 +1,6 @@ import { ACTIONS, GROUP_ATTRIBUTE } from '../../src/lib/constants'; import Reducers from '../../src/lib/reducers'; -//set initial state describe('Validate > Unit > Reducers > Set initial state', () => { it('should compose a new object based on default empty state object and initial payload', async () => { expect.assertions(1); @@ -23,7 +22,6 @@ describe('Validate > Unit > Reducers > Set initial state', () => { }); }); -//clear errors describe('Validate > Unit > Reducers > Clear errors', () => { it('should compose a new object with each group Object containing an empty array of errorMessages and a true validity property', async () => { expect.assertions(1); @@ -69,7 +67,6 @@ describe('Validate > Unit > Reducers > Clear errors', () => { }); }); -//clear error describe('Validate > Unit > Reducers > Clear error', () => { it('should compose a new object with a group Object containing an empty array of errorMessages and a true validity property for a given group name', async () => { expect.assertions(1); @@ -115,7 +112,6 @@ describe('Validate > Unit > Reducers > Clear error', () => { }); }); -//add validation errors describe('Validate > Unit > Reducers > Add validation errors', () => { it('should compose a new object with group Objects containing an array of errorMessages and a false validity property', async () => { expect.assertions(1); @@ -168,8 +164,6 @@ describe('Validate > Unit > Reducers > Add validation errors', () => { }); }); - -//add validation error describe('Validate > Unit > Reducers > Add validation error', () => { it('should compose a new Object updating one of the group Objects with an array of errorMessages and a false validity property', async () => { expect.assertions(1); @@ -212,7 +206,6 @@ describe('Validate > Unit > Reducers > Add validation error', () => { }); }); -//add validation method describe('Validate > Unit > Reducers > Add validation method', () => { it('should add a validator of type custom to an existing field group', async () => { expect.assertions(1); @@ -341,11 +334,9 @@ describe('Validate > Unit > Reducers > Add validation method', () => { }); }); -//Add group describe('Validate > Unit > Reducers > Add group', () => { it('should add a new validation group', async () => { expect.assertions(1); - // const validatorFn = (value, fields, param) => false; const state = { groups: { group1: { @@ -392,11 +383,9 @@ describe('Validate > Unit > Reducers > Add group', () => { }); }); -//Remove group describe('Validate > Unit > Reducers > Remove group', () => { it('should remove a validation group', async () => { expect.assertions(1); - // const validatorFn = (value, fields, param) => false; const state = { groups: { group1: { diff --git a/packages/validate/__tests__/jest/reset.js b/packages/validate/__tests__/jest/reset.js new file mode 100644 index 00000000..383faf10 --- /dev/null +++ b/packages/validate/__tests__/jest/reset.js @@ -0,0 +1,30 @@ +import validate from '../../src'; + +describe('Validate > Reset', () => { + + it('should clear errors messages from state and DOM, remove error classNames and attributes, remove errors from state', async () => { + document.body.innerHTML = ``; + + const [ validator ] = validate('.form'); + await validator.validate(); + expect(validator.getState().groups.group1.valid).toEqual(false); + expect(validator.getState().groups.group2.valid).toEqual(false); + + validator.getState().form.dispatchEvent(new Event('reset')); + expect(validator.getState().groups.group1.valid).toEqual(true); + expect(validator.getState().groups.group2.valid).toEqual(true); + expect(validator.getState().groups.group1.errorMessages).toEqual([]); + expect(validator.getState().groups.group2.errorMessages).toEqual([]); + expect(validator.getState().errors.group1).toBeUndefined(); + expect(validator.getState().errors.group2).toBeUndefined(); + }); +}); diff --git a/packages/validate/__tests__/unit/store.js b/packages/validate/__tests__/jest/store.js similarity index 98% rename from packages/validate/__tests__/unit/store.js rename to packages/validate/__tests__/jest/store.js index f54b4d43..b625aa61 100644 --- a/packages/validate/__tests__/unit/store.js +++ b/packages/validate/__tests__/jest/store.js @@ -5,7 +5,7 @@ let Store; beforeAll(() => { Store = createStore(); }); -//createStore + describe('Validate > Unit > Store > createStore', () => { it('should create a store object with update and get functions', async () => { expect.assertions(5); @@ -17,7 +17,6 @@ describe('Validate > Unit > Store > createStore', () => { }); }); -//getState describe('Validate > Unit > Store > getState', () => { it('should return the state object', async () => { expect.assertions(1); @@ -25,7 +24,6 @@ describe('Validate > Unit > Store > getState', () => { }); }); -//update describe('Validate > Unit > Store > update', () => { it('should update state using reducers and nextState payload', async () => { expect.assertions(1); diff --git a/packages/validate/__tests__/jest/submit.js b/packages/validate/__tests__/jest/submit.js new file mode 100644 index 00000000..eee7b4a0 --- /dev/null +++ b/packages/validate/__tests__/jest/submit.js @@ -0,0 +1,21 @@ +import validate from '../../src'; + +describe('Validate > Integration > Submit', () => { + it('should call the submit function if validation passes', async () => { + expect.assertions(2); + document.body.innerHTML = ``; + + const submit = jest.fn(); + const [ validator ] = validate(document.querySelector('form'), { submit }); + await validator.validate({ target: true, preventDefault(){} }); + expect(validator.getState().settings.submit).toEqual(submit); + expect(submit).toBeCalled(); + }); +}); \ No newline at end of file diff --git a/packages/validate/__tests__/unit/validator/utils.js b/packages/validate/__tests__/jest/utils.js similarity index 98% rename from packages/validate/__tests__/unit/validator/utils.js rename to packages/validate/__tests__/jest/utils.js index a0374663..eab3ad08 100644 --- a/packages/validate/__tests__/unit/validator/utils.js +++ b/packages/validate/__tests__/jest/utils.js @@ -13,7 +13,7 @@ import { escapeAttributeValue, extractValueFromGroup, findErrors -} from '../../../src/lib/validator/utils'; +} from '../../src/lib/validator/utils'; describe('Validate > Unit > Utils > isCheckable', () => { it('should return true if the field is of type radio', async () => { @@ -24,6 +24,7 @@ describe('Validate > Unit > Utils > isCheckable', () => { const field = document.getElementById('radio'); expect(isCheckable(field)).toEqual(true); }); + it('should return true if the field is of type checkbox', async () => { expect.assertions(1); document.body.innerHTML = `