Skip to content

Commit e509da4

Browse files
committed
fix tests mongoose mongodb
1 parent 5357e6b commit e509da4

File tree

3 files changed

+44
-19
lines changed

3 files changed

+44
-19
lines changed

src/models/index.js

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,19 @@ import mongoose from 'mongoose';
22

33
import User from './user';
44
import Message from './message';
5-
6-
const connectDb = () => {
7-
if (process.env.DATABASE_URL) {
8-
return mongoose.connect(process.env.DATABASE_URL);
5+
/**
6+
* @param dbUrl
7+
*/
8+
const connectDb = (dbUrl) => {
9+
if(dbUrl) {
10+
return mongoose.connect(dbUrl, { useNewUrlParser: true });
911
}
10-
12+
// order here is important
1113
if (process.env.TEST_DATABASE_URL) {
12-
return mongoose.connect(process.env.TEST_DATABASE_URL);
14+
return mongoose.connect(process.env.TEST_DATABASE_URL, { useNewUrlParser: true });
15+
}
16+
if (process.env.DATABASE_URL) {
17+
return mongoose.connect(process.env.DATABASE_URL, { useNewUrlParser: true });
1318
}
1419
};
1520

src/testUtils/userTestUtils.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import models from '../models/index';
2+
3+
export const getUsers= async () => {
4+
return models.User.find()
5+
}

src/tests/user.spec.js

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,36 @@
11
import { expect } from 'chai';
22

33
import * 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+
})
516
describe('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

Comments
 (0)