Skip to content
This repository was archived by the owner on Jun 1, 2025. It is now read-only.

Commit 42194be

Browse files
committed
refactor(tests): add more Angular-Slickgrid tests and refactor code base
1 parent e51035a commit 42194be

File tree

2 files changed

+292
-194
lines changed

2 files changed

+292
-194
lines changed

src/app/modules/angular-slickgrid/components/__tests__/angular-slickgrid.component.spec.ts

Lines changed: 101 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,24 @@ import { AngularSlickgridComponent } from '../angular-slickgrid.component';
77
import { SlickPaginationComponent } from '../slick-pagination.component';
88
import { SlickgridConfig } from '../../slickgrid-config';
99
import { FilterFactory } from '../../filters/filterFactory';
10-
import { CurrentSorter, GridOption } from '../../models';
10+
import { Filters } from '../../filters';
11+
import { CurrentFilter, CurrentSorter, GridOption } from '../../models';
1112
import {
1213
AngularUtilService,
1314
CollectionService,
1415
ExcelExportService,
1516
ExportService,
1617
ExtensionService,
1718
FilterService,
19+
GraphqlService,
1820
GridService,
1921
GridEventService,
2022
GridStateService,
2123
GroupingAndColspanService,
2224
PaginationService,
2325
ResizerService,
2426
SharedService,
25-
SortService
27+
SortService,
2628
} from '../../services';
2729
import {
2830
ExtensionUtility,
@@ -78,13 +80,32 @@ const groupingAndColspanServiceStub = {
7880
dispose: jest.fn(),
7981
} as unknown as GroupingAndColspanService;
8082

83+
const mockGraphqlService = {
84+
getDatasetName: jest.fn(),
85+
updateFilters: jest.fn(),
86+
updateSorters: jest.fn(),
87+
updatePagination: jest.fn(),
88+
} as unknown as GraphqlService;
89+
90+
const filterServiceStub = {
91+
dispose: jest.fn(),
92+
init: jest.fn(),
93+
bindBackendOnFilter: jest.fn(),
94+
bindLocalOnFilter: jest.fn(),
95+
populateColumnFilterSearchTermPresets: jest.fn(),
96+
getColumnFilters: jest.fn(),
97+
onFilterCleared: new Subject<CurrentFilter[]>(),
98+
onFilterChanged: new Subject<CurrentFilter[]>(),
99+
} as unknown as FilterService;
100+
81101
const sortServiceStub = {
102+
bindBackendOnSort: jest.fn(),
82103
bindLocalOnSort: jest.fn(),
83104
dispose: jest.fn(),
84105
loadLocalGridPresets: jest.fn(),
85106
onSortChanged: new Subject<CurrentSorter[]>(),
86107
onSortCleared: new Subject<boolean>()
87-
};
108+
} as unknown as SortService;
88109

89110
const mockGroupItemMetaProvider = {
90111
init: jest.fn(),
@@ -146,6 +167,7 @@ Slick.DraggableGrouping = mockDraggableGroupingImplementation;
146167
describe('App Component', () => {
147168
let fixture: ComponentFixture<AngularSlickgridComponent>;
148169
let component: AngularSlickgridComponent;
170+
let graphqlService: GraphqlService;
149171

150172
beforeEach(async(() => {
151173
TestBed.configureTestingModule({
@@ -157,7 +179,6 @@ describe('App Component', () => {
157179
AngularUtilService,
158180
CollectionService,
159181
FilterFactory,
160-
FilterService,
161182
GridService,
162183
GridEventService,
163184
GridStateService,
@@ -178,6 +199,8 @@ describe('App Component', () => {
178199
RowMoveManagerExtension,
179200
RowSelectionExtension,
180201
SlickgridConfig,
202+
{ provide: FilterService, useValue: filterServiceStub },
203+
{ provide: GraphqlService, useValue: mockGraphqlService },
181204
{ provide: ExcelExportService, useValue: excelExportServiceStub },
182205
{ provide: ExportService, useValue: exportServiceStub },
183206
{ provide: ExtensionService, useValue: extensionServiceStub },
@@ -198,6 +221,7 @@ describe('App Component', () => {
198221
// create the component
199222
fixture = TestBed.createComponent(AngularSlickgridComponent);
200223
component = fixture.debugElement.componentInstance;
224+
graphqlService = TestBed.get(GraphqlService);
201225

202226
// setup bindable properties
203227
component.gridId = 'grid1';
@@ -390,6 +414,79 @@ describe('App Component', () => {
390414

391415
expect(spy).toHaveBeenCalled();
392416
});
417+
418+
it('should destroy component and its DOM element when requested', () => {
419+
const spy = jest.spyOn(component, 'destroyGridContainerElm');
420+
421+
component.ngAfterViewInit();
422+
component.destroy(true);
423+
424+
expect(spy).toHaveBeenCalledWith();
425+
});
426+
});
427+
428+
describe('Backend Service API', () => {
429+
beforeEach(() => {
430+
component.gridOptions = {
431+
backendServiceApi: {
432+
service: graphqlService,
433+
preProcess: () => jest.fn(),
434+
process: (query) => new Promise((resolve) => resolve('process resolved')),
435+
}
436+
};
437+
});
438+
439+
afterEach(() => {
440+
jest.clearAllMocks();
441+
});
442+
443+
it('should call the "createBackendApiInternalPostProcessCallback" method when Backend Service API is defined with a Graphql Service', () => {
444+
const spy = jest.spyOn(component, 'createBackendApiInternalPostProcessCallback');
445+
446+
component.ngAfterViewInit();
447+
448+
expect(spy).toHaveBeenCalled();
449+
expect(component.gridOptions.backendServiceApi.internalPostProcess).toEqual(expect.any(Function));
450+
});
451+
452+
it('should invoke "updateFilters" method with filters returned from "getColumnFilters" of the Filter Service when there is no Presets defined', () => {
453+
const mockColumnFilter = { name: { columnId: 'name', columnDef: { id: 'name', field: 'name', filter: { model: Filters.autoComplete } }, operator: 'EQ', searchTerms: ['john'] } };
454+
// @ts-ignore
455+
jest.spyOn(filterServiceStub, 'getColumnFilters').mockReturnValue(mockColumnFilter);
456+
const backendSpy = jest.spyOn(mockGraphqlService, 'updateFilters');
457+
458+
component.gridOptions.presets = undefined;
459+
component.ngAfterViewInit();
460+
461+
expect(backendSpy).toHaveBeenCalledWith(mockColumnFilter, false);
462+
});
463+
464+
it('should call the "updateFilters" method when filters are defined in the "presets" property', () => {
465+
const spy = jest.spyOn(mockGraphqlService, 'updateFilters');
466+
const mockFilters = [{ columnId: 'company', searchTerms: ['xyz'], operator: 'IN' }] as CurrentFilter[];
467+
component.gridOptions.presets = { filters: mockFilters };
468+
component.ngAfterViewInit();
469+
470+
expect(spy).toHaveBeenCalledWith(mockFilters, true);
471+
});
472+
473+
it('should call the "updateSorters" method when filters are defined in the "presets" property', () => {
474+
const spy = jest.spyOn(mockGraphqlService, 'updateSorters');
475+
const mockSorters = [{ columnId: 'name', direction: 'asc' }] as CurrentSorter[];
476+
component.gridOptions.presets = { sorters: mockSorters };
477+
component.ngAfterViewInit();
478+
479+
expect(spy).toHaveBeenCalledWith(undefined, mockSorters);
480+
});
481+
482+
it('should call the "updatePagination" method when filters are defined in the "presets" property', () => {
483+
const spy = jest.spyOn(mockGraphqlService, 'updatePagination');
484+
485+
component.gridOptions.presets = { pagination: { pageNumber: 2, pageSize: 20 } };
486+
component.ngAfterViewInit();
487+
488+
expect(spy).toHaveBeenCalledWith(2, 20);
489+
});
393490
});
394491
});
395492
});

0 commit comments

Comments
 (0)