Skip to content

Commit baa48c7

Browse files
fix(style-editor): Now the style editor form is showed in UVE (#35497)
https://github.com/user-attachments/assets/ca2c5a65-0bde-448c-b94d-363b36e159f9 - Updated tests in and to use instead of for logging warnings. - Adjusted the implementation in and to directly call for logging messages related to missing page queries and schema fetch failures. - Removed unnecessary imports of from the test files to streamline the codebase. This change enhances consistency in logging practices across the codebase. This PR fixes: #35270 --------- Co-authored-by: Jalinson Diaz <zjaaaldev@gmail.com>
1 parent f9ec566 commit baa48c7

4 files changed

Lines changed: 106 additions & 27 deletions

File tree

core-web/libs/sdk/client/src/lib/client/page/page-api.spec.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ import { consola } from 'consola';
66
import {
77
DotCMSClientConfig,
88
DotCMSPageRequestParams,
9-
DotRequestOptions,
9+
DotErrorPage,
1010
DotHttpError,
11-
DotErrorPage
11+
DotRequestOptions
1212
} from '@dotcms/types';
1313

1414
import { PageClient } from './page-api';
@@ -873,11 +873,11 @@ describe('PageClient', () => {
873873
});
874874

875875
it('should omit styleEditorSchemas and log debug when schema endpoint fails', async () => {
876-
const consolaDebugSpy = jest.spyOn(consola, 'debug');
876+
const debugSpy = jest.spyOn(console, 'warn').mockImplementation(() => undefined);
877877

878878
mockRequest.mockImplementation((url: string) => {
879879
if (url.includes('/contenttype-schema')) {
880-
return Promise.reject(new Error('Unauthorized'));
880+
return Promise.reject(new Error('Network error'));
881881
}
882882

883883
return Promise.resolve(mockGraphQLResponse);
@@ -891,10 +891,11 @@ describe('PageClient', () => {
891891
const result = await pageClient.get('/graphql-page');
892892

893893
expect(result.styleEditorSchemas).toBeUndefined();
894-
expect(consolaDebugSpy).toHaveBeenCalledWith(
894+
expect(debugSpy).toHaveBeenCalledWith(
895895
'[DotCMS PageClient]: Skipping style editor schemas:',
896896
expect.any(Error)
897897
);
898+
debugSpy.mockRestore();
898899
});
899900

900901
it('should omit styleEditorSchemas when endpoint returns a non-array entity', async () => {
@@ -917,7 +918,7 @@ describe('PageClient', () => {
917918
});
918919

919920
it('should warn and return empty when pageId is missing from the page response', async () => {
920-
const consolaWarnSpy = jest.spyOn(consola, 'warn');
921+
const consolaWarnSpy = jest.spyOn(console, 'warn');
921922

922923
mockRequest.mockResolvedValue({
923924
...mockGraphQLResponse,

core-web/libs/sdk/client/src/lib/client/page/utils.spec.ts

Lines changed: 95 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
jest.mock('consola');
1+
import { DotHttpError } from '@dotcms/types';
22

3-
import { consola } from 'consola';
4-
5-
import { buildPageQuery, buildQuery, mapContentResponse } from './utils';
3+
import { buildPageQuery, buildQuery, fetchStyleEditorSchemas, mapContentResponse } from './utils';
64

75
describe('buildPageQuery()', () => {
86
it('generates a query containing the PageContent operation', () => {
@@ -42,20 +40,24 @@ describe('buildPageQuery()', () => {
4240
});
4341

4442
it('does not warn when verbose is false and no page provided', () => {
43+
const warnSpy = jest.spyOn(console, 'warn').mockImplementation(() => undefined);
4544
buildPageQuery({ verbose: false });
46-
expect(consola.warn).not.toHaveBeenCalled();
45+
expect(warnSpy).not.toHaveBeenCalled();
46+
warnSpy.mockRestore();
4747
});
4848

4949
it('does not warn when page is provided even with verbose=true', () => {
50+
const warnSpy = jest.spyOn(console, 'warn').mockImplementation(() => undefined);
5051
buildPageQuery({ page: 'title', verbose: true });
51-
expect(consola.warn).not.toHaveBeenCalled();
52+
expect(warnSpy).not.toHaveBeenCalled();
53+
warnSpy.mockRestore();
5254
});
5355

5456
it('warns when verbose=true and no page fragment is provided', () => {
57+
const warnSpy = jest.spyOn(console, 'warn').mockImplementation(() => undefined);
5558
buildPageQuery({ verbose: true });
56-
expect(consola.warn).toHaveBeenCalledWith(
57-
expect.stringContaining('No page query was found')
58-
);
59+
expect(warnSpy).toHaveBeenCalledWith(expect.stringContaining('No page query was found'));
60+
warnSpy.mockRestore();
5961
});
6062
});
6163

@@ -84,6 +86,90 @@ describe('buildQuery()', () => {
8486
});
8587
});
8688

89+
describe('fetchStyleEditorSchemas()', () => {
90+
const config = { dotcmsUrl: 'https://demo.dotcms.com' } as Parameters<
91+
typeof fetchStyleEditorSchemas
92+
>[1];
93+
const requestOptions = {} as Parameters<typeof fetchStyleEditorSchemas>[2];
94+
95+
const makeHttpClient = (impl: () => unknown) =>
96+
({ request: jest.fn().mockImplementation(impl) }) as Parameters<
97+
typeof fetchStyleEditorSchemas
98+
>[3];
99+
100+
it('warns and returns [] when pageId is undefined', async () => {
101+
const warnSpy = jest.spyOn(console, 'warn').mockImplementation(() => undefined);
102+
const result = await fetchStyleEditorSchemas(
103+
undefined,
104+
config,
105+
requestOptions,
106+
makeHttpClient(() => undefined)
107+
);
108+
expect(warnSpy).toHaveBeenCalledWith(
109+
expect.stringContaining('fetchStyleEditorSchemas called without a pageId')
110+
);
111+
expect(result).toEqual([]);
112+
warnSpy.mockRestore();
113+
});
114+
115+
it('returns entity array on success', async () => {
116+
const schemas = [{ id: '1' }];
117+
const httpClient = makeHttpClient(() => Promise.resolve({ entity: schemas }));
118+
const result = await fetchStyleEditorSchemas('page-id', config, requestOptions, httpClient);
119+
expect(result).toEqual(schemas);
120+
});
121+
122+
it('returns [] when entity is not an array', async () => {
123+
const httpClient = makeHttpClient(() => Promise.resolve({ entity: null }));
124+
const result = await fetchStyleEditorSchemas('page-id', config, requestOptions, httpClient);
125+
expect(result).toEqual([]);
126+
});
127+
128+
it('warns with auth message on 401 error', async () => {
129+
const warnSpy = jest.spyOn(console, 'warn').mockImplementation(() => undefined);
130+
const error = new DotHttpError({
131+
status: 401,
132+
statusText: 'Unauthorized',
133+
message: 'Unauthorized'
134+
});
135+
const httpClient = makeHttpClient(() => Promise.reject(error));
136+
const result = await fetchStyleEditorSchemas('page-id', config, requestOptions, httpClient);
137+
expect(warnSpy).toHaveBeenCalledWith(
138+
expect.stringContaining('Style editor schemas request failed with 401')
139+
);
140+
expect(result).toEqual([]);
141+
warnSpy.mockRestore();
142+
});
143+
144+
it('warns with auth message on 403 error', async () => {
145+
const warnSpy = jest.spyOn(console, 'warn').mockImplementation(() => undefined);
146+
const error = new DotHttpError({
147+
status: 403,
148+
statusText: 'Forbidden',
149+
message: 'Forbidden'
150+
});
151+
const httpClient = makeHttpClient(() => Promise.reject(error));
152+
const result = await fetchStyleEditorSchemas('page-id', config, requestOptions, httpClient);
153+
expect(warnSpy).toHaveBeenCalledWith(
154+
expect.stringContaining('Style editor schemas request failed with 403')
155+
);
156+
expect(result).toEqual([]);
157+
warnSpy.mockRestore();
158+
});
159+
160+
it('uses console warn for non-auth errors', async () => {
161+
const warnSpy = jest.spyOn(console, 'warn').mockImplementation(() => undefined);
162+
const httpClient = makeHttpClient(() => Promise.reject(new Error('Network error')));
163+
const result = await fetchStyleEditorSchemas('page-id', config, requestOptions, httpClient);
164+
expect(warnSpy).toHaveBeenCalledWith(
165+
expect.stringContaining('Skipping style editor schemas'),
166+
expect.any(Error)
167+
);
168+
expect(result).toEqual([]);
169+
warnSpy.mockRestore();
170+
});
171+
});
172+
87173
describe('mapContentResponse()', () => {
88174
it('returns undefined when responseData is undefined', () => {
89175
expect(mapContentResponse(undefined, ['blogs'])).toBeUndefined();

core-web/libs/sdk/client/src/lib/client/page/utils.ts

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import { consola } from 'consola';
2-
31
import {
42
DotCMSClientConfig,
53
DotGraphQLApiResponse,
@@ -60,7 +58,7 @@ export const buildPageQuery = ({
6058
verbose?: boolean;
6159
}) => {
6260
if (!page && verbose) {
63-
consola.warn(
61+
console.warn(
6462
"[DotCMS Client]: No page query was found, so we're loading all content using _map. This might slow things down. For better performance, we recommend adding a specific query in the page attribute."
6563
);
6664
}
@@ -285,12 +283,8 @@ export async function fetchStyleEditorSchemas(
285283
requestOptions: DotRequestOptions,
286284
httpClient: DotHttpClient
287285
): Promise<StyleEditorFormSchema[]> {
288-
if (typeof window === 'undefined') {
289-
return [];
290-
}
291-
292286
if (!pageId) {
293-
consola.warn(
287+
console.warn(
294288
'[DotCMS PageClient]: fetchStyleEditorSchemas called without a pageId — ' +
295289
'make sure "identifier" is included in your GraphQL page fragment.'
296290
);
@@ -319,14 +313,13 @@ export async function fetchStyleEditorSchemas(
319313
return entity as StyleEditorFormSchema[];
320314
} catch (error) {
321315
if (error instanceof DotHttpError && (error.status === 401 || error.status === 403)) {
322-
consola.warn(
316+
console.warn(
323317
`[DotCMS PageClient]: Style editor schemas request failed with ${error.status} — ` +
324318
'make sure your DotCMS client is configured with a valid authToken that has READ access to the page.'
325319
);
326320
} else {
327-
consola.debug('[DotCMS PageClient]: Skipping style editor schemas:', error);
321+
console.warn('[DotCMS PageClient]: Skipping style editor schemas:', error);
328322
}
329-
330323
return [];
331324
}
332325
}

examples/nextjs/src/app/[[...slug]]/page.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { redirect } from "next/navigation";
22
import NotFound from "@/app/not-found";
33
import { ErrorPage, ERROR_COPY } from "@/components/error";
4-
import { Page } from "@/views/Page";
54
import { getDotCMSPage } from "@/utils/getDotCMSPage";
65
import { Page } from "@/views/Page";
76

0 commit comments

Comments
 (0)