-
Notifications
You must be signed in to change notification settings - Fork 467
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(edit-content): add unit tests #30215
- Loading branch information
Showing
4 changed files
with
164 additions
and
51 deletions.
There are no files selected for viewing
106 changes: 106 additions & 0 deletions
106
...t-file-field/components/dot-select-existing-file/store/select-existing-file.store.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
})); | ||
|
||
}); | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters