UNPKG

@ifit/mongoose-dao

Version:

Mongo helper methods for working with data in a DAO or repository pattern

74 lines 2.93 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const mongoose = require("mongoose"); const mongo_connect_1 = require("./mongo-connect"); jest.mock("mongoose", () => { return { connection: { readyState: 0, }, connect: jest.fn(), }; }); describe("Mongo Connect", () => { describe("connectToMongo", () => { describe("when using undefined", () => { let MONGO_URI; let MONGO_AUTH; beforeEach(() => { MONGO_URI = process.env.MONGO_URI; MONGO_AUTH = process.env.MONGO_AUTH; process.env.MONGO_URI = "hi"; process.env.MONGO_AUTH = "mom"; }); afterEach(() => { process.env.MONGO_URI = MONGO_URI; process.env.MONGO_AUTH = MONGO_AUTH; }); it("uses the values from process.env", async () => { await (0, mongo_connect_1.connectToMongo)(); expect(mongoose.connect).toHaveBeenCalledWith("hi", mongo_connect_1.defaultOptions); }); }); describe("when using IMongoConnect Object", () => { it("should connect to mongo using options", async () => { const options = { poolSize: 2, useNewUrlParser: true, }; await (0, mongo_connect_1.connectToMongo)({ mongoUri: "mongodb://bar:123@localhost:27017/test", mongoAuth: "foo", replacePattern: "bar", options, }); expect(mongoose.connect).toHaveBeenCalledWith("mongodb://foo:123@localhost:27017/test", options); }); }); describe("when a needed property is undefined", () => { let MONGO_URI; let MONGO_AUTH; beforeEach(() => { MONGO_URI = process.env.MONGO_URI; MONGO_AUTH = process.env.MONGO_AUTH; delete process.env.MONGO_URI; delete process.env.MONGO_AUTH; }); afterEach(() => { process.env.MONGO_URI = MONGO_URI; process.env.MONGO_AUTH = MONGO_AUTH; }); it("when mongoUri is undefined it should throw if ", async () => { await expect((0, mongo_connect_1.connectToMongo)({ mongoAuth: "foo", })).rejects.toEqual(new Error("mongoUri is undefined")); }); it("when mongo mongoAuth is undefined it should throw", async () => { await expect((0, mongo_connect_1.connectToMongo)({ mongoUri: "foo", })).rejects.toEqual(new Error("mongoAuth is undefined")); }); }); }); }); //# sourceMappingURL=mongo-connect.test.js.map