2015-12-08 08:52:46 +08:00
|
|
|
DatabaseStore = require '../../src/flux/stores/database-store'
|
2016-01-12 07:24:41 +08:00
|
|
|
|
|
|
|
QueryRange = require '../../src/flux/models/query-range'
|
|
|
|
MutableQueryResultSet = require '../../src/flux/models/mutable-query-result-set'
|
2015-12-08 08:52:46 +08:00
|
|
|
QuerySubscription = require '../../src/flux/models/query-subscription'
|
|
|
|
Thread = require '../../src/flux/models/thread'
|
|
|
|
Label = require '../../src/flux/models/label'
|
|
|
|
Utils = require '../../src/flux/models/utils'
|
|
|
|
|
|
|
|
describe "QuerySubscription", ->
|
|
|
|
describe "constructor", ->
|
2016-01-12 07:24:41 +08:00
|
|
|
describe "when a query is provided", ->
|
|
|
|
it "should finalize the query", ->
|
|
|
|
query = DatabaseStore.findAll(Thread)
|
|
|
|
subscription = new QuerySubscription(query)
|
|
|
|
expect(query._finalized).toBe(true)
|
|
|
|
|
|
|
|
it "should throw an exception if the query is a count query, which cannot be observed", ->
|
|
|
|
query = DatabaseStore.count(Thread)
|
|
|
|
expect =>
|
|
|
|
subscription = new QuerySubscription(query)
|
|
|
|
.toThrow()
|
|
|
|
|
|
|
|
it "should call `update` to initialize the result set", ->
|
|
|
|
query = DatabaseStore.findAll(Thread)
|
|
|
|
spyOn(QuerySubscription.prototype, 'update')
|
|
|
|
subscription = new QuerySubscription(query)
|
|
|
|
expect(QuerySubscription.prototype.update).toHaveBeenCalled()
|
2015-12-08 08:52:46 +08:00
|
|
|
|
2016-01-12 07:24:41 +08:00
|
|
|
describe "when initialModels are provided", ->
|
|
|
|
it "should apply the models and trigger", ->
|
|
|
|
query = DatabaseStore.findAll(Thread)
|
|
|
|
threads = [1..5].map (i) -> new Thread(id: i)
|
|
|
|
subscription = new QuerySubscription(query, {initialModels: threads})
|
|
|
|
expect(subscription._set).not.toBe(null)
|
2015-12-08 08:52:46 +08:00
|
|
|
|
2016-01-12 07:24:41 +08:00
|
|
|
describe "query", ->
|
|
|
|
it "should return the query", ->
|
|
|
|
query = DatabaseStore.findAll(Thread)
|
|
|
|
subscription = new QuerySubscription(query)
|
|
|
|
expect(subscription.query()).toBe(query)
|
|
|
|
|
|
|
|
describe "addCallback", ->
|
|
|
|
it "should emit the last result to the new callback if one is available", ->
|
|
|
|
cb = jasmine.createSpy('callback')
|
|
|
|
runs =>
|
|
|
|
subscription = new QuerySubscription(DatabaseStore.findAll(Thread))
|
|
|
|
subscription._lastResult = 'something'
|
|
|
|
subscription.addCallback(cb)
|
|
|
|
waitsFor =>
|
|
|
|
cb.callCount > 0
|
|
|
|
expect =>
|
|
|
|
expect(cb).toHaveBeenCalledWith('something')
|
2015-12-08 08:52:46 +08:00
|
|
|
|
|
|
|
describe "applyChangeRecord", ->
|
2015-12-23 06:30:28 +08:00
|
|
|
spyOn(Utils, 'generateTempId').andCallFake => undefined
|
2015-12-08 08:52:46 +08:00
|
|
|
|
|
|
|
scenarios = [{
|
|
|
|
name: "query with full set of objects (4)"
|
|
|
|
query: DatabaseStore.findAll(Thread)
|
|
|
|
.where(Thread.attributes.accountId.equal('a'))
|
|
|
|
.limit(4)
|
|
|
|
.offset(2)
|
2016-01-12 07:24:41 +08:00
|
|
|
lastModels: [
|
2015-12-23 06:30:28 +08:00
|
|
|
new Thread(accountId: 'a', clientId: '4', lastMessageReceivedTimestamp: 4)
|
|
|
|
new Thread(accountId: 'a', clientId: '3', lastMessageReceivedTimestamp: 3),
|
|
|
|
new Thread(accountId: 'a', clientId: '2', lastMessageReceivedTimestamp: 2),
|
|
|
|
new Thread(accountId: 'a', clientId: '1', lastMessageReceivedTimestamp: 1),
|
2015-12-08 08:52:46 +08:00
|
|
|
]
|
|
|
|
tests: [{
|
2016-01-12 07:24:41 +08:00
|
|
|
name: 'Item in set saved - new serverId, same sort value'
|
2015-12-08 08:52:46 +08:00
|
|
|
change:
|
|
|
|
objectClass: Thread.name
|
2015-12-23 06:30:28 +08:00
|
|
|
objects: [new Thread(accountId: 'a', serverId: 's-4', clientId: '4', lastMessageReceivedTimestamp: 4, subject: 'hello')]
|
2015-12-08 08:52:46 +08:00
|
|
|
type: 'persist'
|
2016-01-12 07:24:41 +08:00
|
|
|
nextModels:[
|
2015-12-23 06:30:28 +08:00
|
|
|
new Thread(accountId: 'a', serverId: 's-4', clientId: '4', lastMessageReceivedTimestamp: 4, subject: 'hello')
|
|
|
|
new Thread(accountId: 'a', clientId: '3', lastMessageReceivedTimestamp: 3),
|
|
|
|
new Thread(accountId: 'a', clientId: '2', lastMessageReceivedTimestamp: 2),
|
|
|
|
new Thread(accountId: 'a', clientId: '1', lastMessageReceivedTimestamp: 1),
|
2015-12-08 08:52:46 +08:00
|
|
|
]
|
2016-01-12 07:24:41 +08:00
|
|
|
mustUpdate: true
|
|
|
|
mustRefetchAllIds: false
|
2015-12-08 08:52:46 +08:00
|
|
|
},{
|
2016-01-12 07:24:41 +08:00
|
|
|
name: 'Item in set saved - new sort value'
|
2015-12-08 08:52:46 +08:00
|
|
|
change:
|
|
|
|
objectClass: Thread.name
|
2016-01-12 07:24:41 +08:00
|
|
|
objects: [new Thread(accountId: 'a', clientId: '5', lastMessageReceivedTimestamp: 3.5)]
|
2015-12-08 08:52:46 +08:00
|
|
|
type: 'persist'
|
2016-01-12 07:24:41 +08:00
|
|
|
nextModels:[
|
2015-12-23 06:30:28 +08:00
|
|
|
new Thread(accountId: 'a', clientId: '4', lastMessageReceivedTimestamp: 4),
|
2016-01-12 07:24:41 +08:00
|
|
|
new Thread(accountId: 'a', clientId: '5', lastMessageReceivedTimestamp: 3.5),
|
|
|
|
new Thread(accountId: 'a', clientId: '3', lastMessageReceivedTimestamp: 3),
|
2015-12-23 06:30:28 +08:00
|
|
|
new Thread(accountId: 'a', clientId: '2', lastMessageReceivedTimestamp: 2),
|
2015-12-08 08:52:46 +08:00
|
|
|
]
|
2016-01-12 07:24:41 +08:00
|
|
|
mustUpdate: true
|
|
|
|
mustRefetchAllIds: true
|
2015-12-08 08:52:46 +08:00
|
|
|
},{
|
2016-01-12 07:24:41 +08:00
|
|
|
name: 'Item saved - does not match query clauses'
|
2015-12-08 08:52:46 +08:00
|
|
|
change:
|
|
|
|
objectClass: Thread.name
|
2016-01-12 07:24:41 +08:00
|
|
|
objects: [new Thread(accountId: 'b', clientId: '5', lastMessageReceivedTimestamp: 5)]
|
2015-12-08 08:52:46 +08:00
|
|
|
type: 'persist'
|
2016-01-12 07:24:41 +08:00
|
|
|
nextModels: 'unchanged'
|
|
|
|
mustUpdate: false
|
|
|
|
mustRefetchAllIds: false
|
2015-12-08 08:52:46 +08:00
|
|
|
},{
|
2016-01-12 07:24:41 +08:00
|
|
|
name: 'Item saved - matches query clauses'
|
2015-12-08 08:52:46 +08:00
|
|
|
change:
|
|
|
|
objectClass: Thread.name
|
2016-01-12 07:24:41 +08:00
|
|
|
objects: [new Thread(accountId: 'a', clientId: '5', lastMessageReceivedTimestamp: -2)]
|
2015-12-08 08:52:46 +08:00
|
|
|
type: 'persist'
|
2016-01-12 07:24:41 +08:00
|
|
|
mustUpdate: true
|
|
|
|
mustRefetchAllIds: true
|
2015-12-08 08:52:46 +08:00
|
|
|
},{
|
2016-01-12 07:24:41 +08:00
|
|
|
name: 'Item in set saved - no longer matches query clauses'
|
2015-12-08 08:52:46 +08:00
|
|
|
change:
|
|
|
|
objectClass: Thread.name
|
2015-12-23 06:30:28 +08:00
|
|
|
objects: [new Thread(accountId: 'b', clientId: '4', lastMessageReceivedTimestamp: 4)]
|
2015-12-08 08:52:46 +08:00
|
|
|
type: 'persist'
|
2016-01-12 07:24:41 +08:00
|
|
|
nextModels: [
|
2015-12-23 06:30:28 +08:00
|
|
|
new Thread(accountId: 'a', clientId: '3', lastMessageReceivedTimestamp: 3),
|
|
|
|
new Thread(accountId: 'a', clientId: '2', lastMessageReceivedTimestamp: 2),
|
|
|
|
new Thread(accountId: 'a', clientId: '1', lastMessageReceivedTimestamp: 1),
|
2015-12-08 08:52:46 +08:00
|
|
|
]
|
2016-01-12 07:24:41 +08:00
|
|
|
mustUpdate: true
|
|
|
|
mustRefetchAllIds: false
|
2015-12-08 08:52:46 +08:00
|
|
|
},{
|
|
|
|
name: 'Item in set deleted'
|
|
|
|
change:
|
|
|
|
objectClass: Thread.name
|
2015-12-23 06:30:28 +08:00
|
|
|
objects: [new Thread(accountId: 'a', clientId: '4')]
|
2015-12-08 08:52:46 +08:00
|
|
|
type: 'unpersist'
|
2016-01-12 07:24:41 +08:00
|
|
|
nextModels: [
|
2015-12-23 06:30:28 +08:00
|
|
|
new Thread(accountId: 'a', clientId: '3', lastMessageReceivedTimestamp: 3),
|
|
|
|
new Thread(accountId: 'a', clientId: '2', lastMessageReceivedTimestamp: 2),
|
|
|
|
new Thread(accountId: 'a', clientId: '1', lastMessageReceivedTimestamp: 1),
|
2015-12-08 08:52:46 +08:00
|
|
|
]
|
2016-01-12 07:24:41 +08:00
|
|
|
mustUpdate: true
|
|
|
|
mustRefetchAllIds: false
|
2015-12-08 08:52:46 +08:00
|
|
|
},{
|
|
|
|
name: 'Item not in set deleted'
|
|
|
|
change:
|
|
|
|
objectClass: Thread.name
|
2015-12-23 06:30:28 +08:00
|
|
|
objects: [new Thread(accountId: 'a', clientId: '5')]
|
2015-12-08 08:52:46 +08:00
|
|
|
type: 'unpersist'
|
2016-01-12 07:24:41 +08:00
|
|
|
nextModels: 'unchanged'
|
|
|
|
mustUpdate: false
|
|
|
|
mustRefetchAllIds: false
|
2015-12-08 08:52:46 +08:00
|
|
|
}]
|
|
|
|
|
|
|
|
},{
|
|
|
|
name: "query with multiple sort orders"
|
|
|
|
query: DatabaseStore.findAll(Thread)
|
|
|
|
.where(Thread.attributes.accountId.equal('a'))
|
|
|
|
.limit(4)
|
|
|
|
.offset(2)
|
|
|
|
.order([
|
|
|
|
Thread.attributes.lastMessageReceivedTimestamp.ascending(),
|
|
|
|
Thread.attributes.unread.descending()
|
|
|
|
])
|
2016-01-12 07:24:41 +08:00
|
|
|
lastModels: [
|
2015-12-23 06:30:28 +08:00
|
|
|
new Thread(accountId: 'a', clientId: '1', lastMessageReceivedTimestamp: 1, unread: true)
|
|
|
|
new Thread(accountId: 'a', clientId: '2', lastMessageReceivedTimestamp: 1, unread: false)
|
|
|
|
new Thread(accountId: 'a', clientId: '3', lastMessageReceivedTimestamp: 1, unread: false)
|
|
|
|
new Thread(accountId: 'a', clientId: '4', lastMessageReceivedTimestamp: 2, unread: true)
|
2015-12-08 08:52:46 +08:00
|
|
|
]
|
|
|
|
tests: [{
|
|
|
|
name: 'Item in set saved, secondary sort order changed'
|
|
|
|
change:
|
|
|
|
objectClass: Thread.name
|
2015-12-23 06:30:28 +08:00
|
|
|
objects: [new Thread(accountId: 'a', clientId: '3', lastMessageReceivedTimestamp: 1, unread: true)]
|
2015-12-08 08:52:46 +08:00
|
|
|
type: 'persist'
|
2016-01-12 07:24:41 +08:00
|
|
|
mustUpdate: true
|
|
|
|
mustRefetchAllIds: true
|
2015-12-08 08:52:46 +08:00
|
|
|
}]
|
|
|
|
}]
|
|
|
|
|
|
|
|
jasmine.unspy(Utils, 'generateTempId')
|
|
|
|
|
|
|
|
describe "scenarios", ->
|
|
|
|
scenarios.forEach (scenario) =>
|
|
|
|
scenario.tests.forEach (test) =>
|
|
|
|
it "with #{scenario.name}, should correctly apply #{test.name}", ->
|
2016-01-12 07:24:41 +08:00
|
|
|
@q = new QuerySubscription(scenario.query)
|
|
|
|
@q._set = new MutableQueryResultSet()
|
|
|
|
@q._set.addModelsInRange(scenario.lastModels, new QueryRange(start: 0, end: scenario.lastModels.length))
|
|
|
|
|
|
|
|
spyOn(@q, 'update')
|
2015-12-08 08:52:46 +08:00
|
|
|
@q.applyChangeRecord(test.change)
|
|
|
|
|
2016-01-12 07:24:41 +08:00
|
|
|
if test.mustRefetchAllIds
|
|
|
|
expect(@q._set).toBe(null)
|
|
|
|
else if test.nextModels is 'unchanged'
|
|
|
|
expect(@q._set.models()).toEqual(scenario.lastModels)
|
|
|
|
else
|
|
|
|
expect(@q._set.models()).toEqual(test.nextModels)
|
2015-12-08 08:52:46 +08:00
|
|
|
|
2016-01-12 07:24:41 +08:00
|
|
|
if test.mustUpdate
|
|
|
|
expect(@q.update).toHaveBeenCalled()
|
2015-12-08 08:52:46 +08:00
|
|
|
|
2016-01-12 07:24:41 +08:00
|
|
|
describe "update", ->
|
|
|
|
beforeEach ->
|
|
|
|
spyOn(QuerySubscription.prototype, '_fetchRange').andCallFake ->
|
|
|
|
@_set ?= new MutableQueryResultSet()
|
|
|
|
Promise.resolve()
|
|
|
|
|
|
|
|
it "should increment the version", ->
|
|
|
|
subscription = new QuerySubscription(DatabaseStore.findAll(Thread))
|
|
|
|
expect(subscription._version).toBe(1)
|
|
|
|
subscription.update()
|
|
|
|
expect(subscription._version).toBe(2)
|
|
|
|
|
|
|
|
describe "when the query has an infinite range", ->
|
|
|
|
it "should call _fetchRange for the entire range", ->
|
|
|
|
subscription = new QuerySubscription(DatabaseStore.findAll(Thread))
|
|
|
|
subscription.update()
|
|
|
|
advanceClock()
|
2016-01-26 03:35:11 +08:00
|
|
|
expect(subscription._fetchRange).toHaveBeenCalledWith(QueryRange.infinite(), {entireModels: true, version: 2})
|
2016-01-12 07:24:41 +08:00
|
|
|
|
|
|
|
it "should fetch full full models only when the previous set is empty", ->
|
|
|
|
subscription = new QuerySubscription(DatabaseStore.findAll(Thread))
|
|
|
|
subscription._set = new MutableQueryResultSet()
|
|
|
|
subscription._set.addModelsInRange([new Thread()], new QueryRange(start: 0, end: 1))
|
|
|
|
subscription.update()
|
|
|
|
advanceClock()
|
2016-01-26 03:35:11 +08:00
|
|
|
expect(subscription._fetchRange).toHaveBeenCalledWith(QueryRange.infinite(), {entireModels: false, version: 2})
|
2016-01-12 07:24:41 +08:00
|
|
|
|
|
|
|
describe "when the query has a range", ->
|
|
|
|
beforeEach ->
|
|
|
|
@query = DatabaseStore.findAll(Thread).limit(10)
|
|
|
|
|
|
|
|
describe "when we have no current range", ->
|
|
|
|
it "should call _fetchRange for the entire range and fetch full models", ->
|
|
|
|
subscription = new QuerySubscription(@query)
|
|
|
|
subscription._set = null
|
|
|
|
subscription.update()
|
|
|
|
advanceClock()
|
2016-01-26 03:35:11 +08:00
|
|
|
expect(subscription._fetchRange).toHaveBeenCalledWith(@query.range(), {entireModels: true, version: 2})
|
2016-01-12 07:24:41 +08:00
|
|
|
|
|
|
|
describe "when we have a previous range", ->
|
|
|
|
it "should call _fetchRange for the ranges representing the difference", ->
|
|
|
|
customRange1 = jasmine.createSpy('customRange1')
|
|
|
|
customRange2 = jasmine.createSpy('customRange2')
|
|
|
|
spyOn(QueryRange, 'rangesBySubtracting').andReturn [customRange1, customRange2]
|
|
|
|
|
|
|
|
subscription = new QuerySubscription(@query)
|
|
|
|
subscription._set = new MutableQueryResultSet()
|
|
|
|
subscription._set.addModelsInRange([new Thread()], new QueryRange(start: 0, end: 1))
|
|
|
|
subscription.update()
|
|
|
|
advanceClock()
|
|
|
|
expect(subscription._fetchRange.callCount).toBe(2)
|
2016-01-26 03:35:11 +08:00
|
|
|
expect(subscription._fetchRange.calls[0].args).toEqual([customRange1, {entireModels: true, version: 2}])
|
|
|
|
expect(subscription._fetchRange.calls[1].args).toEqual([customRange2, {entireModels: true, version: 2}])
|