11import { expect } from 'chai' ;
22
33import * as userApi from './api' ;
4-
4+ import { getUsers } from '../testUtils/userTestUtils'
5+ import { connectDb } from '../models' ;
6+ import mongoose from 'mongoose'
7+ let mongooseInstance ;
8+ let users ;
9+ before ( async ( ) => {
10+ mongooseInstance = await connectDb ( 'mongodb://localhost:27017/mytestdatabase' ) ;
11+ users = await getUsers ( )
12+ } )
13+ after ( async ( ) => {
14+ await mongooseInstance . connection . close ( )
15+ } )
516describe ( 'users' , ( ) => {
17+
618 describe ( 'user(id: String!): User' , ( ) => {
719 it ( 'returns a user when user can be found' , async ( ) => {
20+ const firstUser = users [ 0 ]
21+ expect ( firstUser . username ) . to . eql ( 'rwieruch' ) ;
822 const expectedResult = {
923 data : {
1024 user : {
11- id : '1' ,
25+ id : firstUser . id ,
1226 username : 'rwieruch' ,
1327 email : 'hello@robin.com' ,
1428 role : 'ADMIN' ,
1529 } ,
1630 } ,
1731 } ;
1832
19- const result = await userApi . user ( { id : '1' } ) ;
33+ const result = await userApi . user ( { id : firstUser . id } ) ;
2034
2135 expect ( result . data ) . to . eql ( expectedResult ) ;
2236 } ) ;
@@ -27,8 +41,8 @@ describe('users', () => {
2741 user : null ,
2842 } ,
2943 } ;
30-
31- const result = await userApi . user ( { id : '42' } ) ;
44+ // we are generating a random object id that should not be in the database
45+ const result = await userApi . user ( { id : new mongoose . Types . ObjectId ( ) } ) ;
3246
3347 expect ( result . data ) . to . eql ( expectedResult ) ;
3448 } ) ;
@@ -40,13 +54,13 @@ describe('users', () => {
4054 data : {
4155 users : [
4256 {
43- id : '1' ,
57+ id : users [ 0 ] . id ,
4458 username : 'rwieruch' ,
4559 email : 'hello@robin.com' ,
4660 role : 'ADMIN' ,
4761 } ,
4862 {
49- id : '2' ,
63+ id : users [ 1 ] . id ,
5064 username : 'ddavids' ,
5165 email : 'hello@david.com' ,
5266 role : null ,
@@ -78,7 +92,7 @@ describe('users', () => {
7892 const expectedResult = {
7993 data : {
8094 me : {
81- id : '1' ,
95+ id : users [ 0 ] . id ,
8296 username : 'rwieruch' ,
8397 email : 'hello@robin.com' ,
8498 } ,
@@ -117,15 +131,16 @@ describe('users', () => {
117131 email : 'mark@gmule.com' ,
118132 password : 'asdasdasd' ,
119133 } ) ;
120-
134+ // referesh users from db to assert on
135+ users = await getUsers ( ) ;
121136 const {
122137 data : {
123138 data : { me } ,
124139 } ,
125140 } = await userApi . me ( token ) ;
126-
141+
127142 expect ( me ) . to . eql ( {
128- id : '3' ,
143+ id : users [ 2 ] . id ,
129144 username : 'Mark' ,
130145 email : 'mark@gmule.com' ,
131146 } ) ;
@@ -175,10 +190,10 @@ describe('users', () => {
175190 login : 'ddavids' ,
176191 password : 'ddavids' ,
177192 } ) ;
178-
193+ const adminUserId = users [ 0 ] . id ;
179194 const {
180195 data : { errors } ,
181- } = await userApi . deleteUser ( { id : '1' } , token ) ;
196+ } = await userApi . deleteUser ( { id : adminUserId } , token ) ;
182197
183198 expect ( errors [ 0 ] . message ) . to . eql ( 'Not authorized as admin.' ) ;
184199 } ) ;
0 commit comments