Skip to content

Commit 2b0c41d

Browse files
saved filters: fix unit tests
1 parent b9428a5 commit 2b0c41d

2 files changed

Lines changed: 178 additions & 50 deletions

File tree

frontend/src/app/components/dashboard/db-table-view/saved-filters-panel/saved-filters-dialog/saved-filters-dialog.component.spec.ts

Lines changed: 94 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
import { ActivatedRoute, RouterModule } from '@angular/router';
12
import { ComponentFixture, TestBed } from '@angular/core/testing';
23
import { MAT_DIALOG_DATA, MatDialogRef } from '@angular/material/dialog';
34

45
import { ConnectionsService } from 'src/app/services/connections.service';
56
import { MatSnackBar } from '@angular/material/snack-bar';
7+
import { RouterTestingModule } from '@angular/router/testing';
68
import { SavedFiltersDialogComponent } from './saved-filters-dialog.component';
79
import { TablesService } from 'src/app/services/tables.service';
810
import { of } from 'rxjs';
@@ -14,25 +16,37 @@ describe('SavedFiltersDialogComponent', () => {
1416
let connectionsServiceMock: jasmine.SpyObj<ConnectionsService>;
1517

1618
beforeEach(async () => {
17-
const tableSpy = jasmine.createSpyObj('TablesService', ['cast', 'createSavedFilter', 'deleteSavedFilter']);
19+
const tableSpy = jasmine.createSpyObj('TablesService', ['cast', 'createSavedFilter', 'deleteSavedFilter', 'updateSavedFilter']);
1820
tableSpy.cast = jasmine.createSpyObj('BehaviorSubject', ['subscribe']);
19-
21+
2022
const connectionSpy = jasmine.createSpyObj('ConnectionsService', [], {
2123
currentConnection: { type: 'postgres' }
2224
});
2325

2426
await TestBed.configureTestingModule({
25-
imports: [SavedFiltersDialogComponent],
27+
imports: [
28+
SavedFiltersDialogComponent,
29+
RouterTestingModule
30+
],
2631
providers: [
2732
{ provide: TablesService, useValue: tableSpy },
2833
{ provide: ConnectionsService, useValue: connectionSpy },
2934
{ provide: MatDialogRef, useValue: { close: jasmine.createSpy('close') } },
3035
{ provide: MatSnackBar, useValue: { open: jasmine.createSpy('open') } },
36+
{
37+
provide: ActivatedRoute,
38+
useValue: {
39+
snapshot: {
40+
paramMap: { get: () => {} },
41+
queryParamMap: { get: () => {} }
42+
}
43+
}
44+
},
3145
{ provide: MAT_DIALOG_DATA, useValue: {
3246
connectionID: '123',
3347
tableName: 'test_table',
3448
displayTableName: 'Test Table',
35-
filtersSet: {
49+
filtersSet: {
3650
name: 'Test Filter',
3751
filters: {}
3852
},
@@ -55,33 +69,33 @@ describe('SavedFiltersDialogComponent', () => {
5569
it('should create', () => {
5670
expect(component).toBeTruthy();
5771
});
58-
72+
5973
it('should toggle dynamic column', () => {
6074
const fieldName = 'test_field';
61-
75+
6276
// Initially null
6377
expect(component.dynamicColumn).toBeNull();
64-
78+
6579
// First toggle sets to fieldName
6680
component.toggleDynamicColumn(fieldName);
6781
expect(component.dynamicColumn).toBe(fieldName);
68-
82+
6983
// Second toggle sets back to null
7084
component.toggleDynamicColumn(fieldName);
7185
expect(component.dynamicColumn).toBeNull();
7286
});
73-
87+
7488
it('should exclude dynamic column from filters and include it with column_name and comparator', () => {
7589
// Setup
7690
const fieldName = 'test_field';
7791
component.tableRowFieldsShown = { [fieldName]: 'test_value' };
7892
component.tableRowFieldsComparator = { [fieldName]: 'eq' };
7993
component.dynamicColumn = fieldName;
8094
tablesServiceMock.createSavedFilter.and.returnValue(of({}));
81-
82-
// Call saveFilter
83-
component.saveFilter();
84-
95+
96+
// Call handleSaveFilters
97+
component.handleSaveFilters();
98+
8599
// Verify - filters should be empty, and dynamic_column should have column_name and comparator
86100
expect(tablesServiceMock.createSavedFilter).toHaveBeenCalledWith(
87101
component.data.connectionID,
@@ -96,25 +110,25 @@ describe('SavedFiltersDialogComponent', () => {
96110
}
97111
);
98112
});
99-
113+
100114
it('should include dynamic column with column_name and comparator and exclude it from filters', () => {
101115
// Setup
102116
const fieldName = 'test_field';
103117
const dynamicFieldName = 'dynamic_field';
104-
component.tableRowFieldsShown = {
118+
component.tableRowFieldsShown = {
105119
[fieldName]: 'test_value',
106120
[dynamicFieldName]: 'dynamic_value'
107121
};
108-
component.tableRowFieldsComparator = {
122+
component.tableRowFieldsComparator = {
109123
[fieldName]: 'eq',
110124
[dynamicFieldName]: 'contains'
111125
};
112126
component.dynamicColumn = dynamicFieldName; // Different field from the one with comparator
113127
tablesServiceMock.createSavedFilter.and.returnValue(of({}));
114-
115-
// Call saveFilter
116-
component.saveFilter();
117-
128+
129+
// Call handleSaveFilters
130+
component.handleSaveFilters();
131+
118132
// Verify - only fieldName in filters, dynamicFieldName in dynamic_column
119133
expect(tablesServiceMock.createSavedFilter).toHaveBeenCalledWith(
120134
component.data.connectionID,
@@ -129,17 +143,17 @@ describe('SavedFiltersDialogComponent', () => {
129143
}
130144
);
131145
});
132-
146+
133147
it('should handle empty values in filters as null', () => {
134148
// Setup
135149
const fieldName = 'test_field';
136150
component.tableRowFieldsShown = { [fieldName]: '' }; // Empty value
137151
component.tableRowFieldsComparator = { [fieldName]: 'eq' };
138152
tablesServiceMock.createSavedFilter.and.returnValue(of({}));
139-
140-
// Call saveFilter
141-
component.saveFilter();
142-
153+
154+
// Call handleSaveFilters
155+
component.handleSaveFilters();
156+
143157
// Verify - value should be null instead of empty string
144158
expect(tablesServiceMock.createSavedFilter).toHaveBeenCalledWith(
145159
component.data.connectionID,
@@ -150,25 +164,25 @@ describe('SavedFiltersDialogComponent', () => {
150164
}
151165
);
152166
});
153-
167+
154168
it('should handle dynamic column with no comparator', () => {
155169
// Setup
156170
const fieldName = 'test_field';
157171
const dynamicFieldName = 'dynamic_field_no_comparator';
158-
component.tableRowFieldsShown = {
172+
component.tableRowFieldsShown = {
159173
[fieldName]: 'test_value',
160174
[dynamicFieldName]: 'dynamic_value'
161175
};
162-
component.tableRowFieldsComparator = {
176+
component.tableRowFieldsComparator = {
163177
[fieldName]: 'eq'
164178
// No comparator for dynamicFieldName
165179
};
166180
component.dynamicColumn = dynamicFieldName;
167181
tablesServiceMock.createSavedFilter.and.returnValue(of({}));
168-
169-
// Call saveFilter
170-
component.saveFilter();
171-
182+
183+
// Call handleSaveFilters
184+
component.handleSaveFilters();
185+
172186
// Verify - dynamic_column should have empty comparator
173187
expect(tablesServiceMock.createSavedFilter).toHaveBeenCalledWith(
174188
component.data.connectionID,
@@ -183,4 +197,52 @@ describe('SavedFiltersDialogComponent', () => {
183197
}
184198
);
185199
});
200+
201+
it('should update existing filter when id is present', () => {
202+
// Setup
203+
const fieldName = 'test_field';
204+
const filterId = '123';
205+
component.tableRowFieldsShown = { [fieldName]: 'test_value' };
206+
component.tableRowFieldsComparator = { [fieldName]: 'eq' };
207+
component.data.filtersSet.id = filterId;
208+
tablesServiceMock.updateSavedFilter.and.returnValue(of({}));
209+
210+
// Call handleSaveFilters
211+
component.handleSaveFilters();
212+
213+
// Verify updateSavedFilter is called instead of createSavedFilter
214+
expect(tablesServiceMock.updateSavedFilter).toHaveBeenCalledWith(
215+
component.data.connectionID,
216+
component.data.tableName,
217+
filterId,
218+
{
219+
name: component.data.filtersSet.name,
220+
filters: { [fieldName]: { eq: 'test_value' } }
221+
}
222+
);
223+
expect(tablesServiceMock.createSavedFilter).not.toHaveBeenCalled();
224+
});
225+
226+
it('should create new filter when id is not present', () => {
227+
// Setup
228+
const fieldName = 'test_field';
229+
component.tableRowFieldsShown = { [fieldName]: 'test_value' };
230+
component.tableRowFieldsComparator = { [fieldName]: 'eq' };
231+
component.data.filtersSet.id = undefined; // No ID means creating a new filter
232+
tablesServiceMock.createSavedFilter.and.returnValue(of({}));
233+
234+
// Call handleSaveFilters
235+
component.handleSaveFilters();
236+
237+
// Verify createSavedFilter is called
238+
expect(tablesServiceMock.createSavedFilter).toHaveBeenCalledWith(
239+
component.data.connectionID,
240+
component.data.tableName,
241+
{
242+
name: component.data.filtersSet.name,
243+
filters: { [fieldName]: { eq: 'test_value' } }
244+
}
245+
);
246+
expect(tablesServiceMock.updateSavedFilter).not.toHaveBeenCalled();
247+
});
186248
});

0 commit comments

Comments
 (0)