Completed
Push — master ( 57adeb...96f883 )
by Andreu
02:04
created

tests/mocks.js   A

Complexity

Total Complexity 5
Complexity/F 1.25

Size

Lines of Code 33
Function Count 4

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 5
eloc 20
dl 0
loc 33
c 0
b 0
f 0
mnd 1
bc 1
fnc 4
rs 10
bpm 0.25
cpm 1.25
noi 0
1
const globalSetTimeout = global.setTimeout
2
3
exports.__esModule = true
4
5
exports.mockSetTimeout = expectedPause => {
6
  const mockState = {
7
    expectedPause: expectedPause,
8
    passedTimeout: null,
9
    numTimeoutCalls: 0,
10
    // eslint-disable-next-line @typescript-eslint/no-empty-function
11
    pendingCallback: () => {},
12
    pendingCallbackArgs: [],
13
  }
14
15
  global.setTimeout = (callback, ms, ...args) => {
16
    if (ms !== mockState.expectedPause) {
17
      // We use a "magic number" to allow all the other cases work as expected
18
      return globalSetTimeout(callback, ms, ...args)
19
    }
20
21
    mockState.passedTimeout = ms
22
    mockState.numTimeoutCalls += 1
23
    mockState.pendingCallback = callback
24
    mockState.pendingCallbackArgs = args
25
    return null
26
  }
27
28
  return mockState
29
}
30
31
exports.restoreSetTimeout = () => {
32
  global.setTimeout = globalSetTimeout
33
}
34