add tests
This commit is contained in:
parent
f94e266d4a
commit
f3a82bb27e
1
.gitignore
vendored
1
.gitignore
vendored
@ -1,4 +1,5 @@
|
|||||||
.vscode
|
.vscode
|
||||||
.idea
|
.idea
|
||||||
dist
|
dist
|
||||||
|
coverage
|
||||||
node_modules
|
node_modules
|
||||||
|
|||||||
@ -1,6 +1,7 @@
|
|||||||
.vscode
|
.vscode
|
||||||
.idea
|
.idea
|
||||||
node_modules
|
node_modules
|
||||||
|
coverage
|
||||||
src
|
src
|
||||||
.gitignore
|
.gitignore
|
||||||
tsconfig.json
|
tsconfig.json
|
||||||
5074
package-lock.json
generated
5074
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
38
package.json
38
package.json
@ -1,17 +1,26 @@
|
|||||||
{
|
{
|
||||||
"name": "@apihub24/in-memory-repository",
|
"name": "@apihub24/in-memory-repository",
|
||||||
"version": "1.0.1",
|
"version": "1.0.2",
|
||||||
"description": "",
|
"description": "",
|
||||||
"main": "dist/index.js",
|
"main": "dist/index.js",
|
||||||
"types": "./dist/index.d.ts",
|
"types": "./dist/index.d.ts",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"build": "tsc"
|
"build": "rimraf ./dist && tsc",
|
||||||
|
"test": "jest",
|
||||||
|
"test:coverage": "jest --coverage",
|
||||||
|
"pub": "npm publish",
|
||||||
|
"rel": "npm i && npm run build && npm run test:coverage"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@apihub24/repository": "^1.0.2"
|
"@apihub24/repository": "^1.0.3"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"typescript": "^5.9.2"
|
"rimraf": "^6.0.1",
|
||||||
|
"@nestjs/testing": "^11.1.6",
|
||||||
|
"typescript": "^5.9.2",
|
||||||
|
"@types/jest": "^30.0.0",
|
||||||
|
"jest": "^30.0.0",
|
||||||
|
"ts-jest": "^29.4.1"
|
||||||
},
|
},
|
||||||
"keywords": [],
|
"keywords": [],
|
||||||
"author": {
|
"author": {
|
||||||
@ -22,5 +31,26 @@
|
|||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"publishConfig": {
|
"publishConfig": {
|
||||||
"access": "public"
|
"access": "public"
|
||||||
|
},
|
||||||
|
"jest": {
|
||||||
|
"moduleFileExtensions": [
|
||||||
|
"js",
|
||||||
|
"json",
|
||||||
|
"ts"
|
||||||
|
],
|
||||||
|
"rootDir": ".",
|
||||||
|
"testRegex": ".*\\.spec\\.ts$",
|
||||||
|
"transform": {
|
||||||
|
"^.+\\.(t|j)s$": "ts-jest"
|
||||||
|
},
|
||||||
|
"collectCoverageFrom": [
|
||||||
|
"**/*.(t|j)s"
|
||||||
|
],
|
||||||
|
"coverageDirectory": "./coverage",
|
||||||
|
"testEnvironment": "node",
|
||||||
|
"roots": [
|
||||||
|
"<rootDir>/src/"
|
||||||
|
],
|
||||||
|
"moduleNameMapper": {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
67
src/in.memory.repository.spec.ts
Normal file
67
src/in.memory.repository.spec.ts
Normal file
@ -0,0 +1,67 @@
|
|||||||
|
import { InMemoryRepository } from "./";
|
||||||
|
|
||||||
|
class Test {
|
||||||
|
id: string;
|
||||||
|
name: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
describe("InMemoryRepository Tests", () => {
|
||||||
|
it("should export InMemoryRepository", async () => {
|
||||||
|
expect(InMemoryRepository).toBeDefined();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should create something", async () => {
|
||||||
|
const repo = new InMemoryRepository<Test>();
|
||||||
|
const created = await repo.save([{ id: "1", name: "test" }]);
|
||||||
|
expect(created).toBeDefined();
|
||||||
|
expect(created.length).toBeGreaterThan(0);
|
||||||
|
expect(created[0].id).toBe("1");
|
||||||
|
expect(created[0].name).toBe("test");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should generate id", async () => {
|
||||||
|
const repo = new InMemoryRepository<Test>();
|
||||||
|
const created = await repo.save([{ id: "", name: "test" }]);
|
||||||
|
expect(created).toBeDefined();
|
||||||
|
expect(created.length).toBeGreaterThan(0);
|
||||||
|
expect(created[0].id).toBeDefined();
|
||||||
|
expect(created[0].id.length).toBeGreaterThan(3);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should update something", async () => {
|
||||||
|
const repo = new InMemoryRepository<Test>();
|
||||||
|
const created = await repo.save([{ id: "2", name: "test2" }]);
|
||||||
|
expect(created).toBeDefined();
|
||||||
|
expect(created.length).toBeGreaterThan(0);
|
||||||
|
|
||||||
|
created[0].name = "test3";
|
||||||
|
const saved = await repo.save([created[0]]);
|
||||||
|
expect(saved).toBeDefined();
|
||||||
|
expect(saved[0].name).toBe("test3");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should select something", async () => {
|
||||||
|
const repo = new InMemoryRepository<Test>();
|
||||||
|
const created = await repo.save([{ id: "4", name: "test4" }]);
|
||||||
|
expect(created).toBeDefined();
|
||||||
|
expect(created.length).toBeGreaterThan(0);
|
||||||
|
|
||||||
|
const selected = await repo.getBy((x) => x.id === "4");
|
||||||
|
expect(selected).toBeDefined();
|
||||||
|
expect(selected.length).toBe(1);
|
||||||
|
expect(selected[0].id).toBe("4");
|
||||||
|
expect(selected[0].name).toBe("test4");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should delete something", async () => {
|
||||||
|
const repo = new InMemoryRepository<Test>();
|
||||||
|
const created = await repo.save([{ id: "5", name: "test5" }]);
|
||||||
|
expect(created).toBeDefined();
|
||||||
|
expect(created.length).toBeGreaterThan(0);
|
||||||
|
|
||||||
|
expect(await repo.deleteBy((x) => x.id === "5")).toBeTruthy();
|
||||||
|
const selected = await repo.getBy((x) => x.id === "5");
|
||||||
|
expect(selected).toBeDefined();
|
||||||
|
expect(selected.length).toBe(0);
|
||||||
|
});
|
||||||
|
});
|
||||||
@ -31,7 +31,7 @@ export class InMemoryRepository<TModel extends IdModel>
|
|||||||
}
|
}
|
||||||
|
|
||||||
deleteBy(filter: (model: TModel) => boolean): Promise<boolean> {
|
deleteBy(filter: (model: TModel) => boolean): Promise<boolean> {
|
||||||
this.source = this.source.filter(filter);
|
this.source = this.source.filter((x) => !filter(x));
|
||||||
return Promise.resolve(true);
|
return Promise.resolve(true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -22,5 +22,6 @@
|
|||||||
"strictBindCallApply": false,
|
"strictBindCallApply": false,
|
||||||
"noFallthroughCasesInSwitch": false
|
"noFallthroughCasesInSwitch": false
|
||||||
},
|
},
|
||||||
"include": ["./src"]
|
"include": ["./src"],
|
||||||
|
"exclude": ["**/*.spec.ts", "**/*.mock.ts"]
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user