Skip to content

Commit

Permalink
Fix scrutinizer issues
Browse files Browse the repository at this point in the history
  • Loading branch information
Connor committed Dec 13, 2018
1 parent 2e22244 commit 4a61e57
Showing 1 changed file with 44 additions and 105 deletions.
149 changes: 44 additions & 105 deletions src/__tests__/index.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@

/** global: jest */
import { validate } from '../index'
import RULES from '../rules'
import { setMessageHandler, setMessageHandlers, getMessage, getMessageHandler } from '../messages'
Expand Down Expand Up @@ -32,42 +33,7 @@ describe('Custom messages', () => {

describe('Form Validator', () => {

const mockedRules = [];
const mockedMessages = {};

function mockValidateField(returnVal) {
const mock = jest.fn();
mock.mockReturnValue(returnVal);
validate.validateField = mock;
return mock;
}

function mockRule(name, returnVal) {
const mock = jest.fn();
mock.mockReturnValue(returnVal);
RULES[name] = mock;
mockedRules.push(name);
return mock;
}

function mockMessage(rule, returnVal) {
const mock = jest.fn();
mock.mockReturnValue(returnVal);
mockedMessages[rule] = getMessageHandler(rule);
setMessageHandler(rule, mock);
return mock;
}

function restoreMocks() {
validate.validateForm = validateForm;
validate.validateField = validateField;

mockedRules.forEach(rule => RULES[rule] = oldRules[rule]);
mockedRules.length = 0;

Object.keys(mockedMessages).forEach(rule => setMessageHandler(rule, mockedMessages[rule]));
mockedMessages.length = 0;
}


describe('validateForm', () => {
it('can pass field data to validateField', () => {
Expand Down Expand Up @@ -144,74 +110,12 @@ describe('Form Validator', () => {
});

it('can bail on first error', () => {
const formData = {
test: {
value: null,
validation: 'required|bail',
},
test2: {
value: null,
validation: 'required',
}
}

const validateField = mockValidateField();
validateField.mockReturnValueOnce({
errors: ['required'],
});
validateField.mockReturnValueOnce({
errors: ['required'],
});

expect(validateForm({ formData, includeMessages: false } )).toEqual({
errors: {
test: ['required'],
}
})

restoreMocks();
});

it('can bail on first error if bail is on second field', () => {
const formData = {
test: {
value: null,
validation: 'required',
},
test2: {
value: null,
validation: 'required|bail',
}
}

const validateField = mockValidateField();
validateField.mockReturnValueOnce({
errors: ['required'],
});
validateField.mockReturnValueOnce({
errors: ['required'],
});

expect(validateForm({ formData, includeMessages: false } )).toEqual({
errors: {
test: ['required'],
}
})

restoreMocks();
});

it('can bail on first error if bail is on third field', () => {
const formData = {
test: {
value: null,
validation: 'required',
},
test2: {
value: null,
validation: 'required',
},
test3: {
value: null,
validation: 'required|bail',
}
Expand All @@ -224,9 +128,6 @@ describe('Form Validator', () => {
validateField.mockReturnValueOnce({
errors: ['required'],
});
validateField.mockReturnValueOnce({
errors: ['required'],
});

expect(validateForm({ formData, includeMessages: false } )).toEqual({
errors: {
Expand Down Expand Up @@ -286,9 +187,7 @@ describe('Form Validator', () => {
})

describe('validateField', () => {
function createFieldData({ key="test", value, validation}={}) {
return { key, value, validation };
}


const oldWarn = console.warn;

Expand Down Expand Up @@ -402,4 +301,44 @@ describe('Form Validator', () => {

})

})
})

function createFieldData({ key="test", value, validation}={}) {
return { key, value, validation };
}

var mockedRules = [];
var mockedMessages = {};
function mockValidateField(returnVal) {
const mock = jest.fn();
mock.mockReturnValue(returnVal);
validate.validateField = mock;
return mock;
}

function mockRule(name, returnVal) {
const mock = jest.fn();
mock.mockReturnValue(returnVal);
RULES[name] = mock;
mockedRules.push(name);
return mock;
}

function mockMessage(rule, returnVal) {
const mock = jest.fn();
mock.mockReturnValue(returnVal);
mockedMessages[rule] = getMessageHandler(rule);
setMessageHandler(rule, mock);
return mock;
}

function restoreMocks() {
validate.validateForm = validateForm;
validate.validateField = validateField;

mockedRules.forEach(rule => RULES[rule] = oldRules[rule]);
mockedRules.length = 0;

Object.keys(mockedMessages).forEach(rule => setMessageHandler(rule, mockedMessages[rule]));
mockedMessages.length = 0;
}

0 comments on commit 4a61e57

Please sign in to comment.