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
52 changes: 52 additions & 0 deletions script.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { JSDOM } from 'jsdom';
import { expect } from 'chai';
import { generateQR } from './script.js';

describe('generateQR function', () => {
let dom;

beforeEach(() => {
dom = new JSDOM `
<html>
<body>
<button id="generateButton">Generate QR Code</button>
<div id="qrcode"></div>
<input id="text" type="text">
</body>
</html>
`;
globalThis.document = dom.window.document;
globalThis.window = dom.window;
});

afterEach(() => {
delete globalThis.document;
delete globalThis.window;
});

it('should throw an error if input element is null', () => {
const inputElement = null;
expect(() => generateQR()).to.throw(TypeError, 'Cannot read properties of null');
});

it('should throw an error if input element is undefined', () => {
const inputElement = undefined;
expect(() => generateQR()).to.throw(TypeError, 'Cannot read properties of undefined');
});

it('should throw an error if input element is null when clicking the button', () => {
const inputElement = null;
document.getElementById('text').value = '';
document.getElementById('generateButton').addEventListener('click', () => generateQR());
document.getElementById('generateButton').click();
expect(console.error).to.have.been.called.with('Cannot read properties of null');
});

it('should throw an error if input element is undefined when clicking the button', () => {
const inputElement = undefined;
document.getElementById('text').value = '';
document.getElementById('generateButton').addEventListener('click', () => generateQR());
document.getElementById('generateButton').click();
expect(console.error).to.have.been.called.with('Cannot read properties of undefined');
});
});