Skip to content

Commit 2afbb1d

Browse files
authored
add more unit tests (#146)
1 parent 09dbfd4 commit 2afbb1d

File tree

2 files changed

+162
-0
lines changed

2 files changed

+162
-0
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import {ComponentFixture, TestBed} from '@angular/core/testing';
2+
import {FormsModule} from '@angular/forms';
3+
import {TextModelLoaderModalComponent} from './text-model-loader-modal.component';
4+
import {MatFormFieldModule} from '@angular/material/form-field';
5+
import {MatInputModule} from '@angular/material/input';
6+
import {of} from 'rxjs';
7+
import {FileHandlingService} from '../../services';
8+
import {MatDialogModule, MatDialogRef} from '@angular/material/dialog';
9+
import {MockProviders} from 'ng-mocks';
10+
import {TranslateModule, TranslateService} from '@ngx-translate/core';
11+
import {BrowserAnimationsModule} from '@angular/platform-browser/animations';
12+
13+
describe('TextModelLoaderModalComponent', () => {
14+
let component: TextModelLoaderModalComponent;
15+
let fixture: ComponentFixture<TextModelLoaderModalComponent>;
16+
17+
beforeEach(async () => {
18+
await TestBed.configureTestingModule({
19+
declarations: [TextModelLoaderModalComponent],
20+
imports: [FormsModule, MatFormFieldModule, MatInputModule, MatDialogModule, BrowserAnimationsModule, TranslateModule.forRoot()],
21+
providers: [
22+
MockProviders(MatDialogRef),
23+
{
24+
provide: FileHandlingService,
25+
useValue: {
26+
loadModel: () => of(null),
27+
},
28+
},
29+
TranslateService,
30+
],
31+
}).compileComponents();
32+
});
33+
34+
beforeEach(() => {
35+
fixture = TestBed.createComponent(TextModelLoaderModalComponent);
36+
component = fixture.componentInstance;
37+
fixture.detectChanges();
38+
});
39+
40+
it('should not call loadModel when textarea text is empty', () => {
41+
jest.spyOn(component, 'loadModel');
42+
43+
const button = fixture.debugElement.nativeElement.querySelectorAll('button')[1];
44+
button.click();
45+
46+
fixture.detectChanges();
47+
48+
expect(component.loadModel).not.toHaveBeenCalled();
49+
});
50+
it('should call loadModel when textarea text is not empty', () => {
51+
jest.spyOn(component, 'loadModel');
52+
53+
const textarea = fixture.debugElement.nativeElement.querySelector('textarea[matInput]');
54+
const text = 'ttl value';
55+
textarea.value = text;
56+
textarea.dispatchEvent(new Event('input'));
57+
fixture.detectChanges();
58+
59+
const button = fixture.debugElement.nativeElement.querySelectorAll('button')[1];
60+
button.click();
61+
fixture.detectChanges();
62+
63+
expect(component.loadModel).toHaveBeenCalled();
64+
});
65+
});
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
import {ComponentFixture, TestBed} from '@angular/core/testing';
2+
import {FormsModule} from '@angular/forms';
3+
import {MatFormFieldModule} from '@angular/material/form-field';
4+
import {MatInputModule} from '@angular/material/input';
5+
import {MatDialog, MatDialogModule, MatDialogRef} from '@angular/material/dialog';
6+
import {MockProviders} from 'ng-mocks';
7+
import {TranslateModule, TranslateService} from '@ngx-translate/core';
8+
import {BrowserAnimationsModule} from '@angular/platform-browser/animations';
9+
import {FilesSearchComponent} from './files-search.component';
10+
import {SearchesStateService} from '../../search-state.service';
11+
import {SidebarStateService} from '@ame/sidebar';
12+
import {ModelSavingTrackerService, NotificationsService, SearchService} from '@ame/shared';
13+
import {FileHandlingService, SaveModelDialogService} from '@ame/editor';
14+
import {LanguageTranslationService} from '@ame/translation';
15+
import {HttpClientModule} from '@angular/common/http';
16+
import {MxGraphService} from '@ame/mx-graph';
17+
import {By} from '@angular/platform-browser';
18+
19+
describe('Files search', () => {
20+
let component: FilesSearchComponent;
21+
let fixture: ComponentFixture<FilesSearchComponent>;
22+
23+
beforeEach(async () => {
24+
await TestBed.configureTestingModule({
25+
imports: [
26+
FormsModule,
27+
MatFormFieldModule,
28+
MatInputModule,
29+
MatDialogModule,
30+
BrowserAnimationsModule,
31+
TranslateModule.forRoot(),
32+
HttpClientModule,
33+
],
34+
providers: [
35+
MockProviders(MatDialogRef, MxGraphService!, NotificationsService, FileHandlingService),
36+
TranslateService,
37+
SearchesStateService,
38+
SidebarStateService,
39+
MatDialog,
40+
ModelSavingTrackerService,
41+
SaveModelDialogService,
42+
SearchService,
43+
LanguageTranslationService,
44+
],
45+
}).compileComponents();
46+
});
47+
48+
beforeEach(() => {
49+
fixture = TestBed.createComponent(FilesSearchComponent);
50+
component = fixture.componentInstance;
51+
fixture.detectChanges();
52+
});
53+
54+
const files = [
55+
{
56+
name: 'AspectDefault.ttl',
57+
loaded: true,
58+
locked: false,
59+
outdated: false,
60+
errored: false,
61+
sammVersion: '2.1.0',
62+
},
63+
{
64+
name: 'SharedModel.ttl',
65+
locked: false,
66+
outdated: false,
67+
errored: false,
68+
loaded: true,
69+
sammVersion: '2.1.0',
70+
},
71+
];
72+
73+
const namespaces = {
74+
'org.eclipse.examples:1.0.0': files,
75+
};
76+
77+
it('should parse files correctly', () => {
78+
component.parseFiles(namespaces);
79+
80+
expect(component.searchableFiles).toEqual([
81+
{file: 'AspectDefault.ttl', namespace: 'org.eclipse.examples:1.0.0'},
82+
{file: 'SharedModel.ttl', namespace: 'org.eclipse.examples:1.0.0'},
83+
]);
84+
});
85+
86+
it('should have mat option if there are namespaces with files', () => {
87+
jest.spyOn(component, 'openFile');
88+
89+
component.searchableFiles = files;
90+
fixture.detectChanges();
91+
const autocomplete = fixture.debugElement.query(By.css('mat-autocomplete'));
92+
expect(autocomplete).toBeTruthy();
93+
fixture.detectChanges();
94+
const matOptions = autocomplete.nativeElement.querySelectorAll('mat-option');
95+
expect(matOptions).toBeTruthy();
96+
});
97+
});

0 commit comments

Comments
 (0)