-
-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathchart-edit.component.spec.ts
More file actions
287 lines (257 loc) · 9.46 KB
/
Copy pathchart-edit.component.spec.ts
File metadata and controls
287 lines (257 loc) · 9.46 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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
import { provideHttpClient } from '@angular/common/http';
import { provideHttpClientTesting } from '@angular/common/http/testing';
import { NO_ERRORS_SCHEMA, Signal, WritableSignal } from '@angular/core';
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { MatSnackBarModule } from '@angular/material/snack-bar';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { ActivatedRoute, Router } from '@angular/router';
import { RouterTestingModule } from '@angular/router/testing';
import { CodeEditorModule } from '@ngstack/code-editor';
import { Angulartics2Module } from 'angulartics2';
import { of } from 'rxjs';
import { ChartType, SavedQuery } from 'src/app/models/saved-query';
import { ConnectionsService } from 'src/app/services/connections.service';
import { SavedQueriesService } from 'src/app/services/saved-queries.service';
import { UiSettingsService } from 'src/app/services/ui-settings.service';
import { MockCodeEditorComponent } from 'src/app/testing/code-editor.mock';
import { ChartEditComponent } from './chart-edit.component';
type ChartEditComponentTestable = ChartEditComponent & {
isEditMode: WritableSignal<boolean>;
queryName: WritableSignal<string>;
queryText: WritableSignal<string>;
queryDescription: WritableSignal<string>;
saving: WritableSignal<boolean>;
testing: WritableSignal<boolean>;
testResults: WritableSignal<Record<string, unknown>[]>;
resultColumns: WritableSignal<string[]>;
executionTime: WritableSignal<number | null>;
labelColumn: WritableSignal<string>;
valueColumn: WritableSignal<string>;
chartType: WritableSignal<ChartType>;
canSave: Signal<boolean>;
canTest: Signal<boolean>;
hasChartData: Signal<boolean>;
};
describe('ChartEditComponent', () => {
let component: ChartEditComponent;
let fixture: ComponentFixture<ChartEditComponent>;
let mockSavedQueriesService: Partial<SavedQueriesService>;
let mockConnectionsService: Partial<ConnectionsService>;
let mockUiSettingsService: Partial<UiSettingsService>;
let router: Router;
const mockSavedQuery: SavedQuery = {
id: '1',
name: 'Test Query',
description: 'Test description',
widget_type: 'chart',
chart_type: 'bar',
widget_options: { label_column: 'name', value_column: 'count' },
query_text: 'SELECT * FROM users',
connection_id: 'conn-1',
created_at: '2024-01-01',
updated_at: '2024-01-01',
};
beforeEach(async () => {
mockSavedQueriesService = {
fetchSavedQuery: vi.fn().mockReturnValue(of(mockSavedQuery)),
createSavedQuery: vi.fn().mockReturnValue(of(mockSavedQuery)),
updateSavedQuery: vi.fn().mockReturnValue(of(mockSavedQuery)),
testQuery: vi.fn().mockReturnValue(
of({
data: [{ name: 'John', count: 10 }],
execution_time_ms: 50,
}),
),
};
mockConnectionsService = {
getCurrentConnectionTitle: vi.fn().mockReturnValue(of('Test Connection')),
};
mockUiSettingsService = {
editorTheme: 'vs-dark',
};
await TestBed.configureTestingModule({
imports: [
ChartEditComponent,
BrowserAnimationsModule,
MatSnackBarModule,
RouterTestingModule,
Angulartics2Module.forRoot(),
],
providers: [
provideHttpClient(),
provideHttpClientTesting(),
{ provide: SavedQueriesService, useValue: mockSavedQueriesService },
{ provide: ConnectionsService, useValue: mockConnectionsService },
{ provide: UiSettingsService, useValue: mockUiSettingsService },
{
provide: ActivatedRoute,
useValue: {
snapshot: {
paramMap: {
get: (key: string) => {
if (key === 'connection-id') return 'conn-1';
if (key === 'query-id') return null;
return null;
},
},
},
},
},
],
})
.overrideComponent(ChartEditComponent, {
remove: { imports: [CodeEditorModule] },
add: { imports: [MockCodeEditorComponent], schemas: [NO_ERRORS_SCHEMA] },
})
.compileComponents();
router = TestBed.inject(Router);
vi.spyOn(router, 'navigate');
fixture = TestBed.createComponent(ChartEditComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
it('should initialize in create mode when no query-id', () => {
const testable = component as ChartEditComponentTestable;
expect(testable.isEditMode()).toBe(false);
});
it('should set page title on init', () => {
expect(mockConnectionsService.getCurrentConnectionTitle).toHaveBeenCalled();
});
it('should have correct default chart type', () => {
const testable = component as ChartEditComponentTestable;
expect(testable.chartType()).toBe('bar');
});
describe('canSave computed', () => {
it('should return false when name is empty', () => {
const testable = component as ChartEditComponentTestable;
testable.queryName.set('');
testable.queryText.set('SELECT 1');
expect(testable.canSave()).toBe(false);
});
it('should return false when query is empty', () => {
const testable = component as ChartEditComponentTestable;
testable.queryName.set('Test');
testable.queryText.set('');
expect(testable.canSave()).toBe(false);
});
it('should return true when name and query are provided', () => {
const testable = component as ChartEditComponentTestable;
testable.queryName.set('Test');
testable.queryText.set('SELECT 1');
testable.saving.set(false);
expect(testable.canSave()).toBe(true);
});
it('should return false when saving', () => {
const testable = component as ChartEditComponentTestable;
testable.queryName.set('Test');
testable.queryText.set('SELECT 1');
testable.saving.set(true);
expect(testable.canSave()).toBe(false);
});
});
describe('canTest computed', () => {
it('should return false when query is empty', () => {
const testable = component as ChartEditComponentTestable;
testable.queryText.set('');
expect(testable.canTest()).toBe(false);
});
it('should return true when query is provided', () => {
const testable = component as ChartEditComponentTestable;
testable.queryText.set('SELECT 1');
testable.testing.set(false);
expect(testable.canTest()).toBe(true);
});
it('should return false when testing', () => {
const testable = component as ChartEditComponentTestable;
testable.queryText.set('SELECT 1');
testable.testing.set(true);
expect(testable.canTest()).toBe(false);
});
});
describe('testQuery', () => {
it('should call testQuery service method', () => {
const testable = component as ChartEditComponentTestable;
testable.queryText.set('SELECT * FROM users');
component.testQuery();
expect(mockSavedQueriesService.testQuery).toHaveBeenCalledWith('conn-1', {
query_text: 'SELECT * FROM users',
});
});
it('should set results and columns after successful test', () => {
const testable = component as ChartEditComponentTestable;
testable.queryText.set('SELECT * FROM users');
component.testQuery();
expect(testable.testResults()).toEqual([{ name: 'John', count: 10 }]);
expect(testable.resultColumns()).toEqual(['name', 'count']);
expect(testable.executionTime()).toBe(50);
});
it('should auto-select label and value columns', () => {
const testable = component as ChartEditComponentTestable;
testable.queryText.set('SELECT * FROM users');
component.testQuery();
expect(testable.labelColumn()).toBe('name');
expect(testable.valueColumn()).toBe('count');
});
});
describe('saveQuery', () => {
it('should call createSavedQuery in create mode', () => {
const testable = component as ChartEditComponentTestable;
testable.isEditMode.set(false);
testable.queryName.set('New Query');
testable.queryText.set('SELECT 1');
component.saveQuery();
expect(mockSavedQueriesService.createSavedQuery).toHaveBeenCalledWith('conn-1', {
name: 'New Query',
description: undefined,
query_text: 'SELECT 1',
widget_type: 'chart',
chart_type: 'bar',
widget_options: { label_type: 'values' },
});
});
it('should navigate to charts list after save', () => {
const testable = component as ChartEditComponentTestable;
testable.queryName.set('New Query');
testable.queryText.set('SELECT 1');
component.saveQuery();
expect(router.navigate).toHaveBeenCalledWith(['/charts', 'conn-1']);
});
});
describe('cancel', () => {
it('should navigate to charts list', () => {
component.cancel();
expect(router.navigate).toHaveBeenCalledWith(['/charts', 'conn-1']);
});
});
describe('onCodeChange', () => {
it('should update queryText', () => {
const testable = component as ChartEditComponentTestable;
component.onCodeChange('SELECT * FROM table');
expect(testable.queryText()).toBe('SELECT * FROM table');
});
});
describe('hasChartData computed', () => {
it('should return false when no results', () => {
const testable = component as ChartEditComponentTestable;
testable.testResults.set([]);
expect(testable.hasChartData()).toBe(false);
});
it('should return false when no columns selected', () => {
const testable = component as ChartEditComponentTestable;
testable.testResults.set([{ name: 'John' }]);
testable.labelColumn.set('');
testable.valueColumn.set('');
expect(testable.hasChartData()).toBe(false);
});
it('should return true when results and columns are set', () => {
const testable = component as ChartEditComponentTestable;
testable.testResults.set([{ name: 'John', count: 10 }]);
testable.labelColumn.set('name');
testable.valueColumn.set('count');
expect(testable.hasChartData()).toBe(true);
});
});
});