1 | const {MongoClient} = require('mongodb');
|
2 |
|
3 | describe('insert', () => {
|
4 | let connection;
|
5 | let db;
|
6 |
|
7 | beforeAll(async () => {
|
8 | connection = await MongoClient.connect(global.__MONGO_URI__, {useNewUrlParser: true});
|
9 | db = await connection.db(global.__MONGO_DB_NAME__);
|
10 | });
|
11 |
|
12 | afterAll(async () => {
|
13 | await connection.close();
|
14 | await db.close();
|
15 | });
|
16 |
|
17 | it('should insert a doc into collection', async () => {
|
18 | const users = db.collection('users');
|
19 |
|
20 | const mockUser = {_id: 'some-user-id', name: 'John'};
|
21 | await users.insertOne(mockUser);
|
22 |
|
23 | const insertedUser = await users.findOne({_id: 'some-user-id'});
|
24 | expect(insertedUser).toEqual(mockUser);
|
25 | });
|
26 |
|
27 | it('should insert many docs into collection', async () => {
|
28 | const users = db.collection('users');
|
29 |
|
30 | const mockUsers = [{name: 'Alice'}, {name: 'Bob'}];
|
31 | await users.insertMany(mockUsers);
|
32 |
|
33 | const insertedUsers = await users.find().toArray();
|
34 | expect(insertedUsers).toEqual([
|
35 | expect.objectContaining({name: 'John'}),
|
36 | expect.objectContaining({name: 'Alice'}),
|
37 | expect.objectContaining({name: 'Bob'})
|
38 | ]);
|
39 | });
|
40 | });
|