Skip to content

Commit

Permalink
Merge pull request #566 from marmelab/remove_duplicate_multiple_only
Browse files Browse the repository at this point in the history
[RFR] Do not deduplicate single choice remote complete fields
  • Loading branch information
fzaninotto committed Jul 23, 2015
2 parents 428c22c + cc7496d commit de7ed47
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 18 deletions.
11 changes: 8 additions & 3 deletions src/javascripts/ng-admin/Crud/repository/ReferenceRefresher.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,15 @@ class ReferenceRefresher {
var referenceFields = {};
referenceFields[field.name()] = field;

return this.ReadQueries.getAllReferencedData(referenceFields, search)
var promise = this.ReadQueries.getAllReferencedData(referenceFields, search)
.then(r => r[field.name()])
.then(results => this._transformRecords(field, results))
.then(formattedResults => this._removeDuplicates(formattedResults, currentValue));
.then(results => this._transformRecords(field, results));

if (field.type() === 'reference_many' || field.type() === 'choices') {
promise = promise.then(formattedResults => this._removeDuplicates(formattedResults, currentValue));
}

return promise;
}

getInitialChoices(field, values) {
Expand Down
51 changes: 36 additions & 15 deletions src/javascripts/test/unit/Crud/repository/ReferenceRefresherSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ describe('ReferenceRefresher', function() {
targetField: () => {
return { name: () => 'title' }
},
getMappedValue: v => v
getMappedValue: v => v,
type: () => 'reference'
};
});

Expand Down Expand Up @@ -61,23 +62,43 @@ describe('ReferenceRefresher', function() {
});
});

it('should remove already selected values from result list (to fix some UI-Select duplicated options)', function(done) {
var readQueries = new ReadQueries();
sinon.stub(readQueries, 'getAllReferencedData', function() {
return mixins.buildPromise({
post: [
{ id: 1, title: 'Discover some awesome stuff' },
{ id: 2, title: 'Another great post'}
]
describe('Choice deduplication (to fix some UI-Select duplicated options)', function() {
var refresher;
beforeEach(function() {
var readQueries = new ReadQueries();
sinon.stub(readQueries, 'getAllReferencedData', function() {
return mixins.buildPromise({
post: [
{ id: 1, title: 'Discover some awesome stuff' },
{ id: 2, title: 'Another great post'}
]
});
});

refresher = new ReferenceRefresher(readQueries);
});

var refresher = new ReferenceRefresher(readQueries);
refresher.refresh(fakeField, [1], 'foo').then(function(results) {
expect(results).toEqual([
{ value: 2, label: 'Another great post' }
]);
done();
it('should remove already selected values from result list in case of multiple choices component', function(done) {
fakeField.type = () => 'reference_many';

refresher.refresh(fakeField, [1], 'foo').then(function(results) {
expect(results).toEqual([
{ value: 2, label: 'Another great post' }
]);
done();
});
});

it('should not de-duplicate simple choice in case of single choice component', function(done) {
fakeField.type = () => 'reference';

refresher.refresh(fakeField, [1], 'foo').then(function(results) {
expect(results).toEqual([
{ value: 1, label: 'Discover some awesome stuff' },
{ value: 2, label: 'Another great post' }
]);
done();
});
});
});

Expand Down

0 comments on commit de7ed47

Please sign in to comment.