Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Allow using testIndex for numeric fields like order. #598

Merged
merged 1 commit into from
Mar 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions packages/integration-tests/src/EntityManager.factories.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {
PublisherType,
SmallPublisher,
} from "@src/entities";
import { maybeNew, maybeNewPoly, newTestInstance } from "joist-orm";
import { maybeNew, maybeNewPoly, newTestInstance, testIndex } from "joist-orm";
import { newEntityManager } from "./setupDbTests";

describe("EntityManager.factories", () => {
Expand Down Expand Up @@ -129,7 +129,7 @@ describe("EntityManager.factories", () => {
newBook(em);
// Then the newAuthor factory was told to override any `books: [{}]` defaults
expect(lastAuthorFactoryOpts).toStrictEqual({
firstName: "aTEST_INDEX",
firstName: `a${testIndex}`,
image: undefined,
age: 40,
books: [],
Expand All @@ -150,7 +150,7 @@ describe("EntityManager.factories", () => {
expect(a1.publisher.get).toEqual(p1);
// And we explicitly passed the publisher b/c it's an explicit `use` entry
expect(lastAuthorFactoryOpts).toStrictEqual({
firstName: "aTEST_INDEX",
firstName: `a${testIndex}`,
image: undefined,
age: 40,
books: [],
Expand Down Expand Up @@ -191,6 +191,7 @@ describe("EntityManager.factories", () => {
expect(lastBookFactoryOpts).toStrictEqual({
author: expect.any(Author),
use: expect.any(Map),
order: testIndex,
});
});

Expand Down Expand Up @@ -259,8 +260,9 @@ describe("EntityManager.factories", () => {
newBookReview(em, { book: {} });
expect(lastBookFactoryOpts).toStrictEqual({
author: maybeNew<Author>({ age: 40 }),
title: "Book for Review TEST_INDEX",
title: `Book for Review ${testIndex}`,
reviews: [],
order: testIndex,
use: expect.any(Map),
});
});
Expand All @@ -269,7 +271,7 @@ describe("EntityManager.factories", () => {
const em = newEntityManager();
newImage(em, { author: {} });
expect(lastAuthorFactoryOpts).toStrictEqual({
firstName: "aTEST_INDEX",
firstName: `a${testIndex}`,
age: undefined,
image: null,
use: expect.any(Map),
Expand Down Expand Up @@ -617,4 +619,11 @@ describe("EntityManager.factories", () => {
expect(br3.book.get.author.get.age).toBeUndefined();
});
});

it("can have test indexes that are numbers", async () => {
const em = newEntityManager();
const [b1, b2] = [newBook(em), newBook(em)];
expect(b1.order).toBe(1);
expect(b2.order).toBe(2);
});
});
3 changes: 2 additions & 1 deletion packages/integration-tests/src/entities/Book.factories.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { DeepNew, EntityManager, FactoryOpts, maybeNew, newTestInstance } from "joist-orm";
import { DeepNew, EntityManager, FactoryOpts, maybeNew, newTestInstance, testIndex } from "joist-orm";
import { Author, Book } from "./entities";

// for testing factories
Expand All @@ -9,5 +9,6 @@ export function newBook(em: EntityManager, opts: FactoryOpts<Book> = {}): DeepNe
return newTestInstance(em, Book, opts, {
// Pass a default age so that we can test deep-merging author.age and opts.firstName
author: maybeNew<Author>({ age: 40 }),
order: testIndex,
});
}
12 changes: 9 additions & 3 deletions packages/orm/src/newTestInstance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,13 @@ export function newTestInstance<T extends Entity>(
case "enum":
case "primaryKey":
// Look for strings that want to use the test index
if (typeof optValue === "string" && optValue.includes(testIndex)) {
if (typeof optValue === "string" && optValue.includes(testIndexString)) {
const actualIndex = getTestIndex(em, meta.cstr);
return [fieldName, optValue.replace(testIndex, String(actualIndex))];
const value = optValue.replace(testIndexString, String(actualIndex));
return [fieldName, value];
} else if (typeof optValue === "number" && optValue === testIndex) {
const actualIndex = getTestIndex(em, meta.cstr);
return [fieldName, actualIndex];
}
// Otherwise just use the user's opt value as-is
return [fieldName, optValue];
Expand Down Expand Up @@ -332,7 +336,9 @@ function applyUse(optsMaybeNew: object, use: UseMap, metadata: EntityMetadata<an
* This is meant to just be a helpful identifier in fields like entity names/descriptions for
* debugging purposes.
*/
export const testIndex = "TEST_INDEX";
export const testIndex: number = 1_111_111_222;

const testIndexString = String(testIndex);

const defaultValueMarker: any = {};

Expand Down