-
Notifications
You must be signed in to change notification settings - Fork 79
Add generic SimpleCollection base classes to reduce collection boilerplate #964
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Copilot
wants to merge
5
commits into
master
Choose a base branch
from
copilot/adopt-generic-factory-pattern
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
2203e4c
Initial plan
Copilot 9cd4b89
Add generic SimpleCollection and SimpleCollectionWithParent base clas…
Copilot 6268c0f
Fix type imports and generic type parameters based on code review fee…
Copilot 24d5e96
Remove unused IRawBackupConfigurationInfo import
Copilot 49bfe62
Refactor PartitionCollection, ReplicaOnPartitionCollection, and Servi…
Copilot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
159 changes: 159 additions & 0 deletions
159
src/SfxWeb/src/app/Models/DataModels/collections/CollectionBase.spec.ts
This file contains hidden or 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,159 @@ | ||
| import { of, Observable } from 'rxjs'; | ||
| import { DataService } from 'src/app/services/data.service'; | ||
| import { RestClientService } from 'src/app/services/rest-client.service'; | ||
| import { DataModelBase } from '../Base'; | ||
| import { SimpleCollection, SimpleCollectionWithParent } from './CollectionBase'; | ||
|
|
||
| // Mock raw data type | ||
| interface IRawTestItem { | ||
| Id: string; | ||
| Name: string; | ||
| } | ||
|
|
||
| // Mock model class | ||
| class TestModel extends DataModelBase<IRawTestItem> { | ||
| constructor(data: DataService, raw: IRawTestItem) { | ||
| super(data, raw); | ||
| } | ||
|
|
||
| public get uniqueId(): string { | ||
| return this.raw.Id; | ||
| } | ||
| } | ||
|
|
||
| // Mock model class with parent | ||
| class TestModelWithParent extends DataModelBase<IRawTestItem> { | ||
| constructor(data: DataService, raw: IRawTestItem, public parent: { id: string }) { | ||
| super(data, raw, parent); | ||
| } | ||
|
|
||
| public get uniqueId(): string { | ||
| return this.raw.Id; | ||
| } | ||
| } | ||
|
|
||
| // Mock parent type | ||
| interface ITestParent { | ||
| id: string; | ||
| name: string; | ||
| } | ||
|
|
||
| describe('CollectionBase', () => { | ||
| const restClientMock: RestClientService = {} as RestClientService; | ||
|
|
||
| const mockDataService: DataService = { | ||
| restClient: restClientMock, | ||
| } as DataService; | ||
|
|
||
| describe('SimpleCollection', () => { | ||
| let collection: SimpleCollection<TestModel, IRawTestItem>; | ||
| let mockFetchFn: (data: DataService) => Observable<IRawTestItem[]>; | ||
| const mockRawItems: IRawTestItem[] = [ | ||
| { Id: '1', Name: 'Item 1' }, | ||
| { Id: '2', Name: 'Item 2' }, | ||
| { Id: '3', Name: 'Item 3' } | ||
| ]; | ||
|
|
||
| beforeEach(() => { | ||
| mockFetchFn = jasmine.createSpy('fetchFn').and.returnValue(of(mockRawItems)); | ||
| collection = new SimpleCollection<TestModel, IRawTestItem>( | ||
| mockDataService, | ||
| TestModel, | ||
| mockFetchFn | ||
| ); | ||
| }); | ||
|
|
||
| it('should create a collection instance', () => { | ||
| expect(collection).toBeTruthy(); | ||
| expect(collection.length).toBe(0); | ||
| expect(collection.isInitialized).toBe(false); | ||
| }); | ||
|
|
||
| it('should fetch and map items on refresh', async () => { | ||
| await collection.refresh().toPromise(); | ||
|
|
||
| expect(mockFetchFn).toHaveBeenCalled(); | ||
| expect(collection.length).toBe(3); | ||
| expect(collection.isInitialized).toBe(true); | ||
| }); | ||
|
|
||
| it('should create correct model instances', async () => { | ||
| await collection.refresh().toPromise(); | ||
|
|
||
| expect(collection.collection[0]).toBeInstanceOf(TestModel); | ||
| expect(collection.collection[0].raw.Id).toBe('1'); | ||
| expect(collection.collection[0].raw.Name).toBe('Item 1'); | ||
| }); | ||
|
|
||
| it('should find items by uniqueId', async () => { | ||
| await collection.refresh().toPromise(); | ||
|
|
||
| const found = collection.find('2'); | ||
| expect(found).toBeTruthy(); | ||
| expect(found.raw.Name).toBe('Item 2'); | ||
| }); | ||
|
|
||
| it('should return null for non-existing uniqueId', async () => { | ||
| await collection.refresh().toPromise(); | ||
|
|
||
| const found = collection.find('999'); | ||
| expect(found).toBeFalsy(); | ||
| }); | ||
| }); | ||
|
|
||
| describe('SimpleCollectionWithParent', () => { | ||
| let collection: SimpleCollectionWithParent<TestModelWithParent, IRawTestItem, ITestParent>; | ||
| let mockFetchFn: (data: DataService, parent: ITestParent) => Observable<IRawTestItem[]>; | ||
| const mockParent: ITestParent = { id: 'parent-1', name: 'Parent' }; | ||
| const mockRawItems: IRawTestItem[] = [ | ||
| { Id: 'p1-1', Name: 'Item 1' }, | ||
| { Id: 'p1-2', Name: 'Item 2' } | ||
| ]; | ||
|
|
||
| beforeEach(() => { | ||
| mockFetchFn = jasmine.createSpy('fetchFn').and.returnValue(of(mockRawItems)); | ||
| collection = new SimpleCollectionWithParent<TestModelWithParent, IRawTestItem, ITestParent>( | ||
| mockDataService, | ||
| mockParent, | ||
| TestModelWithParent, | ||
| mockFetchFn | ||
| ); | ||
| }); | ||
|
|
||
| it('should create a collection instance with parent', () => { | ||
| expect(collection).toBeTruthy(); | ||
| expect(collection.parent).toBe(mockParent); | ||
| expect(collection.length).toBe(0); | ||
| expect(collection.isInitialized).toBe(false); | ||
| }); | ||
|
|
||
| it('should fetch items using parent in fetch function', async () => { | ||
| await collection.refresh().toPromise(); | ||
|
|
||
| expect(mockFetchFn).toHaveBeenCalled(); | ||
| expect(collection.length).toBe(2); | ||
| }); | ||
|
|
||
| it('should create model instances with parent reference', async () => { | ||
| await collection.refresh().toPromise(); | ||
|
|
||
| expect(collection.collection[0]).toBeInstanceOf(TestModelWithParent); | ||
| expect(collection.collection[0].parent).toBe(mockParent); | ||
| }); | ||
|
|
||
| it('should handle empty collections', async () => { | ||
| mockFetchFn = jasmine.createSpy('fetchFn').and.returnValue(of([])); | ||
| collection = new SimpleCollectionWithParent<TestModelWithParent, IRawTestItem, ITestParent>( | ||
| mockDataService, | ||
| mockParent, | ||
| TestModelWithParent, | ||
| mockFetchFn | ||
| ); | ||
|
|
||
| await collection.refresh().toPromise(); | ||
|
|
||
| expect(collection.length).toBe(0); | ||
| expect(collection.isInitialized).toBe(true); | ||
| }); | ||
| }); | ||
| }); |
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.