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

[RFR] Do not deduplicate single choice remote complete fields #566

Merged
merged 1 commit into from
Jul 23, 2015
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
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