| 1 |
describe('logger unit tests', function() { |
| 2 |
|
| 3 |
let savedLogAll; |
| 4 |
let captured; |
| 5 |
let consoleStubs; |
| 6 |
|
| 7 |
beforeEach(() => { |
| 8 |
savedLogAll = htmx.config.logAll; |
| 9 |
captured = []; |
| 10 |
consoleStubs = { |
| 11 |
log: console.log, |
| 12 |
warn: console.warn, |
| 13 |
error: console.error, |
| 14 |
}; |
| 15 |
console.log = (...args) => captured.push(['log', ...args]); |
| 16 |
console.warn = (...args) => captured.push(['warn', ...args]); |
| 17 |
console.error = (...args) => captured.push(['error', ...args]); |
| 18 |
}); |
| 19 |
|
| 20 |
afterEach(() => { |
| 21 |
htmx.config.logAll = savedLogAll; |
| 22 |
Object.assign(console, consoleStubs); |
| 23 |
}); |
| 24 |
|
| 25 |
it('triggering event with detail.error logs at error level with the Error inlined for devtools', function() { |
| 26 |
htmx.config.logAll = false; |
| 27 |
let div = document.createElement('div'); |
| 28 |
document.body.appendChild(div); |
| 29 |
try { |
| 30 |
let err = new Error('explode'); |
| 31 |
htmx.__trigger(div, 'htmx:custom', { error: err }); |
| 32 |
let errs = captured.filter(c => c[0] === 'error'); |
| 33 |
assert.equal(errs.length, 1); |
| 34 |
assert.include(errs[0][1], 'htmx:custom'); |
| 35 |
assert.include(errs[0][1], 'explode'); |
| 36 |
|
| 37 |
assert.equal(errs[0][2], err); |
| 38 |
} finally { |
| 39 |
div.remove(); |
| 40 |
} |
| 41 |
}); |
| 42 |
|
| 43 |
it('triggering event with detail.warn logs at warn level', function() { |
| 44 |
htmx.config.logAll = false; |
| 45 |
let div = document.createElement('div'); |
| 46 |
document.body.appendChild(div); |
| 47 |
try { |
| 48 |
htmx.__trigger(div, 'htmx:custom', { warn: 'odd thing happened' }); |
| 49 |
let warns = captured.filter(c => c[0] === 'warn'); |
| 50 |
assert.equal(warns.length, 1); |
| 51 |
assert.include(warns[0][1], 'htmx:custom'); |
| 52 |
assert.include(warns[0][1], 'odd thing happened'); |
| 53 |
} finally { |
| 54 |
div.remove(); |
| 55 |
} |
| 56 |
}); |
| 57 |
|
| 58 |
it('triggering normal event does not log unless logAll is set', function() { |
| 59 |
htmx.config.logAll = false; |
| 60 |
let div = document.createElement('div'); |
| 61 |
document.body.appendChild(div); |
| 62 |
try { |
| 63 |
htmx.__trigger(div, 'htmx:custom', { foo: 1 }); |
| 64 |
assert.equal(captured.length, 0); |
| 65 |
} finally { |
| 66 |
div.remove(); |
| 67 |
} |
| 68 |
}); |
| 69 |
|
| 70 |
it('triggering normal event logs at log level when logAll is set', function() { |
| 71 |
htmx.config.logAll = true; |
| 72 |
let div = document.createElement('div'); |
| 73 |
document.body.appendChild(div); |
| 74 |
try { |
| 75 |
htmx.__trigger(div, 'htmx:custom', { foo: 1 }); |
| 76 |
let logs = captured.filter(c => c[0] === 'log'); |
| 77 |
assert.equal(logs.length, 1); |
| 78 |
assert.include(logs[0][1], 'htmx:custom'); |
| 79 |
} finally { |
| 80 |
div.remove(); |
| 81 |
} |
| 82 |
}); |
| 83 |
|
| 84 |
}); |