|
| 1 | +# Class: MockCallHistory |
| 2 | + |
| 3 | +Access to an instance with : |
| 4 | + |
| 5 | +```js |
| 6 | +const mockAgent = new MockAgent({ enableCallHistory: true }) |
| 7 | +mockAgent.getCallHistory() |
| 8 | + |
| 9 | +// or |
| 10 | +const mockAgent = new MockAgent() |
| 11 | +mockAgent.enableMockHistory() |
| 12 | +mockAgent.getCallHistory() |
| 13 | + |
| 14 | +``` |
| 15 | + |
| 16 | +a MockCallHistory instance implements a **Symbol.iterator** letting you iterate on registered logs : |
| 17 | + |
| 18 | +```ts |
| 19 | +for (const log of mockAgent.getCallHistory()) { |
| 20 | + //... |
| 21 | +} |
| 22 | + |
| 23 | +const array: Array<MockCallHistoryLog> = [...mockAgent.getCallHistory()] |
| 24 | +const set: Set<MockCallHistoryLog> = new Set(mockAgent.getCallHistory()) |
| 25 | +``` |
| 26 | + |
| 27 | +## class methods |
| 28 | + |
| 29 | +### clear |
| 30 | + |
| 31 | +Clear all MockCallHistoryLog registered. This is automatically done when calling `mockAgent.close()` |
| 32 | + |
| 33 | +```js |
| 34 | +mockAgent.clearCallHistory() |
| 35 | +// same as |
| 36 | +mockAgent.getCallHistory()?.clear() |
| 37 | +``` |
| 38 | +
|
| 39 | +### calls |
| 40 | +
|
| 41 | +Get all MockCallHistoryLog registered as an array |
| 42 | +
|
| 43 | +```js |
| 44 | +mockAgent.getCallHistory()?.calls() |
| 45 | +``` |
| 46 | +
|
| 47 | +### firstCall |
| 48 | +
|
| 49 | +Get the first MockCallHistoryLog registered or undefined |
| 50 | +
|
| 51 | +```js |
| 52 | +mockAgent.getCallHistory()?.firstCall() |
| 53 | +``` |
| 54 | +
|
| 55 | +### lastCall |
| 56 | +
|
| 57 | +Get the last MockCallHistoryLog registered or undefined |
| 58 | +
|
| 59 | +```js |
| 60 | +mockAgent.getCallHistory()?.lastCall() |
| 61 | +``` |
| 62 | +
|
| 63 | +### nthCall |
| 64 | +
|
| 65 | +Get the nth MockCallHistoryLog registered or undefined |
| 66 | +
|
| 67 | +```js |
| 68 | +mockAgent.getCallHistory()?.nthCall(3) // the third MockCallHistoryLog registered |
| 69 | +``` |
| 70 | +
|
| 71 | +### filterCallsByProtocol |
| 72 | +
|
| 73 | +Filter MockCallHistoryLog by protocol. |
| 74 | +
|
| 75 | +> more details for the first parameter can be found [here](/docs/docs/api/MockCallHistory.md#filter-parameter) |
| 76 | +
|
| 77 | +```js |
| 78 | +mockAgent.getCallHistory()?.filterCallsByProtocol(/https/) |
| 79 | +mockAgent.getCallHistory()?.filterCallsByProtocol('https:') |
| 80 | +``` |
| 81 | +
|
| 82 | +### filterCallsByHost |
| 83 | +
|
| 84 | +Filter MockCallHistoryLog by host. |
| 85 | +
|
| 86 | +> more details for the first parameter can be found [here](/docs/docs/api/MockCallHistory.md#filter-parameter) |
| 87 | +
|
| 88 | +```js |
| 89 | +mockAgent.getCallHistory()?.filterCallsByHost(/localhost/) |
| 90 | +mockAgent.getCallHistory()?.filterCallsByHost('localhost:3000') |
| 91 | +``` |
| 92 | +
|
| 93 | +### filterCallsByPort |
| 94 | +
|
| 95 | +Filter MockCallHistoryLog by port. |
| 96 | +
|
| 97 | +> more details for the first parameter can be found [here](/docs/docs/api/MockCallHistory.md#filter-parameter) |
| 98 | +
|
| 99 | +```js |
| 100 | +mockAgent.getCallHistory()?.filterCallsByPort(/3000/) |
| 101 | +mockAgent.getCallHistory()?.filterCallsByPort('3000') |
| 102 | +mockAgent.getCallHistory()?.filterCallsByPort('') |
| 103 | +``` |
| 104 | +
|
| 105 | +### filterCallsByOrigin |
| 106 | +
|
| 107 | +Filter MockCallHistoryLog by origin. |
| 108 | +
|
| 109 | +> more details for the first parameter can be found [here](/docs/docs/api/MockCallHistory.md#filter-parameter) |
| 110 | +
|
| 111 | +```js |
| 112 | +mockAgent.getCallHistory()?.filterCallsByOrigin(/http:\/\/localhost:3000/) |
| 113 | +mockAgent.getCallHistory()?.filterCallsByOrigin('http://localhost:3000') |
| 114 | +``` |
| 115 | +
|
| 116 | +### filterCallsByPath |
| 117 | +
|
| 118 | +Filter MockCallHistoryLog by path. |
| 119 | +
|
| 120 | +> more details for the first parameter can be found [here](/docs/docs/api/MockCallHistory.md#filter-parameter) |
| 121 | +
|
| 122 | +```js |
| 123 | +mockAgent.getCallHistory()?.filterCallsByPath(/api\/v1\/graphql/) |
| 124 | +mockAgent.getCallHistory()?.filterCallsByPath('/api/v1/graphql') |
| 125 | +``` |
| 126 | +
|
| 127 | +### filterCallsByHash |
| 128 | +
|
| 129 | +Filter MockCallHistoryLog by hash. |
| 130 | +
|
| 131 | +> more details for the first parameter can be found [here](/docs/docs/api/MockCallHistory.md#filter-parameter) |
| 132 | +
|
| 133 | +```js |
| 134 | +mockAgent.getCallHistory()?.filterCallsByPath(/hash/) |
| 135 | +mockAgent.getCallHistory()?.filterCallsByPath('#hash') |
| 136 | +``` |
| 137 | +
|
| 138 | +### filterCallsByFullUrl |
| 139 | +
|
| 140 | +Filter MockCallHistoryLog by fullUrl. fullUrl contains protocol, host, port, path, hash, and query params |
| 141 | +
|
| 142 | +> more details for the first parameter can be found [here](/docs/docs/api/MockCallHistory.md#filter-parameter) |
| 143 | +
|
| 144 | +```js |
| 145 | +mockAgent.getCallHistory()?.filterCallsByFullUrl(/https:\/\/localhost:3000\/\?query=value#hash/) |
| 146 | +mockAgent.getCallHistory()?.filterCallsByFullUrl('https://localhost:3000/?query=value#hash') |
| 147 | +``` |
| 148 | +
|
| 149 | +### filterCallsByMethod |
| 150 | +
|
| 151 | +Filter MockCallHistoryLog by method. |
| 152 | +
|
| 153 | +> more details for the first parameter can be found [here](/docs/docs/api/MockCallHistory.md#filter-parameter) |
| 154 | +
|
| 155 | +```js |
| 156 | +mockAgent.getCallHistory()?.filterCallsByMethod(/POST/) |
| 157 | +mockAgent.getCallHistory()?.filterCallsByMethod('POST') |
| 158 | +``` |
| 159 | +
|
| 160 | +### filterCalls |
| 161 | +
|
| 162 | +This class method is a meta function / alias to apply complex filtering in a single way. |
| 163 | +
|
| 164 | +Parameters : |
| 165 | +
|
| 166 | +- criteria : the first parameter. a function, regexp or object. |
| 167 | + - function : filter MockCallHistoryLog when the function returns false |
| 168 | + - regexp : filter MockCallHistoryLog when the regexp does not match on MockCallHistoryLog.toString() ([see](./MockCallHistoryLog.md#to-string)) |
| 169 | + - object : an object with MockCallHistoryLog properties as keys to apply multiple filters. each values are a [filter parameter](/docs/docs/api/MockCallHistory.md#filter-parameter) |
| 170 | +- options : the second parameter. an object. |
| 171 | + - options.operator : `'AND'` or `'OR'` (default `'OR'`). Used only if criteria is an object. see below |
| 172 | +
|
| 173 | +```js |
| 174 | +mockAgent.getCallHistory()?.filterCalls((log) => log.hash === value && log.headers?.['authorization'] !== undefined) |
| 175 | +mockAgent.getCallHistory()?.filterCalls(/"data": "{ "errors": "wrong body" }"/) |
| 176 | + |
| 177 | +// returns an Array of MockCallHistoryLog which all have |
| 178 | +// - a hash containing my-hash |
| 179 | +// - OR |
| 180 | +// - a path equal to /endpoint |
| 181 | +mockAgent.getCallHistory()?.filterCalls({ hash: /my-hash/, path: '/endpoint' }) |
| 182 | + |
| 183 | +// returns an Array of MockCallHistoryLog which all have |
| 184 | +// - a hash containing my-hash |
| 185 | +// - AND |
| 186 | +// - a path equal to /endpoint |
| 187 | +mockAgent.getCallHistory()?.filterCalls({ hash: /my-hash/, path: '/endpoint' }, { operator: 'AND' }) |
| 188 | +``` |
| 189 | +
|
| 190 | +## filter parameter |
| 191 | +
|
| 192 | +Can be : |
| 193 | +
|
| 194 | +- string. MockCallHistoryLog filtered if `value !== parameterValue` |
| 195 | +- null. MockCallHistoryLog filtered if `value !== parameterValue` |
| 196 | +- undefined. MockCallHistoryLog filtered if `value !== parameterValue` |
| 197 | +- regexp. MockCallHistoryLog filtered if `!parameterValue.test(value)` |
0 commit comments