Skip to content

Commit

Permalink
Merge branch 'dev' into adrian/lifecycle-events-error-logging
Browse files Browse the repository at this point in the history
  • Loading branch information
adrians5j authored Nov 15, 2024
2 parents d6e5034 + fb60f82 commit f171c13
Show file tree
Hide file tree
Showing 12 changed files with 280 additions and 53 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,206 @@
import { useCategoryManageHandler } from "../../testHelpers/useCategoryManageHandler";
import { useArticleManageHandler } from "../../testHelpers/useArticleManageHandler";
import { useArticleReadHandler } from "../../testHelpers/useArticleReadHandler";
import { useGraphQLHandler } from "../../testHelpers/useGraphQLHandler";
import { setupContentModelGroup, setupContentModels } from "../../testHelpers/setup";
import { GenericRecord } from "@webiny/api/types";
import slugify from "slugify";

interface ICreateCategoryItemPrams {
manager: ReturnType<typeof useCategoryManageHandler>;
publish: boolean;
data: GenericRecord;
}

const createCategoryItem = async ({ manager, publish, data }: ICreateCategoryItemPrams) => {
const [response] = await manager.createCategory({ data });
const category = response?.data?.createCategory?.data;
const error = response?.data?.createCategory?.error;
if (!category?.id || error) {
console.log(error.message);
console.log(JSON.stringify(error.data));
throw new Error("Could not create category.");
}
if (!publish) {
return category;
}
const [publishResponse] = await manager.publishCategory({
revision: category.id
});
if (publishResponse?.data?.publishCategory?.error) {
console.log(publishResponse?.data?.publishCategory?.error?.message);
throw new Error("Could not publish category.");
}
return publishResponse.data.publishCategory.data;
};

interface ICreateArticleItemPrams {
manager: ReturnType<typeof useArticleManageHandler>;
publish: boolean;
data: GenericRecord;
}

const createArticleItem = async ({ manager, publish, data }: ICreateArticleItemPrams) => {
const [response] = await manager.createArticle({ data });
const article = response?.data?.createArticle?.data;
const error = response?.data?.createArticle?.error;
if (!article?.id || error) {
console.log(error.message);
console.log(JSON.stringify(error.data));
throw new Error("Could not create article.");
}
if (!publish) {
return article;
}
const [publishResponse] = await manager.publishArticle({
revision: article.id
});
if (publishResponse?.data?.publishArticle?.error) {
console.log(publishResponse?.data?.publishArticle?.error?.message);
throw new Error("Could not publish article.");
}
return publishResponse.data.publishArticle.data;
};

interface ICategoryItem {
id: string;
entryId: string;
title: string;
slug: string;
published: boolean;
}

const categoryNames = ["Tech", "Health", "Space", "Food", "Science", "Sports"];

describe("published and unpublished references", () => {
const manageOpts = { path: "manage/en-US" };
const readOpts = { path: "read/en-US" };

const mainManager = useGraphQLHandler(manageOpts);

it("should populate reference field with some published and some unpublished records", async () => {
const group = await setupContentModelGroup(mainManager);
await setupContentModels(mainManager, group, ["category", "article"]);

const categoryManager = useCategoryManageHandler(manageOpts);
const articleManager = useArticleManageHandler(manageOpts);
const articleRead = useArticleReadHandler(readOpts);

const categories: ICategoryItem[] = [];

for (const index in categoryNames) {
const title = categoryNames[index];
const published = Number(index) % 2 === 0;
const category = await createCategoryItem({
manager: categoryManager,
data: {
title: title,
slug: slugify(title)
},
publish: published
});
categories.push({
...category,
published
});
}
expect(categories.length).toBe(categoryNames.length);

const firstUnpublishedCategoryId = categories.find(c => !c.published)!.id;
expect(firstUnpublishedCategoryId).toMatch(/^([a-zA-Z0-9]+)#0001$/);
/**
* Create an article and make sure all the categories are in it.
*/
const createdArticle = await createArticleItem({
manager: articleManager,
data: {
title: "Tech article",
body: null,
category: {
id: firstUnpublishedCategoryId,
modelId: "category"
},
categories: categories.map(c => {
return {
id: c.id,
modelId: "category"
};
})
},
publish: false
});

const expectedAllCategories = categories.map(c => {
return {
id: c.id,
entryId: c.entryId,
modelId: "category"
};
});
const expectedPublishedCategories = categories
.filter(c => c.published)
.map(c => {
return {
id: c.id,
entryId: c.entryId,
modelId: "category"
};
});
expect(expectedAllCategories).toHaveLength(expectedPublishedCategories.length * 2);

expect(createdArticle.categories).toEqual(expectedAllCategories);

const [articleManageGetResponse] = await articleManager.getArticle({
revision: createdArticle.id
});
expect(articleManageGetResponse?.data?.getArticle?.data?.categories).toEqual(
expectedAllCategories
);
expect(articleManageGetResponse?.data?.getArticle?.data?.category).toMatchObject({
id: firstUnpublishedCategoryId
});
/**
* Now we can publish the article and check that references are still there.
*/
const [publishResponse] = await articleManager.publishArticle({
revision: createdArticle.id
});
expect(publishResponse?.data?.publishArticle?.data?.categories).toEqual(
expectedAllCategories
);
expect(publishResponse?.data?.publishArticle?.data?.category).toMatchObject({
id: firstUnpublishedCategoryId
});
/**
* Now we can read the article, from manage endpoint, and check that references are still there.
*
* There must be all the categories present.
*/
const [articleManageGetPublishedResponse] = await articleManager.getArticle({
revision: createdArticle.id
});
expect(articleManageGetPublishedResponse?.data?.getArticle?.data?.categories).toEqual(
expectedAllCategories
);
expect(articleManageGetPublishedResponse?.data?.getArticle?.data?.category).toMatchObject({
id: firstUnpublishedCategoryId
});
/**
* And read from the read endpoint...
*
* There must be only published categories present.
*/
const [articleReadGetPublishedResponse] = await articleRead.getArticle({
where: {
id: createdArticle.id
}
});
expect(articleReadGetPublishedResponse?.data?.getArticle?.data?.categories).toMatchObject(
expectedPublishedCategories
);
expect(articleReadGetPublishedResponse?.data?.getArticle?.data?.categories).toHaveLength(
expectedPublishedCategories.length
);
expect(articleReadGetPublishedResponse?.data?.getArticle?.data?.category).toBeNull();
});
});
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { mdbid } from "@webiny/utils";
import models from "./mocks/contentModels";
import { useGraphQLHandler } from "../testHelpers/useGraphQLHandler";
import { CmsContext, CmsEntry, CmsGroup, CmsModel, StorageOperationsCmsModel } from "~/types";
import { CmsEntry, CmsGroup, CmsModel, StorageOperationsCmsModel } from "~/types";
import { useCategoryManageHandler } from "../testHelpers/useCategoryManageHandler";
import { useCategoryReadHandler } from "../testHelpers/useCategoryReadHandler";
import { useProductManageHandler } from "../testHelpers/useProductManageHandler";
import { createStorageOperationsContext } from "~tests/storageOperations/context";

const cliPackageJson = require("@webiny/cli/package.json");
const webinyVersion = cliPackageJson.version;
Expand Down Expand Up @@ -288,9 +289,11 @@ describe("Republish entries", () => {

const { storageOperations, plugins } = useCategoryManageHandler(manageOpts);

await storageOperations.beforeInit({
plugins
} as CmsContext);
await storageOperations.beforeInit(
await createStorageOperationsContext({
plugins
})
);

const { entry: galaEntry } = createEntry(productModel, {
title: "Gala",
Expand Down
34 changes: 34 additions & 0 deletions packages/api-headless-cms/__tests__/storageOperations/context.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import dbPlugins from "@webiny/handler-db";
import { PluginsContainer } from "@webiny/plugins";
import { getDocumentClient } from "@webiny/project-utils/testing/dynamodb";
import { DynamoDbDriver } from "@webiny/db-dynamodb";
import { CmsContext } from "~/types";
import { Context } from "@webiny/api";

export interface ICreateStorageOperationsContextParams {
plugins?: PluginsContainer;
}

export const createStorageOperationsContext = async (
params: ICreateStorageOperationsContextParams
): Promise<CmsContext> => {
const dbPluginsInitialized = dbPlugins({
table: process.env.DB_TABLE,
driver: new DynamoDbDriver({
documentClient: getDocumentClient()
})
});
const plugins = params.plugins || new PluginsContainer([]);
plugins.register(...dbPluginsInitialized);

const context = new Context({
plugins,
WEBINY_VERSION: "0.0.0"
}) as unknown as CmsContext;

for (const db of dbPluginsInitialized) {
await db.apply(context);
}

return context;
};
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { createPersonEntries, createPersonModel, deletePersonModel } from "./helpers";
import { useGraphQLHandler } from "../testHelpers/useGraphQLHandler";
import { CmsContext } from "~/types";
import { createStorageOperationsContext } from "~tests/storageOperations/context";

jest.setTimeout(90000);

Expand All @@ -15,9 +15,11 @@ describe("Entries storage operations", () => {
* Some others might not need them...
*/
beforeAll(async () => {
await storageOperations.beforeInit({
plugins
} as unknown as CmsContext);
await storageOperations.beforeInit(
await createStorageOperationsContext({
plugins
})
);
});

beforeEach(async () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { CmsContext } from "~/types";
import {
createPersonEntries,
createPersonModel,
deletePersonModel
} from "~tests/storageOperations/helpers";
import { useGraphQLHandler } from "~tests/testHelpers/useGraphQLHandler";
import { createStorageOperationsContext } from "~tests/storageOperations/context";

describe("field unique values listing", () => {
const { storageOperations, plugins } = useGraphQLHandler({
Expand All @@ -17,9 +17,11 @@ describe("field unique values listing", () => {
* Some others might not need them...
*/
beforeAll(async () => {
await storageOperations.beforeInit({
plugins
} as unknown as CmsContext);
await storageOperations.beforeInit(
await createStorageOperationsContext({
plugins
})
);
});

beforeEach(async () => {
Expand Down
1 change: 1 addition & 0 deletions packages/api-headless-cms/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
"@webiny/api-wcp": "0.0.0",
"@webiny/aws-sdk": "0.0.0",
"@webiny/cli": "0.0.0",
"@webiny/db-dynamodb": "0.0.0",
"@webiny/project-utils": "0.0.0",
"apollo-graphql": "^0.9.5",
"get-yarn-workspaces": "^1.0.2",
Expand Down
3 changes: 2 additions & 1 deletion packages/api-headless-cms/tsconfig.build.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
{ "path": "../utils/tsconfig.build.json" },
{ "path": "../validation/tsconfig.build.json" },
{ "path": "../api-wcp/tsconfig.build.json" },
{ "path": "../aws-sdk/tsconfig.build.json" }
{ "path": "../aws-sdk/tsconfig.build.json" },
{ "path": "../db-dynamodb/tsconfig.build.json" }
],
"compilerOptions": {
"rootDir": "./src",
Expand Down
7 changes: 5 additions & 2 deletions packages/api-headless-cms/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
{ "path": "../utils" },
{ "path": "../validation" },
{ "path": "../api-wcp" },
{ "path": "../aws-sdk" }
{ "path": "../aws-sdk" },
{ "path": "../db-dynamodb" }
],
"compilerOptions": {
"rootDirs": ["./src", "./__tests__"],
Expand Down Expand Up @@ -57,7 +58,9 @@
"@webiny/api-wcp/*": ["../api-wcp/src/*"],
"@webiny/api-wcp": ["../api-wcp/src"],
"@webiny/aws-sdk/*": ["../aws-sdk/src/*"],
"@webiny/aws-sdk": ["../aws-sdk/src"]
"@webiny/aws-sdk": ["../aws-sdk/src"],
"@webiny/db-dynamodb/*": ["../db-dynamodb/src/*"],
"@webiny/db-dynamodb": ["../db-dynamodb/src"]
},
"baseUrl": "."
}
Expand Down
21 changes: 5 additions & 16 deletions packages/cli-plugin-extensions/tsconfig.build.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,16 @@
"extends": "../../tsconfig.build.json",
"include": ["src"],
"references": [
{
"path": "../plugins/tsconfig.build.json"
},
{
"path": "../aws-sdk/tsconfig.build.json"
},
{
"path": "../cli-plugin-scaffold/tsconfig.build.json"
},
{
"path": "../error/tsconfig.build.json"
}
{ "path": "../aws-sdk/tsconfig.build.json" },
{ "path": "../cli-plugin-scaffold/tsconfig.build.json" },
{ "path": "../error/tsconfig.build.json" },
{ "path": "../plugins/tsconfig.build.json" }
],
"compilerOptions": {
"rootDir": "./src",
"outDir": "./dist",
"declarationDir": "./dist",
"paths": {
"~/*": ["./src/*"],
"~tests/*": ["./__tests__/*"]
},
"paths": { "~/*": ["./src/*"], "~tests/*": ["./__tests__/*"] },
"baseUrl": "."
}
}
Loading

0 comments on commit f171c13

Please sign in to comment.