1- import { INestApplication } from '@nestjs/common' ;
2- import { Test } from '@nestjs/testing' ;
3- import request from 'supertest' ;
4- import { AppModule } from '../src/app.module' ;
5- import { TodoTaskDTO } from '../src/todo-item/dto/todo-task.dto' ;
6- import { TodoAppointmentDTO } from '../src/todo-item/dto/todo-appointment.dto' ;
7- import { getModelToken } from '@m8a/nestjs-typegoose' ;
8- import { TodoItemEntity } from '../src/todo-item/entities/todo-item.entity' ;
9- import { Model } from 'mongoose' ;
10- import { TODO_TASK_FRAGMENT , TODO_APPOINTMENT_FRAGMENT } from './graphql-fragments' ;
1+ import { getModelToken } from '@m8a/nestjs-typegoose'
2+ import { INestApplication } from '@nestjs/common'
3+ import { Test } from '@nestjs/testing'
4+ import { Model } from 'mongoose'
5+ import request from 'supertest'
6+
7+ import { AppModule } from '../src/app.module'
8+ import { TodoAppointmentDTO } from '../src/todo-item/dto/todo-appointment.dto'
9+ import { TodoTaskDTO } from '../src/todo-item/dto/todo-task.dto'
10+ import { TodoItemEntity } from '../src/todo-item/entities/todo-item.entity'
11+ import { TODO_APPOINTMENT_FRAGMENT , TODO_TASK_FRAGMENT } from './graphql-fragments'
1112
1213describe ( 'Typegoose Discriminators with Concrete DTOs' , ( ) => {
13- let app : INestApplication ;
14- let todoItemModel : Model < TodoItemEntity > ;
14+ let app : INestApplication
15+ let todoItemModel : Model < TodoItemEntity >
1516
1617 // Variables to hold the created entities so we can use them across tests
17- let task : TodoTaskDTO ;
18- let appointment : TodoAppointmentDTO ;
19- const taskTitle = `Task ${ Date . now ( ) } ` ;
20- const appointmentTitle = `Appointment ${ Date . now ( ) } ` ;
18+ let task : TodoTaskDTO
19+ let appointment : TodoAppointmentDTO
20+ const taskTitle = `Task ${ Date . now ( ) } `
21+ const appointmentTitle = `Appointment ${ Date . now ( ) } `
2122
2223 beforeAll ( async ( ) => {
2324 const moduleRef = await Test . createTestingModule ( {
24- imports : [ AppModule ] ,
25- } ) . compile ( ) ;
25+ imports : [ AppModule ]
26+ } ) . compile ( )
2627
27- app = moduleRef . createNestApplication ( ) ;
28- await app . init ( ) ;
29- todoItemModel = moduleRef . get < Model < TodoItemEntity > > ( getModelToken ( TodoItemEntity . name ) ) ;
30- } ) ;
28+ app = moduleRef . createNestApplication ( )
29+ await app . init ( )
30+ todoItemModel = moduleRef . get < Model < TodoItemEntity > > ( getModelToken ( TodoItemEntity . name ) )
31+ } )
3132
3233 beforeEach ( async ( ) => {
33- await todoItemModel . deleteMany ( { } ) ;
34- } ) ;
34+ await todoItemModel . deleteMany ( { } )
35+ } )
3536
3637 afterAll ( async ( ) => {
3738 if ( app ) {
38- await app . close ( ) ;
39+ await app . close ( )
3940 }
40- } ) ;
41+ } )
4142
4243 describe ( 'create' , ( ) => {
4344 it ( 'should create a new TodoTask' , async ( ) => {
@@ -57,17 +58,17 @@ describe('Typegoose Discriminators with Concrete DTOs', () => {
5758 todoTask : {
5859 title : taskTitle ,
5960 completed : false ,
60- priority : 1 ,
61- } ,
62- } ,
63- } ,
64- } ) ;
61+ priority : 1
62+ }
63+ }
64+ }
65+ } )
6566
66- expect ( response . body . errors ) . toBeUndefined ( ) ;
67- task = response . body . data . createOneTodoTask ;
68- expect ( task . title ) . toBe ( taskTitle ) ;
69- expect ( task . documentType ) . toBe ( 'TodoTaskEntity' ) ;
70- } ) ;
67+ expect ( response . body . errors ) . toBeUndefined ( )
68+ task = response . body . data . createOneTodoTask
69+ expect ( task . title ) . toBe ( taskTitle )
70+ expect ( task . documentType ) . toBe ( 'TodoTaskEntity' )
71+ } )
7172
7273 it ( 'should create a new TodoAppointment' , async ( ) => {
7374 const response = await request ( app . getHttpServer ( ) )
@@ -87,18 +88,18 @@ describe('Typegoose Discriminators with Concrete DTOs', () => {
8788 title : appointmentTitle ,
8889 completed : false ,
8990 dateTime : new Date ( ) ,
90- participants : [ 'Me' , 'You' ] ,
91- } ,
92- } ,
93- } ,
94- } ) ;
91+ participants : [ 'Me' , 'You' ]
92+ }
93+ }
94+ }
95+ } )
9596
96- expect ( response . body . errors ) . toBeUndefined ( ) ;
97- appointment = response . body . data . createOneTodoAppointment ;
98- expect ( appointment . title ) . toBe ( appointmentTitle ) ;
99- expect ( appointment . documentType ) . toBe ( 'TodoAppointmentEntity' ) ;
100- } ) ;
101- } ) ;
97+ expect ( response . body . errors ) . toBeUndefined ( )
98+ appointment = response . body . data . createOneTodoAppointment
99+ expect ( appointment . title ) . toBe ( appointmentTitle )
100+ expect ( appointment . documentType ) . toBe ( 'TodoAppointmentEntity' )
101+ } )
102+ } )
102103
103104 describe ( 'query and filter' , ( ) => {
104105 beforeEach ( async ( ) => {
@@ -119,12 +120,12 @@ describe('Typegoose Discriminators with Concrete DTOs', () => {
119120 todoTask : {
120121 title : taskTitle ,
121122 completed : false ,
122- priority : 1 ,
123- } ,
124- } ,
125- } ,
126- } ) ;
127- task = createTaskResponse . body . data . createOneTodoTask ;
123+ priority : 1
124+ }
125+ }
126+ }
127+ } )
128+ task = createTaskResponse . body . data . createOneTodoTask
128129
129130 const createAppointmentResponse = await request ( app . getHttpServer ( ) )
130131 . post ( '/graphql' )
@@ -143,13 +144,13 @@ describe('Typegoose Discriminators with Concrete DTOs', () => {
143144 title : appointmentTitle ,
144145 completed : false ,
145146 dateTime : new Date ( ) ,
146- participants : [ 'Me' , 'You' ] ,
147- } ,
148- } ,
149- } ,
150- } ) ;
151- appointment = createAppointmentResponse . body . data . createOneTodoAppointment ;
152- } ) ;
147+ participants : [ 'Me' , 'You' ]
148+ }
149+ }
150+ }
151+ } )
152+ appointment = createAppointmentResponse . body . data . createOneTodoAppointment
153+ } )
153154
154155 it ( 'should query for all items and verify both are returned' , async ( ) => {
155156 const queryAllResponse = await request ( app . getHttpServer ( ) )
@@ -167,14 +168,15 @@ describe('Typegoose Discriminators with Concrete DTOs', () => {
167168 }
168169 }
169170 }
170- ` ,
171- } ) ;
172- expect ( queryAllResponse . body . errors ) . toBeUndefined ( ) ;
173- expect ( queryAllResponse . body . data . todoItems . edges ) . toHaveLength ( 2 ) ;
174- const titles = queryAllResponse . body . data . todoItems . edges . map ( ( e : any ) => e . node . title ) ;
175- expect ( titles ) . toContain ( taskTitle ) ;
176- expect ( titles ) . toContain ( appointmentTitle ) ;
177- } ) ;
171+ `
172+ } )
173+ expect ( queryAllResponse . body . errors ) . toBeUndefined ( )
174+ expect ( queryAllResponse . body . data . todoItems . edges ) . toHaveLength ( 2 )
175+ const edges = queryAllResponse . body . data . todoItems . edges as { node : { title : string } } [ ]
176+ const titles = edges . map ( ( e ) => e . node . title )
177+ expect ( titles ) . toContain ( taskTitle )
178+ expect ( titles ) . toContain ( appointmentTitle )
179+ } )
178180
179181 it ( 'should filter on a base field' , async ( ) => {
180182 const filterByTitleResponse = await request ( app . getHttpServer ( ) )
@@ -190,12 +192,12 @@ describe('Typegoose Discriminators with Concrete DTOs', () => {
190192 }
191193 }
192194 }
193- ` ,
194- } ) ;
195- expect ( filterByTitleResponse . body . errors ) . toBeUndefined ( ) ;
196- expect ( filterByTitleResponse . body . data . todoItems . edges ) . toHaveLength ( 1 ) ;
197- expect ( filterByTitleResponse . body . data . todoItems . edges [ 0 ] . node . id ) . toBe ( task . id ) ;
198- } ) ;
195+ `
196+ } )
197+ expect ( filterByTitleResponse . body . errors ) . toBeUndefined ( )
198+ expect ( filterByTitleResponse . body . data . todoItems . edges ) . toHaveLength ( 1 )
199+ expect ( filterByTitleResponse . body . data . todoItems . edges [ 0 ] . node . id ) . toBe ( task . id )
200+ } )
199201
200202 it ( 'should filter on a discriminated field' , async ( ) => {
201203 const filterByPriorityResponse = await request ( app . getHttpServer ( ) )
@@ -211,13 +213,13 @@ describe('Typegoose Discriminators with Concrete DTOs', () => {
211213 }
212214 }
213215 }
214- ` ,
215- } ) ;
216- expect ( filterByPriorityResponse . body . errors ) . toBeUndefined ( ) ;
217- expect ( filterByPriorityResponse . body . data . todoTasks . edges ) . toHaveLength ( 1 ) ;
218- expect ( filterByPriorityResponse . body . data . todoTasks . edges [ 0 ] . node . id ) . toBe ( task . id ) ;
219- } ) ;
220- } ) ;
216+ `
217+ } )
218+ expect ( filterByPriorityResponse . body . errors ) . toBeUndefined ( )
219+ expect ( filterByPriorityResponse . body . data . todoTasks . edges ) . toHaveLength ( 1 )
220+ expect ( filterByPriorityResponse . body . data . todoTasks . edges [ 0 ] . node . id ) . toBe ( task . id )
221+ } )
222+ } )
221223
222224 describe ( 'update and delete' , ( ) => {
223225 beforeEach ( async ( ) => {
@@ -238,12 +240,12 @@ describe('Typegoose Discriminators with Concrete DTOs', () => {
238240 todoTask : {
239241 title : taskTitle ,
240242 completed : false ,
241- priority : 1 ,
242- } ,
243- } ,
244- } ,
245- } ) ;
246- task = createTaskResponse . body . data . createOneTodoTask ;
243+ priority : 1
244+ }
245+ }
246+ }
247+ } )
248+ task = createTaskResponse . body . data . createOneTodoTask
247249
248250 const createAppointmentResponse = await request ( app . getHttpServer ( ) )
249251 . post ( '/graphql' )
@@ -262,16 +264,16 @@ describe('Typegoose Discriminators with Concrete DTOs', () => {
262264 title : appointmentTitle ,
263265 completed : false ,
264266 dateTime : new Date ( ) ,
265- participants : [ 'Me' , 'You' ] ,
266- } ,
267- } ,
268- } ,
269- } ) ;
270- appointment = createAppointmentResponse . body . data . createOneTodoAppointment ;
271- } ) ;
267+ participants : [ 'Me' , 'You' ]
268+ }
269+ }
270+ }
271+ } )
272+ appointment = createAppointmentResponse . body . data . createOneTodoAppointment
273+ } )
272274
273275 it ( 'should update the task' , async ( ) => {
274- const updatedTaskTitle = `Updated Task ${ Date . now ( ) } ` ;
276+ const updatedTaskTitle = `Updated Task ${ Date . now ( ) } `
275277 const updateTaskResponse = await request ( app . getHttpServer ( ) )
276278 . post ( '/graphql' )
277279 . send ( {
@@ -287,14 +289,14 @@ describe('Typegoose Discriminators with Concrete DTOs', () => {
287289 input : {
288290 id : task . id ,
289291 update : {
290- title : updatedTaskTitle ,
291- } ,
292- } ,
293- } ,
294- } ) ;
295- expect ( updateTaskResponse . body . errors ) . toBeUndefined ( ) ;
296- expect ( updateTaskResponse . body . data . updateOneTodoTask . title ) . toBe ( updatedTaskTitle ) ;
297- } ) ;
292+ title : updatedTaskTitle
293+ }
294+ }
295+ }
296+ } )
297+ expect ( updateTaskResponse . body . errors ) . toBeUndefined ( )
298+ expect ( updateTaskResponse . body . data . updateOneTodoTask . title ) . toBe ( updatedTaskTitle )
299+ } )
298300
299301 it ( 'should delete the appointment' , async ( ) => {
300302 const deleteAppointmentResponse = await request ( app . getHttpServer ( ) )
@@ -306,10 +308,10 @@ describe('Typegoose Discriminators with Concrete DTOs', () => {
306308 id
307309 }
308310 }
309- ` ,
310- } ) ;
311- expect ( deleteAppointmentResponse . body . errors ) . toBeUndefined ( ) ;
312- expect ( deleteAppointmentResponse . body . data . deleteOneTodoAppointment . id ) . toBe ( appointment . id ) ;
311+ `
312+ } )
313+ expect ( deleteAppointmentResponse . body . errors ) . toBeUndefined ( )
314+ expect ( deleteAppointmentResponse . body . data . deleteOneTodoAppointment . id ) . toBe ( appointment . id )
313315
314316 // 8. Verify the appointment is deleted
315317 const queryAllAfterDeleteResponse = await request ( app . getHttpServer ( ) )
@@ -325,11 +327,11 @@ describe('Typegoose Discriminators with Concrete DTOs', () => {
325327 }
326328 }
327329 }
328- ` ,
329- } ) ;
330- expect ( queryAllAfterDeleteResponse . body . errors ) . toBeUndefined ( ) ;
331- expect ( queryAllAfterDeleteResponse . body . data . todoItems . edges ) . toHaveLength ( 1 ) ;
332- expect ( queryAllAfterDeleteResponse . body . data . todoItems . edges [ 0 ] . node . id ) . toBe ( task . id ) ;
333- } ) ;
334- } ) ;
335- } ) ;
330+ `
331+ } )
332+ expect ( queryAllAfterDeleteResponse . body . errors ) . toBeUndefined ( )
333+ expect ( queryAllAfterDeleteResponse . body . data . todoItems . edges ) . toHaveLength ( 1 )
334+ expect ( queryAllAfterDeleteResponse . body . data . todoItems . edges [ 0 ] . node . id ) . toBe ( task . id )
335+ } )
336+ } )
337+ } )
0 commit comments