Skip to content

Commit 9a2b1ac

Browse files
ar-macSeyZ
authored andcommitted
Add tests for reused relationships
On shallow and deep level
1 parent 618394c commit 9a2b1ac

1 file changed

Lines changed: 229 additions & 0 deletions

File tree

test/deserializer.js

Lines changed: 229 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -495,6 +495,235 @@ describe('JSON API Deserializer', function () {
495495
done(null, json);
496496
});
497497
});
498+
499+
it('should merge included and reused relationships to attributes of shallow resources', function (done) {
500+
var dataSet = {
501+
data: [{
502+
type: 'users',
503+
id: '54735750e16638ba1eee59cb',
504+
attributes: {
505+
'first-name': 'Sandro',
506+
'last-name': 'Munda'
507+
},
508+
relationships: {
509+
address: {
510+
data: { type: 'addresses', id: '54735722e16620ba1eee36af' }
511+
}
512+
}
513+
}, {
514+
type: 'users',
515+
id: '5490143e69e49d0c8f9fc6bc',
516+
attributes: {
517+
'first-name': 'Lawrence',
518+
'last-name': 'Bennett'
519+
},
520+
relationships: {
521+
address: {
522+
data: { type: 'addresses', id: '54735697e16624ba1eee36bf' }
523+
}
524+
}
525+
}, {
526+
type: 'users',
527+
id: '5490143e69e49d0c8f99bd62',
528+
attributes: {
529+
'first-name': 'Mary',
530+
'last-name': 'Munda'
531+
},
532+
relationships: {
533+
address: {
534+
data: { type: 'addresses', id: '54735722e16620ba1eee36af' }
535+
}
536+
}
537+
}],
538+
included: [{
539+
type: 'addresses',
540+
id: '54735722e16620ba1eee36af',
541+
attributes: {
542+
'address-line1': '406 Madison Court',
543+
'zip-code': '49426',
544+
country: 'USA'
545+
},
546+
relationships: {
547+
locks: {
548+
data: [{ type: 'lock', id: '1' }, { type: 'lock', id: '2' }]
549+
}
550+
}
551+
}, {
552+
type: 'addresses',
553+
id: '54735697e16624ba1eee36bf',
554+
attributes: {
555+
'address-line1': '361 Shady Lane',
556+
'zip-code': '23185',
557+
country: 'USA'
558+
}
559+
}, {
560+
type: 'lock',
561+
id: '1',
562+
attributes: {
563+
'secret-key': 'S*7v0oMf7YxCtFyA$ffy'
564+
}
565+
}, {
566+
type: 'lock',
567+
id: '2',
568+
attributes: {
569+
'secret-key': 'En8zd6ZT6#q&Fz^EwGMy'
570+
}
571+
}]
572+
};
573+
574+
new JSONAPIDeserializer()
575+
.deserialize(dataSet, function (err, json) {
576+
expect(json).to.be.an('array').with.length(3);
577+
578+
expect(json[0]).to.have.key('id', 'first-name', 'last-name',
579+
'address');
580+
581+
expect(json[0].address).to.be.eql({
582+
'address-line1': '406 Madison Court',
583+
'zip-code': '49426',
584+
country: 'USA',
585+
id: '54735722e16620ba1eee36af',
586+
locks: [
587+
{ 'secret-key': 'S*7v0oMf7YxCtFyA$ffy', id: '1' },
588+
{ 'secret-key': 'En8zd6ZT6#q&Fz^EwGMy', id: '2' }
589+
]
590+
});
591+
592+
expect(json[1]).to.have.key('id', 'first-name', 'last-name',
593+
'address');
594+
595+
expect(json[1].address).to.be.eql({
596+
id: '54735697e16624ba1eee36bf',
597+
'address-line1': '361 Shady Lane',
598+
'zip-code': '23185',
599+
country: 'USA'
600+
});
601+
602+
expect(json[2]).to.have.key('id', 'first-name', 'last-name',
603+
'address');
604+
605+
expect(json[2].address).to.be.eql({
606+
'address-line1': '406 Madison Court',
607+
'zip-code': '49426',
608+
country: 'USA',
609+
id: '54735722e16620ba1eee36af',
610+
locks: [
611+
{ 'secret-key': 'S*7v0oMf7YxCtFyA$ffy', id: '1' },
612+
{ 'secret-key': 'En8zd6ZT6#q&Fz^EwGMy', id: '2' }
613+
]
614+
});
615+
616+
done(null, json);
617+
});
618+
});
619+
620+
it('should merge included and reused relationships to attributes of nested resources', function (done) {
621+
var dataSet = {
622+
data: [{
623+
type: 'users',
624+
id: '54735750e16638ba1eee59cb',
625+
attributes: {
626+
'first-name': 'Sandro',
627+
'last-name': 'Munda'
628+
},
629+
relationships: {
630+
addresses: {
631+
data: [
632+
{ type: 'addresses', id: '54735722e16620ba1eee36af' },
633+
{ type: 'addresses', id: '54735697e16624ba1eee36bf' },
634+
{ type: 'addresses', id: '54735697e16624ba1eee36cf' }
635+
]
636+
}
637+
}
638+
}],
639+
included: [{
640+
type: 'addresses',
641+
id: '54735722e16620ba1eee36af',
642+
attributes: {
643+
'address-line1': '406 Madison Court',
644+
'zip-code': '49426',
645+
country: 'USA'
646+
},
647+
relationships: {
648+
lock: {
649+
data: { type: 'lock', id: '1' }
650+
}
651+
}
652+
}, {
653+
type: 'addresses',
654+
id: '54735697e16624ba1eee36bf',
655+
attributes: {
656+
'address-line1': '361 Shady Lane',
657+
'zip-code': '23185',
658+
country: 'USA'
659+
},
660+
relationships: {
661+
lock: {
662+
data: { type: 'lock', id: '2' }
663+
}
664+
}
665+
}, {
666+
type: 'addresses',
667+
id: '54735697e16624ba1eee36cf',
668+
attributes: {
669+
'address-line1': '123 Sth Street',
670+
'zip-code': '12332',
671+
country: 'USA'
672+
},
673+
relationships: {
674+
lock: {
675+
data: { type: 'lock', id: '1' }
676+
}
677+
}
678+
}, {
679+
type: 'lock',
680+
id: '1',
681+
attributes: {
682+
'secret-key': 'S*7v0oMf7YxCtFyA$ffy'
683+
}
684+
}, {
685+
type: 'lock',
686+
id: '2',
687+
attributes: {
688+
'secret-key': 'En8zd6ZT6#q&Fz^EwGMy'
689+
}
690+
}]
691+
};
692+
693+
new JSONAPIDeserializer()
694+
.deserialize(dataSet, function (err, json) {
695+
expect(json).to.be.an('array').with.length(1);
696+
697+
expect(json[0]).to.have.key('id', 'first-name', 'last-name',
698+
'addresses');
699+
700+
expect(json[0].addresses[0]).to.be.eql({
701+
'address-line1': '406 Madison Court',
702+
'zip-code': '49426',
703+
country: 'USA',
704+
id: '54735722e16620ba1eee36af',
705+
lock: { 'secret-key': 'S*7v0oMf7YxCtFyA$ffy', id: '1' }
706+
});
707+
708+
expect(json[0].addresses[1]).to.be.eql({
709+
'address-line1': '361 Shady Lane',
710+
'zip-code': '23185',
711+
country: 'USA',
712+
id: '54735697e16624ba1eee36bf',
713+
lock: { 'secret-key': 'En8zd6ZT6#q&Fz^EwGMy', id: '2' }
714+
});
715+
716+
expect(json[0].addresses[2].lock).to.be.eql({
717+
'address-line1': '123 Sth Street',
718+
'zip-code': '12332',
719+
country: 'USA',
720+
id: '54735697e16624ba1eee36cf',
721+
lock: { 'secret-key': 'S*7v0oMf7YxCtFyA$ffy', id: '1' }
722+
});
723+
724+
done(null, json);
725+
});
726+
});
498727
});
499728

500729
describe('Without included', function () {

0 commit comments

Comments
 (0)