Browse Source

Tests for plans state.

pull/282/head
Sebastian Stehle 8 years ago
parent
commit
8d1d127e1f
  1. 2
      src/Squidex/app/features/administration/state/event-consumers.state.ts
  2. 4
      src/Squidex/app/features/administration/state/users.state.ts
  3. 6
      src/Squidex/app/shared/services/plans.service.spec.ts
  4. 15
      src/Squidex/app/shared/services/plans.service.ts
  5. 132
      src/Squidex/app/shared/state/plans.state.spec.ts
  6. 6
      src/Squidex/app/shared/state/plans.state.ts

2
src/Squidex/app/features/administration/state/event-consumers.state.ts

@ -97,7 +97,7 @@ export class EventConsumersState extends State<Snapshot> {
}
const setStopped = (es: EventConsumerDto, isStoped: boolean) =>
new EventConsumerDto(es.name, false, false, es.error, es.position);
new EventConsumerDto(es.name, isStoped, false, es.error, es.position);
const reset = (es: EventConsumerDto) =>
new EventConsumerDto(es.name, es.isStopped, true, es.error, es.position);

4
src/Squidex/app/features/administration/state/users.state.ts

@ -229,5 +229,5 @@ export class UsersState extends State<Snapshot> {
const update = (user: UserDto, request: UpdateUserDto) =>
new UserDto(user.id, request.email, request.displayName, user.isLocked);
const setLocked = (user: UserDto, locked: boolean) =>
new UserDto(user.id, user.email, user.displayName, locked);
const setLocked = (user: UserDto, isLocked: boolean) =>
new UserDto(user.id, user.email, user.displayName, isLocked);

6
src/Squidex/app/shared/services/plans.service.spec.ts

@ -11,10 +11,10 @@ import { inject, TestBed } from '@angular/core/testing';
import {
AnalyticsService,
ApiUrlConfig,
AppPlansDto,
ChangePlanDto,
PlanChangedDto,
PlanDto,
PlansDto,
PlansService,
Version
} from './../';
@ -42,7 +42,7 @@ describe('PlansService', () => {
it('should make get request to get app plans',
inject([PlansService, HttpTestingController], (plansService: PlansService, httpMock: HttpTestingController) => {
let plans: AppPlansDto | null = null;
let plans: PlansDto | null = null;
plansService.getPlans('my-app').subscribe(result => {
plans = result;
@ -86,7 +86,7 @@ describe('PlansService', () => {
});
expect(plans).toEqual(
new AppPlansDto(
new PlansDto(
'123',
'456',
true,

15
src/Squidex/app/shared/services/plans.service.ts

@ -19,7 +19,7 @@ import {
Versioned
} from '@app/framework';
export class AppPlansDto {
export class PlansDto {
constructor(
public readonly currentPlanId: string,
public readonly planOwner: string,
@ -28,15 +28,6 @@ export class AppPlansDto {
public readonly version: Version
) {
}
public changePlanId(planId: string, version?: Version): AppPlansDto {
return new AppPlansDto(
planId,
this.planOwner,
this.hasPortal,
this.plans,
version || this.version);
}
}
export class PlanDto {
@ -76,7 +67,7 @@ export class PlansService {
) {
}
public getPlans(appName: string): Observable<AppPlansDto> {
public getPlans(appName: string): Observable<PlansDto> {
const url = this.apiUrl.buildUrl(`api/apps/${appName}/plans`);
return HTTP.getVersioned<any>(this.http, url)
@ -85,7 +76,7 @@ export class PlansService {
const items: any[] = body.plans;
return new AppPlansDto(
return new PlansDto(
body.currentPlanId,
body.planOwner,
body.hasPortal,

132
src/Squidex/app/shared/state/plans.state.spec.ts

@ -0,0 +1,132 @@
/*
* Squidex Headless CMS
*
* @license
* Copyright (c) Squidex UG (haftungsbeschränkt). All rights reserved.
*/
import { Observable } from 'rxjs';
import { IMock, It, Mock, Times } from 'typemoq';
import {
AppsState,
AuthService,
DialogService,
PlanChangedDto,
PlanDto,
PlansDto,
PlansService,
PlansState,
Version,
Versioned
} from '@app/shared';
describe('PlansState', () => {
const app = 'my-app';
const version = new Version('1');
const newVersion = new Version('2');
const oldPlans =
new PlansDto('id1', 'id2', true, [
new PlanDto('id1', 'name1', '100€', 'id1_yearly', '200€', 1, 1, 1),
new PlanDto('id2', 'name2', '400€', 'id2_yearly', '800€', 2, 2, 2)
],
version);
let dialogs: IMock<DialogService>;
let appsState: IMock<AppsState>;
let authService: IMock<AuthService>;
let plansService: IMock<PlansService>;
let plansState: PlansState;
beforeEach(() => {
dialogs = Mock.ofType<DialogService>();
authService = Mock.ofType<AuthService>();
authService.setup(x => x.user)
.returns(() => <any>{ id: 'id3' });
appsState = Mock.ofType<AppsState>();
appsState.setup(x => x.appName)
.returns(() => app);
plansService = Mock.ofType<PlansService>();
plansService.setup(x => x.getPlans(app))
.returns(() => Observable.of(oldPlans));
plansState = new PlansState(appsState.object, authService.object, dialogs.object, plansService.object);
});
it('should load plans', () => {
plansState.load().onErrorResumeNext().subscribe();
expect(plansState.snapshot.plans.values).toEqual([
{ isSelected: true, isYearlySelected: false, plan: oldPlans.plans[0] },
{ isSelected: false, isYearlySelected: false, plan: oldPlans.plans[1] }
]);
expect(plansState.snapshot.isOwner).toBeFalsy();
expect(plansState.snapshot.isLoaded).toBeTruthy();
expect(plansState.snapshot.hasPortal).toBeTruthy();
expect(plansState.snapshot.version).toEqual(version);
dialogs.verify(x => x.notifyInfo(It.isAnyString()), Times.never());
});
it('should load plans with overriden id', () => {
plansState.load(false, 'id2_yearly').onErrorResumeNext().subscribe();
expect(plansState.snapshot.plans.values).toEqual([
{ isSelected: false, isYearlySelected: false, plan: oldPlans.plans[0] },
{ isSelected: false, isYearlySelected: true, plan: oldPlans.plans[1] }
]);
expect(plansState.snapshot.isOwner).toBeFalsy();
expect(plansState.snapshot.isLoaded).toBeTruthy();
expect(plansState.snapshot.hasPortal).toBeTruthy();
expect(plansState.snapshot.version).toEqual(version);
dialogs.verify(x => x.notifyInfo(It.isAnyString()), Times.never());
});
it('should show notification on load when flag is true', () => {
plansState.load(true).subscribe();
dialogs.verify(x => x.notifyInfo(It.isAnyString()), Times.once());
});
it('should redirect when returning url', () => {
plansState.window = <any>{ location: {} };
plansService.setup(x => x.putPlan(app, It.isAny(), version))
.returns(() => Observable.of(new Versioned<PlanChangedDto>(newVersion, new PlanChangedDto('URI'))));
plansState.load().subscribe();
plansState.change('free').onErrorResumeNext().subscribe();
expect(plansState.snapshot.plans.values).toEqual([
{ isSelected: true, isYearlySelected: false, plan: oldPlans.plans[0] },
{ isSelected: false, isYearlySelected: false, plan: oldPlans.plans[1] }
]);
expect(plansState.window.location.href).toBe('URI');
expect(plansState.snapshot.version).toEqual(version);
});
it('should update plans when no returning url', () => {
plansState.window = <any>{ location: {} };
plansService.setup(x => x.putPlan(app, It.isAny(), version))
.returns(() => Observable.of(new Versioned<PlanChangedDto>(newVersion, new PlanChangedDto(''))));
plansState.load().subscribe();
plansState.change('id2_yearly').onErrorResumeNext().subscribe();
expect(plansState.snapshot.plans.values).toEqual([
{ isSelected: false, isYearlySelected: false, plan: oldPlans.plans[0] },
{ isSelected: false, isYearlySelected: true, plan: oldPlans.plans[1] }
]);
expect(plansState.snapshot.isOwner).toBeTruthy();
expect(plansState.snapshot.version).toEqual(newVersion);
});
});

6
src/Squidex/app/shared/state/plans.state.ts

@ -86,12 +86,14 @@ export class PlansState extends State<Snapshot> {
this.next(s => {
const planId = overridePlanId || dto.currentPlanId;
const plans = ImmutableArray.of(dto.plans.map(x => this.createPlan(x, planId)));
return {
...s,
plans: ImmutableArray.of(dto.plans.map(x => this.createPlan(x, planId))),
plans: plans,
isOwner: !dto.planOwner || dto.planOwner === this.userId,
isLoaded: true,
version: dto.version,
hasPortal: dto.hasPortal
};
});
@ -108,7 +110,7 @@ export class PlansState extends State<Snapshot> {
this.next(s => {
const plans = s.plans.map(x => this.createPlan(x.plan, planId));
return { ...s, plans };
return { ...s, plans, isOwner: true, version: dto.version };
});
}
})

Loading…
Cancel
Save