Skip to content

Commit

Permalink
feat(editor-content): add unit tests to DotFileFieldUiMessageComponent
Browse files Browse the repository at this point in the history
  • Loading branch information
nicobytes committed Sep 27, 2024
1 parent 25869cf commit a89e9e2
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,41 @@ import { createHttpFactory, HttpMethod, SpectatorHttp } from '@ngneat/spectator/
import { DotUploadFileService } from './dot-upload-file.service';

describe('DotUploadFileService', () => {
let spectator: SpectatorHttp<DotUploadFileService>;
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<DotUploadFileService>;
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' } });
});
});
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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<DotCMSContentlet[]>;
}),
catchError((error) => throwError(error))
Expand Down
Original file line number Diff line number Diff line change
@@ -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<DotFileFieldUiMessageComponent>;

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


});

0 comments on commit a89e9e2

Please sign in to comment.