Skip to content

Commit

Permalink
Poll rate wasn't used correctly by EntityView
Browse files Browse the repository at this point in the history
  • Loading branch information
gingi committed Jan 11, 2023
1 parent 19ae786 commit 2801d21
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 17 deletions.
56 changes: 40 additions & 16 deletions src/@batch-flask/core/data/entity-view/entity-view.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ describe("EntityView", () => {
view.fetch();
tick();
expect(dataSpy).toHaveBeenCalledTimes(1);
expect(item!.toJS()).toEqual(fake1);
expect(item.toJS()).toEqual(fake1);
expect(dataSpy).toHaveBeenCalledWith({ parentId: "parent-1", id: "1" });
}));

Expand All @@ -57,57 +57,81 @@ describe("EntityView", () => {
view.item.subscribe(x => item = x);
view.fetch();
expect(item).not.toBeFalsy();
expect(item!.toJS()).toEqual({ id: "1", parentId: "parent-1", state: "creating", name: "Fake1" });
expect(item.toJS()).toEqual({ id: "1", parentId: "parent-1", state: "creating", name: "Fake1" });

tick(); // This should be the return from the fetched data
expect(item!.toJS()).toEqual(fake1);
expect(item.toJS()).toEqual(fake1);
}));

it("Update the data when refreshing", fakeAsync(() => {
view.item.subscribe(x => item = x);
view.fetch();
tick();
expect(item!.toJS()).toEqual(fake1);
expect(item.toJS()).toEqual(fake1);

view.refresh();
tick();
expect(item!.toJS()).toEqual(fake2);
expect(item.toJS()).toEqual(fake2);

view.refresh();
tick();
expect(item!.toJS()).toEqual(fake3);
expect(item.toJS()).toEqual(fake3);
expect(dataSpy).toHaveBeenCalledTimes(3);
}));

it("Update the params", fakeAsync(() => {
it("updates at the poll rate", fakeAsync(() => {
const cache2 = new DataCache<FakeModel>();
const dataSpy2 = jasmine.createSpy("supplyDataSpy")
.and.returnValues(...data.map(x => from(Promise.resolve(x))));
const getter2 = new BasicEntityGetter(FakeModel, {
cache: () => cache2,
supplyData: dataSpy2,
});

const view2 = new EntityView({
cache: () => cache2,
getter: getter2,
poll: 1000
});
view2.params = { parentId: "parent-1", id: "2" };

let item2: FakeModel;

view.item.subscribe(x => item = x);
view.params = { parentId: "parent-1", id: "2" };
view2.item.subscribe(x => item2 = x);
view.fetch();
view2.fetch();
tick();
expect(item!.toJS()).toEqual(fake1);
expect(dataSpy).toHaveBeenCalledWith({ parentId: "parent-1", id: "2" });
expect(item.toJS()).toEqual(fake1);
expect(item2.toJS()).toEqual(fake1);

tick(1000);
expect(item.toJS()).toEqual(fake1);
expect(item2.toJS()).toEqual(fake2);

view2.dispose();
}));

it("Update the params", fakeAsync(() => {
view.item.subscribe(x => item = x);
view.params = { parentId: "parent-1", id: "2" };
view.fetch();
tick();
expect(item!.toJS()).toEqual(fake1);
expect(dataSpy).toHaveBeenCalledWith({parentId: "parent-1", id: "2" });
expect(item.toJS()).toEqual(fake1);
expect(dataSpy).toHaveBeenCalledWith({ parentId: "parent-1", id: "2" });
}));

it("Update the cache should update the item", fakeAsync(() => {
view.item.subscribe(x => item = x);
view.fetch();
tick();
expect(item!.toJS()).toEqual(fake1);
expect(item.toJS()).toEqual(fake1);
expect(dataSpy).toHaveBeenCalledTimes(1);
expect(dataSpy).toHaveBeenCalledWith({parentId: "parent-1", id: "1" });
expect(dataSpy).toHaveBeenCalledWith({ parentId: "parent-1", id: "1" });

cache.addItem(new FakeModel({...data[2], parentId: "parent-1"}));
cache.addItem(new FakeModel({ ...data[2], parentId: "parent-1" }));
tick();
expect(item!.toJS()).toEqual(fake3);
expect(item.toJS()).toEqual(fake3);
}));

describe("When it return a 404 error", () => {
Expand Down
2 changes: 1 addition & 1 deletion src/@batch-flask/core/data/entity-view/entity-view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export class EntityView<TEntity extends Record<any>, TParams> extends GenericVie
);

if (config.poll) {
this._pollTracker = this.startPoll(5000);
this._pollTracker = this.startPoll(config.poll);
}
}

Expand Down

0 comments on commit 2801d21

Please sign in to comment.