Please find below code, i need to trigger load event so that my test function get execute
const test = () => {
return true;
};
window.addEventListener('load', () => {
test();
});
Make sure the test environment is jsdom for jest, then you can trigger the load event by using dispatchEvent
E.g.
window.addEventListener('load', () => {
console.log('load event triggered');
});
describe('71220140', () => {
test('should pass', () => {
window.dispatchEvent(new Event('load'));
});
});
Test result:
PASS stackoverflow/71220140/index.test.ts
71220140
✓ should pass (13 ms)
console.log
load event triggered
at stackoverflow/71220140/index.test.ts:2:11
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: 2.301 s