Skip to content

Commit

Permalink
feat: add basic tests
Browse files Browse the repository at this point in the history
  • Loading branch information
darkbasic committed Nov 18, 2023
1 parent 48de1bf commit 04defb9
Show file tree
Hide file tree
Showing 9 changed files with 2,313 additions and 131 deletions.
36 changes: 0 additions & 36 deletions .changeset/sixty-bobcats-pump.md

This file was deleted.

3 changes: 2 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ module.exports = {
},
"extends": [
"standard-with-typescript",
"plugin:prettier/recommended"
"plugin:prettier/recommended",
'plugin:jest/recommended'
],
"overrides": [
{
Expand Down
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
**/node_modules
**/dist
/coverage
**/coverage
/temp
/tests/**/temp
/tests/generated-entities
Expand Down
2 changes: 1 addition & 1 deletion examples/graphql/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ void (async () => {
// return await author.books.load();
// return await author.books.load({ dataloader: true });
// return await em.find(Book, { author: author.id });
return entityDataLoader.find(Book, { author: author.id });
return await entityDataLoader.find(Book, { author: author.id });
},
},
},
Expand Down
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
],
"scripts": {
"build": "yarn workspace mikro-orm-find-dataloader run build",
"test": "yarn workspace mikro-orm-find-dataloader run test",
"version": "yarn changeset version && yarn install --immutable",
"release": "yarn run compile && yarn changeset publish"
},
Expand All @@ -22,18 +23,22 @@
"@changesets/changelog-github": "^0.4.8",
"@changesets/cli": "^2.26.2",
"@types/eslint": "^8.44.7",
"@types/jest": "^29.5.8",
"@types/node": "^20.9.1",
"@typescript-eslint/eslint-plugin": "^6.11.0",
"eslint": "^8.54.0",
"eslint-config-prettier": "^9.0.0",
"eslint-config-standard-with-typescript": "^40.0.0",
"eslint-plugin-import": "^2.29.0",
"eslint-plugin-jest": "^27.6.0",
"eslint-plugin-n": "^16.3.1",
"eslint-plugin-prettier": "^5.0.1",
"eslint-plugin-promise": "^6.1.1",
"jest": "^29.7.0",
"nodemon": "^3.0.1",
"prettier": "^3.1.0",
"rimraf": "^5.0.5",
"ts-jest": "^29.1.1",
"ts-node": "^10.9.1",
"typescript": "^5.2.2"
}
Expand Down
12 changes: 10 additions & 2 deletions packages/find/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,21 @@
"scripts": {
"build": "yarn clean && yarn compile",
"clean": "yarn run -T rimraf ./dist",
"compile": "yarn run -T tsc"
"compile": "yarn run -T tsc",
"test": "yarn run -T jest"
},
"jest": {
"testEnvironment": "node",
"preset": "ts-jest",
"collectCoverage": true,
"testPathIgnorePatterns": ["/node_modules/", "/dist/"]
},
"publishConfig": {
"access": "public"
},
"devDependencies": {
"@mikro-orm/core": "6.0.0-dev.187"
"@mikro-orm/core": "6.0.0-dev.187",
"@mikro-orm/sqlite": "6.0.0-dev.187"
},
"peerDependencies": {
"@mikro-orm/core": "^6.0.0"
Expand Down
44 changes: 44 additions & 0 deletions packages/find/src/__snapshots__/find.test.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`find should fetch books with the find dataloader 1`] = `
[
[
{
"author": 1,
"id": 1,
"title": "One",
},
{
"author": 1,
"id": 2,
"title": "Two",
},
],
[
{
"author": 2,
"id": 3,
"title": "Three",
},
],
[
{
"author": 3,
"id": 4,
"title": "Four",
},
{
"author": 3,
"id": 5,
"title": "Five",
},
{
"author": 3,
"id": 6,
"title": "Six",
},
],
[],
[],
]
`;
125 changes: 125 additions & 0 deletions packages/find/src/find.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
/* eslint-disable @typescript-eslint/no-non-null-assertion */
import {
type SqlEntityManager,
MikroORM,
Entity,
PrimaryKey,
Property,
Collection,
OneToMany,
Ref,
ref,
ManyToOne,
} from "@mikro-orm/sqlite";
import { EntityDataLoader } from "./EntityDataLoader";

@Entity()
class Author {
@PrimaryKey()
id!: number;

@Property()
name: string;

@OneToMany(() => Book, (book) => book.author)
books = new Collection<Book>(this);

constructor({ id, name }: { id?: number; name: string }) {
if (id != null) {
this.id = id;
}
this.name = name;
}
}

@Entity()
class Book {
@PrimaryKey()
id!: number;

@Property()
title: string;

@ManyToOne(() => Author, { ref: true })
author: Ref<Author>;

constructor({ id, title, author }: { id?: number; title: string; author: Author | Ref<Author> }) {
if (id != null) {
this.id = id;
}
this.title = title;
this.author = ref(author);
}
}

async function populateDatabase(em: MikroORM["em"]): Promise<void> {
const authors = [
new Author({ id: 1, name: "a" }),
new Author({ id: 2, name: "b" }),
new Author({ id: 3, name: "c" }),
new Author({ id: 4, name: "d" }),
new Author({ id: 5, name: "e" }),
];
em.persist(authors);

const books = [
new Book({ id: 1, title: "One", author: authors[0]! }),
new Book({ id: 2, title: "Two", author: authors[0]! }),
new Book({ id: 3, title: "Three", author: authors[1]! }),
new Book({ id: 4, title: "Four", author: authors[2]! }),
new Book({ id: 5, title: "Five", author: authors[2]! }),
new Book({ id: 6, title: "Six", author: authors[2]! }),
];
em.persist(books);
await em.flush();
}

describe("find", () => {
let orm: MikroORM;
let emFork: SqlEntityManager;
let dataloader: EntityDataLoader;

beforeAll(async () => {
orm = await MikroORM.init({
dbName: ":memory:",
entities: [Author, Book],
});
try {
await orm.schema.clearDatabase();
} catch {}
try {
const generator = orm.getSchemaGenerator();
await generator.createSchema({ wrap: true });
} catch {}
await populateDatabase(orm.em.fork());
});

beforeEach(async () => {
emFork = orm.em.fork();
dataloader = new EntityDataLoader(orm.em.fork());
});

it("should fetch books with the find dataloader", async () => {
const authors = await emFork.find(Author, {});
const authorBooks = await Promise.all(authors.map(async ({ id }) => await dataloader.find(Book, { author: id })));
expect(authorBooks).toBeDefined();
expect(authorBooks).toMatchSnapshot();
});

it("should return the same books as find", async () => {
const authors = await emFork.find(Author, {});
const dataloaderBooks = await Promise.all(
authors.map(async ({ id }) => await dataloader.find(Book, { author: id })),
);
const findBooks = await Promise.all(authors.map(async ({ id }) => await emFork.find(Book, { author: id })));
expect(dataloaderBooks.map((res) => res.map(({ id }) => id))).toEqual(
findBooks.map((res) => res.map(({ id }) => id)),
);
});

afterEach(async () => {});

afterAll(async () => {
await orm.close(true);
});
});
Loading

0 comments on commit 04defb9

Please sign in to comment.