import { HttpClientTestingModule } from '@angular/common/http/testing';
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { ReactiveFormsModule } from '@angular/forms';
+import { ActivatedRoute } from '@angular/router';
import { RouterTestingModule } from '@angular/router/testing';
import { ToastModule } from 'ng2-toastr';
+import { ActivatedRouteStub } from '../../../../testing/activated-route-stub';
+import { RbdService } from '../../../shared/api/rbd.service';
import { SharedModule } from '../../../shared/shared.module';
import { configureTestBed } from '../../../shared/unit-test-helper';
+import { RbdFormMode } from './rbd-form-mode.enum';
import { RbdFormComponent } from './rbd-form.component';
describe('RbdFormComponent', () => {
let component: RbdFormComponent;
let fixture: ComponentFixture<RbdFormComponent>;
+ let activatedRoute: ActivatedRouteStub;
configureTestBed({
imports: [
ToastModule.forRoot(),
SharedModule
],
- declarations: [RbdFormComponent]
+ declarations: [RbdFormComponent],
+ providers: [
+ {
+ provide: ActivatedRoute,
+ useValue: new ActivatedRouteStub({ pool: 'foo', name: 'bar', snap: undefined })
+ }
+ ]
});
beforeEach(() => {
fixture = TestBed.createComponent(RbdFormComponent);
component = fixture.componentInstance;
- fixture.detectChanges();
+ activatedRoute = TestBed.get(ActivatedRoute);
});
it('should create', () => {
expect(component).toBeTruthy();
});
+
+ describe('should test decodeURIComponent of params', () => {
+ let rbdService: RbdService;
+
+ beforeEach(() => {
+ rbdService = TestBed.get(RbdService);
+ component.mode = RbdFormMode.editing;
+ fixture.detectChanges();
+ spyOn(rbdService, 'get').and.callThrough();
+ });
+
+ it('without snapName', () => {
+ activatedRoute.setParams({ pool: 'foo%2Ffoo', name: 'bar%2Fbar', snap: undefined });
+
+ expect(rbdService.get).toHaveBeenCalledWith('foo/foo', 'bar/bar');
+ expect(component.snapName).toBeUndefined();
+ });
+
+ it('with snapName', () => {
+ activatedRoute.setParams({ pool: 'foo%2Ffoo', name: 'bar%2Fbar', snap: 'baz%2Fbaz' });
+
+ expect(rbdService.get).toHaveBeenCalledWith('foo/foo', 'bar/bar');
+ expect(component.snapName).toBe('baz/baz');
+ });
+ });
});
--- /dev/null
+import { ReplaySubject } from 'rxjs';
+
+/**
+ * An ActivateRoute test double with a `params` observable.
+ * Use the `setParams()` method to add the next `params` value.
+ */
+export class ActivatedRouteStub {
+ // Use a ReplaySubject to share previous values with subscribers
+ // and pump new values into the `params` observable
+ private subject = new ReplaySubject<object>();
+
+ constructor(initialParams?: object) {
+ this.setParams(initialParams);
+ }
+
+ /** The mock params observable */
+ readonly params = this.subject.asObservable();
+
+ /** Set the params observables's next value */
+ setParams(params?: object) {
+ this.subject.next(params);
+ }
+}