|
1 | | -jest.mock('consola'); |
| 1 | +import { DotHttpError } from '@dotcms/types'; |
2 | 2 |
|
3 | | -import { consola } from 'consola'; |
4 | | - |
5 | | -import { buildPageQuery, buildQuery, mapContentResponse } from './utils'; |
| 3 | +import { buildPageQuery, buildQuery, fetchStyleEditorSchemas, mapContentResponse } from './utils'; |
6 | 4 |
|
7 | 5 | describe('buildPageQuery()', () => { |
8 | 6 | it('generates a query containing the PageContent operation', () => { |
@@ -42,20 +40,24 @@ describe('buildPageQuery()', () => { |
42 | 40 | }); |
43 | 41 |
|
44 | 42 | it('does not warn when verbose is false and no page provided', () => { |
| 43 | + const warnSpy = jest.spyOn(console, 'warn').mockImplementation(() => undefined); |
45 | 44 | buildPageQuery({ verbose: false }); |
46 | | - expect(consola.warn).not.toHaveBeenCalled(); |
| 45 | + expect(warnSpy).not.toHaveBeenCalled(); |
| 46 | + warnSpy.mockRestore(); |
47 | 47 | }); |
48 | 48 |
|
49 | 49 | it('does not warn when page is provided even with verbose=true', () => { |
| 50 | + const warnSpy = jest.spyOn(console, 'warn').mockImplementation(() => undefined); |
50 | 51 | buildPageQuery({ page: 'title', verbose: true }); |
51 | | - expect(consola.warn).not.toHaveBeenCalled(); |
| 52 | + expect(warnSpy).not.toHaveBeenCalled(); |
| 53 | + warnSpy.mockRestore(); |
52 | 54 | }); |
53 | 55 |
|
54 | 56 | it('warns when verbose=true and no page fragment is provided', () => { |
| 57 | + const warnSpy = jest.spyOn(console, 'warn').mockImplementation(() => undefined); |
55 | 58 | 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(); |
59 | 61 | }); |
60 | 62 | }); |
61 | 63 |
|
@@ -84,6 +86,90 @@ describe('buildQuery()', () => { |
84 | 86 | }); |
85 | 87 | }); |
86 | 88 |
|
| 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 | + |
87 | 173 | describe('mapContentResponse()', () => { |
88 | 174 | it('returns undefined when responseData is undefined', () => { |
89 | 175 | expect(mapContentResponse(undefined, ['blogs'])).toBeUndefined(); |
|
0 commit comments