Skip to content

Commit

Permalink
chore(event): improve event coverage and fix typings
Browse files Browse the repository at this point in the history
  • Loading branch information
pikax committed Mar 29, 2020
1 parent 319a81f commit 1acdbad
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 8 deletions.
6 changes: 4 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

<!--
## [Unreleased]

---
-->

## Changed

- [event][https://pikax.me/vue-composable/composable/event/event.html] - improve typing

## 1.0.0-dev.16

Expand Down
36 changes: 35 additions & 1 deletion packages/web/__tests__/event/event.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Ref, ref } from "@vue/composition-api";
import { Vue } from "../utils";
import { Vue, nextTick } from "../utils";
import { useEvent } from "../../src";
import { NO_OP } from "@vue-composable/core";

Expand Down Expand Up @@ -140,4 +140,38 @@ describe("event", () => {
it("should not throw if undefined passed", () => {
expect(useEvent(undefined as any, "load", NO_OP)).toBe(NO_OP);
});

it("should remove event listener if ref changes", async () => {
const element: Element = {
addEventListener: jest.fn(),
removeEventListener: jest.fn()
} as any;
const mockHandler = jest.fn();
const options = {};

const el = ref(element);

new Vue({
template: "<div></div>",
setup() {
useEvent(el, "load", mockHandler, options);
}
}).$mount();
expect(element.removeEventListener).not.toHaveBeenCalled();
expect(element.addEventListener).toHaveBeenCalled();

el.value = {
...element
};

await nextTick();

expect(element.removeEventListener).toHaveBeenCalled();
expect(element.addEventListener).toHaveBeenCalledTimes(2);

el.value = null as any;

await nextTick();
expect(element.removeEventListener).toHaveBeenCalledTimes(2);
});
});
8 changes: 4 additions & 4 deletions packages/web/src/event/event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export function useEvent<
M,
K extends keyof M
>(
el: RefTyped<T>,
el: T | Ref<T | undefined>,
name: K,
listener: (this: T, ev: M[K]) => any
): RemoveEventFunction;
Expand All @@ -30,7 +30,7 @@ export function useEvent<
M,
K extends keyof M
>(
el: RefTyped<T>,
el: T | Ref<T | undefined>,
name: K,
listener: (this: T, ev: M[K]) => any,
options?: boolean | AddEventListenerOptions
Expand All @@ -42,13 +42,13 @@ export function useEvent<K extends keyof WindowEventMap>(
options?: boolean | AddEventListenerOptions
): RemoveEventFunction;
export function useEvent<K extends keyof DocumentEventMap>(
el: RefTyped<Element>,
el: Element | Ref<Element | undefined>,
name: K,
listener: (this: Document, ev: DocumentEventMap[K]) => any,
options?: boolean | AddEventListenerOptions
): RemoveEventFunction;
export function useEvent(
el: RefTyped<Element> | RefTyped<Window> | RefTyped<any>,
el: Element | Ref<Element | undefined> | RefTyped<Window> | RefTyped<any>,
name: string,
listener: EventListenerOrEventListenerObject,
options?: boolean | AddEventListenerOptions
Expand Down
2 changes: 1 addition & 1 deletion packages/web/src/event/onResize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export function useOnResize(
// resize seems only to be fired against the window
const remove = isClient
? useEvent(window, "resize", handler, eventOptions || PASSIVE_EV)
: NO_OP;
: /* istanbul ignore next */ NO_OP;

return {
height,
Expand Down

0 comments on commit 1acdbad

Please sign in to comment.