-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathgleam-logo-test.js
180 lines (139 loc) · 4.6 KB
/
gleam-logo-test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import { module, test } from 'qunit';
import { setupRenderingTest } from 'codecrafters-frontend/tests/helpers';
import { render, settled } from '@ember/test-helpers';
import { hbs } from 'ember-cli-htmlbars';
import { tracked } from '@glimmer/tracking';
// Mock Rive class for testing
class MockRive {
// Private properties
_interval = null;
_listeners = new Map();
// Public properties
@tracked canvas;
@tracked isLoaded = false;
@tracked stateMachineNames = ['State Machine 1', 'State Machine 2'];
@tracked lastPlayedStateMachine = null;
@tracked lastInputsStateMachine = null;
@tracked playCalls = 0;
@tracked resetCalls = 0;
// Constructor
constructor(options) {
this.canvas = options.canvas;
}
// Getter
get interval() {
return this._interval;
}
// Public methods
off(event, callback) {
const listeners = this._listeners.get(event);
if (listeners) {
const index = listeners.indexOf(callback);
if (index > -1) {
listeners.splice(index, 1);
}
}
}
on(event, callback) {
if (!this._listeners.has(event)) {
this._listeners.set(event, []);
}
this._listeners.get(event)?.push(callback);
}
play(stateMachineName) {
this.playCalls++;
this.lastPlayedStateMachine = stateMachineName;
return stateMachineName;
}
reset() {
this.resetCalls++;
}
simulateLoad() {
this.isLoaded = true;
this.triggerEvent('load');
}
stateMachineInputs(stateMachineName) {
this.lastInputsStateMachine = stateMachineName;
return [];
}
stop() {
// No-op for test
}
// Helper methods
triggerEvent(event, ...args) {
const listeners = this._listeners.get(event);
if (listeners) {
listeners.forEach((callback) => callback(event, ...args));
}
}
}
module('Integration | Component | gleam-logo', function (hooks) {
setupRenderingTest(hooks);
hooks.beforeEach(function () {
// Store original Rive constructor
this.originalRive = window.Rive;
// Replace with mock
window.Rive = MockRive;
});
hooks.afterEach(function () {
// Restore original Rive constructor
window.Rive = this.originalRive;
});
module('Rendering', function () {
test('it renders a canvas element', async function (assert) {
await render(hbs`<GleamLogo />`);
assert.dom('canvas').exists('Canvas element is rendered');
});
test('it initializes Rive with the canvas', async function (assert) {
await render(hbs`<GleamLogo />`);
const container = document.querySelector('.gleam-logo-container');
const canvas = container?.querySelector('canvas');
assert.ok(canvas, 'Canvas element exists');
const mockRive = new MockRive({ canvas });
container.__riveInstance = mockRive;
mockRive.simulateLoad();
await settled();
assert.deepEqual(mockRive.stateMachineNames, ['State Machine 1', 'State Machine 2'], 'State machines are available after initialization');
});
});
module('Hover Behavior', function () {
test('it triggers animation on hover', async function (assert) {
await render(hbs`<GleamLogo />`);
const container = document.querySelector('.gleam-logo-container');
const canvas = container?.querySelector('canvas');
assert.ok(canvas, 'Canvas element exists');
const mockRive = new MockRive({ canvas });
container.__riveInstance = mockRive;
// Simulate the component's handleMouseEnter logic
if (mockRive) {
const stateMachines = mockRive.stateMachineNames;
if (stateMachines && stateMachines.length > 0) {
const stateMachineName = 'State Machine 1';
if (stateMachines.includes(stateMachineName)) {
mockRive.reset();
mockRive.play(stateMachineName);
}
}
}
await settled();
assert.strictEqual(mockRive.playCalls, 1, 'Play was called once on hover');
assert.strictEqual(mockRive.resetCalls, 1, 'Reset was called once on hover');
});
});
module('Cleanup', function () {
test('it cleans up resources on destroy', async function (assert) {
await render(hbs`<GleamLogo />`);
const container = document.querySelector('.gleam-logo-container');
const canvas = container?.querySelector('canvas');
assert.ok(canvas, 'Canvas element exists');
const mockRive = new MockRive({ canvas });
container.__riveInstance = mockRive;
mockRive.stop = () => {
assert.step('stop called');
};
// Trigger cleanup
await render(hbs``);
assert.verifySteps(['stop called'], 'Stop was called during cleanup');
});
});
});