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(api-headless-cms-ddb-es): entry values modifier #3422

5 changes: 4 additions & 1 deletion jest.config.base.setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,7 @@

// Runs failed tests five times until they pass or until the max number of retries is exhausted.
// https://jestjs.io/docs/jest-object#jestretrytimesnumretries-options
jest.retryTimes(3);
jest.retryTimes(0);
if (process.env.CI === "true") {
jest.retryTimes(3);
}
2 changes: 1 addition & 1 deletion packages/api-aco/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"directory": "dist"
},
"dependencies": {
"@ungap/structured-clone": "1.2.0",
"@ungap/structured-clone": "^1.2.0",
"@webiny/api": "0.0.0",
"@webiny/api-headless-cms": "0.0.0",
"@webiny/api-i18n": "0.0.0",
Expand Down
4 changes: 2 additions & 2 deletions packages/api-elasticsearch/src/compression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const getCompressionPlugins = (plugins: PluginsContainer): CompressionPlugin[] =
export const compress = async (
pluginsContainer: PluginsContainer,
data: Record<string, any>
): Promise<Record<string, any>> => {
): Promise<Record<string, any> | string> => {
brunozoric marked this conversation as resolved.
Show resolved Hide resolved
const plugins = getCompressionPlugins(pluginsContainer);
if (plugins.length === 0) {
console.log("No compression plugins");
Expand All @@ -34,7 +34,7 @@ export const compress = async (
export const decompress = async (
pluginsContainer: PluginsContainer,
data: Record<string, any>
): Promise<Record<string, any>> => {
): Promise<Record<string, any> | string> => {
brunozoric marked this conversation as resolved.
Show resolved Hide resolved
const plugins = getCompressionPlugins(pluginsContainer);
if (plugins.length === 0) {
return data;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
import structuredClone from "@ungap/structured-clone";
import { createCmsEntryElasticsearchValuesModifier } from "~/plugins";
import { CmsEntry, CmsIdentity, CmsModel } from "@webiny/api-headless-cms/types";

interface MockCmsEntryValues {
title: string;
age: number;
}

const mockModel: CmsModel = {
locale: "en-US",
tenant: "root",
modelId: "abcdefghijklmn",
name: "Test model",
description: "",
layout: [],
fields: [],
singularApiName: "TestModel",
pluralApiName: "TestModels",
titleFieldId: "id",
group: {
id: "group",
name: "group"
},
webinyVersion: "0.0.0"
};
const createdBy: CmsIdentity = {
id: "a",
displayName: "a",
type: "a"
};

const mockEntry: CmsEntry<MockCmsEntryValues> = {
id: "abcdefg#0001",
entryId: "abcdefg",
location: {
folderId: "root"
},
values: {
title: "Initial title",
age: 55
},
status: "draft",
locked: false,
locale: "en-US",
tenant: "root",
webinyVersion: "0.0.0",
createdBy,
ownedBy: createdBy,
meta: {},
createdOn: new Date().toISOString(),
savedOn: new Date().toISOString(),
modelId: mockModel.modelId,
version: 1
};

const getMockData = () => {
return {
model: structuredClone<CmsModel>(mockModel),
entry: structuredClone<CmsEntry<MockCmsEntryValues>>(mockEntry),
values: structuredClone<MockCmsEntryValues>(mockEntry.values)
};
};

describe("entry values modifier", () => {
it("should modify the original values with a single plugin", async () => {
const { model, values: initialValues, entry } = getMockData();
let values = structuredClone(initialValues);

const modifier = createCmsEntryElasticsearchValuesModifier<MockCmsEntryValues>(
async ({ setValues }) => {
setValues(() => {
return {
title: "Test title"
};
});
}
);

values = await modifier.modify({
entry,
model,
values
});

expect(values).toEqual({
title: "Test title"
});
expect(values.age).toBeUndefined();
});

it("should modify the original values with multiple plugins", async () => {
const { model, values: initialValues, entry } = getMockData();
let values = structuredClone(initialValues);
const titleModifier = createCmsEntryElasticsearchValuesModifier<MockCmsEntryValues>(
async ({ setValues }) => {
setValues(() => {
return {
title: "Test title"
};
});
}
);

values = await titleModifier.modify({
entry,
model,
values
});

expect(values).toEqual({
title: "Test title"
});
expect(values.age).toBeUndefined();

const ageModifier = createCmsEntryElasticsearchValuesModifier<MockCmsEntryValues>(
async ({ setValues }) => {
setValues(prev => {
return {
...prev,
age: 2
};
});
}
);

values = await ageModifier.modify({
entry,
model,
values
});
expect(values).toEqual({
title: "Test title",
age: 2
});
});

it("should not modify anything because model is not supported", async () => {
const { model } = getMockData();
const nothingWillGetModified =
createCmsEntryElasticsearchValuesModifier<MockCmsEntryValues>({
models: ["nonExisting"],
modifier: async ({ setValues }) => {
setValues(() => {
return {
title: "Test title"
};
});
}
});

const result = nothingWillGetModified.canModify(model.modelId);
expect(result).toEqual(false);
});
});
1 change: 1 addition & 0 deletions packages/api-headless-cms-ddb-es/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"@babel/preset-env": "^7.22.7",
"@elastic/elasticsearch": "7.12.0",
"@types/jsonpack": "^1.1.0",
"@ungap/structured-clone": "^1.2.0",
"@webiny/api-dynamodb-to-elasticsearch": "0.0.0",
"@webiny/api-i18n": "0.0.0",
"@webiny/api-security": "0.0.0",
Expand Down
Loading
Loading