diff --git a/script.test.js b/script.test.js new file mode 100644 index 0000000..1f9207d --- /dev/null +++ b/script.test.js @@ -0,0 +1,46 @@ +describe('generateQR function', () => { + let inputElement; + let qrCodeDiv; + let generateButton; + let consoleSpy; + + beforeEach(() => { + document.body.innerHTML = ` + +
+ + `; + inputElement = document.getElementById("text"); + qrCodeDiv = document.getElementById("qrcode"); + generateButton = document.getElementById("generateButton"); + consoleSpy = jest.spyOn(console, 'error'); + }); + + afterEach(() => { + consoleSpy.mockRestore(); + }); + + it('should throw an error when input is null', () => { + inputElement.value = null; + expect(() => generateQR()).toThrowError('Input must be a string'); + expect(consoleSpy).not.toHaveBeenCalled(); + }); + + it('should throw an error when input is an empty string', () => { + inputElement.value = ""; + expect(() => generateQR()).toThrowError('Input must be a string'); + expect(consoleSpy).not.toHaveBeenCalled(); + }); + + it('should throw an error when input is an empty element', () => { + inputElement.value = ""; + expect(() => generateQR()).toThrowError('Input must be a string'); + expect(consoleSpy).not.toHaveBeenCalled(); + }); + + it('should handle input validation correctly', () => { + inputElement.value = "Hello, world!"; + generateButton.click(); + expect(consoleSpy).not.toHaveBeenCalled(); + }); +}); \ No newline at end of file