2015-12-18 03:46:05 +08:00
|
|
|
{DestroyCategoryTask,
|
|
|
|
NylasAPI,
|
2016-11-30 08:32:23 +08:00
|
|
|
NylasAPIRequest,
|
2015-12-18 03:46:05 +08:00
|
|
|
Task,
|
2016-01-23 06:52:19 +08:00
|
|
|
Category,
|
|
|
|
AccountStore,
|
2015-12-18 03:46:05 +08:00
|
|
|
APIError,
|
2016-01-18 16:47:04 +08:00
|
|
|
Category,
|
2015-12-18 03:46:05 +08:00
|
|
|
DatabaseStore,
|
|
|
|
DatabaseTransaction} = require "nylas-exports"
|
2015-11-24 11:41:53 +08:00
|
|
|
|
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 "DestroyCategoryTask", ->
|
2015-11-24 11:41:53 +08:00
|
|
|
pathOf = (fn) ->
|
|
|
|
fn.calls[0].args[0].path
|
|
|
|
|
|
|
|
methodOf = (fn) ->
|
|
|
|
fn.calls[0].args[0].method
|
|
|
|
|
|
|
|
accountIdOf = (fn) ->
|
|
|
|
fn.calls[0].args[0].accountId
|
|
|
|
|
|
|
|
nameOf = (fn) ->
|
|
|
|
fn.calls[0].args[0].body.display_name
|
|
|
|
|
2016-01-23 06:52:19 +08:00
|
|
|
makeAccount = ({usesFolders, usesLabels} = {}) ->
|
|
|
|
spyOn(AccountStore, "accountForId").andReturn {
|
|
|
|
usesFolders: -> usesFolders
|
|
|
|
usesLabels: -> usesLabels
|
|
|
|
}
|
2016-01-18 16:47:04 +08:00
|
|
|
makeTask = ->
|
|
|
|
category = new Category
|
2015-11-24 11:41:53 +08:00
|
|
|
displayName: "important emails"
|
|
|
|
accountId: "account 123"
|
|
|
|
serverId: "server-444"
|
|
|
|
new DestroyCategoryTask
|
|
|
|
category: category
|
|
|
|
|
2015-12-18 03:46:05 +08:00
|
|
|
beforeEach ->
|
2016-07-29 03:10:40 +08:00
|
|
|
spyOn(DatabaseTransaction.prototype, 'unpersistModel').andCallFake -> Promise.resolve()
|
2015-12-18 03:46:05 +08:00
|
|
|
spyOn(DatabaseTransaction.prototype, 'persistModel').andCallFake -> Promise.resolve()
|
2015-11-24 11:41:53 +08:00
|
|
|
|
2015-12-18 03:46:05 +08:00
|
|
|
describe "performLocal", ->
|
|
|
|
it "sets an `isDeleted` flag and persists the category", ->
|
2016-01-18 16:47:04 +08:00
|
|
|
task = makeTask()
|
2015-12-18 03:46:05 +08:00
|
|
|
runs =>
|
|
|
|
task.performLocal()
|
|
|
|
waitsFor =>
|
2016-07-29 03:10:40 +08:00
|
|
|
DatabaseTransaction.prototype.unpersistModel.callCount > 0
|
2015-12-18 03:46:05 +08:00
|
|
|
runs =>
|
2016-07-29 03:10:40 +08:00
|
|
|
model = DatabaseTransaction.prototype.unpersistModel.calls[0].args[0]
|
2015-12-18 03:46:05 +08:00
|
|
|
expect(model.serverId).toEqual "server-444"
|
2015-11-24 11:41:53 +08:00
|
|
|
|
|
|
|
describe "performRemote", ->
|
|
|
|
it "throws error when no category present", ->
|
|
|
|
waitsForPromise ->
|
2016-01-18 16:47:04 +08:00
|
|
|
task = makeTask()
|
2015-11-24 11:41:53 +08:00
|
|
|
task.category = null
|
|
|
|
task.performRemote()
|
|
|
|
.then ->
|
|
|
|
throw new Error('The promise should reject')
|
|
|
|
.catch Error, (err) ->
|
|
|
|
expect(err).toBeDefined()
|
|
|
|
|
|
|
|
it "throws error when category does not have a serverId", ->
|
|
|
|
waitsForPromise ->
|
2016-01-18 16:47:04 +08:00
|
|
|
task = makeTask()
|
2015-11-24 11:41:53 +08:00
|
|
|
task.category.serverId = undefined
|
|
|
|
task.performRemote()
|
|
|
|
.then ->
|
|
|
|
throw new Error('The promise should reject')
|
|
|
|
.catch Error, (err) ->
|
|
|
|
expect(err).toBeDefined()
|
|
|
|
|
|
|
|
describe "when request succeeds", ->
|
|
|
|
beforeEach ->
|
2016-11-30 08:32:23 +08:00
|
|
|
spyOn(NylasAPIRequest.prototype, "run").andCallFake -> Promise.resolve("null")
|
2016-07-29 03:10:40 +08:00
|
|
|
spyOn(NylasAPI, "incrementRemoteChangeLock")
|
2015-11-24 11:41:53 +08:00
|
|
|
|
2016-07-29 03:10:40 +08:00
|
|
|
it "blocks other remote changes to that category", ->
|
|
|
|
makeAccount()
|
|
|
|
task = makeTask()
|
|
|
|
task.performRemote()
|
|
|
|
expect(NylasAPI.incrementRemoteChangeLock).toHaveBeenCalled()
|
2015-11-24 11:41:53 +08:00
|
|
|
it "sends API req to /labels if user uses labels", ->
|
2016-01-23 06:52:19 +08:00
|
|
|
makeAccount(usesLabels: true)
|
2016-01-18 16:47:04 +08:00
|
|
|
task = makeTask()
|
2015-11-24 11:41:53 +08:00
|
|
|
task.performRemote()
|
2016-11-30 08:32:23 +08:00
|
|
|
expect(pathOf(NylasAPIRequest.prototype.run)).toBe "/labels/server-444"
|
2015-11-24 11:41:53 +08:00
|
|
|
|
|
|
|
it "sends API req to /folders if user uses folders", ->
|
2016-01-23 06:52:19 +08:00
|
|
|
makeAccount(usesFolders: true)
|
2016-01-18 16:47:04 +08:00
|
|
|
task = makeTask()
|
2015-11-24 11:41:53 +08:00
|
|
|
task.performRemote()
|
2016-11-30 08:32:23 +08:00
|
|
|
expect(pathOf(NylasAPIRequest.prototype.run)).toBe "/folders/server-444"
|
2015-11-24 11:41:53 +08:00
|
|
|
|
|
|
|
it "sends DELETE request", ->
|
2016-01-23 06:52:19 +08:00
|
|
|
makeAccount()
|
2016-01-18 16:47:04 +08:00
|
|
|
task = makeTask()
|
2015-11-24 11:41:53 +08:00
|
|
|
task.performRemote()
|
2016-11-30 08:32:23 +08:00
|
|
|
expect(methodOf(NylasAPIRequest.prototype.run)).toBe "DELETE"
|
2015-11-24 11:41:53 +08:00
|
|
|
|
|
|
|
it "sends the account id", ->
|
2016-01-23 06:52:19 +08:00
|
|
|
makeAccount()
|
2016-01-18 16:47:04 +08:00
|
|
|
task = makeTask()
|
2015-11-24 11:41:53 +08:00
|
|
|
task.performRemote()
|
2016-11-30 08:32:23 +08:00
|
|
|
expect(accountIdOf(NylasAPIRequest.prototype.run)).toBe "account 123"
|
2015-11-24 11:41:53 +08:00
|
|
|
|
|
|
|
describe "when request fails", ->
|
|
|
|
beforeEach ->
|
2016-01-23 06:52:19 +08:00
|
|
|
makeAccount()
|
2016-07-29 03:10:40 +08:00
|
|
|
spyOn(NylasAPI, 'decrementRemoteChangeLock')
|
2016-02-04 07:06:52 +08:00
|
|
|
spyOn(NylasEnv, 'reportError')
|
2016-11-30 08:32:23 +08:00
|
|
|
spyOn(NylasAPIRequest.prototype, 'run').andCallFake ->
|
2015-11-24 11:41:53 +08:00
|
|
|
Promise.reject(new APIError({statusCode: 403}))
|
|
|
|
|
2016-07-29 03:10:40 +08:00
|
|
|
it "persists the category and notifies error", ->
|
2015-11-24 11:41:53 +08:00
|
|
|
waitsForPromise ->
|
2016-01-18 16:47:04 +08:00
|
|
|
task = makeTask()
|
2015-11-24 11:41:53 +08:00
|
|
|
spyOn(task, "_notifyUserOfError")
|
|
|
|
|
|
|
|
task.performRemote().then (status) ->
|
|
|
|
expect(status).toEqual Task.Status.Failed
|
|
|
|
expect(task._notifyUserOfError).toHaveBeenCalled()
|
2016-02-04 07:06:52 +08:00
|
|
|
expect(NylasEnv.reportError).toHaveBeenCalled()
|
2015-12-18 03:46:05 +08:00
|
|
|
expect(DatabaseTransaction.prototype.persistModel).toHaveBeenCalled()
|
|
|
|
model = DatabaseTransaction.prototype.persistModel.calls[0].args[0]
|
2015-11-24 11:41:53 +08:00
|
|
|
expect(model.serverId).toEqual "server-444"
|
2016-07-29 03:10:40 +08:00
|
|
|
expect(NylasAPI.decrementRemoteChangeLock).toHaveBeenCalled
|
2016-02-09 09:40:37 +08:00
|
|
|
|
|
|
|
describe "_notifyUserOfError", ->
|
|
|
|
it "should present an error dialog", ->
|
|
|
|
spyOn(NylasEnv, 'showErrorDialog')
|
|
|
|
task = makeTask()
|
2016-03-22 07:50:10 +08:00
|
|
|
task._notifyUserOfError(task.category)
|
2016-02-09 09:40:37 +08:00
|
|
|
expect(NylasEnv.showErrorDialog).toHaveBeenCalled()
|