Skip to content

Commit

Permalink
chore(edit-content): add unit tests #30215
Browse files Browse the repository at this point in the history
  • Loading branch information
nicobytes committed Nov 5, 2024
1 parent 23cdfc5 commit 9364dd5
Show file tree
Hide file tree
Showing 4 changed files with 164 additions and 51 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
import { createFakeEvent } from '@ngneat/spectator';
import { SpyObject, mockProvider } from '@ngneat/spectator/jest';
import { of, throwError } from 'rxjs';

import { fakeAsync, TestBed, tick } from '@angular/core/testing';

import { ComponentStatus } from '@dotcms/dotcms-models';

import { SelectExisingFileStore } from './select-existing-file.store';


import { DotEditContentService } from '../../../../../services/dot-edit-content.service';
import { TREE_SELECT_MOCK, TREE_SELECT_SITES_MOCK } from '../../../../../utils/mocks';

describe('SelectExisingFileStore', () => {
let store: InstanceType<typeof SelectExisingFileStore>;
let service: SpyObject<DotEditContentService>;

beforeEach(() => {
TestBed.configureTestingModule({
providers: [SelectExisingFileStore, mockProvider(DotEditContentService)],
});

store = TestBed.inject(SelectExisingFileStore);
service = TestBed.inject(DotEditContentService) as SpyObject<DotEditContentService>;
});

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



describe('Method: loadFolders', () => {

it('should set folders status to LOADING and then to LOADED with data', fakeAsync(() => {
service.getSitesTreePath.mockReturnValue(of(TREE_SELECT_SITES_MOCK));

store.loadFolders();

tick(50);

expect(store.folders().status).toBe(ComponentStatus.LOADED);
expect(store.folders().data).toEqual(TREE_SELECT_SITES_MOCK);
}));



it('should set folders status to ERROR on service error', fakeAsync(() => {
service.getSitesTreePath.mockReturnValue(throwError('error'));

store.loadFolders();

tick(50);

expect(store.folders().status).toBe(ComponentStatus.ERROR);
expect(store.folders().data).toEqual([]);
}));
});

describe('Method: loadChildren', () => {

it('should load children for a node', fakeAsync(() => {

const mockChildren = [...TREE_SELECT_SITES_MOCK];

service.getFoldersTreeNode.mockReturnValue(of(mockChildren));

const node = { ...TREE_SELECT_MOCK[0] };

const mockItem = {
originalEvent: createFakeEvent('click'),
node,
};
store.loadChildren(mockItem);

tick(50);

expect(node.children).toEqual(mockChildren);
expect(node.loading).toBe(false);
expect(node.leaf).toBe(true);
expect(node.icon).toBe('pi pi-folder-open');
expect(store.folders().nodeExpaned).toBe(node);
}));

it('should handle error when loading children', fakeAsync(() => {

service.getFoldersTreeNode.mockReturnValue(throwError('error'));

const node = { ...TREE_SELECT_MOCK[0], children: [] };

const mockItem = {
originalEvent: createFakeEvent('click'),
node,
};
store.loadChildren(mockItem);

tick(50);

expect(node.children).toEqual([]);
expect(node.loading).toBe(false);
}));

});

});
Original file line number Diff line number Diff line change
Expand Up @@ -131,14 +131,19 @@ export const SelectExisingFileStore = signalStore(
node.loading = true;

return dotEditContentService.getFoldersTreeNode(hostname, path).pipe(
tap((children) => {
node.loading = false;
node.leaf = true;
node.icon = 'pi pi-folder-open';
node.children = [...children];

const folders = store.folders();
patchState(store, { folders: { ...folders, nodeExpaned: node } });
tapResponse({
next: (children) => {
node.loading = false;
node.leaf = true;
node.icon = 'pi pi-folder-open';
node.children = [...children];

const folders = store.folders();
patchState(store, { folders: { ...folders, nodeExpaned: node } });
},
error: () => {
node.loading = false;
}
})
);
})
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { SpyObject, mockProvider } from '@ngneat/spectator/jest';
import { patchState } from '@ngrx/signals';
import { of, throwError } from 'rxjs';

import { TestBed } from '@angular/core/testing';
import { TestBed, fakeAsync, tick } from '@angular/core/testing';

import { FileFieldStore } from './file-field.store';

Expand All @@ -16,11 +15,11 @@ describe('FileFieldStore', () => {
let service: SpyObject<DotFileFieldUploadService>;

beforeEach(() => {
store = TestBed.overrideProvider(
DotFileFieldUploadService,
mockProvider(DotFileFieldUploadService)
).runInInjectionContext(() => new FileFieldStore());
TestBed.configureTestingModule({
providers: [FileFieldStore, mockProvider(DotFileFieldUploadService)],
});

store = TestBed.inject(FileFieldStore);
service = TestBed.inject(DotFileFieldUploadService) as SpyObject<DotFileFieldUploadService>;
});

Expand Down Expand Up @@ -98,59 +97,54 @@ describe('FileFieldStore', () => {
});

describe('Method: removeFile', () => {
it('should set the state properly when removeFile is called', () => {
patchState(store, {
uploadedFile: {
source: 'contentlet',
file: NEW_FILE_MOCK.entity
},
value: 'some value',
fileStatus: 'preview',
uiMessage: getUiMessage('SERVER_ERROR')
});
it('should set the state properly when removeFile is called', fakeAsync(() => {
const mockContentlet = NEW_FILE_MOCK.entity;
service.uploadFile.mockReturnValue(of({ source: 'contentlet', file: mockContentlet }));

const file = new File([''], 'filename', { type: 'text/plain' });
Object.defineProperty(file, 'size', { value: 5000 });

store.handleUploadFile(file);

tick(50);

store.removeFile();
expect(store.value()).toBe('');
expect(store.fileStatus()).toBe('init');
expect(store.uiMessage()).toBe(getUiMessage('DEFAULT'));
expect(store.uploadedFile()).toBeNull();
});
}));
});

describe('Method: setDropZoneState', () => {
it('should set dropZoneActive to true', () => {
patchState(store, {
dropZoneActive: false
});
store.setDropZoneState(true);
expect(store.dropZoneActive()).toBe(true);
});

it('should set dropZoneActive to false', () => {
patchState(store, {
dropZoneActive: true
});
store.setDropZoneState(false);
expect(store.dropZoneActive()).toBe(false);
});
});

describe('Method: handleUploadFile', () => {
it('should does not call uploadService with maxFileSize exceeded', () => {
patchState(store, {
maxFileSize: 10000
});
it('should does not call uploadService with maxFileSize exceeded', fakeAsync(() => {
store.setMaxSizeFile(10000);

tick(50);

const file = new File([''], 'filename', { type: 'text/plain' });
Object.defineProperty(file, 'size', { value: 20000 });

store.handleUploadFile(file);
expect(service.uploadFile).not.toHaveBeenCalled();
});
}));

it('should set state properly with maxFileSize exceeded', () => {
patchState(store, {
maxFileSize: 10000
});
it('should set state properly with maxFileSize exceeded', fakeAsync(() => {
store.setMaxSizeFile(10000);

tick(50);

const file = new File([''], 'filename', { type: 'text/plain' });
Object.defineProperty(file, 'size', { value: 20000 });
Expand All @@ -162,15 +156,15 @@ describe('FileFieldStore', () => {
...getUiMessage('MAX_FILE_SIZE_EXCEEDED'),
args: ['10000']
});
});
}));

it('should call uploadService with maxFileSize not exceeded', () => {
it('should call uploadService with maxFileSize not exceeded', fakeAsync(() => {
const mockContentlet = NEW_FILE_MOCK.entity;
service.uploadFile.mockReturnValue(of({ source: 'contentlet', file: mockContentlet }));

patchState(store, {
maxFileSize: 10000
});
store.setMaxSizeFile(10000);

tick(50);

const file = new File([''], 'filename', { type: 'text/plain' });
Object.defineProperty(file, 'size', { value: 5000 });
Expand All @@ -182,15 +176,15 @@ describe('FileFieldStore', () => {
maxSize: '10000',
uploadType: 'dotasset'
});
});
}));

it('should set state properly with maxFileSize not exceeded', () => {
it('should set state properly with maxFileSize not exceeded', fakeAsync(() => {
const mockContentlet = NEW_FILE_MOCK.entity;
service.uploadFile.mockReturnValue(of({ source: 'contentlet', file: mockContentlet }));

patchState(store, {
maxFileSize: 10000
});
store.setMaxSizeFile(10000);

tick(50);

const file = new File([''], 'filename', { type: 'text/plain' });
Object.defineProperty(file, 'size', { value: 5000 });
Expand All @@ -202,7 +196,7 @@ describe('FileFieldStore', () => {
source: 'contentlet',
file: mockContentlet
});
});
}));

it('should set state properly with an error calling uploadFile', () => {
service.uploadFile.mockReturnValue(throwError('error'));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,14 @@ export const FileFieldStore = signalStore(
...actions
});
},
/**
* setAcceptedFiles is used to set accepted files
*/
setMaxSizeFile: (maxFileSize: number) => {
patchState(store, {
maxFileSize
});
},
/**
* setUIMessage is used to set uiMessage
* @param uiMessage
Expand Down

0 comments on commit 9364dd5

Please sign in to comment.