Fixes enum comparisons

Adds check for undefined
This commit is contained in:
Joshi
2020-04-09 09:14:31 -07:00
parent 11574b7c40
commit c68edd9b7d
2 changed files with 63 additions and 5 deletions

View File

@ -214,7 +214,9 @@ export const OPERATIONS = [
{ {
name: 'enumValueIs', name: 'enumValueIs',
operation: function (input) { operation: function (input) {
return input[0] === input[1]; let stringInputs = [];
input.forEach(inputValue => stringInputs.push(inputValue !== undefined ? inputValue.toString() : ''));
return stringInputs[0] === stringInputs[1];
}, },
text: 'is', text: 'is',
appliesTo: ['enum'], appliesTo: ['enum'],
@ -226,7 +228,9 @@ export const OPERATIONS = [
{ {
name: 'enumValueIsNot', name: 'enumValueIsNot',
operation: function (input) { operation: function (input) {
return input[0] !== input[1]; let stringInputs = [];
input.forEach(inputValue => stringInputs.push(inputValue !== undefined ? inputValue.toString() : ''));
return stringInputs[0] !== stringInputs[1];
}, },
text: 'is not', text: 'is not',
appliesTo: ['enum'], appliesTo: ['enum'],
@ -238,9 +242,10 @@ export const OPERATIONS = [
{ {
name: 'valueIs', name: 'valueIs',
operation: function (input) { operation: function (input) {
const lhsValue = input[0] !== undefined ? input[0].toString() : '';
if (input[1]) { if (input[1]) {
const values = input[1].split(','); const values = input[1].split(',');
return values.find((value) => input[0].toString() === _.trim(value.toString())); return values.find((value) => lhsValue === _.trim(value.toString()));
} }
return false; return false;
}, },
@ -254,9 +259,10 @@ export const OPERATIONS = [
{ {
name: 'valueIsNot', name: 'valueIsNot',
operation: function (input) { operation: function (input) {
const lhsValue = input[0] !== undefined ? input[0].toString() : '';
if (input[1]) { if (input[1]) {
const values = input[1].split(','); const values = input[1].split(',');
const found = values.find((value) => input[0].toString() === _.trim(value.toString())); const found = values.find((value) => lhsValue === _.trim(value.toString()));
return !found; return !found;
} }
return false; return false;

View File

@ -25,8 +25,10 @@ let isOneOfOperation = OPERATIONS.find((operation) => operation.name === 'valueI
let isNotOneOfOperation = OPERATIONS.find((operation) => operation.name === 'valueIsNot'); let isNotOneOfOperation = OPERATIONS.find((operation) => operation.name === 'valueIsNot');
let isBetween = OPERATIONS.find((operation) => operation.name === 'between'); let isBetween = OPERATIONS.find((operation) => operation.name === 'between');
let isNotBetween = OPERATIONS.find((operation) => operation.name === 'notBetween'); let isNotBetween = OPERATIONS.find((operation) => operation.name === 'notBetween');
let enumIsOperation = OPERATIONS.find((operation) => operation.name === 'enumValueIs');
let enumIsNotOperation = OPERATIONS.find((operation) => operation.name === 'enumValueIsNot');
describe('Is one of and is not one of operations', function () { describe('operations', function () {
it('should evaluate isOneOf to true for number inputs', () => { it('should evaluate isOneOf to true for number inputs', () => {
const inputs = [45, "5,6,45,8"]; const inputs = [45, "5,6,45,8"];
@ -87,4 +89,54 @@ describe('Is one of and is not one of operations', function () {
const inputs = ["45", "30", "50"]; const inputs = ["45", "30", "50"];
expect(!!isNotBetween.operation(inputs)).toBeFalse(); expect(!!isNotBetween.operation(inputs)).toBeFalse();
}); });
it('should evaluate enumValueIs to true for number inputs', () => {
const inputs = [1, "1"];
expect(!!enumIsOperation.operation(inputs)).toBeTrue();
});
it('should evaluate enumValueIs to true for string inputs', () => {
const inputs = ["45", "45"];
expect(!!enumIsOperation.operation(inputs)).toBeTrue();
});
it('should evaluate enumValueIsNot to true for number inputs', () => {
const inputs = [45, "46"];
expect(!!enumIsNotOperation.operation(inputs)).toBeTrue();
});
it('should evaluate enumValueIsNot to true for string inputs', () => {
const inputs = ["45", "46"];
expect(!!enumIsNotOperation.operation(inputs)).toBeTrue();
});
it('should evaluate enumValueIs to false for number inputs', () => {
const inputs = [1, "2"];
expect(!!enumIsOperation.operation(inputs)).toBeFalse();
});
it('should evaluate enumValueIs to false for string inputs', () => {
const inputs = ["45", "46"];
expect(!!enumIsOperation.operation(inputs)).toBeFalse();
});
it('should evaluate enumValueIsNot to false for number inputs', () => {
const inputs = [45, "45"];
expect(!!enumIsNotOperation.operation(inputs)).toBeFalse();
});
it('should evaluate enumValueIsNot to false for string inputs', () => {
const inputs = ["45", "45"];
expect(!!enumIsNotOperation.operation(inputs)).toBeFalse();
});
it('should evaluate enumValueIs to false for undefined input', () => {
const inputs = [undefined, "45"];
expect(!!enumIsOperation.operation(inputs)).toBeFalse();
});
it('should evaluate enumValueIsNot to true for undefined input', () => {
const inputs = [undefined, "45"];
expect(!!enumIsNotOperation.operation(inputs)).toBeTrue();
});
}); });