Skip to content

Commit

Permalink
Merge pull request #3 from Kcazer/make-onrelease-a-settable-attribute
Browse files Browse the repository at this point in the history
feat: add the onrelease attribute
  • Loading branch information
jorisre authored Nov 16, 2020
2 parents 925d0e3 + d781bbd commit 6dff528
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 12 deletions.
11 changes: 8 additions & 3 deletions src/WakeLock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ type ReleaseEventName = typeof RELEASE_EVENT_NAME;
export class WakeLockSentinel {
#released: boolean;
#type: WakeLockType;
#onrelease: EventListener | undefined;
#releaseEvent: Event;
#wakeLockEventTarget: EventTarget;

constructor(type: WakeLockType) {
this.#released = false;
this.#type = type;
this.#onrelease = undefined;
this.#releaseEvent = new Event(RELEASE_EVENT_NAME);
this.#wakeLockEventTarget = new EventTarget();
}
Expand All @@ -30,11 +32,14 @@ export class WakeLockSentinel {

async release() {
this.#released = true;
this.onrelease(this.#releaseEvent);
this.#wakeLockEventTarget.dispatchEvent(this.#releaseEvent);
}

onrelease(_event: Event) {}
set onrelease(listener: EventListener) {
if (this.#onrelease) this.removeEventListener('release', this.#onrelease);
this.addEventListener('release', listener);
this.#onrelease = listener;
}

get released() {
return this.#released;
Expand All @@ -43,4 +48,4 @@ export class WakeLockSentinel {
get type() {
return this.#type;
}
}
}
35 changes: 26 additions & 9 deletions test/jest-wake-lock-mock.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ test('wakeLock handles request then release with success', async () => {
expect(handleRelease).not.toHaveBeenCalled();

if (wakeLock) {
const onReleaseSpy = jest.spyOn(wakeLock, 'onrelease');

expect(wakeLock?.type).toEqual('screen');
expect(wakeLock?.released).toBe(false);

Expand All @@ -30,7 +28,6 @@ test('wakeLock handles request then release with success', async () => {
expect(wakeLock?.type).toEqual('screen');
expect(wakeLock?.released).toBe(true);
expect(handleRelease).toHaveBeenCalledWith(expect.any(Event));
expect(onReleaseSpy).toHaveBeenCalledWith(expect.any(Event));

wakeLock?.removeEventListener('release', handleRelease);
expect(handleRelease).toHaveBeenLastCalledWith(expect.any(Event));
Expand Down Expand Up @@ -59,8 +56,6 @@ test('wakeLock handles request then release multiple times', async () => {
expect(firstHandleRelease).not.toHaveBeenCalled();

if (wakeLock) {
const onReleaseSpy = jest.spyOn(wakeLock, 'onrelease');

expect(wakeLock?.type).toEqual('screen');
expect(wakeLock?.released).toBe(false);

Expand All @@ -69,7 +64,6 @@ test('wakeLock handles request then release multiple times', async () => {
expect(wakeLock?.type).toEqual('screen');
expect(wakeLock?.released).toBe(true);
expect(firstHandleRelease).toHaveBeenCalledWith(expect.any(Event));
expect(onReleaseSpy).toHaveBeenCalledWith(expect.any(Event));

wakeLock?.removeEventListener('release', firstHandleRelease);
expect(firstHandleRelease).toHaveBeenLastCalledWith(expect.any(Event));
Expand All @@ -86,8 +80,6 @@ test('wakeLock handles request then release multiple times', async () => {
expect(ndHandleRelease).not.toHaveBeenCalled();

if (ndWakeLock) {
const onReleaseSpy = jest.spyOn(ndWakeLock, 'onrelease');

expect(ndWakeLock?.type).toEqual('screen');
expect(ndWakeLock?.released).toBe(false);

Expand All @@ -96,9 +88,34 @@ test('wakeLock handles request then release multiple times', async () => {
expect(ndWakeLock?.type).toEqual('screen');
expect(ndWakeLock?.released).toBe(true);
expect(ndHandleRelease).toHaveBeenCalledWith(expect.any(Event));
expect(onReleaseSpy).toHaveBeenCalledWith(expect.any(Event));

ndWakeLock?.removeEventListener('release', ndHandleRelease);
expect(ndHandleRelease).toHaveBeenLastCalledWith(expect.any(Event));
}
});

test('wakeLock calls only the last defined "onrelease" attribute', async () => {
const handleRelease = jest.fn();
const onReleaseAttributeOne = jest.fn();
const onReleaseAttributeTwo = jest.fn();
const onReleaseAttributeThree = jest.fn();
const { wakeLock, error } = await requestWakeLock(handleRelease);

expect(error).not.toBeDefined();
expect(wakeLock).toBeDefined();
expect(handleRelease).not.toHaveBeenCalled();

if (wakeLock) {
// Assign the onrelease attribute several times
wakeLock.onrelease = onReleaseAttributeOne;
wakeLock.onrelease = onReleaseAttributeTwo;
wakeLock.onrelease = onReleaseAttributeThree;

// Trigger wakelock release
await wakeLock.release();
expect(handleRelease).toHaveBeenCalledWith(expect.any(Event));
expect(onReleaseAttributeOne).not.toHaveBeenCalled();
expect(onReleaseAttributeTwo).not.toHaveBeenCalled();
expect(onReleaseAttributeThree).toHaveBeenCalledWith(expect.any(Event));
}
})

0 comments on commit 6dff528

Please sign in to comment.