Mailspring/spec/time-override.coffee

101 lines
2.8 KiB
CoffeeScript
Raw Normal View History

feat(tests): add integration tests comment Adding test harness Using key strokes in main window test Tests work now Clean up argument variables Rename list manager and get rid of old spec-helper methods Extract out time overrides from spec-helper Spectron test for contenteditable fix spec exit codes and boot mode fix(spec): cleanup N1.sh and make specs fail with exit code 1 Revert tests and get it working in window Move to spec_integration and add window load tester Specs pass. Console logs still in Remove console logs Extract N1 Launcher ready method Make integrated unit test runner feat(tests): adding integration tests Summary: The /spectron folder got moved to /spec_integration There are now unit tests (the old ones) run via the renamed `script/grunt run-unit-tests` There are now integration tests run via the command `script/grunt run-integration-tests`. There are two types of integration tests: 1. Tests that operate on the whole app via Selenium/Chromedriver. These tests have access to Spectron APIs but do NOT have access to any JS object running inside the application. See the `app-boot-spec.es6` for an example of these tests. This is tricky because we want to test the Main window, but Spectron may latch onto any other of our loading windows. Code in `integration-helper` give us an API that finds and loads the main window so we can test it 2. Tests that run in the unit test suite that need Spectron to perform integration-like behavior. These are the contentedtiable specs. The Spectron server is accessed from the app and can be used to trigger actions on the running app, from the app. These tests use the windowed-test runner so Spectron can identify whether the tests have completed, passed, or failed. Unfortunately Spectron can't access the logs , nor the exit code of the test script thereby forcing us to parse the HTML DOM. (Note this is still a WIP) I also revamped the `N1.sh` file when getting the launch arguments to work properly. It's much cleaner. We didn't need most of the data. Test Plan: new tests Reviewers: juan, bengotow Differential Revision: https://phab.nylas.com/D2289 Fix composer specs Tests can properly detect when Spectron is in the environment Report plain text output in specs fixing contenteditable specs Testing slow keymaps on contenteditable specs Move to DOm mutation Spell as `subtree` not `subTree`
2015-11-20 07:29:49 +08:00
_ = require 'underscore'
# Public: To make specs easier to test, we make all asynchronous behavior
# actually synchronous. We do this by overriding all global timeout and
# Promise functions.
#
# You must now manually call `advanceClock()` in order to move the "clock"
# forward.
class TimeOverride
@advanceClock = (delta=1) =>
@now += delta
callbacks = []
@timeouts ?= []
@timeouts = @timeouts.filter ([id, strikeTime, callback]) =>
if strikeTime <= @now
callbacks.push(callback)
false
else
true
callback() for callback in callbacks
@resetTime = =>
@now = 0
@timeoutCount = 0
@intervalCount = 0
@timeouts = []
@intervalTimeouts = {}
@originalPromiseScheduler = null
@enableSpies = =>
window.advanceClock = @advanceClock
window.originalSetInterval = window.setInterval
spyOn(window, "setTimeout").andCallFake @_fakeSetTimeout
spyOn(window, "clearTimeout").andCallFake @_fakeClearTimeout
spyOn(window, "setInterval").andCallFake @_fakeSetInterval
spyOn(window, "clearInterval").andCallFake @_fakeClearInterval
spyOn(_._, "now").andCallFake => @now
# spyOn(Date, "now").andCallFake => @now
# spyOn(Date.prototype, "getTime").andCallFake => @now
@_setPromiseScheduler()
@_setPromiseScheduler: =>
# Make Bluebird use setTimeout so that it hooks into our stubs, and you
# can advance promises using `advanceClock()`. To avoid breaking any
# specs that `dont` manually call advanceClock, call it automatically on
# the next tick.
@originalPromiseScheduler ?= Promise.setScheduler (fn) =>
setTimeout(fn, 0)
process.nextTick =>
@advanceClock(1)
@disableSpies = =>
window.advanceClock = null
jasmine.unspy(window, 'setTimeout')
jasmine.unspy(window, 'clearTimeout')
jasmine.unspy(window, 'setInterval')
jasmine.unspy(window, 'clearInterval')
jasmine.unspy(_._, "now")
Promise.setScheduler(@originalPromiseScheduler) if @originalPromiseScheduler
@originalPromiseScheduler = null
@resetSpyData = ->
window.setTimeout.reset?()
window.clearTimeout.reset?()
window.setInterval.reset?()
window.clearInterval.reset?()
Date.now.reset?()
Date.prototype.getTime.reset?()
@_fakeSetTimeout = (callback, ms) =>
id = ++@timeoutCount
@timeouts.push([id, @now + ms, callback])
id
@_fakeClearTimeout = (idToClear) =>
@timeouts ?= []
@timeouts = @timeouts.filter ([id]) -> id != idToClear
@_fakeSetInterval = (callback, ms) =>
id = ++@intervalCount
action = ->
callback()
@intervalTimeouts[id] = @_fakeSetTimeout(action, ms)
@intervalTimeouts[id] = @_fakeSetTimeout(action, ms)
id
@_fakeClearInterval = (idToClear) =>
@_fakeClearTimeout(@intervalTimeouts[idToClear])
module.exports = TimeOverride