Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Fix for Catalog Item with Tag Control element cannot be ordered #278

Merged
merged 1 commit into from
Apr 16, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
136 changes: 136 additions & 0 deletions src/dialog-user/services/dialogData.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,142 @@ describe('DialogDataService test', () => {
});
});

describe('#validateField', () => {
describe('when the field is required', () => {
describe('when the field is a tag control', () => {
describe('when the field forces a single value', () => {
describe('when the field value is 0 (the "choose" option)', () => {
let testField;

beforeEach(() => {
testField = {
'type': 'DialogFieldTagControl',
'default_value': 0,
'required': true,
'options': {
'force_single_value': true
}
};
});

it('does not pass validation', () => {
let validation = dialogData.validateField(testField);
expect(validation.isValid).toEqual(false);
expect(validation.message).toEqual('This field is required');
});
});

describe('when the field value is null', () => {
let testField;

beforeEach(() => {
testField = {
'type': 'DialogFieldTagControl',
'default_value': null,
'required': true,
'options': {
'force_single_value': true
}
};
});

it('does not pass validation', () => {
let validation = dialogData.validateField(testField);
expect(validation.isValid).toEqual(false);
expect(validation.message).toEqual('This field is required');
});
});

describe('when the field value is any other number', () => {
let testField;

beforeEach(() => {
testField = {
'type': 'DialogFieldTagControl',
'default_value': 1234,
'required': true,
'options': {
'force_single_value': true
}
};
});

it('passes validation', () => {
let validation = dialogData.validateField(testField);
expect(validation.isValid).toEqual(true);
expect(validation.message).toEqual('');
});
});
});

describe('when the field does not force a single value', () => {
describe('when the field value is empty', () => {
let testField;

beforeEach(() => {
testField = {
'type': 'DialogFieldTagControl',
'default_value': [],
'required': true,
'options': {
'force_single_value': false
}
};
});

it('does not pass validation', () => {
let validation = dialogData.validateField(testField);
expect(validation.isValid).toEqual(false);
expect(validation.message).toEqual('This field is required');
});
});

describe('when the field value is null', () => {
let testField;

beforeEach(() => {
testField = {
'type': 'DialogFieldTagControl',
'default_value': null,
'required': true,
'options': {
'force_single_value': false
}
};
});

it('does not pass validation', () => {
let validation = dialogData.validateField(testField);
expect(validation.isValid).toEqual(false);
expect(validation.message).toEqual('This field is required');
});
});

describe('when the field value has selected values', () => {
let testField;

beforeEach(() => {
testField = {
'type': 'DialogFieldTagControl',
'default_value': [1234],
'required': true,
'options': {
'force_single_value': false
}
};
});

it('passes validation', () => {
let validation = dialogData.validateField(testField);
expect(validation.isValid).toEqual(true);
expect(validation.message).toEqual('');
});
});
});
});
});
});

describe('#setDefaultValue', () => {
it('should allow a default value to be set', () => {
let testField = dialogField;
Expand Down
32 changes: 32 additions & 0 deletions src/dialog-user/services/dialogData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,11 @@ export default class DialogDataService {
if (field.type === 'DialogFieldCheckBox' && fieldValue === 'f') {
validation.isValid = false;
validation.message = __('This field is required');
} else if (field.type === 'DialogFieldTagControl') {
if (this.isInvalidTagControl(field.options.force_single_value, fieldValue)) {
validation.isValid = false;
validation.message = __('This field is required');
}
} else if (_.isEmpty(fieldValue)) {
validation.isValid = false;
validation.message = __('This field is required');
Expand All @@ -143,4 +148,31 @@ export default class DialogDataService {

return validation;
}

/**
* Determines if a value is a tag control and whether or not that value is valid
* @memberof DialogDataService
* @function isInvalidTagControl
* @param forceSingleValue {boolean} Whether or not the field allows multiple selections
* @param fieldValue {any} This is the value of the field in question to be validated
**/
private isInvalidTagControl(forceSingleValue, fieldValue) {
let invalid = false;

if (forceSingleValue) {
if (_.isNumber(fieldValue)) {
if (fieldValue === 0) {
invalid = true;
}
} else if (_.isEmpty(fieldValue)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this can be made a little more concise by removing it from here and removing the else below and just changing the else to an if.

invalid = true;
}
} else {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove the else but keep the if statement that is inside this. Same logic, just cuts down on a few lines

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not the same logic though, because _.isEmpty will return true when the value that gets passed in is a number, which is wrong.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, gotcha. Sorry I misinterpreted it. Looks good to me.

if (_.isEmpty(fieldValue)) {
invalid = true;
}
}

return invalid;
}
}