2026-07-03 11:59:28 +02:00
|
|
|
// @vitest-environment happy-dom
|
|
|
|
|
import { describe, it, expect, vi, afterEach } from 'vitest'
|
|
|
|
|
import { generaReferto } from '../../src/referto.js'
|
|
|
|
|
import { createInitialState } from '../../src/gameState.js'
|
|
|
|
|
|
|
|
|
|
// generaReferto è l'unico punto con effetti collaterali di referto.js (window.open
|
|
|
|
|
// + stampa): qui verifichiamo che orchestri correttamente le API del browser,
|
|
|
|
|
// mockando window.open per non aprire davvero una finestra durante i test.
|
|
|
|
|
describe('generaReferto (referto.js)', () => {
|
|
|
|
|
afterEach(() => {
|
|
|
|
|
vi.restoreAllMocks()
|
|
|
|
|
})
|
|
|
|
|
|
2026-07-15 10:05:44 +02:00
|
|
|
it('apre una nuova scheda, scrive il referto e avvia la stampa dopo il caricamento', () => {
|
|
|
|
|
const listeners = {}
|
2026-07-03 11:59:28 +02:00
|
|
|
const popup = {
|
|
|
|
|
document: { write: vi.fn(), close: vi.fn() },
|
|
|
|
|
print: vi.fn(),
|
2026-07-15 10:05:44 +02:00
|
|
|
addEventListener: vi.fn((evt, cb) => { listeners[evt] = cb }),
|
2026-07-03 11:59:28 +02:00
|
|
|
}
|
|
|
|
|
const openSpy = vi.spyOn(window, 'open').mockReturnValue(popup)
|
|
|
|
|
|
|
|
|
|
const state = createInitialState()
|
|
|
|
|
state.sp.nomi = { home: 'Antoniana', guest: 'Rivali' }
|
|
|
|
|
generaReferto(state)
|
|
|
|
|
|
|
|
|
|
expect(openSpy).toHaveBeenCalledWith('', '_blank')
|
|
|
|
|
expect(popup.document.write).toHaveBeenCalledTimes(1)
|
|
|
|
|
expect(popup.document.write.mock.calls[0][0]).toContain('Antoniana')
|
|
|
|
|
expect(popup.document.close).toHaveBeenCalledTimes(1)
|
2026-07-15 10:05:44 +02:00
|
|
|
expect(popup.print).not.toHaveBeenCalled()
|
|
|
|
|
|
|
|
|
|
listeners.load()
|
2026-07-03 11:59:28 +02:00
|
|
|
expect(popup.print).toHaveBeenCalledTimes(1)
|
|
|
|
|
})
|
|
|
|
|
})
|