-
Notifications
You must be signed in to change notification settings - Fork 184
Expand file tree
/
Copy pathpostgresql.attachErrorHandler.test.js
More file actions
42 lines (37 loc) · 1.44 KB
/
postgresql.attachErrorHandler.test.js
File metadata and controls
42 lines (37 loc) · 1.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
// Copyright IBM Corp. 2013,2025. All Rights Reserved.
// Node module: loopback-connector-postgresql
// This file is licensed under the Artistic License 2.0.
// License text available at https://opensource.org/licenses/Artistic-2.0
'use strict';
const sinon = require('sinon');
const assert = require('assert');
const rewire = require('rewire');
const postgresqlModule = rewire('../lib/postgresql');
const attachErrorHandler = postgresqlModule.__get__('attachErrorHandler');
describe('attachErrorHandler', function() {
let pg;
beforeEach(function() {
pg = {
on: sinon.spy(),
listenerCount: sinon.stub().returns(0),
};
});
it('should attach custom handler if onError is a function', function() {
const handler = sinon.spy();
const settings = {onError: handler};
attachErrorHandler(settings, pg);
assert(pg.on.calledOnce, 'pg.on should be called once');
assert(pg.on.firstCall.args[1] === handler, 'should attach the custom handler');
});
it('should not attach handler if already attached', function() {
pg.listenerCount.returns(1);
const settings = {onError: 'ignore'};
attachErrorHandler(settings, pg);
assert(pg.on.notCalled, 'pg.on should not be called if already attached');
});
it('should do nothing if onError is not set', function() {
const settings = {};
attachErrorHandler(settings, pg);
assert(pg.on.notCalled, 'pg.on should not be called if onError is not set');
});
});