2016-10-15 03:06:07 +08:00
|
|
|
import QuerySubscriptionPool from '../../src/flux/models/query-subscription-pool';
|
|
|
|
import DatabaseStore from '../../src/flux/stores/database-store';
|
|
|
|
import Label from '../../src/flux/models/label';
|
|
|
|
|
2017-09-27 02:33:08 +08:00
|
|
|
describe('QuerySubscriptionPool', function QuerySubscriptionPoolSpecs() {
|
2016-10-15 03:06:07 +08:00
|
|
|
beforeEach(() => {
|
|
|
|
this.query = DatabaseStore.findAll(Label);
|
|
|
|
this.queryKey = this.query.sql();
|
|
|
|
QuerySubscriptionPool._subscriptions = {};
|
|
|
|
QuerySubscriptionPool._cleanupChecks = [];
|
|
|
|
});
|
|
|
|
|
2017-09-27 02:33:08 +08:00
|
|
|
describe('add', () => {
|
|
|
|
it('should add a new subscription with the callback', () => {
|
2016-10-15 03:06:07 +08:00
|
|
|
const callback = jasmine.createSpy('callback');
|
|
|
|
QuerySubscriptionPool.add(this.query, callback);
|
|
|
|
expect(QuerySubscriptionPool._subscriptions[this.queryKey]).toBeDefined();
|
|
|
|
|
|
|
|
const subscription = QuerySubscriptionPool._subscriptions[this.queryKey];
|
|
|
|
expect(subscription.hasCallback(callback)).toBe(true);
|
|
|
|
});
|
|
|
|
|
2017-09-27 02:33:08 +08:00
|
|
|
it('should yield database changes to the subscription', () => {
|
2016-10-15 03:06:07 +08:00
|
|
|
const callback = jasmine.createSpy('callback');
|
|
|
|
QuerySubscriptionPool.add(this.query, callback);
|
|
|
|
const subscription = QuerySubscriptionPool._subscriptions[this.queryKey];
|
|
|
|
spyOn(subscription, 'applyChangeRecord');
|
|
|
|
|
2017-09-27 02:33:08 +08:00
|
|
|
const record = { objectType: 'whateves' };
|
2016-10-15 03:06:07 +08:00
|
|
|
QuerySubscriptionPool._onChange(record);
|
|
|
|
expect(subscription.applyChangeRecord).toHaveBeenCalledWith(record);
|
|
|
|
});
|
|
|
|
|
2017-09-27 02:33:08 +08:00
|
|
|
describe('unsubscribe', () => {
|
|
|
|
it('should return an unsubscribe method', () => {
|
2016-10-15 03:06:07 +08:00
|
|
|
expect(QuerySubscriptionPool.add(this.query, () => {}) instanceof Function).toBe(true);
|
|
|
|
});
|
|
|
|
|
2017-09-27 02:33:08 +08:00
|
|
|
it('should remove the callback from the subscription', () => {
|
2016-10-15 03:06:07 +08:00
|
|
|
const cb = () => {};
|
|
|
|
|
|
|
|
const unsub = QuerySubscriptionPool.add(this.query, cb);
|
|
|
|
const subscription = QuerySubscriptionPool._subscriptions[this.queryKey];
|
|
|
|
|
|
|
|
expect(subscription.hasCallback(cb)).toBe(true);
|
|
|
|
unsub();
|
|
|
|
expect(subscription.hasCallback(cb)).toBe(false);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should wait before removing th subscription to make sure it's not reused", () => {
|
|
|
|
const unsub = QuerySubscriptionPool.add(this.query, () => {});
|
|
|
|
expect(QuerySubscriptionPool._subscriptions[this.queryKey]).toBeDefined();
|
|
|
|
unsub();
|
|
|
|
expect(QuerySubscriptionPool._subscriptions[this.queryKey]).toBeDefined();
|
|
|
|
advanceClock();
|
|
|
|
expect(QuerySubscriptionPool._subscriptions[this.queryKey]).toBeUndefined();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|