Skip to content

Commit

Permalink
test(component): task respository
Browse files Browse the repository at this point in the history
  • Loading branch information
acellam committed Aug 16, 2024
1 parent dfb3c9c commit ebc1b68
Show file tree
Hide file tree
Showing 8 changed files with 130 additions and 10 deletions.
4 changes: 2 additions & 2 deletions .mocharc.dev.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
],
"slow": 75,
"spec": [
"activerecord/**/*.spec.ts",
"app/**/*.spec.ts"
"components/**/*.spec.ts",
"tests/**/*.spec.ts"
],
"timeout": 60000,
"ui": "bdd",
Expand Down
4 changes: 2 additions & 2 deletions .mocharc.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
],
"slow": 75,
"spec": [
"activerecord/**/*.spec.ts",
"app/**/*.spec.ts"
"components/**/*.spec.ts",
"tests/**/*.spec.ts"
],
"timeout": 60000,
"ui": "bdd",
Expand Down
2 changes: 1 addition & 1 deletion components/task/models/task_model.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ActiveRecord } from "@fractalerp/active-record-js"
import { ActiveRecord } from "@fractalerp/active-record-js";

export interface ITaskModelDocument {
name: string;
Expand Down
11 changes: 11 additions & 0 deletions components/task/tests/factories/tasks/task_data.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export const taskOne = {
name: "Task one",
description: "Task one description",
_id: "6431bf51b21766f35002b58d"
};

export const taskTwo = {
name: "Task two",
description: "Task two description",
_id: "64328f8ca18a7ea4fc3f83b9"
};
Empty file removed components/task/tests/index.ts
Empty file.
109 changes: 109 additions & 0 deletions components/task/tests/public/respositories/task_respository.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
import { expect } from "chai";
import * as sinon from "sinon";
import { TaskRepository } from "../../../public/repositories/task_repository";
import { taskOne, taskTwo } from "../../factories/tasks/task_data";

describe("Task Repository", () => {
const sandbox = sinon.createSandbox();
let modelMock!: sinon.SinonStub<any[], any>;
const taskRepository: TaskRepository = new TaskRepository();

before(() => {
modelMock = sandbox.stub();
taskRepository.create = modelMock;
taskRepository.read = modelMock;
taskRepository.update = modelMock;
taskRepository.delete = modelMock;
});

describe("find()", () => {

context("when present", () => {

it("should find one task", async () => {
const findOneMock = modelMock.returns(Promise.resolve(taskOne));

findOneMock.resolves(taskOne);

const result = await taskRepository.read({ id: taskOne._id });

expect(result).to.deep.equal(taskOne);
expect(modelMock.calledWith({})).to.be.true;
});
});

it("should find list of tasks", async () => {
const findMock = modelMock.returns(Promise.resolve([taskOne, taskTwo]));

findMock.resolves([taskOne, taskTwo]);

const result = await taskRepository.read({});

expect(result.length).to.equal(2);
// TODO: this might be flaky
expect(result[0]).to.deep.equal(taskOne);
expect(findMock.calledWith({})).to.be.true;
});

context("when not present", () => {
it("should return empty array", async () => {
const findOneMock = modelMock.returns(Promise.resolve([]));

findOneMock.resolves([]);

const result = await taskRepository.read({ id: taskOne._id });

expect(result).to.be.empty;
expect(modelMock.calledWith({})).to.be.true;
});
});

});

describe("should create a task", async () => {
const createMock = modelMock.returns(Promise.resolve(taskOne));

createMock.resolves(taskOne);

const result = await taskRepository.create({
name: taskOne.name,
description: taskOne.description
});

expect(result).to.deep.equal(taskOne);
});

it("should update a task", async () => {
const updateMock = modelMock.returns(Promise.resolve(taskTwo));
updateMock.resolves(taskTwo);

const result = await taskRepository.update({
id: taskOne._id,
name: taskTwo.name,
description: taskTwo.description
});

expect({
name: result.name,
description: result.description
}).to.deep.equal({
name: taskTwo.name,
description: taskTwo.description
});
});

it("should delete a task", async () => {
const deleteMock = modelMock.returns(Promise.resolve(undefined));

deleteMock.resolves(undefined);
const result = await taskRepository.delete({ id: taskOne._id });

expect(result).to.equal(undefined);
});

// Tear down
afterEach(() => {
sinon.restore();
sandbox.restore();
});
});
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
"prod:server:stop": "pm2 stop ./dist/server.js",
"emit": "tsc",
"pretest": "npm run lint && npm run build:test && tsc",
"test": "DATABASE_URI='mysql://' TS_NODE_COMPILER_OPTIONS='{\"module\":\"commonjs\"}' mocha --config .mocharc.json",
"test:dev": "DATABASE_URI='mysql://' TS_NODE_COMPILER_OPTIONS='{\"module\":\"commonjs\"}' mocha --config .mocharc.dev.json",
"test:unit": "DATABASE_URI='mysql://' TS_NODE_COMPILER_OPTIONS='{\"module\":\"commonjs\"}' nyc -r lcov -e .ts -x \"*.spec.ts\" mocha --config .mocharc.json -r ts-node/register activerecord/**/*.spec.ts app/**/*.spec.ts && nyc report",
"upload_coverage": "nyc npm test && nyc report --reporter=text-lcov | coveralls",
"test": "NODE_ENV='test' RDBMS_DATABASE_URI='mysql://' TS_NODE_COMPILER_OPTIONS='{\"module\":\"commonjs\"}' mocha --config .mocharc.json",
"test:dev": "NODE_ENV='test' RDBMS_DATABASE_URI='mysql://' TS_NODE_COMPILER_OPTIONS='{\"module\":\"commonjs\"}' mocha --config .mocharc.dev.json",
"test:unit": "NODE_ENV='test' RDBMS_DATABASE_URI='mysql://' TS_NODE_COMPILER_OPTIONS='{\"module\":\"commonjs\"}' nyc -r lcov -e .ts -x \"*.spec.ts\" mocha --config .mocharc.json -r ts-node/register && nyc report",
"coverage": "NODE_ENV='test' RDBMS_DATABASE_URI='mysql://' nyc --reporter cobertura mocha --config .mocharc.dev.json",
"lint": "eslint -c .eslintrc.js '**/*.ts'",
"lint:fix": "npm run lint -- --fix",
"build:development": "grunt development",
Expand Down
2 changes: 1 addition & 1 deletion routes/fractal_router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import * as passport from "passport";
import * as appRoot from "app-root-path";
import { StatusCodes } from "http-status-codes";

import { FractalHome } from "./fractal_home_route";
import { FractalJs } from "../app";
import { FractalHome } from "./fractal_home_route";

export class FractalRouter {
public fractalJs!: FractalJs;
Expand Down

0 comments on commit ebc1b68

Please sign in to comment.