Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions script.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
describe('generateQR function', () => {
let inputElement;
let qrCodeDiv;
let generateButton;
let consoleSpy;

beforeEach(() => {
document.body.innerHTML = `
<input id="text" type="text" />
<div id="qrcode"></div>
<button id="generateButton">Generate QR</button>
`;
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();
});
});