diff --git a/script.test.js b/script.test.js new file mode 100644 index 0000000..8bef850 --- /dev/null +++ b/script.test.js @@ -0,0 +1,62 @@ +import { JSDOM } from 'jsdom'; +import { expect } from 'chai'; + +const { window } = new JSDOM(''); +globalThis.document = window.document; +globalThis.console = { error: () => {} }; + +const QRCode = function() {} + +describe('generateQR function', () => { + beforeEach(() => { + globalThis.document.body.innerHTML = ` + +
+ + `; + + globalThis.QRCode = QRCode; + }); + + it('should display an alert and prevent execution when input is empty', () => { + const button = document.getElementById('generateButton'); + button.click(); + + expect(alert).to.have.been.called; + + const inputElement = document.getElementById('text'); + inputElement.value = 'Hello World'; + inputElement.dispatchEvent(new globalThis.Event('input')); + + button.click(); + expect(document.getElementById('qrcode')).not.to.be.empty; + }); + + it('should throw an error when input is not a string', () => { + const inputElement = document.getElementById('text'); + inputElement.value = 123; + const button = document.getElementById('generateButton'); + button.click(); + + expect(globalThis.console.error).to.have.been.called; + }); + + it('should display an error message when an error occurs while generating the QR code', () => { + const inputElement = document.getElementById('text'); + inputElement.value = 'Hello World'; + const button = document.getElementById('generateButton'); + button.click(); + + globalThis.console.error = (error) => { + expect(error).to.be.an('error'); + }; + + // Simulate an error + globalThis.QRCode = function() { + throw new Error('Mocked error'); + }; + + button.click(); + expect(document.getElementById('qrcode')).not.to.be.empty; + }); +}); \ No newline at end of file