Skip to content

Commit 6dff528

Browse files
authored
Merge pull request #3 from Kcazer/make-onrelease-a-settable-attribute
feat: add the onrelease attribute
2 parents 925d0e3 + d781bbd commit 6dff528

2 files changed

Lines changed: 34 additions & 12 deletions

File tree

src/WakeLock.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,14 @@ type ReleaseEventName = typeof RELEASE_EVENT_NAME;
1010
export class WakeLockSentinel {
1111
#released: boolean;
1212
#type: WakeLockType;
13+
#onrelease: EventListener | undefined;
1314
#releaseEvent: Event;
1415
#wakeLockEventTarget: EventTarget;
1516

1617
constructor(type: WakeLockType) {
1718
this.#released = false;
1819
this.#type = type;
20+
this.#onrelease = undefined;
1921
this.#releaseEvent = new Event(RELEASE_EVENT_NAME);
2022
this.#wakeLockEventTarget = new EventTarget();
2123
}
@@ -30,11 +32,14 @@ export class WakeLockSentinel {
3032

3133
async release() {
3234
this.#released = true;
33-
this.onrelease(this.#releaseEvent);
3435
this.#wakeLockEventTarget.dispatchEvent(this.#releaseEvent);
3536
}
3637

37-
onrelease(_event: Event) {}
38+
set onrelease(listener: EventListener) {
39+
if (this.#onrelease) this.removeEventListener('release', this.#onrelease);
40+
this.addEventListener('release', listener);
41+
this.#onrelease = listener;
42+
}
3843

3944
get released() {
4045
return this.#released;
@@ -43,4 +48,4 @@ export class WakeLockSentinel {
4348
get type() {
4449
return this.#type;
4550
}
46-
}
51+
}

test/jest-wake-lock-mock.test.ts

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ test('wakeLock handles request then release with success', async () => {
2020
expect(handleRelease).not.toHaveBeenCalled();
2121

2222
if (wakeLock) {
23-
const onReleaseSpy = jest.spyOn(wakeLock, 'onrelease');
24-
2523
expect(wakeLock?.type).toEqual('screen');
2624
expect(wakeLock?.released).toBe(false);
2725

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

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

6158
if (wakeLock) {
62-
const onReleaseSpy = jest.spyOn(wakeLock, 'onrelease');
63-
6459
expect(wakeLock?.type).toEqual('screen');
6560
expect(wakeLock?.released).toBe(false);
6661

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

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

8882
if (ndWakeLock) {
89-
const onReleaseSpy = jest.spyOn(ndWakeLock, 'onrelease');
90-
9183
expect(ndWakeLock?.type).toEqual('screen');
9284
expect(ndWakeLock?.released).toBe(false);
9385

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

10192
ndWakeLock?.removeEventListener('release', ndHandleRelease);
10293
expect(ndHandleRelease).toHaveBeenLastCalledWith(expect.any(Event));
10394
}
10495
});
96+
97+
test('wakeLock calls only the last defined "onrelease" attribute', async () => {
98+
const handleRelease = jest.fn();
99+
const onReleaseAttributeOne = jest.fn();
100+
const onReleaseAttributeTwo = jest.fn();
101+
const onReleaseAttributeThree = jest.fn();
102+
const { wakeLock, error } = await requestWakeLock(handleRelease);
103+
104+
expect(error).not.toBeDefined();
105+
expect(wakeLock).toBeDefined();
106+
expect(handleRelease).not.toHaveBeenCalled();
107+
108+
if (wakeLock) {
109+
// Assign the onrelease attribute several times
110+
wakeLock.onrelease = onReleaseAttributeOne;
111+
wakeLock.onrelease = onReleaseAttributeTwo;
112+
wakeLock.onrelease = onReleaseAttributeThree;
113+
114+
// Trigger wakelock release
115+
await wakeLock.release();
116+
expect(handleRelease).toHaveBeenCalledWith(expect.any(Event));
117+
expect(onReleaseAttributeOne).not.toHaveBeenCalled();
118+
expect(onReleaseAttributeTwo).not.toHaveBeenCalled();
119+
expect(onReleaseAttributeThree).toHaveBeenCalledWith(expect.any(Event));
120+
}
121+
})

0 commit comments

Comments
 (0)