Skip to content

Commit a9f436f

Browse files
committed
Periodic refresh timer tests
Signed-off-by: Jens Reinecke <[email protected]>
1 parent 0b54797 commit a9f436f

File tree

1 file changed

+83
-0
lines changed

1 file changed

+83
-0
lines changed
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/**
2+
* Copyright 2025 Arm Limited
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import { PeriodicRefreshTimer } from './periodic-refresh-timer';
18+
19+
describe('PeriodicRefreshTimer', () => {
20+
let refreshTimer: PeriodicRefreshTimer<string>;
21+
22+
beforeEach(() => {
23+
refreshTimer = new PeriodicRefreshTimer<string>('test-timer', 10);
24+
});
25+
26+
afterEach(() => {
27+
// Ensure underlying node timer is cleaned up.
28+
refreshTimer.stop();
29+
});
30+
31+
it('returns correct enabled state through getter function', () => {
32+
refreshTimer.enabled = true;
33+
expect(refreshTimer.enabled).toBe(true);
34+
refreshTimer.enabled = false;
35+
expect(refreshTimer.enabled).toBe(false);
36+
});
37+
38+
it('does not start if not enabled', () => {
39+
// Start timer when not enabled
40+
refreshTimer.start();
41+
expect(refreshTimer.isRunning).toBe(false);
42+
});
43+
44+
it('starts if enabled and stops', () => {
45+
// Start timer when enabled
46+
refreshTimer.enabled = true;
47+
refreshTimer.start();
48+
expect(refreshTimer.isRunning).toBe(true);
49+
// Stop timer
50+
refreshTimer.stop();
51+
expect(refreshTimer.isRunning).toBe(false);
52+
});
53+
54+
it('starts if enabled and stops when disabled', () => {
55+
// Start timer when enabled
56+
refreshTimer.enabled = true;
57+
refreshTimer.start();
58+
expect(refreshTimer.isRunning).toBe(true);
59+
// Disable timer
60+
refreshTimer.enabled = false;
61+
expect(refreshTimer.isRunning).toBe(false);
62+
});
63+
64+
it('fires periodic refresh events', async () => {
65+
// Register listener function
66+
const refreshListener = jest.fn();
67+
refreshTimer.onRefresh(refreshListener);
68+
// Start timer when enabled
69+
refreshTimer.enabled = true;
70+
refreshTimer.start();
71+
expect(refreshTimer.isRunning).toBe(true);
72+
// Wait for a few intervals
73+
await new Promise((resolve) => setTimeout(resolve, 25));
74+
// Stop timer
75+
refreshTimer.stop();
76+
// Between one and two calls should have been made.
77+
// Timing sensitive, so only check we are in an expected range rather
78+
// than full accurracy.
79+
expect(refreshListener.mock.calls.length).toBeGreaterThanOrEqual(1);
80+
expect(refreshListener.mock.calls.length).toBeLessThanOrEqual(2);
81+
expect(refreshListener).toHaveBeenCalledWith('test-timer');
82+
});
83+
});

0 commit comments

Comments
 (0)