Skip to content

Commit

Permalink
feat: Finish category CRUD
Browse files Browse the repository at this point in the history
  • Loading branch information
argentinaluiz committed Mar 13, 2022
1 parent 77d3bd4 commit ff0e947
Show file tree
Hide file tree
Showing 43 changed files with 1,155 additions and 249 deletions.
4 changes: 2 additions & 2 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
// Add the IDs of extensions you want installed when the container is created.
"extensions": [
"firsttris.vscode-jest-runner"
]
],

// Use 'forwardPorts' to make a list of ports inside the container available locally.
// "forwardPorts": [],
Expand All @@ -37,7 +37,7 @@
// "runServices": [],

// Uncomment the next line if you want to keep your containers running after VS Code shuts down.
// "shutdownAction": "none",
"shutdownAction": "stopCompose",

// Uncomment the next line to run commands after the container is created - for example installing curl.
// "postCreateCommand": "apt-get update && apt-get install -y curl",
Expand Down
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
node_modules/
.history/
.docker/zsh/history/.zsh_history
tsconfig.tsbuildinfo
tsconfig.tsbuildinfo
src/__coverage
9 changes: 5 additions & 4 deletions jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export default {
// collectCoverageFrom: undefined,

// The directory where Jest should output its coverage files
coverageDirectory: "coverage",
coverageDirectory: "__coverage",

// An array of regexp pattern strings used to skip coverage collection
// coveragePathIgnorePatterns: [
Expand All @@ -34,12 +34,13 @@ export default {
coverageProvider: "v8",

// A list of reporter names that Jest uses when writing coverage reports
// coverageReporters: [
// "json",
coverageReporters: [
"json",
"html"
// "text",
// "lcov",
// "clover"
// ],
],

// An object that configures minimum threshold enforcement for coverage results
// coverageThreshold: undefined,
Expand Down
120 changes: 60 additions & 60 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
"license": "ISC",
"devDependencies": {
"@sucrase/jest-plugin": "^2.2.0",
"@swc/core": "^1.2.140",
"@swc/jest": "^0.2.17",
"@swc/core": "^1.2.154",
"@swc/jest": "^0.2.20",
"@types/jest": "^27.4.0",
"@types/node": "^17.0.18",
"@types/uuid": "^8.3.4",
Expand Down
4 changes: 3 additions & 1 deletion src/@core/@seedwork/@types/jest.d.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
/// <reference types="jest" />

type Expected = { validator: ClassValidatorFields; data: any } | Entity;

declare namespace jest {
// noinspection JSUnusedGlobalSymbols
interface Matchers<R> {
containErrorMessages: (expected: { [field: string]: string[] }) => R;
containErrorMessages: (expected: Expected) => R;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import ErrorBag from "../../domain/errors/error-bag";

export default class BadEntityOperationError extends Error {
constructor(public error: ErrorBag, message?: string) {
super(message ?? "An entity operation was executed with error");
this.name = "BadEntityOperationError";
}
}
3 changes: 3 additions & 0 deletions src/@core/@seedwork/application/use-case.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default interface UseCase<Input, Output>{
execute(input: Input): Output | Promise<Output>;
}
39 changes: 38 additions & 1 deletion src/@core/@seedwork/domain/entity/__tests__/entity.spec.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,26 @@
import Entity from "../entity";
import { validate as uuidValidate } from "uuid";
import UniqueEntityId from "../../value-objects/unique-entity-id";
import ErrorBag from "../../errors/error-bag";

class StubEntity extends Entity<{ prop1: string; prop2: number }> {}
class StubEntity extends Entity<{ prop1: string; prop2: number }> {
protected validate(): boolean {
if (this.error.hasError()) {
return false;
}
if (this.props.prop1 === "invalid") {
this.error.add("The prop1 is invalid");
}
return this.error.notHasError();
}
}

describe("Entity Unit Tests", () => {
it("should set props and id", () => {
const entity = new StubEntity({ prop1: "prop1 value", prop2: 10 });
expect(entity.props).toMatchObject({ prop1: "prop1 value", prop2: 10 });
expect(entity.id).not.toBeNull();
expect(entity.uniqueEntityId).toBeInstanceOf(UniqueEntityId);
expect(uuidValidate(entity.id)).toBeTruthy();
});

Expand All @@ -17,6 +29,7 @@ describe("Entity Unit Tests", () => {
{ prop1: "prop1 value", prop2: 10 },
new UniqueEntityId("5490020a-e866-4229-9adc-aa44b83234c4")
);
expect(entity.uniqueEntityId).toBeInstanceOf(UniqueEntityId);
expect(entity.id).toBe("5490020a-e866-4229-9adc-aa44b83234c4");
});

Expand All @@ -31,4 +44,28 @@ describe("Entity Unit Tests", () => {
prop2: 10,
});
});

test('is_valid getter', () => {
let entity = new StubEntity({prop1: 'valid', prop2: 0});
entity['validate']();
expect(entity.is_valid).toBeTruthy();

entity = new StubEntity({prop1: 'invalid', prop2: 0});
entity['validate']();
expect(entity.is_valid).toBeFalsy();
});

test("validate method and error variable", () => {
const mockIsValidMethod = jest.spyOn(StubEntity.prototype as any, "validate");
let entity = new StubEntity({prop1: 'valid', prop2: 0});
expect(entity['validate']()).toBeTruthy();
expect(entity.error).toStrictEqual(new ErrorBag);
expect(mockIsValidMethod).toHaveBeenCalled();

const errorBag = new ErrorBag();
errorBag.add("The prop1 is invalid");
entity = new StubEntity({prop1: 'invalid', prop2: 0});
expect(entity['validate']()).toBeFalsy();
expect(entity.error).toStrictEqual(errorBag);
});
});
17 changes: 13 additions & 4 deletions src/@core/@seedwork/domain/entity/entity.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,28 @@
import ErrorBag from "../errors/error-bag";
import UniqueEntityId from "../value-objects/unique-entity-id";

export default abstract class Entity<Props = any> {
protected readonly _id: UniqueEntityId;
readonly uniqueEntityId: UniqueEntityId;
readonly error = new ErrorBag;

constructor(public readonly props: Props, id?: UniqueEntityId) {
this._id = id || new UniqueEntityId();
this.uniqueEntityId = id || new UniqueEntityId();
}

protected abstract validate(): boolean;

get is_valid(): boolean{
return this.error.notHasError();
}

get id(): string {
return this._id.value;
return this.uniqueEntityId.value;
}


toJSON(): Required<{ id: string } & Props> {
return {
id: this._id.value,
id: this.uniqueEntityId.value,
...this.props,
} as Required<{id: string} & Props>;
}
Expand Down
Loading

0 comments on commit ff0e947

Please sign in to comment.