|
1 |
| -import { useTimer, useTime } from '../src/index' |
2 |
| -import MockDate from 'mockdate' |
| 1 | +import { Validate } from '../src/utils' |
| 2 | +import { useTimer, useTime, useStopwatch } from '../src/index' |
3 | 3 |
|
4 | 4 | beforeEach(() => {
|
5 | 5 | jest.useFakeTimers()
|
6 |
| - MockDate.set(1628693892000) |
| 6 | + jest.setSystemTime(1628693892000) |
7 | 7 | })
|
8 | 8 |
|
9 | 9 | afterEach(() => {
|
10 | 10 | jest.useRealTimers()
|
11 |
| - MockDate.reset() |
12 | 11 | })
|
13 | 12 |
|
14 | 13 | test('init value useTime 24-hour', () => {
|
15 |
| - const { seconds, minutes, hours, ampm } = useTime() |
16 |
| - expect(setInterval).toHaveBeenCalledTimes(1) |
17 |
| - expect(setInterval).toHaveBeenLastCalledWith(expect.any(Function), 1000) |
18 |
| - expect(seconds).toBe(12) |
19 |
| - expect(minutes).toBe(58) |
20 |
| - expect(hours).toBe(16) |
21 |
| - expect(ampm).toBe('') |
| 14 | + const time = useTime() |
| 15 | + expect(time.seconds.value).toBe(12) |
| 16 | + expect(time.minutes.value).toBe(58) |
| 17 | + expect(time.hours.value).toBe(16) |
| 18 | + expect(time.ampm.value).toBe('') |
22 | 19 | })
|
23 | 20 |
|
24 | 21 | test('init value useTime 12-hour', () => {
|
25 |
| - const { seconds, minutes, hours, ampm } = useTime('12-hour') |
26 |
| - expect(seconds).toBe(12) |
27 |
| - expect(minutes).toBe(58) |
28 |
| - expect(hours).toBe(4) |
29 |
| - expect(ampm).toBe('pm') |
| 22 | + const time = useTime('12-hour') |
| 23 | + expect(time.seconds.value).toBe(12) |
| 24 | + expect(time.minutes.value).toBe(58) |
| 25 | + expect(time.hours.value).toBe(4) |
| 26 | + expect(time.ampm.value).toBe('pm') |
30 | 27 | })
|
31 | 28 |
|
32 |
| -test('run useTime for 3', async () => { |
33 |
| - const { seconds, minutes, hours, ampm } = useTime() |
34 |
| - expect(seconds).toBe(12) |
35 |
| - expect(minutes).toBe(58) |
36 |
| - expect(hours).toBe(16) |
37 |
| - expect(ampm).toBe('') |
38 |
| - jest.advanceTimersByTime(1000) |
39 |
| - expect(setInterval).toHaveBeenCalledTimes(1) |
40 |
| - MockDate.set(1638693892000) |
41 |
| - expect(seconds).toBe(13) |
42 |
| - expect(minutes).toBe(58) |
43 |
| - expect(hours).toBe(16) |
44 |
| - expect(ampm).toBe('') |
| 29 | +test('run useTime', async () => { |
| 30 | + const time = useTime() |
| 31 | + expect(time.seconds.value).toBe(12) |
| 32 | + expect(time.minutes.value).toBe(58) |
| 33 | + expect(time.hours.value).toBe(16) |
| 34 | + expect(time.ampm.value).toBe('') |
| 35 | + jest.advanceTimersByTime(3 * 1000) |
| 36 | + expect(time.seconds.value).toBe(15) |
| 37 | + expect(time.minutes.value).toBe(58) |
| 38 | + expect(time.hours.value).toBe(16) |
| 39 | + expect(time.ampm.value).toBe('') |
| 40 | + jest.advanceTimersByTime(60 * 1000) |
| 41 | + expect(time.seconds.value).toBe(15) |
| 42 | + expect(time.minutes.value).toBe(59) |
| 43 | + expect(time.hours.value).toBe(16) |
| 44 | + expect(time.ampm.value).toBe('') |
| 45 | + jest.advanceTimersByTime(60 * 60 * 1000) |
| 46 | + expect(time.seconds.value).toBe(15) |
| 47 | + expect(time.minutes.value).toBe(59) |
| 48 | + expect(time.hours.value).toBe(17) |
| 49 | + expect(time.ampm.value).toBe('') |
| 50 | +}) |
| 51 | + |
| 52 | +test('init value useStopwatch', () => { |
| 53 | + const stopwatch = useStopwatch(60, false) |
| 54 | + expect(stopwatch.isRunning.value).toBe(false) |
| 55 | + expect(stopwatch.seconds.value).toBe(0) |
| 56 | + expect(stopwatch.minutes.value).toBe(1) |
| 57 | + expect(stopwatch.hours.value).toBe(0) |
| 58 | +}) |
| 59 | + |
| 60 | +test('run useStopwatch', () => { |
| 61 | + const stopwatch = useStopwatch(0) |
| 62 | + expect(stopwatch.isRunning.value).toBe(true) |
| 63 | + jest.advanceTimersByTime(3 * 1000) |
| 64 | + expect(stopwatch.seconds.value).toBe(3) |
| 65 | + expect(stopwatch.minutes.value).toBe(0) |
| 66 | + expect(stopwatch.hours.value).toBe(0) |
| 67 | + jest.advanceTimersByTime(60 * 1000) |
| 68 | + expect(stopwatch.seconds.value).toBe(3) |
| 69 | + expect(stopwatch.minutes.value).toBe(1) |
| 70 | + expect(stopwatch.hours.value).toBe(0) |
| 71 | + jest.advanceTimersByTime(60 * 60 * 1000) |
| 72 | + expect(stopwatch.seconds.value).toBe(3) |
| 73 | + expect(stopwatch.minutes.value).toBe(1) |
| 74 | + expect(stopwatch.hours.value).toBe(1) |
| 75 | + stopwatch.reset(6, false) |
| 76 | + expect(stopwatch.isRunning.value).toBe(false) |
| 77 | + expect(stopwatch.seconds.value).toBe(6) |
| 78 | + expect(stopwatch.minutes.value).toBe(0) |
| 79 | + expect(stopwatch.hours.value).toBe(0) |
| 80 | + stopwatch.reset(6, true) |
| 81 | + expect(stopwatch.isRunning.value).toBe(true) |
| 82 | +}) |
| 83 | + |
| 84 | +test('start/stop useStopwatch', () => { |
| 85 | + const stopwatch = useStopwatch(60, false) |
| 86 | + stopwatch.start() |
| 87 | + expect(stopwatch.isRunning.value).toBe(true) |
| 88 | + jest.advanceTimersByTime(3 * 1000) |
| 89 | + expect(stopwatch.seconds.value).toBe(3) |
| 90 | + expect(stopwatch.minutes.value).toBe(1) |
| 91 | + expect(stopwatch.hours.value).toBe(0) |
| 92 | + expect(stopwatch.days.value).toBe(0) |
| 93 | + stopwatch.pause() |
| 94 | + jest.advanceTimersByTime(3 * 1000) |
| 95 | + expect(stopwatch.seconds.value).toBe(3) |
| 96 | + expect(stopwatch.minutes.value).toBe(1) |
| 97 | + expect(stopwatch.hours.value).toBe(0) |
| 98 | + expect(stopwatch.days.value).toBe(0) |
45 | 99 | })
|
46 | 100 |
|
47 | 101 | test('init value useTimer', () => {
|
48 |
| - const { seconds, minutes, hours, isRunning } = useTimer(60, false) |
49 |
| - expect(seconds).toBe(0) |
50 |
| - expect(minutes).toBe(0) |
51 |
| - expect(hours).toBe(0) |
52 |
| - expect(isRunning.value).toBe(false) |
| 102 | + const d = new Date().setSeconds(new Date().getSeconds() + 600) |
| 103 | + const timer = useTimer(d, false) |
| 104 | + expect(timer.seconds.value).toBe(0) |
| 105 | + expect(timer.minutes.value).toBe(10) |
| 106 | + expect(timer.hours.value).toBe(0) |
| 107 | + expect(timer.isRunning.value).toBe(false) |
| 108 | +}) |
| 109 | + |
| 110 | +test('start/stop/resume useTimer', () => { |
| 111 | + const timer = useTimer() |
| 112 | + expect(timer.isRunning.value).toBe(true) |
| 113 | + timer.start() |
| 114 | + expect(timer.isRunning.value).toBe(true) |
| 115 | + timer.pause() |
| 116 | + expect(timer.isRunning.value).toBe(false) |
| 117 | + timer.resume() |
| 118 | + expect(timer.isRunning.value).toBe(true) |
53 | 119 | })
|
54 | 120 |
|
55 |
| -// test('run useTimer for 6', async () => { |
56 |
| -// jest.useFakeTimers() |
57 |
| -// MockDate.set(1628693892000) |
58 |
| -// const { seconds, minutes, hours, isRunning, pause } = useTimer(6) |
59 |
| -// expect(isRunning.value).toBe(true) |
60 |
| -// jest.advanceTimersByTime(3000) |
61 |
| -// expect(setInterval).toHaveBeenCalledTimes(3); |
62 |
| -// MockDate.set(1638693892000) |
63 |
| -// // jest.runOnlyPendingTimers() |
64 |
| -// pause() |
65 |
| -// expect(isRunning.value).toBe(false) |
66 |
| -// expect(seconds).toBe(3) |
67 |
| -// expect(minutes).toBe(0) |
68 |
| -// expect(hours).toBe(0) |
69 |
| -// }); |
| 121 | +test('run useTimer for 6', () => { |
| 122 | + const d = new Date().setSeconds(new Date().getSeconds() + 600) |
| 123 | + const timer = useTimer(d) |
| 124 | + expect(timer.isRunning.value).toBe(true) |
| 125 | + jest.advanceTimersByTime(3 * 1000) |
| 126 | + expect(timer.seconds.value).toBe(57) |
| 127 | + expect(timer.minutes.value).toBe(9) |
| 128 | + expect(timer.hours.value).toBe(0) |
| 129 | +}) |
| 130 | + |
| 131 | +test('pause useTimer for 6', () => { |
| 132 | + const d = new Date().setSeconds(new Date().getSeconds() + 600) |
| 133 | + const timer = useTimer(d) |
| 134 | + expect(timer.isRunning.value).toBe(true) |
| 135 | + timer.pause() |
| 136 | + expect(timer.isRunning.value).toBe(false) |
| 137 | + jest.advanceTimersByTime(3 * 1000) |
| 138 | + expect(timer.seconds.value).toBe(0) |
| 139 | + expect(timer.minutes.value).toBe(10) |
| 140 | + expect(timer.hours.value).toBe(0) |
| 141 | +}) |
| 142 | + |
| 143 | +test('expire useTimer', () => { |
| 144 | + const timer = useTimer() |
| 145 | + expect(timer.isRunning.value).toBe(true) |
| 146 | + expect(timer.isExpired.value).toBe(false) |
| 147 | + jest.advanceTimersByTime(60 * 1000) |
| 148 | + expect(timer.seconds.value).toBe(0) |
| 149 | + expect(timer.minutes.value).toBe(0) |
| 150 | + expect(timer.hours.value).toBe(0) |
| 151 | + expect(timer.isRunning.value).toBe(false) |
| 152 | + expect(timer.isExpired.value).toBe(true) |
| 153 | +}) |
| 154 | + |
| 155 | +it('calls console.warn if error param', () => { |
| 156 | + const consoleSpy = jest.spyOn(console, 'warn') |
| 157 | + useTimer(-10) |
| 158 | + expect(consoleSpy).toHaveBeenCalledWith( |
| 159 | + 'vue-timer-hook: { useTimer } Invalid expiryTimestamp settings', |
| 160 | + -10 |
| 161 | + ) |
| 162 | + const valid = Validate.expiryTimestamp(-10) |
| 163 | + expect(valid).toBe(false) |
| 164 | +}) |
0 commit comments