@ifit/mongoose-dao
Version:
Mongo helper methods for working with data in a DAO or repository pattern
102 lines • 4.72 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
require("reflect-metadata");
const list_options_1 = require("../list-interfaces/list-options");
const foo_dao_1 = require("../sample/data/data-access-objects/foo-dao");
const empty_mongoose_1 = require("../test-utils/empty-mongoose");
describe("Data Access Object", () => {
let dao;
beforeEach(async () => {
dao = new foo_dao_1.FooDAO();
});
afterEach(async () => {
await (0, empty_mongoose_1.emptyMongoose)();
});
it("Create", async () => {
const result = await dao.create({ name: "test" });
expect(result.name).toEqual("test");
expect(result.id).toBeDefined();
});
it("InsertMany", async () => {
const result = await dao.insertMany([{ name: "test1" }, { name: "test2" }, { name: "test3" }]);
expect(result).toBeInstanceOf(Array);
expect(result.length).toBe(3);
result.forEach((res) => {
expect(res.id).toBeDefined();
});
});
it("InsertMany - rawResult", async () => {
const response = await dao.insertMany([{ name: "test1" }, { name: "test2" }, { name: "test3" }], {
rawResult: true,
});
expect(response).toBeInstanceOf(Object);
expect(response).toHaveProperty("insertedCount", 3);
expect(response).toHaveProperty("result", { n: 3, ok: 1 });
});
it("Find by id", async () => {
const result = await dao.create({ name: "test" });
const findResult = await dao.findById(result.id);
expect(findResult).toEqual(result);
});
it("Find by id - no match", async () => {
const result = await dao.create({ name: "test" });
await dao.deleteById(result.id);
const findResult = await dao.findById(result.id);
expect(findResult).toBeNull();
});
it("Update", async () => {
const result = await dao.create({ name: "test" });
result.name = "test2";
const updatedResult = await dao.update(result);
const findResult = await dao.findById(result.id);
expect(updatedResult.name).toMatch(result.name);
expect(findResult.name).toMatch(findResult.name);
});
it("Update - no match", async () => {
const result = await dao.create({ name: "test" });
await dao.deleteById(result.id);
result.name = "test2";
const updatedResult = await dao.update(result);
expect(updatedResult).toBeNull();
});
it("Delete by id", async () => {
const result = await dao.create({ name: "test" });
const deleteResult = await dao.deleteById(result.id);
expect(deleteResult).toEqual(result);
const findResult = await dao.findById(result.id);
expect(findResult).toBeNull();
});
it("List - all", async () => {
await Promise.all([dao.create({ name: "test1" }), dao.create({ name: "test2" }), dao.create({ name: "test3" })]);
const all = await dao.list();
expect(all.total).toEqual(3);
expect(all.results.length).toEqual(3);
});
it("List - all", async () => {
await Promise.all([dao.create({ name: "test1" }), dao.create({ name: "test2" }), dao.create({ name: "test3" })]);
const all = await dao.list({}, new list_options_1.ListOptions({ lean: true }));
expect(all.total).toEqual(3);
expect(all.results.length).toEqual(3);
});
it("List - limited", async () => {
await Promise.all([dao.create({ name: "test1" }), dao.create({ name: "test2" }), dao.create({ name: "test3" })]);
const all = await dao.list({}, new list_options_1.ListOptions({ page: 0, pageSize: 1 }));
expect(all.total).toEqual(3);
expect(all.results.length).toEqual(1);
});
it("List - sort", async () => {
await Promise.all([dao.create({ name: "test1" }), dao.create({ name: "test2" }), dao.create({ name: "test3" })]);
const asc = await dao.list({}, new list_options_1.ListOptions({ sortAsc: true, sortField: "name" }));
const desc = await dao.list({}, new list_options_1.ListOptions({ sortAsc: false, sortField: "name" }));
expect(asc.results[0].name).toEqual("test1");
expect(desc.results[0].name).toEqual("test3");
});
it("List - search criteria", async () => {
await Promise.all([dao.create({ name: "test1" }), dao.create({ name: "test2" }), dao.create({ name: "test3" })]);
const test2 = await dao.list({ name: "test2" });
expect(test2.total).toEqual(1);
expect(test2.results.length).toEqual(1);
expect(test2.results[0].name).toEqual("test2");
});
});
//# sourceMappingURL=data-access-object.test.js.map