-
-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathchart-delete-dialog.component.spec.ts
More file actions
121 lines (106 loc) · 4.02 KB
/
Copy pathchart-delete-dialog.component.spec.ts
File metadata and controls
121 lines (106 loc) · 4.02 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
import { provideHttpClient } from '@angular/common/http';
import { provideHttpClientTesting } from '@angular/common/http/testing';
import { WritableSignal } from '@angular/core';
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { MAT_DIALOG_DATA, MatDialogModule, MatDialogRef } from '@angular/material/dialog';
import { MatSnackBarModule } from '@angular/material/snack-bar';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { Angulartics2Module } from 'angulartics2';
import { of, throwError } from 'rxjs';
import { SavedQuery } from 'src/app/models/saved-query';
import { SavedQueriesService } from 'src/app/services/saved-queries.service';
import { ChartDeleteDialogComponent } from './chart-delete-dialog.component';
type ChartDeleteDialogComponentTestable = ChartDeleteDialogComponent & {
submitting: WritableSignal<boolean>;
};
describe('ChartDeleteDialogComponent', () => {
let component: ChartDeleteDialogComponent;
let fixture: ComponentFixture<ChartDeleteDialogComponent>;
let mockSavedQueriesService: Partial<SavedQueriesService>;
let mockDialogRef: Partial<MatDialogRef<ChartDeleteDialogComponent>>;
const mockSavedQuery: SavedQuery = {
id: '1',
name: 'Test Query',
description: 'Test description',
widget_type: 'chart',
chart_type: 'bar',
widget_options: null,
query_text: 'SELECT * FROM users',
connection_id: 'conn-1',
created_at: '2024-01-01',
updated_at: '2024-01-01',
};
beforeEach(async () => {
mockSavedQueriesService = {
deleteSavedQuery: vi.fn().mockReturnValue(of(mockSavedQuery)),
};
mockDialogRef = {
close: vi.fn(),
};
await TestBed.configureTestingModule({
imports: [
ChartDeleteDialogComponent,
BrowserAnimationsModule,
MatSnackBarModule,
MatDialogModule,
Angulartics2Module.forRoot(),
],
providers: [
provideHttpClient(),
provideHttpClientTesting(),
{ provide: SavedQueriesService, useValue: mockSavedQueriesService },
{ provide: MatDialogRef, useValue: mockDialogRef },
{
provide: MAT_DIALOG_DATA,
useValue: { query: mockSavedQuery, connectionId: 'conn-1' },
},
],
}).compileComponents();
fixture = TestBed.createComponent(ChartDeleteDialogComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
it('should have submitting signal initialized to false', () => {
const testable = component as ChartDeleteDialogComponentTestable;
expect(testable.submitting()).toBe(false);
});
describe('onDelete', () => {
it('should call deleteSavedQuery service method', () => {
component.onDelete();
expect(mockSavedQueriesService.deleteSavedQuery).toHaveBeenCalledWith('conn-1', '1');
});
it('should set submitting to true during delete', () => {
component.onDelete();
// The deleteSavedQuery returns synchronously in the mock, so submitting would be false after
// In real usage, submitting would be true while the request is in flight
expect(mockSavedQueriesService.deleteSavedQuery).toHaveBeenCalled();
});
it('should close dialog with true on success', () => {
component.onDelete();
expect(mockDialogRef.close).toHaveBeenCalledWith(true);
});
it('should set submitting to false after successful delete', () => {
const testable = component as ChartDeleteDialogComponentTestable;
component.onDelete();
expect(testable.submitting()).toBe(false);
});
it('should set submitting to false on error', () => {
const testable = component as ChartDeleteDialogComponentTestable;
vi.mocked(mockSavedQueriesService.deleteSavedQuery!).mockReturnValue(
throwError(() => new Error('Delete failed')),
);
component.onDelete();
expect(testable.submitting()).toBe(false);
});
it('should not close dialog on error', () => {
vi.mocked(mockSavedQueriesService.deleteSavedQuery!).mockReturnValue(
throwError(() => new Error('Delete failed')),
);
component.onDelete();
expect(mockDialogRef.close).not.toHaveBeenCalled();
});
});
});