summaryrefslogtreecommitdiff
path: root/preact/compat/test/browser/compat.options.test.js
diff options
context:
space:
mode:
Diffstat (limited to 'preact/compat/test/browser/compat.options.test.js')
-rw-r--r--preact/compat/test/browser/compat.options.test.js85
1 files changed, 85 insertions, 0 deletions
diff --git a/preact/compat/test/browser/compat.options.test.js b/preact/compat/test/browser/compat.options.test.js
new file mode 100644
index 0000000..6c52278
--- /dev/null
+++ b/preact/compat/test/browser/compat.options.test.js
@@ -0,0 +1,85 @@
+import { vnodeSpy, eventSpy } from '../../../test/_util/optionSpies';
+import React, {
+ createElement,
+ render,
+ Component,
+ createRef
+} from 'preact/compat';
+import { setupRerender } from 'preact/test-utils';
+import {
+ setupScratch,
+ teardown,
+ createEvent
+} from '../../../test/_util/helpers';
+
+describe('compat options', () => {
+ /** @type {HTMLDivElement} */
+ let scratch;
+
+ /** @type {() => void} */
+ let rerender;
+
+ /** @type {() => void} */
+ let increment;
+
+ /** @type {import('../../src/index').PropRef<HTMLButtonElement | null>} */
+ let buttonRef;
+
+ beforeEach(() => {
+ scratch = setupScratch();
+ rerender = setupRerender();
+
+ vnodeSpy.resetHistory();
+ eventSpy.resetHistory();
+
+ buttonRef = createRef();
+ });
+
+ afterEach(() => {
+ teardown(scratch);
+ });
+
+ class ClassApp extends Component {
+ constructor() {
+ super();
+ this.state = { count: 0 };
+ increment = () =>
+ this.setState(({ count }) => ({
+ count: count + 1
+ }));
+ }
+
+ render() {
+ return (
+ <button ref={buttonRef} onClick={increment}>
+ {this.state.count}
+ </button>
+ );
+ }
+ }
+
+ it('should call old options on mount', () => {
+ render(<ClassApp />, scratch);
+
+ expect(vnodeSpy).to.have.been.called;
+ });
+
+ it('should call old options on event and update', () => {
+ render(<ClassApp />, scratch);
+ expect(scratch.innerHTML).to.equal('<button>0</button>');
+
+ buttonRef.current.dispatchEvent(createEvent('click'));
+ rerender();
+ expect(scratch.innerHTML).to.equal('<button>1</button>');
+
+ expect(vnodeSpy).to.have.been.called;
+ expect(eventSpy).to.have.been.called;
+ });
+
+ it('should call old options on unmount', () => {
+ render(<ClassApp />, scratch);
+ render(null, scratch);
+
+ expect(vnodeSpy).to.have.been.called;
+ });
+});