subsystem-list.component.spec.ts 9.06 KB
Newer Older
Vitali Stupin's avatar
Initial    
Vitali Stupin committed
1
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
Vitali Stupin's avatar
Vitali Stupin committed
2
3
import { TranslateModule } from '@ngx-translate/core';
import { HttpClientModule } from '@angular/common/http';
4
import { SubsystemListComponent } from './subsystem-list.component';
Vitali Stupin's avatar
Vitali Stupin committed
5
import { Component, Input } from '@angular/core';
Vitali Stupin's avatar
Vitali Stupin committed
6
import { Subsystem } from '../subsystem';
Vitali Stupin's avatar
Vitali Stupin committed
7
import { ActivatedRoute, Router, Scroll } from '@angular/router';
Vitali Stupin's avatar
Vitali Stupin committed
8
import { of } from 'rxjs';
Vitali Stupin's avatar
Vitali Stupin committed
9
10
import { SubsystemsService } from '../subsystems.service';
import { ViewportScroller } from '@angular/common';
11
12
import { AppConfigMock } from 'src/app/app.config-mock';
import { AppConfig } from 'src/app/app.config';
13
import { FormsModule } from '@angular/forms';
Vitali Stupin's avatar
Vitali Stupin committed
14

Vitali Stupin's avatar
Vitali Stupin committed
15
16
@Component({selector: 'app-header', template: ''})
class HeaderStubComponent {}
17
18
19
20
@Component({selector: 'app-messages', template: ''})
class MessagesStubComponent {
  @Input() message: string;
}
Vitali Stupin's avatar
Vitali Stupin committed
21
22
23
24
@Component({selector: 'app-search', template: ''})
class SearchStubComponent {}
@Component({selector: 'app-subsystem-item', template: ''})
class SubsystemItemStubComponent {
Vitali Stupin's avatar
Vitali Stupin committed
25
  @Input() subsystem: Subsystem;
Vitali Stupin's avatar
Vitali Stupin committed
26
}
Vitali Stupin's avatar
Initial    
Vitali Stupin committed
27

28
29
30
describe('SubsystemListComponent', () => {
  let component: SubsystemListComponent;
  let fixture: ComponentFixture<SubsystemListComponent>;
Vitali Stupin's avatar
Vitali Stupin committed
31
32
33
  let getInstanceSpy;
  let getInstancesSpy;
  let subsystemsService: SubsystemsService;
Vitali Stupin's avatar
Initial    
Vitali Stupin committed
34
35
36

  beforeEach(async(() => {
    TestBed.configureTestingModule({
Vitali Stupin's avatar
Vitali Stupin committed
37
38
39
      declarations: [
        SubsystemListComponent,
        HeaderStubComponent,
40
        MessagesStubComponent,
Vitali Stupin's avatar
Vitali Stupin committed
41
42
43
44
        SearchStubComponent,
        SubsystemItemStubComponent
      ],
      imports: [
45
        FormsModule,
Vitali Stupin's avatar
Vitali Stupin committed
46
        TranslateModule.forRoot(),
Vitali Stupin's avatar
Vitali Stupin committed
47
48
49
50
51
52
53
54
55
56
57
        HttpClientModule
      ],
      providers: [
        { provide: ActivatedRoute, useValue: {
          params: of({
            instance: 'INST'
          })
        }},
        { provide: Router, useValue: {
            events: of(new Scroll(null, [11, 12], null)),
            navigateByUrl: jasmine.createSpy('navigateByUrl')
58
59
        }},
        { provide: AppConfig, useClass: AppConfigMock }
Vitali Stupin's avatar
Vitali Stupin committed
60
      ]
Vitali Stupin's avatar
Initial    
Vitali Stupin committed
61
62
63
64
65
    })
    .compileComponents();
  }));

  beforeEach(() => {
Vitali Stupin's avatar
Vitali Stupin committed
66
67
68
69
70
71
72
73
74
75
    subsystemsService = TestBed.get(SubsystemsService);
    getInstanceSpy = spyOn(subsystemsService, 'getInstance').and.returnValue('INST');
    getInstancesSpy = spyOn(subsystemsService, 'getInstances').and.returnValue(['INST']);
    spyOn(TestBed.get(ViewportScroller), 'scrollToPosition');
    spyOn(subsystemsService, 'setInstance').and.returnValue(null);
    spyOn(subsystemsService, 'getDefaultInstance').and.returnValue('DEFINST');
    spyOn(TestBed.get(SubsystemsService), 'getApiUrlBase').and.returnValue('base');
  });

  it('should create', () => {
76
    fixture = TestBed.createComponent(SubsystemListComponent);
Vitali Stupin's avatar
Initial    
Vitali Stupin committed
77
78
    component = fixture.componentInstance;
    fixture.detectChanges();
Vitali Stupin's avatar
Vitali Stupin committed
79
    expect(component).toBeTruthy();
Vitali Stupin's avatar
Initial    
Vitali Stupin committed
80
81
  });

Vitali Stupin's avatar
Vitali Stupin committed
82
83
84
85
86
87
88
89
90
91
92
93
94
  it('should redirect on incorrect instance', () => {
    getInstancesSpy.and.returnValue(['XXX']);
    fixture = TestBed.createComponent(SubsystemListComponent);
    component = fixture.componentInstance;
    fixture.detectChanges();
    expect(TestBed.get(Router).navigateByUrl).toHaveBeenCalledWith('/DEFINST');
  });

  it('should detect when instance is not selected', () => {
    getInstanceSpy.and.returnValue('');
    fixture = TestBed.createComponent(SubsystemListComponent);
    component = fixture.componentInstance;
    fixture.detectChanges();
95
    expect(subsystemsService.setInstance).toHaveBeenCalledWith('INST', '');
Vitali Stupin's avatar
Vitali Stupin committed
96
97
98
99
100
101
102
103
  });

  it('should detect change instance', () => {
    getInstanceSpy.and.returnValue('INST2');
    getInstancesSpy.and.returnValue(['INST', 'INST2']);
    fixture = TestBed.createComponent(SubsystemListComponent);
    component = fixture.componentInstance;
    fixture.detectChanges();
104
    expect(subsystemsService.setInstance).toHaveBeenCalledWith('INST', '');
Vitali Stupin's avatar
Vitali Stupin committed
105
106
107
108
109
110
111
112
113
114
115
116
117
  });

  it('should scroll to position', () => {
    fixture = TestBed.createComponent(SubsystemListComponent);
    component = fixture.componentInstance;
    fixture.detectChanges();
    expect(TestBed.get(ViewportScroller).scrollToPosition).toHaveBeenCalledWith([11, 12]);
  });

  it('switchInstance should work', () => {
    fixture = TestBed.createComponent(SubsystemListComponent);
    component = fixture.componentInstance;
    fixture.detectChanges();
Vitali Stupin's avatar
Vitali Stupin committed
118
119
    const spy = TestBed.get(Router).navigateByUrl;

Vitali Stupin's avatar
Vitali Stupin committed
120
    component.switchInstance('NEWINST');
Vitali Stupin's avatar
Vitali Stupin committed
121
122
123
124
125
    expect(spy).toHaveBeenCalledWith('/NEWINST');

    spy.calls.reset();
    component.switchInstance('INST');
    expect(spy).toHaveBeenCalledWith('/INST');
Vitali Stupin's avatar
Vitali Stupin committed
126
127
128
129
130
131
132
133
  });

  it('should receive service warnings', () => {
    fixture = TestBed.createComponent(SubsystemListComponent);
    component = fixture.componentInstance;
    fixture.detectChanges();
    subsystemsService.warnings.emit('WARN');
    expect(component.message).toBe('WARN');
Vitali Stupin's avatar
Initial    
Vitali Stupin committed
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

  it('scrollToTop should work', () => {
    fixture = TestBed.createComponent(SubsystemListComponent);
    component = fixture.componentInstance;
    fixture.detectChanges();
    const spy = TestBed.get(ViewportScroller).scrollToPosition;
    spy.calls.reset();
    component.scrollToTop();
    expect(spy).toHaveBeenCalledWith([0, 0]);
  });

  it('isPartialList should work', () => {
    fixture = TestBed.createComponent(SubsystemListComponent);
    component = fixture.componentInstance;
    fixture.detectChanges();

    subsystemsService.filteredSubsystemsSubject.next([new Subsystem(), new Subsystem()]);

    const getLimitSpy = spyOn(subsystemsService, 'getLimit').and.returnValue(['all']);
    expect(component.isPartialList()).toBeFalsy();

    getLimitSpy.and.returnValue(['2']);
    expect(component.isPartialList()).toBeTruthy();

    getLimitSpy.and.returnValue(['3']);
    expect(component.isPartialList()).toBeFalsy();
  });
162
163
164
165
166
167
168
169

  it('setInstanceVersion should work without instance version', () => {
    fixture = TestBed.createComponent(SubsystemListComponent);
    component = fixture.componentInstance;
    fixture.detectChanges();
    component.setInstanceVersion();
    expect(TestBed.get(Router).navigateByUrl).toHaveBeenCalledWith('/INST');
  });
170
171
172
173
174
175
176
177
178
179

  it('setMaxLimit should work', () => {
    fixture = TestBed.createComponent(SubsystemListComponent);
    component = fixture.componentInstance;
    component.search = TestBed.createComponent(SearchStubComponent).componentInstance;
    fixture.detectChanges();
    component.search.setLimit = jasmine.createSpy();
    component.setMaxLimit();
    expect(component.search.setLimit).toHaveBeenCalledWith('all');
  });
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
});

describe('SubsystemListComponent (with instance version)', () => {
  let component: SubsystemListComponent;
  let fixture: ComponentFixture<SubsystemListComponent>;
  let getInstanceSpy;
  let getInstancesSpy;
  let subsystemsService: SubsystemsService;

  beforeEach(async(() => {
    TestBed.configureTestingModule({
      declarations: [
        SubsystemListComponent,
        HeaderStubComponent,
        MessagesStubComponent,
        SearchStubComponent,
        SubsystemItemStubComponent
      ],
      imports: [
        FormsModule,
        TranslateModule.forRoot(),
        HttpClientModule
      ],
      providers: [
        { provide: ActivatedRoute, useValue: {
          params: of({
            instance: 'INST'
          }),
          snapshot: {
            queryParams: {
              at: '12345'
            }
          }
        }},
        { provide: Router, useValue: {
            events: of(new Scroll(null, [11, 12], null)),
            navigateByUrl: jasmine.createSpy('navigateByUrl')
        }},
        { provide: AppConfig, useClass: AppConfigMock }
      ]
    })
    .compileComponents();
  }));

  beforeEach(() => {
    subsystemsService = TestBed.get(SubsystemsService);
    getInstanceSpy = spyOn(subsystemsService, 'getInstance').and.returnValue('INST');
    getInstancesSpy = spyOn(subsystemsService, 'getInstances').and.returnValue(['INST']);
    spyOn(TestBed.get(ViewportScroller), 'scrollToPosition');
    spyOn(subsystemsService, 'setInstance').and.returnValue(null);
    spyOn(subsystemsService, 'getDefaultInstance').and.returnValue('DEFINST');
    spyOn(TestBed.get(SubsystemsService), 'getApiUrlBase').and.returnValue('base');
  });

  it('should create', () => {
    fixture = TestBed.createComponent(SubsystemListComponent);
    component = fixture.componentInstance;
    fixture.detectChanges();
    expect(component).toBeTruthy();
    expect(component.instanceVersion).toBe('12345');
  });

  it('setInstanceVersion should work with instance version', () => {
    fixture = TestBed.createComponent(SubsystemListComponent);
    component = fixture.componentInstance;
    fixture.detectChanges();
    component.setInstanceVersion();
    expect(TestBed.get(Router).navigateByUrl).toHaveBeenCalledWith('/INST?at=12345');
  });
Vitali Stupin's avatar
Vitali Stupin committed
249
250
251
252
253
254
255
256
257
258

  it('switchInstance should reset instance version when instance does not change', () => {
    fixture = TestBed.createComponent(SubsystemListComponent);
    component = fixture.componentInstance;
    fixture.detectChanges();
    component.instanceVersion = 'test';
    component.switchInstance('INST');
    expect(TestBed.get(Router).navigateByUrl).toHaveBeenCalledWith('/INST');
    expect(component.instanceVersion).toBe('');
  });
Vitali Stupin's avatar
Vitali Stupin committed
259
});