From a89e9e2dacb2266488e27b05f1dd042b93a3d326 Mon Sep 17 00:00:00 2001 From: Nicolas Molina Date: Fri, 27 Sep 2024 06:27:02 -0400 Subject: [PATCH] feat(editor-content): add unit tests to DotFileFieldUiMessageComponent #29872 --- .../dot-upload-file.service.spec.ts | 65 ++++++++++--------- .../dot-upload-file.service.ts | 15 ++--- ...ot-file-field-ui-message.component.spec.ts | 63 ++++++++++++++++++ 3 files changed, 102 insertions(+), 41 deletions(-) create mode 100644 core-web/libs/edit-content/src/lib/fields/dot-edit-content-file-field/components/dot-file-field-ui-message/dot-file-field-ui-message.component.spec.ts diff --git a/core-web/libs/data-access/src/lib/dot-upload-file/dot-upload-file.service.spec.ts b/core-web/libs/data-access/src/lib/dot-upload-file/dot-upload-file.service.spec.ts index afcb79906568..400125c0cc2f 100644 --- a/core-web/libs/data-access/src/lib/dot-upload-file/dot-upload-file.service.spec.ts +++ b/core-web/libs/data-access/src/lib/dot-upload-file/dot-upload-file.service.spec.ts @@ -3,38 +3,41 @@ import { createHttpFactory, HttpMethod, SpectatorHttp } from '@ngneat/spectator/ import { DotUploadFileService } from './dot-upload-file.service'; describe('DotUploadFileService', () => { - let spectator: SpectatorHttp; - const createHttp = createHttpFactory({ - service: DotUploadFileService, - providers: [DotUploadFileService] - }); - - beforeEach(() => spectator = createHttp()); - - it('should be created', () => { - expect(spectator.service).toBeTruthy(); - }); - - describe('uploadDotAsset', () => { - it('should upload a file as a dotAsset', () => { - const file = new File([''], 'test.png', { - type: 'image/png' - }); - - spectator.service.uploadDotAsset(file).subscribe(); + let spectator: SpectatorHttp; + const createHttp = createHttpFactory({ + service: DotUploadFileService, + providers: [DotUploadFileService] + }); - const req = spectator.expectOne('/api/v1/workflow/actions/default/fire/NEW', HttpMethod.PUT); + beforeEach(() => (spectator = createHttp())); - expect(req.request.body.get('json')).toEqual( - JSON.stringify({ - contentlet: { - file: 'test.png', - contentType: 'dotAsset' - } - }) - ); + it('should be created', () => { + expect(spectator.service).toBeTruthy(); + }); - req.flush({ entity: { identifier: 'test' } }); + describe('uploadDotAsset', () => { + it('should upload a file as a dotAsset', () => { + const file = new File([''], 'test.png', { + type: 'image/png' + }); + + spectator.service.uploadDotAsset(file).subscribe(); + + const req = spectator.expectOne( + '/api/v1/workflow/actions/default/fire/NEW', + HttpMethod.PUT + ); + + expect(req.request.body.get('json')).toEqual( + JSON.stringify({ + contentlet: { + file: 'test.png', + contentType: 'dotAsset' + } + }) + ); + + req.flush({ entity: { identifier: 'test' } }); + }); }); - }); -}); \ No newline at end of file +}); diff --git a/core-web/libs/data-access/src/lib/dot-upload-file/dot-upload-file.service.ts b/core-web/libs/data-access/src/lib/dot-upload-file/dot-upload-file.service.ts index 5437d6b09672..0d898139b571 100644 --- a/core-web/libs/data-access/src/lib/dot-upload-file/dot-upload-file.service.ts +++ b/core-web/libs/data-access/src/lib/dot-upload-file/dot-upload-file.service.ts @@ -29,7 +29,6 @@ interface PublishContentProps { */ @Injectable() export class DotUploadFileService { - readonly #BASE_URL = '/api/v1/workflow/actions/default'; readonly #httpClient = inject(HttpClient); readonly #uploadService = inject(DotUploadService); @@ -60,16 +59,12 @@ export class DotUploadFileService { statusCallback(FileStatus.IMPORT); return this.#httpClient - .post( - `${this.#BASE_URL}/fire/PUBLISH`, - JSON.stringify({ contentlets }), - { - headers: { - Origin: window.location.hostname, - 'Content-Type': 'application/json;charset=UTF-8' - } + .post(`${this.#BASE_URL}/fire/PUBLISH`, JSON.stringify({ contentlets }), { + headers: { + Origin: window.location.hostname, + 'Content-Type': 'application/json;charset=UTF-8' } - ) + }) .pipe(pluck('entity', 'results')) as Observable; }), catchError((error) => throwError(error)) diff --git a/core-web/libs/edit-content/src/lib/fields/dot-edit-content-file-field/components/dot-file-field-ui-message/dot-file-field-ui-message.component.spec.ts b/core-web/libs/edit-content/src/lib/fields/dot-edit-content-file-field/components/dot-file-field-ui-message/dot-file-field-ui-message.component.spec.ts new file mode 100644 index 000000000000..f1e116be12f2 --- /dev/null +++ b/core-web/libs/edit-content/src/lib/fields/dot-edit-content-file-field/components/dot-file-field-ui-message/dot-file-field-ui-message.component.spec.ts @@ -0,0 +1,63 @@ +import { + Spectator, + byTestId, + createComponentFactory, +} from '@ngneat/spectator/jest'; + + +import { DotMessageService } from '@dotcms/data-access'; +import { DotMessagePipe } from '@dotcms/ui'; +import { MockDotMessageService } from '@dotcms/utils-testing'; + +import { DotFileFieldUiMessageComponent } from './dot-file-field-ui-message.component'; + +import { getUiMessage } from '../../utils/messages'; + +describe('DotFileFieldUiMessageComponent', () => { + let spectator: Spectator; + + const createComponent = createComponentFactory({ + component: DotFileFieldUiMessageComponent, + detectChanges: false, + imports: [DotMessagePipe], + providers: [ + { + provide: DotMessageService, + useValue: new MockDotMessageService({ + 'dot.file.field.drag.and.drop.message': 'Drag and Drop File' + }) + } + ] + }); + + describe('default uiMessage', () => { + beforeEach(() => { + spectator = createComponent({ + props: { + uiMessage: getUiMessage('DEFAULT') + } as unknown + }); + }); + + it('should be created', () => { + spectator.detectChanges(); + expect(spectator.component).toBeTruthy(); + }); + + it('should have proper data', () => { + spectator.detectChanges(); + + const expectMessage = getUiMessage('DEFAULT') + + const severity = spectator.query(byTestId('ui-message-icon-container')); + const messageIcon = spectator.query(byTestId('ui-message-icon')); + const messageText = spectator.query(byTestId('ui-message-span')); + + expect(severity).toHaveClass(expectMessage.severity); + expect(messageIcon).toHaveClass(expectMessage.icon); + expect(messageText).toContainText('Drag and Drop File'); + }); + }); + + +});