Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,10 @@ export default class PrimaryKeySchema extends BaseUISchema {
multiple: true,
formatter: {
fromRaw: (backendVal, allOptions)=>{
/* remove the column key and pass as array */
let optValues = (backendVal||[]).map((singleVal)=>singleVal.column);
return _.filter(allOptions, (opt)=>optValues.indexOf(opt.value)>-1);
/* remove the column key and pass as array preserving constraint column order */
return (backendVal||[]).map((singleVal)=>
_.find(allOptions, (opt)=>opt.value === singleVal.column)
).filter(Boolean);
},
toRaw: (value)=>{
/* take the array and convert to column key collection */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,10 @@ export default class UniqueConstraintSchema extends BaseUISchema {
multiple: true,
formatter: {
fromRaw: (backendVal, allOptions) => {
/* remove the column key and pass as array */
let optValues = (backendVal||[]).map((singleVal) => singleVal.column);
return _.filter(allOptions, (opt) => optValues.indexOf(opt.value)>-1);
/* remove the column key and pass as array preserving constraint column order */
return (backendVal||[]).map((singleVal) =>
_.find(allOptions, (opt) => opt.value === singleVal.column)
).filter(Boolean);
},
toRaw: (value)=>{
/* take the array and convert to column key collection */
Expand Down
47 changes: 47 additions & 0 deletions web/regression/javascript/schema_ui_files/primary_key.ui.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,53 @@ describe('PrimaryKeySchema', ()=>{

});

it('columns cell formatter', ()=>{
let cellFormatter = _.find(schemaObj.fields, (f)=>f.id=='columns').cell().controlProps.formatter;
expect(cellFormatter.fromRaw([{
column: 'user_id',
},{
column: 'client_order_id',
}])).toBe('user_id,client_order_id');

expect(cellFormatter.fromRaw([])).toBe('');
});

it('columns type formatter preserves constraint column order', ()=>{
let typeFormatter = _.find(schemaObj.fields, (f)=>f.id=='columns').type().controlProps.formatter;

/* allOptions are in table column position order (alphabetical here) */
let allOptions = [
{value: 'alpha', label: 'alpha'},
{value: 'beta', label: 'beta'},
{value: 'gamma', label: 'gamma'},
];

/* backendVal comes from the constraint definition in a different order */
let backendVal = [{column: 'gamma'}, {column: 'alpha'}];

let result = typeFormatter.fromRaw(backendVal, allOptions);

/* result must preserve backendVal order, not allOptions order */
expect(result).toEqual([
{value: 'gamma', label: 'gamma'},
{value: 'alpha', label: 'alpha'},
]);

/* empty and null values should be handled gracefully */
expect(typeFormatter.fromRaw([], allOptions)).toEqual([]);
expect(typeFormatter.fromRaw(null, allOptions)).toEqual([]);
});

it('columns type formatter toRaw', ()=>{
let typeFormatter = _.find(schemaObj.fields, (f)=>f.id=='columns').type().controlProps.formatter;
expect(typeFormatter.toRaw([{value: 'user_id'}, {value: 'client_order_id'}])).toEqual([
{column: 'user_id'},
{column: 'client_order_id'},
]);
expect(typeFormatter.toRaw([])).toEqual([]);
expect(typeFormatter.toRaw(null)).toEqual([]);
});

it('validate', ()=>{
let state = {};
let setError = jest.fn();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,53 @@ describe('UniqueConstraintSchema', ()=>{

});

it('columns cell formatter', ()=>{
let cellFormatter = _.find(schemaObj.fields, (f)=>f.id=='columns').cell().controlProps.formatter;
expect(cellFormatter.fromRaw([{
column: 'user_id',
},{
column: 'client_order_id',
}])).toBe('user_id,client_order_id');

expect(cellFormatter.fromRaw([])).toBe('');
});

it('columns type formatter preserves constraint column order', ()=>{
let typeFormatter = _.find(schemaObj.fields, (f)=>f.id=='columns').type().controlProps.formatter;

/* allOptions are in table column position order (alphabetical here) */
let allOptions = [
{value: 'alpha', label: 'alpha'},
{value: 'beta', label: 'beta'},
{value: 'gamma', label: 'gamma'},
];

/* backendVal comes from the constraint definition in a different order */
let backendVal = [{column: 'gamma'}, {column: 'alpha'}];

let result = typeFormatter.fromRaw(backendVal, allOptions);

/* result must preserve backendVal order, not allOptions order */
expect(result).toEqual([
{value: 'gamma', label: 'gamma'},
{value: 'alpha', label: 'alpha'},
]);

/* empty and null values should be handled gracefully */
expect(typeFormatter.fromRaw([], allOptions)).toEqual([]);
expect(typeFormatter.fromRaw(null, allOptions)).toEqual([]);
});

it('columns type formatter toRaw', ()=>{
let typeFormatter = _.find(schemaObj.fields, (f)=>f.id=='columns').type().controlProps.formatter;
expect(typeFormatter.toRaw([{value: 'user_id'}, {value: 'client_order_id'}])).toEqual([
{column: 'user_id'},
{column: 'client_order_id'},
]);
expect(typeFormatter.toRaw([])).toEqual([]);
expect(typeFormatter.toRaw(null)).toEqual([]);
});

it('validate', ()=>{
let state = {};
let setError = jest.fn();
Expand Down
Loading