|
1 | | -const {Db, Collection} = require('mongodb'); |
| 1 | +// const {Db, Collection} = require('mongodb'); |
2 | 2 | const { Collection } = require('yaml/types'); |
3 | | -const { ensureIndex } = require('./utils') |
| 3 | +const utils = require('./utils') |
4 | 4 |
|
5 | 5 | describe("ensure-index", () => { |
6 | 6 | describe("ensureIndex", () => { |
7 | | - it("gets user input from prompt and returns true/false", () => { |
8 | | - Db.collection() = jest.fn(); |
9 | | - Collection.createIndexes() = jest.fn() |
| 7 | + utils.getUserInput = jest.fn() |
| 8 | + const mockCollection = { |
| 9 | + indexExists: jest.fn(), |
| 10 | + estimatedDocumentCount: jest.fn().mockReturnValue(700), |
| 11 | + createIndex: jest.fn(), |
| 12 | + }; |
| 13 | + const mockDatabase = { |
| 14 | + collection() { |
| 15 | + return mockCollection; |
| 16 | + }, |
| 17 | + }; |
10 | 18 |
|
11 | | - const database = new Db('db'); |
| 19 | + beforeEach(() => { |
| 20 | + mockCollection.indexExists.mockClear(); |
| 21 | + mockCollection.estimatedDocumentCount.mockClear(); |
| 22 | + mockCollection.createIndex.mockClear(); |
| 23 | + utils.getUserInput.mockClear(); |
| 24 | + }); |
| 25 | + |
| 26 | + it("index does not exists and user says yes on prompt", async () => { |
| 27 | + //setup |
| 28 | + mockCollection.indexExists.mockReturnValue(false); |
| 29 | + utils.getUserInput.mockReturnValue(true); |
| 30 | + const collection = 'whatever'; |
| 31 | + const expectedIndexObj = { jobId: 1, accountId: 1, time: 1 } |
| 32 | + |
| 33 | + //execute |
| 34 | + await utils.ensureIndex(mockDatabase, collection); |
| 35 | + |
| 36 | + //check |
| 37 | + expect(mockCollection.indexExists).toBeCalledTimes(1); |
| 38 | + expect(mockCollection.indexExists).toBeCalledWith("jobId_1_accountId_1_time_1"); |
| 39 | + expect(mockCollection.estimatedDocumentCount).toBeCalledTimes(1); |
| 40 | + expect(utils.getUserInput).toBeCalledTimes(1); |
| 41 | + expect(mockCollection.createIndex).toBeCalledTimes(1); |
| 42 | + expect(mockCollection.createIndex).toBeCalledWith(expectedIndexObj) |
| 43 | + }); |
| 44 | + it("index does not exists and user says no on prompt", async () => { |
| 45 | + //setup |
| 46 | + mockCollection.indexExists.mockReturnValue(false); |
| 47 | + utils.getUserInput.mockReturnValue(false); |
12 | 48 | const collection = 'whatever'; |
13 | | - ensureIndex(database, collection); |
| 49 | + |
| 50 | + //execute |
| 51 | + await utils.ensureIndex(mockDatabase, collection); |
| 52 | + |
| 53 | + //check |
| 54 | + expect(mockCollection.indexExists).toBeCalledTimes(1); |
| 55 | + expect(mockCollection.indexExists).toBeCalledWith("jobId_1_accountId_1_time_1"); |
| 56 | + expect(mockCollection.estimatedDocumentCount).toBeCalledTimes(1); |
| 57 | + expect(utils.getUserInput).toBeCalledTimes(1); |
| 58 | + expect(mockCollection.createIndex).toBeCalledTimes(0); |
| 59 | + }); |
| 60 | + it("index exists", async () => { |
| 61 | + //setup |
| 62 | + mockCollection.indexExists.mockReturnValue(true); |
| 63 | + const collection = 'whatever'; |
| 64 | + |
| 65 | + //execute |
| 66 | + await utils.ensureIndex(mockDatabase, collection); |
| 67 | + |
| 68 | + //check |
| 69 | + expect(mockCollection.indexExists).toBeCalledTimes(1); |
| 70 | + expect(mockCollection.indexExists).toBeCalledWith("jobId_1_accountId_1_time_1"); |
| 71 | + expect(mockCollection.estimatedDocumentCount).toBeCalledTimes(0); |
| 72 | + expect(utils.getUserInput).toBeCalledTimes(0); |
| 73 | + expect(mockCollection.createIndex).toBeCalledTimes(0); |
14 | 74 | }); |
| 75 | + |
15 | 76 | }); |
16 | 77 | }); |
0 commit comments