-
-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathdb-table-view.component.spec.ts
More file actions
271 lines (231 loc) · 7.54 KB
/
db-table-view.component.spec.ts
File metadata and controls
271 lines (231 loc) · 7.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
import { SelectionModel } from '@angular/cdk/collections';
import { provideHttpClient } from '@angular/common/http';
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { FormsModule } from '@angular/forms';
import { MatDialogModule } from '@angular/material/dialog';
import { MatIconTestingModule } from '@angular/material/icon/testing';
import { MatMenuModule } from '@angular/material/menu';
import { MatPaginatorModule } from '@angular/material/paginator';
import { MatSnackBarModule } from '@angular/material/snack-bar';
import { MatSortModule } from '@angular/material/sort';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { provideRouter } from '@angular/router';
import { Angulartics2Module } from 'angulartics2';
import { TablesDataSource } from '../db-tables-data-source';
import { DbTableViewComponent } from './db-table-view.component';
describe('DbTableViewComponent', () => {
let component: DbTableViewComponent;
let fixture: ComponentFixture<DbTableViewComponent>;
const _mockWidgets = {
Region: {
id: 'c768dde8-7348-46e8-a522-718a29b705e8',
field_name: 'Region',
widget_type: 'Select',
widget_params: {
options: [
{
value: 'AK',
label: 'Alaska',
},
{
value: 'CA',
label: 'California',
},
],
},
widget_options: null,
name: 'State',
description: '',
},
address_id: {
id: 'ee3125ca-86cc-4c20-93f9-8a0b2c43d61f',
field_name: 'address_id',
widget_type: 'String',
widget_params: '',
widget_options: null,
name: '',
description: '',
},
};
const _mockSelectOption = {
Region: [
{
value: 'AK',
label: 'Alaska',
},
{
value: 'CA',
label: 'California',
},
],
};
const mockFilterComparators = {
first_name: 'startswith',
second_name: 'endswith',
email: 'contains',
age: 'gt',
};
beforeEach(async () => {
await TestBed.configureTestingModule({
imports: [
MatMenuModule,
MatSnackBarModule,
MatPaginatorModule,
BrowserAnimationsModule,
MatSortModule,
FormsModule,
MatDialogModule,
MatIconTestingModule,
Angulartics2Module.forRoot({}),
DbTableViewComponent,
],
providers: [provideHttpClient(), provideRouter([])],
}).compileComponents();
});
beforeEach(() => {
fixture = TestBed.createComponent(DbTableViewComponent);
component = fixture.componentInstance;
component.table = new TablesDataSource({} as any, {} as any, {} as any);
component.selection = new SelectionModel<any>(true, []);
component.filterComparators = mockFilterComparators;
fixture.autoDetectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
it('should check if column is foreign key', () => {
component.tableData.foreignKeysList = ['ProductId', 'CustomerId'];
const isForeignKeyResult = component.isForeignKey('ProductId');
expect(isForeignKeyResult).toBe(true);
});
it('should return query params for link for foreign key', () => {
const foreignKey = {
autocomplete_columns: ['FirstName'],
column_name: 'CustomerId',
column_default: null,
constraint_name: 'Orders_ibfk_2',
referenced_column_name: 'Id',
referenced_table_name: 'Customers',
};
const cell = {
Id: 42,
};
const queryParams = component.getForeignKeyQueryParams(foreignKey, cell);
expect(queryParams).toEqual({ Id: 42 });
});
it('should check if it is widget by column name', () => {
component.tableData.widgetsList = ['Name', 'Age'];
const isWigetAge = component.isWidget('Age');
expect(isWigetAge).toBe(true);
});
it('should return 2 for active filters with object of two fileds', () => {
const numberOfFilters = component.getFiltersCount({ Country: 'С', Name: 'John' });
expect(numberOfFilters).toEqual(2);
});
it('should emit open filters actions to parent', () => {
const mockStructure = [
{
column_name: 'first_name',
column_default: null,
data_type: 'character varying',
isExcluded: false,
isSearched: false,
auto_increment: false,
allow_null: true,
character_maximum_length: 45,
},
];
const mockForeignKeysList = [];
const mockForeignKeys = {};
const mockWidgets = [];
component.tableData.structure = mockStructure;
component.tableData.foreignKeysList = mockForeignKeysList;
component.tableData.foreignKeys = mockForeignKeys;
component.tableData.widgets = mockWidgets;
vi.spyOn(component.openFilters, 'emit');
component.handleOpenFilters();
expect(component.openFilters.emit).toHaveBeenCalledWith({
structure: mockStructure,
foreignKeysList: mockForeignKeysList,
foreignKeys: mockForeignKeys,
widgets: mockWidgets,
});
expect(component.searchString).toEqual('');
});
it('should clear search string and emit search by string to parent', () => {
vi.spyOn(component.search, 'emit');
component.clearSearch();
expect(component.searchString).toBeNull();
expect(component.search.emit).toHaveBeenCalledWith(null);
});
it('should return filter chip label for starts with comparator', () => {
const chipFilterLable = component.getFilter({ key: 'first_name', value: { startswith: 'A' } });
expect(chipFilterLable).toEqual('First Names = A...');
});
it('should return filter chip label for ends with comparator', () => {
const chipFilterLable = component.getFilter({ key: 'second_name', value: { endswith: 'y' } });
expect(chipFilterLable).toEqual('Second Names = ...y');
});
it('should return filter chip label for contains comparator', () => {
const chipFilterLable = component.getFilter({ key: 'email', value: { contains: 'gmail' } });
expect(chipFilterLable).toEqual('Emails = ...gmail...');
});
it('should return filter chip label for greater than comparator', () => {
const chipFilterLable = component.getFilter({ key: 'age', value: { gt: '20' } });
expect(chipFilterLable).toEqual('Ages > 20');
});
it('should return human readable value of foreign key if identitty field was not pointed', () => {
const foreignKey = {
autocomplete_columns: ['FirstName'],
column_name: 'CustomerId',
column_default: null,
constraint_name: 'Orders_ibfk_2',
referenced_column_name: 'Id',
referenced_table_name: 'Customers',
};
const cell = {
Id: 42,
};
const value = component.getCellValue(foreignKey, cell);
expect(value).toEqual(42);
});
it('should return human readable value of foreign key if identity field was pointed', () => {
const foreignKey = {
autocomplete_columns: ['FirstName'],
column_name: 'CustomerId',
column_default: null,
constraint_name: 'Orders_ibfk_2',
referenced_column_name: 'Id',
referenced_table_name: 'Customers',
};
const cell = {
Id: 42,
name: 'John',
};
const value = component.getCellValue(foreignKey, cell);
expect(value).toEqual('John');
});
it('should return null (not throw) when foreign key cell is null', () => {
const foreignKey = {
autocomplete_columns: ['FirstName'],
column_name: 'CustomerId',
column_default: null,
constraint_name: 'Orders_ibfk_2',
referenced_column_name: 'Id',
referenced_table_name: 'Customers',
};
expect(component.getCellValue(foreignKey, null)).toBeNull();
expect(component.getCellValue(foreignKey, undefined)).toBeNull();
});
it('should not throw in isForeignKeySelected when record is null', () => {
const foreignKey = {
autocomplete_columns: ['FirstName'],
column_name: 'CustomerId',
column_default: null,
constraint_name: 'Orders_ibfk_2',
referenced_column_name: 'Id',
referenced_table_name: 'Customers',
};
expect(component.isForeignKeySelected(null, foreignKey)).toBe(false);
});
});