Skip to content

Commit

Permalink
feat(editor-content): add unit tests to DotUploadFileService #29872
Browse files Browse the repository at this point in the history
  • Loading branch information
nicobytes committed Sep 27, 2024
1 parent 987928b commit 25869cf
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 29 deletions.
Original file line number Diff line number Diff line change
@@ -1,27 +1,40 @@
import { createServiceFactory, SpectatorService } from '@ngneat/spectator';

import { HttpClientTestingModule } from '@angular/common/http/testing';
import { createHttpFactory, HttpMethod, SpectatorHttp } from '@ngneat/spectator/jest';

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

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

describe('DotUploadFileService', () => {
let spectator: SpectatorService<DotUploadFileService>;
let service: DotUploadFileService;
let spectator: SpectatorHttp<DotUploadFileService>;
const createHttp = createHttpFactory({
service: DotUploadFileService,
providers: [DotUploadFileService]
});

const createService = createServiceFactory({
service: DotUploadFileService,
imports: [HttpClientTestingModule],
providers: [DotUploadService]
});
beforeEach(() => spectator = createHttp());

beforeEach(() => {
spectator = createService();
service = spectator.service;
});
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();

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'
}
})
);

it('should be created', () => {
expect(service).toBeTruthy();
req.flush({ entity: { identifier: 'test' } });
});
});
});
});
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { from, Observable, throwError } from 'rxjs';

import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { inject, Injectable } from '@angular/core';

import { catchError, pluck, switchMap } from 'rxjs/operators';

Expand Down Expand Up @@ -29,10 +29,10 @@ interface PublishContentProps {
*/
@Injectable()
export class DotUploadFileService {
constructor(
private http: HttpClient,
private dotUploadService: DotUploadService
) {}

readonly #BASE_URL = '/api/v1/workflow/actions/default';
readonly #httpClient = inject(HttpClient);
readonly #uploadService = inject(DotUploadService);

publishContent({
data,
Expand All @@ -59,9 +59,9 @@ export class DotUploadFileService {

statusCallback(FileStatus.IMPORT);

return this.http
return this.#httpClient
.post(
'/api/v1/workflow/actions/default/fire/PUBLISH',
`${this.#BASE_URL}/fire/PUBLISH`,
JSON.stringify({ contentlets }),
{
headers: {
Expand All @@ -82,14 +82,19 @@ export class DotUploadFileService {
signal
}: PublishContentProps): Observable<DotCMSTempFile | DotCMSTempFile[]> {
return from(
this.dotUploadService.uploadFile({
this.#uploadService.uploadFile({
file,
maxSize,
signal
})
);
}

/**
* Uploads a file to dotCMS and creates a new dotAsset contentlet
* @param file the file to be uploaded
* @returns an Observable that emits the created contentlet
*/
uploadDotAsset(file: File): Observable<DotCMSContentlet> {
const formData = new FormData();
formData.append('file', file);
Expand All @@ -103,8 +108,8 @@ export class DotUploadFileService {
})
);

return this.http
.put<DotCMSContentlet>(`/api/v1/workflow/actions/default/fire/NEW`, formData)
return this.#httpClient
.put<DotCMSContentlet>(`${this.#BASE_URL}/fire/NEW`, formData)
.pipe(pluck('entity'));
}
}

0 comments on commit 25869cf

Please sign in to comment.