Skip to content

Commit

Permalink
fix(charts): objective charts now returns correct values (#790)
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffladiray authored Aug 25, 2021
1 parent 46236be commit c8e7b43
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/routes/stats.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ module.exports = function Stats(app, model, Implementation, opts) {
}

const params = { timezone: request.query.timezone, ...request.body };
let { type } = request.body;
if (type === CHART_TYPE_OBJECTIVE) { type = CHART_TYPE_VALUE; }
const { type } = request.body;

if (type === CHART_TYPE_LEADERBOARD) {
const schema = Schemas.schemas[model.name];
Expand All @@ -47,7 +46,10 @@ module.exports = function Stats(app, model, Implementation, opts) {
promise = new Implementation
.LeaderboardStatGetter(model, modelRelationship, params, request.user).perform();
} else {
promise = new Implementation[`${type}StatGetter`](model, params, opts, request.user).perform();
// Objective chart uses a value stat getter to retrieve the value.
const statGetterType = type === CHART_TYPE_OBJECTIVE ? CHART_TYPE_VALUE : type;

promise = new Implementation[`${statGetterType}StatGetter`](model, params, opts, request.user).perform();
}

if (!promise) {
Expand Down
39 changes: 39 additions & 0 deletions test/routes/stats.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
const Stats = require('../../src/routes/stats');

describe('routes > stats', () => {
describe('.get', () => {
it('should return a value for objective chart', async () => {
expect.assertions(1);

const app = {};
const model = { name: 'book' };

class FakeValueStatGetter {
perform = jest.fn(async () => ({ value: { countCurrent: 5 } }))
}

const Implementation = {
getModelName: jest.fn(() => 'books'),
ValueStatGetter: FakeValueStatGetter,
};

const next = jest.fn();
const req = {
params: { recordId: '1' },
query: { timezone: 'Europe/Paris' },
user: { renderingId: 1 },
body: { type: 'Objective' },
};
const res = {
send: jest.fn(),
};

const subject = new Stats(app, model, Implementation);
await subject.get(req, res, next);

const resMockParameters = res.send.mock.calls[0][0];

expect(resMockParameters.data.attributes.value.value).toStrictEqual(5);
});
});
});

0 comments on commit c8e7b43

Please sign in to comment.