Fix contributor list, add kredits service tests #115

Merged
raucao merged 7 commits from bugfix/114-contributor_list into master 2019-05-08 10:35:01 +00:00
4 changed files with 82 additions and 9 deletions
+18 -3
View File
@@ -45,19 +45,34 @@ export default Service.extend({
}); });
}), }),
galfert commented 2019-05-08 09:05:51 +00:00 (Migrated from github.com)
Review

I think you might want to change the dependency, so the computed property updates when the vetoed status of any of the contributions changes.

   kreditsByContributor: computed('contributionsUnconfirmed.@each.vetoed', 'contributors', function() {
I think you might want to change the dependency, so the computed property updates when the `vetoed` status of any of the contributions changes. ```suggestion kreditsByContributor: computed('contributionsUnconfirmed.@each.vetoed', 'contributors', function() { ```
raucao commented 2019-05-08 09:45:19 +00:00 (Migrated from github.com)
Review

Cool. Will that still account for new and deleted objects in the collection, and changes of other properties?

Cool. Will that still account for new and deleted objects in the collection, and changes of other properties?
galfert commented 2019-05-08 10:19:30 +00:00 (Migrated from github.com)
Review

Yes, it will also update when the array itself changes (e.g. elements added or removed). If you want to add additional properties as dependencies, you would add them like this contributionsUnconfirmed.@each.{vetoed,otherProperty}.

Yes, it will also update when the array itself changes (e.g. elements added or removed). If you want to add additional properties as dependencies, you would add them like this `contributionsUnconfirmed.@each.{vetoed,otherProperty}`.
kreditsByContributor: computed('contributionsUnconfirmed.[]', 'contributors', function() { kreditsByContributor: computed('contributionsUnconfirmed.@each.vetoed', 'contributors', function() {
const contributionsGrouped = groupBy(this.contributionsUnconfirmed, 'contributorId'); const contributionsUnconfirmed = this.contributionsUnconfirmed.filterBy('vetoed', false);
const contributionsGrouped = groupBy(contributionsUnconfirmed, 'contributorId');
const contributorsWithUnconfirmed = contributionsGrouped.map(c => c.value.toString());
const contributorsWithOnlyConfirmed = this.contributors.reject(c => contributorsWithUnconfirmed.includes(c.id))
return contributionsGrouped.map(c => { const kreditsByContributor = contributionsGrouped.map(c => {
const amountUnconfirmed = c.items.mapBy('amount').reduce((a, b) => a + b); const amountUnconfirmed = c.items.mapBy('amount').reduce((a, b) => a + b);
const contributor = this.contributors.findBy('id', c.value.toString()); const contributor = this.contributors.findBy('id', c.value.toString());
return EmberObject.create({ return EmberObject.create({
contributor: contributor, contributor: contributor,
amountUnconfirmed: amountUnconfirmed, amountUnconfirmed: amountUnconfirmed,
amountConfirmed: contributor.totalKreditsEarned, amountConfirmed: contributor.totalKreditsEarned,
amountTotal: contributor.totalKreditsEarned + amountUnconfirmed amountTotal: contributor.totalKreditsEarned + amountUnconfirmed
}) })
});
contributorsWithOnlyConfirmed.forEach(c => {
kreditsByContributor.push(EmberObject.create({
contributor: c,
amountUnconfirmed: 0,
amountConfirmed: c.totalKreditsEarned,
amountTotal: c.totalKreditsEarned
}));
}) })
return kreditsByContributor;
}), }),
init () { init () {
+18
View File
@@ -0,0 +1,18 @@
import Model from 'kredits-web/models/contribution';
const items = [];
const data = [
{ id: 1, contributorId: 1, confirmedAtBlock: 1000, claimed: false, vetoed: false, amount: 1500 },
{ id: 2, contributorId: 1, confirmedAtBlock: 1000, claimed: false, vetoed: false, amount: 5000 },
{ id: 3, contributorId: 2, confirmedAtBlock: 1000, claimed: false, vetoed: false, amount: 1500 },
{ id: 4, contributorId: 2, confirmedAtBlock: 1000, claimed: false, vetoed: false, amount: 1500 },
{ id: 5, contributorId: 1, confirmedAtBlock: 1000, claimed: false, vetoed: false, amount: 5000 },
{ id: 6, contributorId: 3, confirmedAtBlock: 2000, claimed: false, vetoed: false, amount: 5000 },
{ id: 7, contributorId: 1, confirmedAtBlock: 2000, claimed: false, vetoed: false, amount: 1500 },
{ id: 8, contributorId: 3, confirmedAtBlock: 2000, claimed: false, vetoed: true, amount: 1500 },
];
data.forEach(attrs => items.push(Model.create(attrs)));
export default items;
+3 -3
View File
@@ -3,9 +3,9 @@ import Contributor from 'kredits-web/models/contributor';
const contributors = []; const contributors = [];
const data = [ const data = [
{ id: 1, name: 'Bumi', totalKreditsEarned: 5500 }, { id: 1, name: 'Bumi', totalKreditsEarned: 11500 },
{ id: 2, name: 'Râu Cao', totalKreditsEarned: 1500 }, { id: 2, name: 'Râu Cao', totalKreditsEarned: 3000 },
{ id: 3, name: 'Manuel', totalKreditsEarned: 3000 } { id: 3, name: 'Manuel', totalKreditsEarned: 0 }
]; ];
data.forEach(attrs => contributors.push(Contributor.create(attrs))); data.forEach(attrs => contributors.push(Contributor.create(attrs)));
+43 -3
View File
@@ -1,12 +1,52 @@
import { module, test } from 'qunit'; import { module, test } from 'qunit';
import { setupTest } from 'ember-qunit'; import { setupTest } from 'ember-qunit';
import contributors from '../../fixtures/contributors';
import contributions from '../../fixtures/contributions';
module('Unit | Service | kredits', function(hooks) { module('Unit | Service | kredits', function(hooks) {
setupTest(hooks); setupTest(hooks);
// Replace this with your real tests. test('#contributionsConfirmed', function(assert) {
test('it exists', function(assert) {
let service = this.owner.lookup('service:kredits'); let service = this.owner.lookup('service:kredits');
assert.ok(service); service.set('contributions', contributions);
service.set('currentBlock', 1023);
const items = service.contributionsConfirmed;
assert.equal(items.length, 5);
});
test('#contributionsUnconfirmed', function(assert) {
let service = this.owner.lookup('service:kredits');
service.set('contributions', contributions);
service.set('currentBlock', 1023);
const items = service.contributionsUnconfirmed;
assert.equal(items.length, 3);
});
test('#kreditsByContributor', function(assert) {
let service = this.owner.lookup('service:kredits');
service.set('contributors', contributors);
service.set('contributions', contributions);
service.set('currentBlock', 1023);
const kreditsByContributor = service.kreditsByContributor;
assert.equal(kreditsByContributor.length, 3, 'includes all contributors with confirmed kredits');
const c1 = kreditsByContributor.find(k => k.contributor.id == 1);
assert.equal(c1.amountConfirmed, 11500, 'correct amount confirmed');
assert.equal(c1.amountUnconfirmed, 1500, 'correct amount unconfirmed');
assert.equal(c1.amountTotal, 13000, 'correct amount total');
const c2 = kreditsByContributor.find(k => k.contributor.id == 2);
assert.equal(c2.amountConfirmed, 3000, 'correct amount confirmed');
assert.equal(c2.amountUnconfirmed, 0, 'correct amount unconfirmed');
assert.equal(c2.amountTotal, 3000, 'correct amount total');
const c3 = kreditsByContributor.find(k => k.contributor.id == 3);
assert.equal(c3.amountConfirmed, 0, 'correct amount confirmed');
assert.equal(c3.amountUnconfirmed, 5000, 'correct amount unconfirmed');
assert.equal(c3.amountTotal, 5000, 'correct amount total');
}); });
}); });