mirror of https://github.com/Squidex/squidex.git
19 changed files with 298 additions and 106 deletions
@ -0,0 +1,88 @@ |
|||||
|
/* |
||||
|
* 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, |
||||
|
DateTime, |
||||
|
DialogService |
||||
|
} from '@app/shared'; |
||||
|
|
||||
|
import { RuleEventsState } from './rule-events.state'; |
||||
|
|
||||
|
import { |
||||
|
RuleEventDto, |
||||
|
RuleEventsDto, |
||||
|
RulesService |
||||
|
} from './../services/rules.service'; |
||||
|
|
||||
|
describe('RuleEventsState', () => { |
||||
|
const app = 'my-app'; |
||||
|
|
||||
|
const oldRuleEvents = [ |
||||
|
new RuleEventDto('id1', DateTime.now(), null, 'event1', 'description', 'dump1', 'result1', 'result1', 1), |
||||
|
new RuleEventDto('id2', DateTime.now(), null, 'event2', 'description', 'dump2', 'result2', 'result2', 2) |
||||
|
]; |
||||
|
|
||||
|
let appsState: IMock<AppsState>; |
||||
|
let dialogs: IMock<DialogService>; |
||||
|
let rulesService: IMock<RulesService>; |
||||
|
let ruleEventsState: RuleEventsState; |
||||
|
|
||||
|
beforeEach(() => { |
||||
|
dialogs = Mock.ofType<DialogService>(); |
||||
|
|
||||
|
appsState = Mock.ofType<AppsState>(); |
||||
|
|
||||
|
appsState.setup(x => x.appName) |
||||
|
.returns(() => app); |
||||
|
|
||||
|
rulesService = Mock.ofType<RulesService>(); |
||||
|
|
||||
|
rulesService.setup(x => x.getEvents(app, 10, 0)) |
||||
|
.returns(() => Observable.of(new RuleEventsDto(200, oldRuleEvents))); |
||||
|
|
||||
|
ruleEventsState = new RuleEventsState(appsState.object, dialogs.object, rulesService.object); |
||||
|
ruleEventsState.load().subscribe(); |
||||
|
}); |
||||
|
|
||||
|
it('should load ruleEvents', () => { |
||||
|
expect(ruleEventsState.snapshot.ruleEvents.values).toEqual(oldRuleEvents); |
||||
|
expect(ruleEventsState.snapshot.ruleEventsPager.numberOfItems).toEqual(200); |
||||
|
expect(ruleEventsState.snapshot.isLoaded).toBeTruthy(); |
||||
|
|
||||
|
dialogs.verify(x => x.notifyInfo(It.isAnyString()), Times.never()); |
||||
|
}); |
||||
|
|
||||
|
it('should show notification on load when flag is true', () => { |
||||
|
ruleEventsState.load(true).subscribe(); |
||||
|
|
||||
|
dialogs.verify(x => x.notifyInfo(It.isAnyString()), Times.once()); |
||||
|
}); |
||||
|
|
||||
|
it('should load next page and prev page when paging', () => { |
||||
|
rulesService.setup(x => x.getEvents(app, 10, 10)) |
||||
|
.returns(() => Observable.of(new RuleEventsDto(200, []))); |
||||
|
|
||||
|
ruleEventsState.goNext().subscribe(); |
||||
|
ruleEventsState.goPrev().subscribe(); |
||||
|
|
||||
|
rulesService.verify(x => x.getEvents(app, 10, 10), Times.once()); |
||||
|
rulesService.verify(x => x.getEvents(app, 10, 0), Times.exactly(2)); |
||||
|
}); |
||||
|
|
||||
|
it('should call service when enqueuing event', () => { |
||||
|
rulesService.setup(x => x.enqueueEvent(app, oldRuleEvents[0].id)) |
||||
|
.returns(() => Observable.of({})); |
||||
|
|
||||
|
ruleEventsState.enqueue(oldRuleEvents[0]).subscribe(); |
||||
|
|
||||
|
rulesService.verify(x => x.enqueueEvent(app, oldRuleEvents[0].id), Times.once()); |
||||
|
}); |
||||
|
}); |
||||
@ -0,0 +1,95 @@ |
|||||
|
/* |
||||
|
* Squidex Headless CMS |
||||
|
* |
||||
|
* @license |
||||
|
* Copyright (c) Squidex UG (haftungsbeschränkt). All rights reserved. |
||||
|
*/ |
||||
|
|
||||
|
import { Injectable } from '@angular/core'; |
||||
|
import { Observable } from 'rxjs'; |
||||
|
|
||||
|
import '@app/framework/utils/rxjs-extensions'; |
||||
|
|
||||
|
import { |
||||
|
DialogService, |
||||
|
ImmutableArray, |
||||
|
Pager, |
||||
|
State |
||||
|
} from '@app/framework'; |
||||
|
|
||||
|
import { AppsState } from './apps.state'; |
||||
|
|
||||
|
import { RuleEventDto, RulesService } from './../services/rules.service'; |
||||
|
|
||||
|
interface Snapshot { |
||||
|
ruleEvents: ImmutableArray<RuleEventDto>; |
||||
|
ruleEventsPager: Pager; |
||||
|
|
||||
|
isLoaded?: boolean; |
||||
|
} |
||||
|
|
||||
|
@Injectable() |
||||
|
export class RuleEventsState extends State<Snapshot> { |
||||
|
public ruleEvents = |
||||
|
this.changes.map(x => x.ruleEvents) |
||||
|
.distinctUntilChanged(); |
||||
|
|
||||
|
public ruleEventsPager = |
||||
|
this.changes.map(x => x.ruleEventsPager) |
||||
|
.distinctUntilChanged(); |
||||
|
|
||||
|
public isLoaded = |
||||
|
this.changes.map(x => !!x.isLoaded) |
||||
|
.distinctUntilChanged(); |
||||
|
|
||||
|
constructor( |
||||
|
private readonly appsState: AppsState, |
||||
|
private readonly dialogs: DialogService, |
||||
|
private readonly rulesService: RulesService |
||||
|
) { |
||||
|
super({ ruleEvents: ImmutableArray.of(), ruleEventsPager: new Pager(0) }); |
||||
|
} |
||||
|
|
||||
|
public load(notifyLoad = false): Observable<any> { |
||||
|
return this.rulesService.getEvents(this.appName, |
||||
|
this.snapshot.ruleEventsPager.pageSize, |
||||
|
this.snapshot.ruleEventsPager.skip) |
||||
|
.do(dtos => { |
||||
|
if (notifyLoad) { |
||||
|
this.dialogs.notifyInfo('RuleEvents reloaded.'); |
||||
|
} |
||||
|
|
||||
|
return this.next(s => { |
||||
|
const ruleEvents = ImmutableArray.of(dtos.items); |
||||
|
const ruleEventsPager = s.ruleEventsPager.setCount(dtos.total); |
||||
|
|
||||
|
return { ...s, ruleEvents, ruleEventsPager, isLoaded: true }; |
||||
|
}); |
||||
|
}) |
||||
|
.notify(this.dialogs); |
||||
|
} |
||||
|
|
||||
|
public enqueue(event: RuleEventDto): Observable<any> { |
||||
|
return this.rulesService.enqueueEvent(this.appsState.appName, event.id) |
||||
|
.do(() => { |
||||
|
this.dialogs.notifyInfo('Events enqueued. Will be resend in a few seconds.'); |
||||
|
}) |
||||
|
.notify(this.dialogs); |
||||
|
} |
||||
|
|
||||
|
public goNext(): Observable<any> { |
||||
|
this.next(s => ({ ...s, ruleEventsPager: s.ruleEventsPager.goNext() })); |
||||
|
|
||||
|
return this.load(); |
||||
|
} |
||||
|
|
||||
|
public goPrev(): Observable<any> { |
||||
|
this.next(s => ({ ...s, ruleEventsPager: s.ruleEventsPager.goPrev() })); |
||||
|
|
||||
|
return this.load(); |
||||
|
} |
||||
|
|
||||
|
private get appName() { |
||||
|
return this.appsState.appName; |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue