@@ -3,7 +3,14 @@ import { getDataSourceToken, getRepositoryToken } from '@nestjs/typeorm';
3
3
import { BadRequestException , NotFoundException } from '@nestjs/common' ;
4
4
5
5
import { 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' ;
7
14
import { ConfigParam , QueryField , QueryParams } from '../../../../../types' ;
8
15
import { DataSource , Repository } from 'typeorm' ;
9
16
import {
@@ -84,11 +91,42 @@ describe('GetOne methode test', () => {
84
91
} )
85
92
) ;
86
93
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
+
87
123
const user = {
88
124
login : 'login' ,
89
125
lastName : 'lastName' ,
90
126
isActive : true ,
91
127
addresses : addresses ,
128
+ comments,
129
+ notes,
92
130
} ;
93
131
await repository . save ( Object . assign ( new Users ( ) , user ) ) ;
94
132
} ) ;
@@ -188,6 +226,17 @@ describe('GetOne methode test', () => {
188
226
expect ( selectSpy ) . toBeCalledWith ( [ ...defaultField [ 'include' ] , aliasString ] ) ;
189
227
} ) ;
190
228
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
+
191
240
it ( 'Should be correct query with params' , async ( ) => {
192
241
let joinSpy ;
193
242
let selectSpy ;
0 commit comments