Skip to content
This repository has been archived by the owner on Mar 7, 2023. It is now read-only.

Commit

Permalink
[US-526] [FEATURE] Add pix-total-score on profile serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
MBrandone committed Jul 5, 2017
1 parent 6790c7e commit 98b338a
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
const JSONAPISerializer = require('./jsonapi-serializer');

const _ = require('lodash');

class ProfileSerializer extends JSONAPISerializer {

constructor() {
Expand All @@ -16,6 +18,9 @@ class ProfileSerializer extends JSONAPISerializer {
serializeAttributes(model, data) {
data.attributes['first-name'] = model.firstName;
data.attributes['last-name'] = model.lastName;

if(!_.isUndefined(model['pix-score']))
data.attributes['total-pix-score'] = model['pix-score'];
}

serializeRelationships(model, modelName, data) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const {describe, it, expect} = require('../../../../test-helper');
const { describe, it, expect } = require('../../../../test-helper');
const serializer = require('../../../../../lib/infrastructure/serializers/jsonapi/profile-serializer');
const Profile = require('../../../../../lib/domain/models/data/profile');
const User = require('../../../../../lib/domain/models/data/user');
Expand All @@ -8,15 +8,19 @@ describe('Unit | Serializer | JSONAPI | profile-serializer', () => {

describe('#serialize', function() {

it('should serialize a Profile into JSON:API data of type "users"', function() {
// given
const user = new User({
let user;
let areas;
let competences;
let expectedJson;

beforeEach(() => {
user = new User({
id: 'user_id',
'firstName': 'Luke',
'lastName': 'Skywalker'
});

const areas = [
areas = [
{
id: 'recAreaA',
name: 'area-name-1'
Expand All @@ -27,7 +31,7 @@ describe('Unit | Serializer | JSONAPI | profile-serializer', () => {
}
];

const competences = [
competences = [
{
id: 'recCompA',
name: 'competence-name-1',
Expand All @@ -39,19 +43,20 @@ describe('Unit | Serializer | JSONAPI | profile-serializer', () => {
areaId: 'recAreaB'
}];

const expectedJson = {
expectedJson = {
data: [{
type: 'user',
id: 'user_id',
attributes: {
'first-name': 'Luke',
'last-name': 'Skywalker'
'last-name': 'Skywalker',
'total-pix-score': 128
},
relationships: {
competences: {
data: [
{type: 'competences', id: 'recCompA'},
{type: 'competences', id: 'recCompB'}
{ type: 'competences', id: 'recCompA' },
{ type: 'competences', id: 'recCompB' }
]
}
},
Expand Down Expand Up @@ -92,7 +97,7 @@ describe('Unit | Serializer | JSONAPI | profile-serializer', () => {
attributes: {
name: 'competence-name-2',
level: 8,
'pix-score' : 128
'pix-score': 128
},
relationships: {
area: {
Expand All @@ -103,19 +108,36 @@ describe('Unit | Serializer | JSONAPI | profile-serializer', () => {
}
]
};
});

const profile = new Profile(user, competences, areas, [new Assessment({
courseId: 'courseID1',
estimatedLevel: 8,
pixScore : 128
})], [{id: 'courseID1', competences: ['recCompB']}]);
it('should serialize a Profile into JSON:API data of type "users"', function() {
// Given
const profile = new Profile(user, competences, areas,
[new Assessment(
{
courseId: 'courseID1',
estimatedLevel: 8,
pixScore: 128
})],
[{ id: 'courseID1', competences: ['recCompB'] }]);

// When
const userSerialized = serializer.serialize(profile);

// Then
expect(userSerialized).to.be.deep.equal(expectedJson);
});

it('should not serialize "total-pix-score" user attribute when no assessment', function() {
// Given
const profile = new Profile(user, competences, areas, [], []);

// When
const userSerialized = serializer.serialize(profile);

// Then
expect(userSerialized.data[0].attributes).not.to.have.property('total-pix-score');
});
});

});

0 comments on commit 98b338a

Please sign in to comment.