Mailspring/app/spec/models/query-subscription-pool-spec.es6

61 lines
2.4 KiB
Text
Raw Normal View History

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() {
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', () => {
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', () => {
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' };
QuerySubscriptionPool._onChange(record);
expect(subscription.applyChangeRecord).toHaveBeenCalledWith(record);
});
2017-09-27 02:33:08 +08:00
describe('unsubscribe', () => {
it('should return an unsubscribe method', () => {
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', () => {
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();
});
});
});
});