Mailspring/packages/client-app/spec/stores/draft-editing-session-spec.coffee

263 lines
11 KiB
CoffeeScript
Raw Normal View History

Message = require('../../src/flux/models/message').default
Actions = require('../../src/flux/actions').default
2016-10-01 06:24:34 +08:00
DatabaseStore = require('../../src/flux/stores/database-store').default
DatabaseWriter = require('../../src/flux/stores/database-writer').default
DraftEditingSession = require '../../src/flux/stores/draft-editing-session'
DraftChangeSet = DraftEditingSession.DraftChangeSet
_ = require 'underscore'
fix(spec): add support for async specs and disable misbehaving ones More spec fixes replace process.nextTick with setTimeout(fn, 0) for specs Also added an unspy in the afterEach Temporarily disable specs fix(spec): start fixing specs Summary: This is the WIP fix to our spec runner. Several tests have been completely commented out that will require substantially more work to fix. These have been added to our sprint backlog. Other tests have been fixed to update to new APIs or to deal with genuine bugs that were introduced without our knowing! The most common non-trivial change relates to observing the `NylasAPI` and `NylasAPIRequest`. We used to observe the arguments to `makeRequest`. Unfortunately `NylasAPIRequest.run` is argumentless. Instead you can do: `NylasAPIRequest.prototype.run.mostRecentCall.object.options` to get the `options` passed into the object. the `.object` property grabs the context of the spy when it was last called. Fixing these tests uncovered several concerning issues with our test runner. I spent a while tracking down why our participant-text-field-spec was failling every so often. I chose that spec because it was the first spec to likely fail, thereby requiring looking at the least number of preceding files. I tried binary searching, turning on and off, several files beforehand only to realize that the failure rate was not determined by a particular preceding test, but rather the existing and quantity of preceding tests, AND the number of console.log statements I had. There is some processor-dependent race condition going on that needs further investigation. I also discovered an issue with the file-download-spec. We were getting errors about it accessing a file, which was very suspicious given the code stubs out all fs access. This was caused due to a spec that called an async function outside ot a `waitsForPromise` block or a `waitsFor` block. The test completed, the spies were cleaned up, but the downstream async chain was still running. By the time the async chain finished the runner was already working on the next spec and the spies had been restored (causing the real fs access to run). Juan had an idea to kill the specs once one fails to prevent cascading failures. I'll implement this in the next diff update Test Plan: npm test Reviewers: juan, halla, jackie Differential Revision: https://phab.nylas.com/D3501 Disable other specs Disable more broken specs All specs turned off till passing state Use async-safe versions of spec functions Add async test spec Remove unused package code Remove canary spec
2016-12-13 04:12:20 +08:00
xdescribe "DraftEditingSession Specs", ->
describe "DraftChangeSet", ->
beforeEach ->
@onDidAddChanges = jasmine.createSpy('onDidAddChanges')
@onWillAddChanges = jasmine.createSpy('onWillAddChanges')
@commitResolve = null
@commitResolves = []
@onCommit = jasmine.createSpy('commit').andCallFake =>
new Promise (resolve, reject) =>
@commitResolves.push(resolve)
@commitResolve = resolve
@changeSet = new DraftChangeSet({
onDidAddChanges: @onDidAddChanges,
onWillAddChanges: @onWillAddChanges,
onCommit: @onCommit,
})
@changeSet._pending =
subject: 'Change to subject line'
describe "teardown", ->
it "should remove all of the pending and saving changes", ->
@changeSet.teardown()
expect(@changeSet._saving).toEqual({})
expect(@changeSet._pending).toEqual({})
describe "add", ->
it "should mark that the draft is not pristine", ->
@changeSet.add(body: 'Hello World!')
expect(@changeSet._pending.pristine).toEqual(false)
it "should add the changes to the _pending set", ->
@changeSet.add(body: 'Hello World!')
expect(@changeSet._pending.body).toEqual('Hello World!')
describe "otherwise", ->
it "should commit after thirty seconds", ->
spyOn(@changeSet, 'commit')
@changeSet.add({body: 'Hello World!'})
expect(@changeSet.commit).not.toHaveBeenCalled()
advanceClock(31000)
expect(@changeSet.commit).toHaveBeenCalled()
describe "commit", ->
it "should resolve immediately if the pending set is empty", ->
@changeSet._pending = {}
waitsForPromise =>
@changeSet.commit().then =>
expect(@onCommit).not.toHaveBeenCalled()
it "should move changes to the saving set", ->
pendingBefore = _.extend({}, @changeSet._pending)
expect(@changeSet._saving).toEqual({})
@changeSet.commit()
advanceClock()
expect(@changeSet._pending).toEqual({})
expect(@changeSet._saving).toEqual(pendingBefore)
it "should call the commit handler and then clear the saving set", ->
@changeSet.commit()
advanceClock()
expect(@changeSet._saving).not.toEqual({})
@commitResolve()
advanceClock()
expect(@changeSet._saving).toEqual({})
describe "concurrency", ->
it "the commit function should always run serially", ->
firstFulfilled = false
secondFulfilled = false
@changeSet._pending = {subject: 'A'}
@changeSet.commit().then =>
@changeSet._pending = {subject: 'B'}
firstFulfilled = true
@changeSet.commit().then =>
secondFulfilled = true
advanceClock()
expect(firstFulfilled).toBe(false)
expect(secondFulfilled).toBe(false)
@commitResolves[0]()
advanceClock()
expect(firstFulfilled).toBe(true)
expect(secondFulfilled).toBe(false)
@commitResolves[1]()
advanceClock()
expect(firstFulfilled).toBe(true)
expect(secondFulfilled).toBe(true)
describe "applyToModel", ->
it "should apply the saving and then the pending change sets, in that order", ->
@changeSet._saving = {subject: 'A', body: 'Basketb'}
@changeSet._pending = {body: 'Basketball'}
m = new Message()
@changeSet.applyToModel(m)
expect(m.subject).toEqual('A')
expect(m.body).toEqual('Basketball')
describe "DraftEditingSession", ->
describe "constructor", ->
it "should make a query to fetch the draft", ->
spyOn(DatabaseStore, 'run').andCallFake =>
new Promise (resolve, reject) =>
session = new DraftEditingSession('client-id')
expect(DatabaseStore.run).toHaveBeenCalled()
describe "when given a draft object", ->
beforeEach ->
spyOn(DatabaseStore, 'run')
@draft = new Message(draft: true, body: '123')
@session = new DraftEditingSession('client-id', @draft)
it "should not make a query for the draft", ->
expect(DatabaseStore.run).not.toHaveBeenCalled()
it "prepare should resolve without querying for the draft", ->
waitsForPromise => @session.prepare().then =>
expect(@session.draft()).toBeDefined()
expect(DatabaseStore.run).not.toHaveBeenCalled()
describe "teardown", ->
it "should mark the session as destroyed", ->
spyOn(DraftEditingSession.prototype, "prepare")
session = new DraftEditingSession('client-id')
session.teardown()
expect(session._destroyed).toEqual(true)
describe "prepare", ->
beforeEach ->
2017-06-22 04:12:49 +08:00
@draft = new Message(draft: true, body: '123', id: 'client-id')
spyOn(DraftEditingSession.prototype, "prepare")
@session = new DraftEditingSession('client-id')
spyOn(@session, '_setDraft').andCallThrough()
spyOn(DatabaseStore, 'run').andCallFake (modelQuery) =>
Promise.resolve(@draft)
jasmine.unspy(DraftEditingSession.prototype, "prepare")
it "should call setDraft with the retrieved draft", ->
waitsForPromise =>
@session.prepare().then =>
expect(@session._setDraft).toHaveBeenCalledWith(@draft)
it "should resolve with the DraftEditingSession", ->
waitsForPromise =>
@session.prepare().then (val) =>
expect(val).toBe(@session)
describe "error handling", ->
it "should reject if the draft session has already been destroyed", ->
@session._destroyed = true
waitsForPromise =>
@session.prepare().then =>
expect(false).toBe(true)
.catch (val) =>
expect(val instanceof Error).toBe(true)
it "should reject if the draft cannot be found", ->
@draft = null
waitsForPromise =>
@session.prepare().then =>
expect(false).toBe(true)
.catch (val) =>
expect(val instanceof Error).toBe(true)
describe "when a draft changes", ->
beforeEach ->
2017-06-22 04:12:49 +08:00
@draft = new Message(draft: true, id: 'client-id', body: 'A', subject: 'initial')
@session = new DraftEditingSession('client-id', @draft)
advanceClock()
spyOn(DatabaseWriter.prototype, "persistModel").andReturn Promise.resolve()
spyOn(Actions, "queueTask").andReturn Promise.resolve()
it "should ignore the update unless it applies to the current draft", ->
spyOn(@session, 'trigger')
@session._onDraftChanged(objectClass: 'message', objects: [new Message()])
expect(@session.trigger).not.toHaveBeenCalled()
@session._onDraftChanged(objectClass: 'message', objects: [@draft])
expect(@session.trigger).toHaveBeenCalled()
it "should apply the update to the current draft", ->
updatedDraft = @draft.clone()
updatedDraft.subject = 'This is the new subject'
spyOn(@session, '_setDraft')
@session._onDraftChanged(objectClass: 'message', objects: [updatedDraft])
expect(@session._setDraft).toHaveBeenCalled()
draft = @session._setDraft.calls[0].args[0]
expect(draft.subject).toEqual(updatedDraft.subject)
it "atomically commits changes", ->
spyOn(DatabaseStore, "run").andReturn(Promise.resolve(@draft))
spyOn(DatabaseStore, 'inTransaction').andCallThrough()
@session.changes.add({body: "123"})
waitsForPromise =>
@session.changes.commit().then =>
expect(DatabaseStore.inTransaction).toHaveBeenCalled()
expect(DatabaseStore.inTransaction.calls.length).toBe 1
it "persist the applied changes", ->
spyOn(DatabaseStore, "run").andReturn(Promise.resolve(@draft))
@session.changes.add({body: "123"})
waitsForPromise =>
@session.changes.commit().then =>
expect(DatabaseWriter.prototype.persistModel).toHaveBeenCalled()
updated = DatabaseWriter.prototype.persistModel.calls[0].args[0]
expect(updated.body).toBe "123"
it "doesn't queues a SyncbackDraftTask if no Syncback is passed", ->
spyOn(DatabaseStore, "run").andReturn(Promise.resolve(@draft))
waitsForPromise =>
@session.changes.commit({noSyncback: true}).then =>
expect(Actions.queueTask).not.toHaveBeenCalled()
describe "when findBy does not return a draft", ->
it "continues and persists it's local draft reference, so it is resaved and draft editing can continue", ->
spyOn(DatabaseStore, "run").andReturn(Promise.resolve(null))
@session.changes.add({body: "123"})
waitsForPromise =>
@session.changes.commit().then =>
expect(DatabaseWriter.prototype.persistModel).toHaveBeenCalled()
updated = DatabaseWriter.prototype.persistModel.calls[0].args[0]
expect(updated.body).toBe "123"
it "does nothing if the draft is marked as destroyed", ->
spyOn(DatabaseStore, "run").andReturn(Promise.resolve(@draft))
spyOn(DatabaseStore, 'inTransaction').andCallThrough()
waitsForPromise =>
@session._destroyed = true
@session.changes.add({body: "123"})
@session.changes.commit().then =>
expect(DatabaseStore.inTransaction).not.toHaveBeenCalled()
describe "draft pristine body", ->
describe "when the draft given to the session is pristine", ->
it "should return the initial body", ->
2017-06-22 04:12:49 +08:00
pristineDraft = new Message(draft: true, body: 'Hiya', pristine: true, id: 'client-id')
updatedDraft = pristineDraft.clone()
updatedDraft.body = '123444'
updatedDraft.pristine = false
@session = new DraftEditingSession('client-id', pristineDraft)
waitsForPromise =>
@session._draftPromise.then =>
@session._onDraftChanged(objectClass: 'message', objects: [updatedDraft])
expect(@session.draftPristineBody()).toBe('Hiya')
describe "when the draft given to the session is not pristine", ->
it "should return null", ->
dirtyDraft = new Message(draft: true, body: 'Hiya', pristine: false)
@session = new DraftEditingSession('client-id', dirtyDraft)
expect(@session.draftPristineBody()).toBe(null)