UNPKG

1.08 kBJavaScriptView Raw
1import _ from './wrap/lodash'
2import isMatcher from './matchers/is-matcher'
3
4export default (expectedArgs, actualArgs, config = {}) => {
5 if (arityMismatch(expectedArgs, actualArgs, config)) {
6 return false
7 } else if (config.allowMatchers !== false) {
8 return equalsWithMatchers(expectedArgs, actualArgs)
9 } else {
10 return _.isEqual(expectedArgs, actualArgs)
11 }
12}
13
14const arityMismatch = (expectedArgs, actualArgs, config) =>
15 expectedArgs.length !== actualArgs.length && !config.ignoreExtraArgs
16
17const equalsWithMatchers = (expectedArgs, actualArgs) =>
18 _.every(expectedArgs, (expectedArg, key) =>
19 argumentMatchesExpectation(expectedArg, actualArgs[key]))
20
21const argumentMatchesExpectation = (expectedArg, actualArg) => {
22 if (isMatcher(expectedArg)) {
23 return matcherTestFor(expectedArg)(actualArg)
24 } else {
25 return _.isEqualWith(expectedArg, actualArg, (expectedEl, actualEl) => {
26 if (isMatcher(expectedEl)) {
27 return matcherTestFor(expectedEl)(actualEl)
28 }
29 })
30 }
31}
32
33const matcherTestFor = (matcher) =>
34 matcher.__matches