@@ -3,7 +3,14 @@ import { getDataSourceToken, getRepositoryToken } from '@nestjs/typeorm';
33import { BadRequestException , NotFoundException } from '@nestjs/common' ;
44
55import { TypeormMixinService } from '../../typeorm.mixin' ;
6- import { mockDBTestModule , Users , Addresses } from '../../../../../mock-utils' ;
6+ import {
7+ mockDBTestModule ,
8+ Users ,
9+ Addresses ,
10+ Notes ,
11+ Comments ,
12+ Roles ,
13+ } from '../../../../../mock-utils' ;
714import { ConfigParam , QueryField , QueryParams } from '../../../../../types' ;
815import { DataSource , Repository } from 'typeorm' ;
916import {
@@ -84,11 +91,42 @@ describe('GetOne methode test', () => {
8491 } )
8592 ) ;
8693
94+ await repository . manager . getRepository ( Roles ) . save (
95+ Object . assign ( new Roles ( ) , {
96+ name : 'user' ,
97+ key : 'USER' ,
98+ isDefault : true ,
99+ } )
100+ ) ;
101+
102+ const comments = await repository . manager . getRepository ( Comments ) . save ( [
103+ Object . assign ( new Comments ( ) , {
104+ kind : 'COMMENT' ,
105+ text : 'text' ,
106+ } ) ,
107+ Object . assign ( new Comments ( ) , {
108+ kind : 'COMMENT' ,
109+ text : 'text' ,
110+ } ) ,
111+ Object . assign ( new Comments ( ) , {
112+ kind : 'COMMENT' ,
113+ text : 'text' ,
114+ } ) ,
115+ ] ) ;
116+
117+ const notes = await repository . manager . getRepository ( Notes ) . save ( [
118+ Object . assign ( new Notes ( ) , {
119+ text : 'text' ,
120+ } ) ,
121+ ] ) ;
122+
87123 const user = {
88124 login : 'login' ,
89125 lastName : 'lastName' ,
90126 isActive : true ,
91127 addresses : addresses ,
128+ comments,
129+ notes,
92130 } ;
93131 await repository . save ( Object . assign ( new Users ( ) , user ) ) ;
94132 } ) ;
@@ -188,6 +226,17 @@ describe('GetOne methode test', () => {
188226 expect ( selectSpy ) . toBeCalledWith ( [ ...defaultField [ 'include' ] , aliasString ] ) ;
189227 } ) ;
190228
229+ it ( 'included relationships should only return unique values' , async ( ) => {
230+ defaultField [ 'include' ] = [ 'comments' , 'notes' ] ;
231+ const res = await typeormService . getOne ( {
232+ query : defaultField ,
233+ route : { id : params } ,
234+ } ) ;
235+
236+ expect ( res . data [ 'relationships' ] . comments . data . length ) . toBe ( 3 ) ;
237+ expect ( res . data [ 'relationships' ] . notes . data . length ) . toBe ( 1 ) ;
238+ } ) ;
239+
191240 it ( 'Should be correct query with params' , async ( ) => {
192241 let joinSpy ;
193242 let selectSpy ;
0 commit comments