Skip to content

Commit

Permalink
Start partial refactor of remoteService
Browse files Browse the repository at this point in the history
  • Loading branch information
minottic committed Mar 11, 2024
1 parent acda237 commit 0059f4a
Show file tree
Hide file tree
Showing 2 changed files with 210 additions and 74 deletions.
153 changes: 141 additions & 12 deletions scilog/src/app/core/remote-data.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ import { TestBed } from '@angular/core/testing';
import { RemoteDataService } from '@shared/remote-data.service';
import { HttpClientTestingModule } from '@angular/common/http/testing';
import { AppConfigService } from '../app-config.service';
import { LogbookDataService, LogbookItemDataService } from './remote-data.service';
import { LogbookDataService, LogbookItemDataService, SearchDataService } from './remote-data.service';
import { of } from 'rxjs';
import { WidgetItemConfig } from './model/config';

const getConfig = () => ({});

Expand All @@ -23,6 +24,36 @@ describe('RemoteDataService', () => {
expect(service).toBeTruthy();
});

it('should test staticFilters', () => {
expect(service['staticFilters']()).toEqual([{ snippetType: { inq: ["paragraph", "image"] } }, { deleted: false }]);
});

it('should test addIncludeScope', () => {
expect(service['addIncludeScope']()).toEqual({
scope:
{
include: [{
relation: 'subsnippets',
scope: {
where: { snippetType: 'edit' }
}
}]
}
});
});

it('should test tagsFilter', () => {
expect(service['tagsFilter']({ tags: ['a', 'b'], excludeTags: ['c', 'd'] })).toEqual(
[{ tags: { inq: ['a', 'b'] } }, { tags: { nin: ['c', 'd'] } }]
);
});

it('should test parentFilter', () => {
expect(service['parentFilter']({ targetId: 'target', additionalLogbooks: ['add1', 'add2'] })).toEqual(
[{ parentId: { inq: ['target', 'add1', 'add2'] } }]
);
});

});


Expand Down Expand Up @@ -58,29 +89,56 @@ describe('LogbookItemDataService', () => {
showSnippetHeader: false
}
};
let filter = LogbookItemDataService._prepareFilters(config);
expect(filter["where"].and[0]).toEqual({ "and": [{ "or": [{ "snippetType": "paragraph" }, { "snippetType": "image" }] }, { "deleted": false }] });
let filter = service['_prepareFilters'](config);
expect(filter["where"]).toEqual({
and: [
{ snippetType: { inq: ["paragraph", "image"] } },
{ deleted: false },
{ parentId: { inq: ['target'] } }
]
});
});

it('should include paragraph targetId filter', () => {
it('should include paragraph targetIds and tags', () => {
let config = {
general: {
type: 'logbook',
title: 'Logbook view',
},
filter: {
targetId: "target",
additionalLogbooks: [],
tags: []
additionalLogbooks: ['target2'],
tags: ['a', 'b'],
excludeTags: ['c', 'd']
},
view: {
order: ['defaultOrder ASC'],
hideMetadata: false,
showSnippetHeader: false
}
};
let filter = LogbookItemDataService._prepareFilters(config);
expect(filter["where"].and[1]).toEqual({ "or": [{ "parentId": { "eq": config.filter.targetId } }] });
let filter = service['_prepareFilters'](config);
expect(filter["where"]).toEqual({
and: [
{ snippetType: { inq: ["paragraph", "image"] } },
{ deleted: false },
{ tags: { inq: ['a', 'b'] } },
{ tags: { nin: ['c', 'd'] } },
{ parentId: { inq: ['target', 'target2'] } }
]
});
expect(filter['include']).toEqual([{
relation: 'subsnippets',
scope:
{
include: [{
relation: 'subsnippets',
scope: {
where: { snippetType: 'edit' }
}
}]
}
}]);
});

it('should include tag filter', () => {
Expand All @@ -100,7 +158,7 @@ describe('LogbookItemDataService', () => {
showSnippetHeader: false
}
};
let filter = LogbookItemDataService._prepareFilters(config);
let filter = service['_prepareFilters'](config);
expect(filter["where"].and[2]).toEqual({ "tags": { "inq": config.filter.tags } });
});

Expand All @@ -121,7 +179,7 @@ describe('LogbookItemDataService', () => {
showSnippetHeader: false
}
};
let filter = LogbookItemDataService._prepareFilters(config);
let filter = service['_prepareFilters'](config);
expect(filter["where"].and[2]).toEqual({ "tags": { "inq": config.filter.tags } });
});

Expand All @@ -143,7 +201,7 @@ describe('LogbookItemDataService', () => {
showSnippetHeader: false
}
};
let filter = LogbookItemDataService._prepareFilters(config);
let filter = service['_prepareFilters'](config);
expect(filter["where"].and[3]).toEqual({ "tags": { "nin": config.filter.excludeTags } });
});

Expand All @@ -165,7 +223,7 @@ describe('LogbookItemDataService', () => {
showSnippetHeader: false
}
};
let filter = LogbookItemDataService._prepareFilters(config);
let filter = service['_prepareFilters'](config);
expect(filter["where"].and[2]).toEqual({ "tags": { "inq": config.filter.tags } });
expect(filter["where"].and[3]).toEqual({ "tags": { "nin": config.filter.excludeTags } });
});
Expand Down Expand Up @@ -208,3 +266,74 @@ describe('LogbookDataService', () => {
});

});

describe('SearchDataService', () => {
let service: SearchDataService;

beforeEach(() => {
TestBed.configureTestingModule({
imports: [HttpClientTestingModule],
providers: [{ provide: AppConfigService, useValue: { getConfig } }],
});
service = TestBed.inject(SearchDataService);
});

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

it('should be created', () => {
const config = {
general: {
type: 'logbook',
title: 'Logbook view',
},
filter: {
targetId: "target",
additionalLogbooks: ['target2'],
tags: ['a', 'b'],
excludeTags: ['c', 'd']
},
view: {
order: ['defaultOrder ASC'],
hideMetadata: false,
showSnippetHeader: false
}
};
const filter = service['_prepareFilters'](config);
expect(filter["where"]).toEqual({
and: [
{ snippetType: { inq: ["paragraph", "image"] } },
{ deleted: false },
{ tags: { inq: ['a', 'b'] } },
{ tags: { nin: ['c', 'd'] } },
{ parentId: { inq: ['target', 'target2'] } }
]
});

expect(filter['include']).toEqual([{
relation: 'subsnippets',
}]);
});

it('should test addIncludeScope', () => {
expect(service['addIncludeScope']()).toEqual({});
});

[
['#search', '%23search'],
['@search', '%40search'],
['search', 'search'],
].forEach(t => {
it(`should test getDataBuffer ${t[0]}`, () => {
service.searchString = t[0];
const getSnippetSpy = spyOn<any>(service, 'getSnippets').and.returnValue({ toPromise: () => { } });
spyOn<any>(service, '_prepareParams');
service.getDataBuffer(0, 0, {} as WidgetItemConfig);
expect(getSnippetSpy.calls.mostRecent().args[0]).toEqual(
`basesnippets/search=${t[1]}`
)
});
});

});
Loading

0 comments on commit 0059f4a

Please sign in to comment.