|
| 1 | +import { Controller } from "../.." |
| 2 | +import { Application } from "../../application" |
| 3 | +import { ControllerTestCase } from "../cases/controller_test_case" |
| 4 | + |
| 5 | +class MockLogger { |
| 6 | + errors: any[] = [] |
| 7 | + logs: any[] = [] |
| 8 | + warns: any[] = [] |
| 9 | + |
| 10 | + log(event: any) { |
| 11 | + this.logs.push(event) |
| 12 | + } |
| 13 | + |
| 14 | + error(event: any) { |
| 15 | + this.errors.push(event) |
| 16 | + } |
| 17 | + |
| 18 | + warn(event: any) { |
| 19 | + this.warns.push(event) |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +class ErrorWhileConnectingController extends Controller { |
| 24 | + connect() { |
| 25 | + throw new Error('bad!'); |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +class TestApplicationWithDefaultErrorBehavior extends Application { |
| 30 | +} |
| 31 | + |
| 32 | +export default class ErrorHandlerTests extends ControllerTestCase(ErrorWhileConnectingController) { |
| 33 | + controllerConstructor = ErrorWhileConnectingController |
| 34 | + |
| 35 | + async setupApplication() { |
| 36 | + const logger = new MockLogger() |
| 37 | + |
| 38 | + this.application = new TestApplicationWithDefaultErrorBehavior(this.fixtureElement, this.schema) |
| 39 | + this.application.logger = logger |
| 40 | + |
| 41 | + window.onerror = function(message, source, lineno, colno, error) { |
| 42 | + logger.log(`error from window.onerror. message = ${message}, source = ${source}, lineno = ${lineno}, colno = ${colno}`) |
| 43 | + } |
| 44 | + |
| 45 | + await super.setupApplication() |
| 46 | + } |
| 47 | + |
| 48 | + async "test errors in connect are thrown and handled by built in logger"() { |
| 49 | + const mockLogger: any = this.application.logger |
| 50 | + |
| 51 | + // when `ErrorWhileConnectingController#connect` throws, the controller's application's logger's `error` function |
| 52 | + // is called; in this case that's `MockLogger#error`. |
| 53 | + this.assert.equal(1, mockLogger.errors.length) |
| 54 | + } |
| 55 | + |
| 56 | + async "test errors in connect are thrown and handled by window.onerror"() { |
| 57 | + const mockLogger: any = this.application.logger |
| 58 | + |
| 59 | + this.assert.equal(1, mockLogger.logs.length) |
| 60 | + this.assert.equal('error from window.onerror. message = Error connecting controller, source = , lineno = 0, colno = 0', mockLogger.logs[0]) |
| 61 | + } |
| 62 | +} |
0 commit comments