Skip to content

Commit

Permalink
feat(editor-content): refactor upload service #29872
Browse files Browse the repository at this point in the history
  • Loading branch information
nicobytes committed Sep 27, 2024
1 parent b531157 commit 6ffdd79
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -1,45 +1,41 @@
import { createHttpFactory, HttpMethod, SpectatorHttp } from '@ngneat/spectator/jest';
import { createHttpFactory, SpectatorHttp, SpyObject, mockProvider } from '@ngneat/spectator/jest';
import { of } from 'rxjs';

import { DotWorkflowActionsFireService } from '@dotcms/data-access';

import { DotUploadFileService } from './dot-upload-file.service';

describe('DotUploadFileService', () => {
let spectator: SpectatorHttp<DotUploadFileService>;
let dotWorkflowActionsFireService: SpyObject<DotWorkflowActionsFireService>;


const createHttp = createHttpFactory({
service: DotUploadFileService,
providers: [DotUploadFileService, DotWorkflowActionsFireService]
providers: [DotUploadFileService, mockProvider(DotWorkflowActionsFireService)]
});

beforeEach(() => (spectator = createHttp()));
beforeEach(() => {
spectator = createHttp();

dotWorkflowActionsFireService = spectator.inject(DotWorkflowActionsFireService);
});

it('should be created', () => {
expect(spectator.service).toBeTruthy();
});

describe('uploadDotAsset', () => {
it('should upload a file as a dotAsset', () => {
dotWorkflowActionsFireService.newContentlet.mockReturnValueOnce(of({ entity: { identifier: 'test' } }));

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' } });
expect(dotWorkflowActionsFireService.newContentlet).toHaveBeenCalled();
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,40 @@ describe('DotWorkflowActionsFireService', () => {
});
});

it('should SAVE and return a new contentlet with FormData', (done) => {
const mockResult = {
name: 'test'
};

const file = new File(['hello'], 'hello.txt', { type: 'text/plain' });

const requestBody = {
contentlet: {
contentType: 'dotAsset',
file: file.name
}
};

const formData = new FormData();
formData.append('file', file);

spectator.service.newContentlet('dotAsset', { file: file.name }, formData).subscribe((res) => {
expect(res).toEqual([mockResult]);
done();
});

const req = spectator.expectOne(
'/api/v1/workflow/actions/default/fire/NEW',
HttpMethod.PUT
);

expect(req.request.body.get('json')).toEqual(JSON.stringify(requestBody));

req.flush({
entity: [mockResult]
});
});

it('should EDIT and return the updated contentlet', (done) => {
const mockResult = {
inode: '123'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ export class DotWorkflowActionsFireService {
.put(
url,
formData ? formData : bodyRequest,
{ headers: this.defaultHeaders }
{ headers: formData ? new HttpHeaders() : this.defaultHeaders }
)
.pipe(take(1), pluck('entity'));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ import {
DotHttpErrorManagerService,
DotLicenseService,
DotMessageDisplayService,
DotMessageService
DotMessageService,
DotWorkflowActionsFireService
} from '@dotcms/data-access';
import { DotKeyValueComponent } from '@dotcms/ui';

Expand Down Expand Up @@ -159,6 +160,16 @@ const FIELD_TYPES_COMPONENTS: Record<FIELD_TYPES, Type<unknown> | DotEditFieldTe
},
[FIELD_TYPES.WYSIWYG]: {
component: DotEditContentWYSIWYGFieldComponent,
providers: [
{
provide: DotFileFieldUploadService,
useValue: {}
},
{
provide: DotWorkflowActionsFireService,
useValue: {}
}
],
declarations: [MockComponent(EditorComponent)]
},
[FIELD_TYPES.CATEGORY]: {
Expand Down

0 comments on commit 6ffdd79

Please sign in to comment.