Skip to content
This repository has been archived by the owner on May 19, 2023. It is now read-only.

Commit

Permalink
test: browser.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
aidenybai committed Feb 5, 2021
1 parent 17c6699 commit fb65ca9
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 4 deletions.
5 changes: 1 addition & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,6 @@
"jest": {
"transform": {
".(ts)": "ts-jest"
},
"collectCoverageFrom": [
"src/**/{!(index|browser|structs|collectAndInitDirectives|reactive),}.ts"
]
}
}
}
74 changes: 74 additions & 0 deletions src/__test__/browser.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import _browser from '../browser';
import { getCustomProp } from '../core/utils/customProp';

// Reset to clean, uninitialized template
const reset = () => {
document.body.innerHTML = `
<div l-state="{ foo: 'bar' }">
<p l-text="foo"></p>
</div>
`;
};

// Does overall checks on whether it is a component or not
const validateComponentFunctionality = (error = false) => {
const componentElement = document.querySelector('body > div') as HTMLElement;
const componentText = componentElement.querySelector('p') as HTMLElement;
const component = getCustomProp(componentElement, '__l');

if (error) {
expect(component).toBeUndefined();
} else {
expect(component).toBeDefined();
expect(component.state).toEqual({ foo: 'bar' });
expect(componentText.innerHTML).toEqual('bar');
}
};

reset();

describe('.browser', () => {
it('should initialize component on document load', () => {
document.addEventListener('DOMContentLoaded', () => {
validateComponentFunctionality();
});
});

it('should initialize for turbolinks:load and turbo:load events', () => {
reset();
const turbolinksLoad = new CustomEvent('turbolinks:load');
document.dispatchEvent(turbolinksLoad);
setTimeout(() => validateComponentFunctionality, 0);

reset();
const turboDriveLoad = new CustomEvent('turbo:load');
document.dispatchEvent(turboDriveLoad);
setTimeout(() => validateComponentFunctionality, 0);
});

it('should initialize for turbolinks:load and turbo:load events', () => {
reset();
const turbolinksLoad = new CustomEvent('turbolinks:load');
document.dispatchEvent(turbolinksLoad);
setTimeout(() => validateComponentFunctionality, 0);

reset();
const turboDriveLoad = new CustomEvent('turbo:load');
document.dispatchEvent(turboDriveLoad);
setTimeout(() => validateComponentFunctionality, 0);
});

it('should intercept `start()`', () => {
const customLoad = new CustomEvent('customLoad');

// @ts-ignore
window.__l = (start: Function) => {
document.addEventListener('customLoad', () => {
start();
});
};
validateComponentFunctionality(true);
document.dispatchEvent(customLoad);
setTimeout(() => validateComponentFunctionality, 0);
});
});

0 comments on commit fb65ca9

Please # to comment.