Skip to content

Commit 570b0c3

Browse files
committed
Hide animation mocks inside a function
1 parent 824062f commit 570b0c3

14 files changed

+277
-136
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "jsdom-testing-mocks",
3-
"version": "1.5.0-beta.5",
3+
"version": "1.5.0-beta.6",
44
"author": "Ivan Galiatin",
55
"license": "MIT",
66
"description": "A set of tools for emulating browser behavior in jsdom environment",

src/mocks/web-animations-api/Animation.ts

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
import './AnimationEffect';
2-
import './KeyframeEffect';
3-
import './AnimationPlaybackEvent';
4-
import './DocumentTimeline';
1+
import { mockKeyframeEffect } from './KeyframeEffect';
2+
import { mockAnimationPlaybackEvent } from './AnimationPlaybackEvent';
3+
import { mockDocumentTimeline } from './DocumentTimeline';
54
import { getEasingFunctionFromString } from './easingFunctions';
65

76
type ActiveAnimationTimeline = AnimationTimeline & {
@@ -167,6 +166,11 @@ class MockedAnimation extends EventTarget implements Animation {
167166
});
168167
}
169168

169+
#silentlyRejectFinishedPromise(error: Error) {
170+
this.#finishedPromise.catch(noop);
171+
this.#resolvers.finished.reject(error);
172+
}
173+
170174
#hasPendingTask() {
171175
return this.#pendingPauseTask || this.#pendingPlayTask;
172176
}
@@ -401,7 +405,9 @@ class MockedAnimation extends EventTarget implements Animation {
401405
this.#applyPendingPlaybackRate();
402406

403407
// 5. Reject animation’s current ready promise with a DOMException named "AbortError".
404-
this.#resolvers.ready.reject(new DOMException('AbortError'));
408+
this.#silentlyRejectFinishedPromise(
409+
new DOMException(undefined, 'AbortError')
410+
);
405411

406412
// 6. Set the [[PromiseIsHandled]] internal slot of animation’s current ready promise to true.
407413

@@ -1145,8 +1151,7 @@ class MockedAnimation extends EventTarget implements Animation {
11451151
this.#resetPendingTasks();
11461152

11471153
// Reject the current finished promise with a DOMException named "AbortError".
1148-
// this.#resolvers.finished.reject(new DOMException('AbortError'));
1149-
this.#resolvers.finished.reject(
1154+
this.#silentlyRejectFinishedPromise(
11501155
new DOMException('The user aborted a request.', 'AbortError')
11511156
);
11521157

@@ -1688,12 +1693,18 @@ class MockedAnimation extends EventTarget implements Animation {
16881693
}
16891694
}
16901695

1691-
if (typeof Animation === 'undefined') {
1692-
Object.defineProperty(window, 'Animation', {
1693-
writable: true,
1694-
configurable: true,
1695-
value: MockedAnimation,
1696-
});
1696+
function mockAnimation() {
1697+
mockKeyframeEffect();
1698+
mockAnimationPlaybackEvent();
1699+
mockDocumentTimeline();
1700+
1701+
if (typeof Animation === 'undefined') {
1702+
Object.defineProperty(window, 'Animation', {
1703+
writable: true,
1704+
configurable: true,
1705+
value: MockedAnimation,
1706+
});
1707+
}
16971708
}
16981709

1699-
export { MockedAnimation };
1710+
export { MockedAnimation, mockAnimation };

src/mocks/web-animations-api/AnimationEffect.test.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
import './AnimationEffect';
1+
import { mockAnimationEffect } from './AnimationEffect';
2+
3+
mockAnimationEffect();
24

35
describe('AnimationEffect', () => {
46
it('should be defined', () => {

src/mocks/web-animations-api/AnimationEffect.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,14 @@ class MockedAnimationEffect implements AnimationEffect {
7171
}
7272
}
7373

74-
if (typeof AnimationEffect === 'undefined') {
75-
Object.defineProperty(window, 'AnimationEffect', {
76-
writable: true,
77-
configurable: true,
78-
value: MockedAnimationEffect,
79-
});
74+
function mockAnimationEffect() {
75+
if (typeof AnimationEffect === 'undefined') {
76+
Object.defineProperty(window, 'AnimationEffect', {
77+
writable: true,
78+
configurable: true,
79+
value: MockedAnimationEffect,
80+
});
81+
}
8082
}
8183

82-
export { MockedAnimationEffect };
84+
export { MockedAnimationEffect, mockAnimationEffect };
Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
1-
class MockedAnimationPlaybackEvent extends Event
2-
implements AnimationPlaybackEvent {
1+
class MockedAnimationPlaybackEvent
2+
extends Event
3+
implements AnimationPlaybackEvent
4+
{
35
readonly currentTime = null;
46
readonly timelineTime = null;
57
}
68

7-
if (typeof AnimationPlaybackEvent === 'undefined') {
8-
Object.defineProperty(window, 'AnimationPlaybackEvent', {
9-
writable: true,
10-
configurable: true,
11-
value: MockedAnimationPlaybackEvent,
12-
});
9+
function mockAnimationPlaybackEvent() {
10+
if (typeof AnimationPlaybackEvent === 'undefined') {
11+
Object.defineProperty(window, 'AnimationPlaybackEvent', {
12+
writable: true,
13+
configurable: true,
14+
value: MockedAnimationPlaybackEvent,
15+
});
16+
}
1317
}
18+
19+
export { mockAnimationPlaybackEvent };

src/mocks/web-animations-api/AnimationTimeline.test.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
import './AnimationTimeline';
1+
import { mockAnimationTimeline } from './AnimationTimeline';
2+
3+
mockAnimationTimeline();
24

35
describe('AnimationTimeline', () => {
46
it('should be defined', () => {

src/mocks/web-animations-api/AnimationTimeline.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,14 @@ class MockedAnimationTimeline implements AnimationTimeline {
1010
}
1111
}
1212

13-
if (typeof AnimationTimeline === 'undefined') {
14-
Object.defineProperty(window, 'AnimationTimeline', {
15-
writable: true,
16-
configurable: true,
17-
value: MockedAnimationTimeline,
18-
});
13+
function mockAnimationTimeline() {
14+
if (typeof AnimationTimeline === 'undefined') {
15+
Object.defineProperty(window, 'AnimationTimeline', {
16+
writable: true,
17+
configurable: true,
18+
value: MockedAnimationTimeline,
19+
});
20+
}
1921
}
22+
23+
export { MockedAnimationTimeline, mockAnimationTimeline };

src/mocks/web-animations-api/DocumentTimeline.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import './DocumentTimeline';
1+
import { mockDocumentTimeline } from './DocumentTimeline';
22

3+
mockDocumentTimeline();
34
jest.useFakeTimers();
45

56
describe('DocumentTimeline', () => {
Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1-
import './AnimationTimeline';
1+
import {
2+
mockAnimationTimeline,
3+
MockedAnimationTimeline,
4+
} from './AnimationTimeline';
25

36
class MockedDocumentTimeline
4-
extends AnimationTimeline
7+
extends MockedAnimationTimeline
58
implements DocumentTimeline
69
{
710
#originTime = 0;
@@ -17,16 +20,22 @@ class MockedDocumentTimeline
1720
}
1821
}
1922

20-
if (typeof DocumentTimeline === 'undefined') {
21-
Object.defineProperty(window, 'DocumentTimeline', {
22-
writable: true,
23-
configurable: true,
24-
value: MockedDocumentTimeline,
25-
});
26-
27-
Object.defineProperty(Document.prototype, 'timeline', {
28-
writable: true,
29-
configurable: true,
30-
value: new MockedDocumentTimeline(),
31-
});
23+
function mockDocumentTimeline() {
24+
mockAnimationTimeline();
25+
26+
if (typeof DocumentTimeline === 'undefined') {
27+
Object.defineProperty(window, 'DocumentTimeline', {
28+
writable: true,
29+
configurable: true,
30+
value: MockedDocumentTimeline,
31+
});
32+
33+
Object.defineProperty(Document.prototype, 'timeline', {
34+
writable: true,
35+
configurable: true,
36+
value: new MockedDocumentTimeline(),
37+
});
38+
}
3239
}
40+
41+
export { mockDocumentTimeline };

src/mocks/web-animations-api/KeyframeEffect.test.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
import {
22
MockedKeyframeEffect,
33
convertPropertyIndexedKeyframes,
4+
mockKeyframeEffect,
45
} from './KeyframeEffect';
56

7+
mockKeyframeEffect();
8+
69
describe('KeyframeEffect', () => {
710
it('should have correct properties by default', () => {
811
const element = document.createElement('div');

0 commit comments

Comments
 (0)