chore: Improve perf of server tests (#5785)

This commit is contained in:
Tom Moor
2023-09-06 07:14:49 -04:00
committed by GitHub
parent a724a21c21
commit 3eb947e9a5
69 changed files with 2045 additions and 1551 deletions

View File

@@ -7,7 +7,7 @@ import {
buildTeam,
buildDocument,
} from "@server/test/factories";
import { setupTestDatabase, seed } from "@server/test/support";
import { setupTestDatabase } from "@server/test/support";
import slugify from "@server/utils/slugify";
import Collection from "./Collection";
import Document from "./Document";
@@ -103,7 +103,7 @@ describe("getDocumentTree", () => {
describe("#addDocumentToStructure", () => {
test("should add as last element without index", async () => {
const { collection } = await seed();
const collection = await buildCollection();
const id = uuidv4();
const newDocument = await buildDocument({
id,
@@ -112,12 +112,12 @@ describe("#addDocumentToStructure", () => {
teamId: collection.teamId,
});
await collection.addDocumentToStructure(newDocument);
expect(collection.documentStructure!.length).toBe(2);
expect(collection.documentStructure![1].id).toBe(id);
expect(collection.documentStructure!.length).toBe(1);
expect(collection.documentStructure![0].id).toBe(id);
});
test("should add with an index", async () => {
const { collection } = await seed();
const collection = await buildCollection();
const id = uuidv4();
const newDocument = await buildDocument({
id,
@@ -126,12 +126,15 @@ describe("#addDocumentToStructure", () => {
teamId: collection.teamId,
});
await collection.addDocumentToStructure(newDocument, 1);
expect(collection.documentStructure!.length).toBe(2);
expect(collection.documentStructure![1].id).toBe(id);
expect(collection.documentStructure!.length).toBe(1);
expect(collection.documentStructure![0].id).toBe(id);
});
test("should add as a child if with parent", async () => {
const { collection, document } = await seed();
const collection = await buildCollection();
const document = await buildDocument({ collectionId: collection.id });
await collection.reload();
const id = uuidv4();
const newDocument = await buildDocument({
id,
@@ -147,7 +150,10 @@ describe("#addDocumentToStructure", () => {
});
test("should add as a child if with parent with index", async () => {
const { collection, document } = await seed();
const collection = await buildCollection();
const document = await buildDocument({ collectionId: collection.id });
await collection.reload();
const newDocument = await buildDocument({
id: uuidv4(),
title: "node",
@@ -170,7 +176,7 @@ describe("#addDocumentToStructure", () => {
});
describe("options: documentJson", () => {
test("should append supplied json over document's own", async () => {
const { collection } = await seed();
const collection = await buildCollection();
const id = uuidv4();
const newDocument = await buildDocument({
id: uuidv4(),
@@ -193,15 +199,18 @@ describe("#addDocumentToStructure", () => {
],
},
});
expect(collection.documentStructure![1].children.length).toBe(1);
expect(collection.documentStructure![1].children[0].id).toBe(id);
expect(collection.documentStructure![0].children.length).toBe(1);
expect(collection.documentStructure![0].children[0].id).toBe(id);
});
});
});
describe("#updateDocument", () => {
test("should update root document's data", async () => {
const { collection, document } = await seed();
const collection = await buildCollection();
const document = await buildDocument({ collectionId: collection.id });
await collection.reload();
document.title = "Updated title";
await document.save();
await collection.updateDocument(document);
@@ -209,7 +218,10 @@ describe("#updateDocument", () => {
});
test("should update child document's data", async () => {
const { collection, document } = await seed();
const collection = await buildCollection();
const document = await buildDocument({ collectionId: collection.id });
await collection.reload();
const newDocument = await Document.create({
parentDocumentId: document.id,
collectionId: collection.id,
@@ -233,14 +245,20 @@ describe("#updateDocument", () => {
describe("#removeDocument", () => {
test("should save if removing", async () => {
const { collection, document } = await seed();
const collection = await buildCollection();
const document = await buildDocument({ collectionId: collection.id });
await collection.reload();
jest.spyOn(collection, "save");
await collection.deleteDocument(document);
expect(collection.save).toBeCalled();
});
test("should remove documents from root", async () => {
const { collection, document } = await seed();
const collection = await buildCollection();
const document = await buildDocument({ collectionId: collection.id });
await collection.reload();
await collection.deleteDocument(document);
expect(collection.documentStructure!.length).toBe(0);
// Verify that the document was removed
@@ -253,7 +271,10 @@ describe("#removeDocument", () => {
});
test("should remove a document with child documents", async () => {
const { collection, document } = await seed();
const collection = await buildCollection();
const document = await buildDocument({ collectionId: collection.id });
await collection.reload();
// Add a child for testing
const newDocument = await Document.create({
parentDocumentId: document.id,
@@ -279,7 +300,10 @@ describe("#removeDocument", () => {
});
test("should remove a child document", async () => {
const { collection, document } = await seed();
const collection = await buildCollection();
const document = await buildDocument({ collectionId: collection.id });
await collection.reload();
// Add a child for testing
const newDocument = await Document.create({
parentDocumentId: document.id,

View File

@@ -6,7 +6,7 @@ import {
buildTeam,
buildUser,
} from "@server/test/factories";
import { setupTestDatabase, seed } from "@server/test/support";
import { setupTestDatabase } from "@server/test/support";
import slugify from "@server/utils/slugify";
setupTestDatabase();
@@ -116,9 +116,7 @@ describe("#save", () => {
describe("#getChildDocumentIds", () => {
test("should return empty array if no children", async () => {
const team = await buildTeam();
const user = await buildUser({
teamId: team.id,
});
const user = await buildUser({ teamId: team.id });
const collection = await buildCollection({
userId: user.id,
teamId: team.id,
@@ -135,9 +133,7 @@ describe("#getChildDocumentIds", () => {
test("should return nested child document ids", async () => {
const team = await buildTeam();
const user = await buildUser({
teamId: team.id,
});
const user = await buildUser({ teamId: team.id });
const collection = await buildCollection({
userId: user.id,
teamId: team.id,
@@ -171,14 +167,14 @@ describe("#getChildDocumentIds", () => {
describe("#findByPk", () => {
test("should return document when urlId is correct", async () => {
const { document } = await seed();
const document = await buildDocument();
const id = `${slugify(document.title)}-${document.urlId}`;
const response = await Document.findByPk(id);
expect(response?.id).toBe(document.id);
});
test("should return document when urlId is given without the slug prefix", async () => {
const { document } = await seed();
const document = await buildDocument();
const id = document.urlId;
const response = await Document.findByPk(id);
expect(response?.id).toBe(document.id);

View File

@@ -1,61 +0,0 @@
import { buildUser, buildGroup, buildCollection } from "@server/test/factories";
import { setupTestDatabase } from "@server/test/support";
import CollectionGroup from "./CollectionGroup";
import GroupUser from "./GroupUser";
setupTestDatabase();
beforeEach(async () => {
jest.resetAllMocks();
});
describe("afterDestroy hook", () => {
test("should destroy associated group and collection join relations", async () => {
const group = await buildGroup();
const teamId = group.teamId;
const user1 = await buildUser({
teamId,
});
const user2 = await buildUser({
teamId,
});
const collection1 = await buildCollection({
permission: null,
teamId,
});
const collection2 = await buildCollection({
permission: null,
teamId,
});
const createdById = user1.id;
await group.$add("user", user1, {
through: {
createdById,
},
});
await group.$add("user", user2, {
through: {
createdById,
},
});
await collection1.$add("group", group, {
through: {
createdById,
},
});
await collection2.$add("group", group, {
through: {
createdById,
},
});
let collectionGroupCount = await CollectionGroup.count();
let groupUserCount = await GroupUser.count();
expect(collectionGroupCount).toBe(2);
expect(groupUserCount).toBe(2);
await group.destroy();
collectionGroupCount = await CollectionGroup.count();
groupUserCount = await GroupUser.count();
expect(collectionGroupCount).toBe(0);
expect(groupUserCount).toBe(0);
});
});

View File

@@ -18,9 +18,21 @@ describe("user model", () => {
describe("destroy", () => {
it("should delete user authentications", async () => {
const user = await buildUser();
expect(await UserAuthentication.count()).toBe(1);
expect(
await UserAuthentication.count({
where: {
userId: user.id,
},
})
).toBe(1);
await user.destroy();
expect(await UserAuthentication.count()).toBe(0);
expect(
await UserAuthentication.count({
where: {
userId: user.id,
},
})
).toBe(0);
});
});
@@ -33,7 +45,9 @@ describe("user model", () => {
describe("availableTeams", () => {
it("should return teams where another user with the same email exists", async () => {
const user = await buildUser();
const user = await buildUser({
email: "user-available-teams@example.com",
});
const anotherUser = await buildUser({ email: user.email });
const response = await user.availableTeams();

View File

@@ -165,9 +165,7 @@ describe("#searchForTeam", () => {
describe("#searchForUser", () => {
test("should return search results from collections", async () => {
const team = await buildTeam();
const user = await buildUser({
teamId: team.id,
});
const user = await buildUser({ teamId: team.id });
const collection = await buildCollection({
userId: user.id,
teamId: team.id,
@@ -185,9 +183,7 @@ describe("#searchForUser", () => {
test("should handle no collections", async () => {
const team = await buildTeam();
const user = await buildUser({
teamId: team.id,
});
const user = await buildUser({ teamId: team.id });
const { results } = await SearchHelper.searchForUser(user, "test");
expect(results.length).toBe(0);
});
@@ -265,9 +261,7 @@ describe("#searchForUser", () => {
test("should return the total count of search results", async () => {
const team = await buildTeam();
const user = await buildUser({
teamId: team.id,
});
const user = await buildUser({ teamId: team.id });
const collection = await buildCollection({
userId: user.id,
teamId: team.id,
@@ -290,9 +284,7 @@ describe("#searchForUser", () => {
test("should return the document when searched with their previous titles", async () => {
const team = await buildTeam();
const user = await buildUser({
teamId: team.id,
});
const user = await buildUser({ teamId: team.id });
const collection = await buildCollection({
teamId: team.id,
userId: user.id,
@@ -314,9 +306,7 @@ describe("#searchForUser", () => {
test("should not return the document when searched with neither the titles nor the previous titles", async () => {
const team = await buildTeam();
const user = await buildUser({
teamId: team.id,
});
const user = await buildUser({ teamId: team.id });
const collection = await buildCollection({
teamId: team.id,
userId: user.id,
@@ -340,9 +330,7 @@ describe("#searchForUser", () => {
describe("#searchTitlesForUser", () => {
test("should return search results from collections", async () => {
const team = await buildTeam();
const user = await buildUser({
teamId: team.id,
});
const user = await buildUser({ teamId: team.id });
const collection = await buildCollection({
userId: user.id,
teamId: team.id,
@@ -360,9 +348,7 @@ describe("#searchTitlesForUser", () => {
test("should filter to specific collection", async () => {
const team = await buildTeam();
const user = await buildUser({
teamId: team.id,
});
const user = await buildUser({ teamId: team.id });
const collection = await buildCollection({
userId: user.id,
teamId: team.id,
@@ -397,9 +383,7 @@ describe("#searchTitlesForUser", () => {
test("should handle no collections", async () => {
const team = await buildTeam();
const user = await buildUser({
teamId: team.id,
});
const user = await buildUser({ teamId: team.id });
const documents = await SearchHelper.searchTitlesForUser(user, "test");
expect(documents.length).toBe(0);
});